drv_usart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "drv_usart.h"
  2. #include "app_rs485_1.h"
  3. #include "app_can.h"
  4. #include "drv_can.h"
  5. static u16 shark_uart_poll_ticks;
  6. static u8 shark_uart_poll_index;
  7. static void shark_uart_gpio_init(void)
  8. {
  9. /* enable GPIO clock */
  10. rcu_periph_clock_enable(RCU_GPIOA);
  11. rcu_periph_clock_enable(RCU_AF);
  12. rcu_periph_clock_enable(RCU_USART0);
  13. rcu_periph_clock_enable(RCU_USART1);
  14. /* connect port to USARTx_Tx */
  15. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2 | GPIO_PIN_9);
  16. /* connect port to USARTx_Rx */
  17. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_10);
  18. }
  19. static void shark_uart_irq_init(void)
  20. {
  21. nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  22. nvic_irq_enable(USART0_IRQn, 1, 0);
  23. nvic_irq_enable(USART1_IRQn, 1, 0);
  24. }
  25. static void shark_uart_dev_init(SUB_BMS_INFO *info)
  26. {
  27. usart_deinit(info->uart);
  28. usart_baudrate_set(info->uart, USART_BAUND);
  29. usart_word_length_set(info->uart, USART_WL_8BIT);
  30. usart_stop_bit_set(info->uart, USART_STB_1BIT);
  31. usart_parity_config(info->uart, USART_PM_NONE);
  32. usart_hardware_flow_rts_config(info->uart, USART_RTS_DISABLE);
  33. usart_hardware_flow_cts_config(info->uart, USART_CTS_DISABLE);
  34. usart_receive_config(info->uart, USART_RECEIVE_ENABLE);
  35. usart_transmit_config(info->uart, USART_TRANSMIT_ENABLE);
  36. usart_interrupt_enable(info->uart, USART_INT_RBNE);
  37. usart_enable(info->uart);
  38. }
  39. static void shark_uart_isr(SUB_BMS_INFO *info)
  40. {
  41. if (usart_flag_get(info->uart, USART_FLAG_RBNE) != RESET) {
  42. u8 value = usart_data_receive(info->uart);
  43. if (info->rx_length < sizeof(info->rx_buff)) {
  44. info->rx_buff[info->rx_length] = value;
  45. info->rx_length++;
  46. info->rx_busy = 5;
  47. } else {
  48. info->rx_busy = 0;
  49. info->rx_pending = shark_true;
  50. }
  51. }
  52. if (usart_flag_get(info->uart, USART_FLAG_TC) != RESET) {
  53. if (info->tx_index < info->tx_length) {
  54. usart_data_transmit(info->uart, info->tx_buff[info->tx_index]);
  55. info->tx_index++;
  56. } else {
  57. usart_interrupt_disable(info->uart, USART_INT_TC);
  58. }
  59. }
  60. }
  61. void shark_uart_init(void)
  62. {
  63. shark_uart_gpio_init();
  64. shark_uart_dev_init(&sub_bms_info_1);
  65. shark_uart_dev_init(&sub_bms_info_2);
  66. shark_uart_irq_init();
  67. }
  68. u16 shark_uart_read(SUB_BMS_INFO *info, u8 *buff, u8 length)
  69. {
  70. if (info->rx_length == 0) {
  71. return 0;
  72. }
  73. if (length > info->rx_length) {
  74. length = info->rx_length;
  75. }
  76. memcpy(buff, info->rx_buff, length);
  77. info->rx_pending = shark_false;
  78. info->rx_length = 0;
  79. return length;
  80. }
  81. void shark_uart_write(SUB_BMS_INFO *info, u16 size)
  82. {
  83. info->tx_index = 0;
  84. info->tx_length = size;
  85. info->tx_busy = CONFIG_UART_TIMEOUT;
  86. usart_interrupt_enable(info->uart, USART_INT_TC);
  87. }
  88. shark_bool shark_uart_write2(SUB_BMS_INFO *info, const u8 *buff, u16 size)
  89. {
  90. if (size > sizeof(info->tx_buff)) {
  91. return shark_false;
  92. }
  93. memcpy(info->tx_buff, buff, size);
  94. shark_uart_write(info, size);
  95. return shark_true;
  96. }
  97. int8_t Send_Data_RS485(uint8_t *data, uint16_t size)
  98. {
  99. return shark_uart_write2(&sub_bms_info_1, data, size);
  100. }
  101. uint16_t Get_RS485_Data(uint8_t * dbuf, uint16_t dbuf_len)
  102. {
  103. return shark_uart_read(&sub_bms_info_1, dbuf, dbuf_len);
  104. }
  105. static void shark_uart_disconnect(SUB_BMS_INFO *info)
  106. {
  107. shark_battery_clear(info);
  108. }
  109. static void shark_uart_tick_raw(SUB_BMS_INFO *info)
  110. {
  111. if (info->rx_busy > 0) {
  112. if (info->rx_busy > 1) {
  113. info->rx_busy--;
  114. } else {
  115. info->rx_busy = 0;
  116. info->rx_pending = shark_true;
  117. }
  118. }
  119. if (info->send_state == SHARK_SEND_PENDING) {
  120. if (info->tx_busy > 0) {
  121. info->tx_busy--;
  122. } else {
  123. info->send_state = SHARK_SEND_TIMEOUT;
  124. if (info->connected > 0) {
  125. if (info->connected > 1) {
  126. info->connected--;
  127. info->tx_pending = shark_true;
  128. } else {
  129. info->connected = 0;
  130. shark_uart_disconnect(info);
  131. }
  132. }
  133. }
  134. }
  135. if (info->update_ticks == 0 && info->connected) {
  136. info->update_mask = 0xFF;
  137. }
  138. info->update_ticks++;
  139. }
  140. void shark_uart_tick(void)
  141. {
  142. if (shark_uart_poll_ticks < (CONFIG_UART_POLL_DELAY / 2)) {
  143. shark_uart_poll_ticks++;
  144. } else {
  145. shark_uart_poll_ticks = 0;
  146. shark_uart_poll_index++;
  147. if ((shark_uart_poll_index & 1) == 0) {
  148. sub_bms_info_1.tx_pending = shark_true;
  149. } else {
  150. sub_bms_info_2.tx_pending = shark_true;
  151. }
  152. }
  153. shark_uart_tick_raw(&sub_bms_info_1);
  154. shark_uart_tick_raw(&sub_bms_info_2);
  155. }
  156. void shark_uart_poll_raw(SUB_BMS_INFO *info)
  157. {
  158. if (info->rx_pending) {
  159. info->rx_pending = shark_false;
  160. shark_battery_process(info, info->rx_buff, info->rx_length);
  161. info->rx_length = 0;
  162. }
  163. if (info->tx_pending) {
  164. info->tx_pending = shark_false;
  165. if (!shark_battery_switch_busy) {
  166. shark_battery_send_command(info);
  167. }
  168. }
  169. }
  170. void shark_uart_poll(void)
  171. {
  172. shark_uart_poll_raw(&sub_bms_info_1);
  173. shark_uart_poll_raw(&sub_bms_info_2);
  174. }
  175. void shark_uart_poll_safe(void)
  176. {
  177. fwdgt_counter_reload();
  178. Handle_Can_Data();
  179. shark_uart_poll();
  180. shark_can_tx_flush();
  181. }
  182. void USART0_IRQHandler(void)
  183. {
  184. shark_uart_isr(&sub_bms_info_1);
  185. }
  186. void USART1_IRQHandler(void)
  187. {
  188. shark_uart_isr(&sub_bms_info_2);
  189. }