drv_usart.c 4.7 KB

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