drv_usart.c 5.1 KB

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