bsp_uart.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "include.h"
  2. #if UART1_EN
  3. typedef struct {
  4. volatile u8 w_cnt;
  5. volatile u8 r_cnt;
  6. u8 resv0;
  7. u8 resv1;
  8. u8 *buf;
  9. u32 ticks;
  10. } uart1_cb_t;
  11. static uart1_cb_t uart1_cb;
  12. static u8 uart1_rx_buf[64];
  13. vh_packet_t vh_packet;
  14. #if (CHARGE_BOX_INTF_SEL == INTF_UART1) || (TEST_INTF_SEL == INTF_UART1)
  15. AT(.com_text.bsp.uart)
  16. u8 bsp_uart_packet_parse(vh_packet_t *p, u8 data)
  17. {
  18. u8 recv_valid = 0, parse_done = 0;
  19. u8 cnt = p->cnt;
  20. if ((cnt == 0) && (data == 0x55)) {
  21. recv_valid = 1;
  22. } else if ((cnt == 1) && (data == 0xAA)) {
  23. p->header = 0xAA55;
  24. recv_valid = 1;
  25. } else if ((cnt == 2) && (data == VHOUSE_DISTINGUISH)) {
  26. p->distinguish = data;
  27. recv_valid = 1;
  28. } else if (cnt == 3) {
  29. p->cmd = data;
  30. recv_valid = 1;
  31. } else if (cnt == 4) {
  32. if (data <= VH_DATA_LEN) {
  33. p->length = data;
  34. recv_valid = 1;
  35. }
  36. } else if ((cnt > 4) && (cnt <= (p->length + 1 + 4))) {
  37. recv_valid = 1;
  38. if (cnt > p->length + 4) {
  39. p->checksum = data;
  40. parse_done = 1; //匹配完整充电仓命令包
  41. recv_valid = 0;
  42. } else {
  43. p->buf[cnt - 5] = data;
  44. }
  45. }
  46. if (recv_valid) {
  47. p->crc = crc8_tbl[p->crc ^ data];
  48. p->cnt++;
  49. } else {
  50. if ((parse_done) && (p->crc != p->checksum)) {
  51. parse_done = 0;
  52. }
  53. p->cnt = 0;
  54. p->crc = 0;
  55. }
  56. return parse_done;
  57. }
  58. #endif
  59. AT(.com_text.uart1.isr)
  60. static void uart1_rx_done_cb(u8 data)
  61. {
  62. #if ((CHARGE_BOX_INTF_SEL == INTF_UART1) || ((TEST_INTF_SEL == INTF_UART1) && IODM_TEST_EN))
  63. vh_packet_t *p = &vh_packet;
  64. u8 parse_done = bsp_uart_packet_parse(p,data);
  65. if (parse_done) {
  66. #if (CHARGE_BOX_INTF_SEL == INTF_UART1)
  67. charge_box_packet_recv();
  68. #endif
  69. #if IODM_TEST_EN && (TEST_INTF_SEL == INTF_UART1)
  70. iodm_packet_recv();
  71. #endif
  72. }
  73. #endif
  74. #if QTEST_EN && (TEST_INTF_SEL == INTF_UART1)
  75. if(QTEST_IS_ENABLE()) {
  76. qtest_packet_uart_recv(data);
  77. }
  78. #endif
  79. #if ONEMORE_SPP_TEST_EN
  80. u8 *rx_data = &vh_packet;
  81. onemore_hq51_uart_reveice_data_handle(rx_data);
  82. #endif
  83. }
  84. AT(.com_text.uart1.isr)
  85. static void uart1_isr(void)
  86. {
  87. u8 data;
  88. if(UART1CON & BIT(9)) {
  89. data = UART1DATA;
  90. UART1CPND = BIT(9);
  91. if(tick_check_expire(uart1_cb.ticks,50)){
  92. uart1_cb.w_cnt = uart1_cb.r_cnt = 0;
  93. }
  94. uart1_cb.ticks = tick_get();
  95. uart1_cb.buf[uart1_cb.w_cnt & 0x3f] = data;
  96. uart1_cb.w_cnt++;
  97. uart1_rx_done_cb(data);
  98. }
  99. }
  100. AT(.com_text.uart)
  101. u8 bsp_uart1_get(u8 *ch)
  102. {
  103. if (uart1_cb.r_cnt != uart1_cb.w_cnt) {
  104. *ch = uart1_cb.buf[uart1_cb.r_cnt & 0x3f];
  105. uart1_cb.r_cnt++;
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. void bsp_uart1_init(u32 baudrate)
  111. {
  112. memset(&uart1_cb, 0, sizeof(uart1_cb));
  113. memset(&vh_packet, 0, sizeof(vh_packet));
  114. uart1_cb.buf = uart1_rx_buf;
  115. u32 baud = 0;
  116. if (xcfg_cb.uart1_sel == UART1_TR_PA7) {
  117. if (UART0_PRINTF_SEL == PRINTF_PA7) {
  118. FUNCMCON0 = 0x0f << 8;
  119. }
  120. GPIOADE |= BIT(7); //数字io
  121. GPIOAFEN |= BIT(7); //function io
  122. GPIOADIR |= BIT(7); //input
  123. }else if(xcfg_cb.uart1_sel == UART1_TR_VUSB){
  124. if(!sys_cb.vusb_uart_flag){
  125. if (UART0_PRINTF_SEL == PRINTF_VUSB) {
  126. FUNCMCON0 = 0x0f << 8;
  127. }
  128. PWRCON0 |= BIT(30); //Enable VUSB GPIO
  129. #if (CHARGE_BOX_INTF_SEL == INTF_UART1) || (QTEST_EN && (TEST_INTF_SEL == INTF_UART1))
  130. baudrate = 9600;
  131. #endif
  132. xcfg_cb.chg_inbox_pwrdwn_en = 0; //入仓耳机关机
  133. sys_cb.vusb_uart_flag = 1;
  134. }else{
  135. return;
  136. }
  137. } else if (xcfg_cb.uart1_sel == UART1_TR_PF0){
  138. GPIOFDE |= BIT(0); //数字io
  139. GPIOFFEN |= BIT(0); //function io
  140. GPIOFDIR |= BIT(0);
  141. }
  142. baud = ((get_sysclk_nhz() + (baudrate / 2)) / baudrate) - 1;
  143. CLKGAT0 |= BIT(7); //enable uart1 clk
  144. UART1CON = 0;
  145. FUNCMCON0 = (0x7 << 28) | ((xcfg_cb.uart1_sel + 1) << 24); //when RX = 0X7, TX RX共用
  146. UART1BAUD = (baud << 16) | baud;
  147. UART1CON = BIT(7) | BIT(6) | BIT(5) | BIT(4) | BIT(0); //RX EN, One line, fix baud, 2 Stop bits, Uart EN
  148. UART1CON |= 0xaaa << 16; //KEY RESET MODE
  149. UART1CPND = BIT(8) | BIT(9);
  150. UART1CPND |= BIT(10) | BIT(15) ;
  151. uart1_register_isr(uart1_isr);
  152. }
  153. #endif
  154. #if UART2_EN
  155. typedef struct {
  156. volatile u8 w_cnt;
  157. volatile u8 r_cnt;
  158. u8 resv0;
  159. u8 resv1;
  160. u8 *buf;
  161. u32 ticks;
  162. } uart2_cb_t;
  163. static uart2_cb_t uart2_cb;
  164. static u8 uart2_rx_buf[64];
  165. vh_packet_t vh_packet;
  166. #if (CHARGE_BOX_INTF_SEL == INTF_UART2) || (TEST_INTF_SEL == INTF_UART2)
  167. AT(.com_text.bsp.uart)
  168. u8 bsp_uart_packet_parse(vh_packet_t *p, u8 data)
  169. {
  170. u8 recv_valid = 0, parse_done = 0;
  171. u8 cnt = p->cnt;
  172. if ((cnt == 0) && (data == 0x55)) {
  173. recv_valid = 1;
  174. } else if ((cnt == 1) && (data == 0xAA)) {
  175. p->header = 0xAA55;
  176. recv_valid = 1;
  177. } else if ((cnt == 2) && (data == VHOUSE_DISTINGUISH)) {
  178. p->distinguish = data;
  179. recv_valid = 1;
  180. } else if (cnt == 3) {
  181. p->cmd = data;
  182. recv_valid = 1;
  183. } else if (cnt == 4) {
  184. if (data <= VH_DATA_LEN) {
  185. p->length = data;
  186. recv_valid = 1;
  187. }
  188. } else if ((cnt > 4) && (cnt <= (p->length + 1 + 4))) {
  189. recv_valid = 1;
  190. if (cnt > p->length + 4) {
  191. p->checksum = data;
  192. parse_done = 1; //匹配完整充电仓命令包
  193. recv_valid = 0;
  194. } else {
  195. p->buf[cnt - 5] = data;
  196. }
  197. }
  198. if (recv_valid) {
  199. p->crc = crc8_tbl[p->crc ^ data];
  200. p->cnt++;
  201. } else {
  202. if ((parse_done) && (p->crc != p->checksum)) {
  203. parse_done = 0;
  204. }
  205. p->cnt = 0;
  206. p->crc = 0;
  207. }
  208. return parse_done;
  209. }
  210. #endif
  211. AT(.com_text.uart2.isr)
  212. static void uart2_rx_done_cb(u8 data)
  213. {
  214. #if ((CHARGE_BOX_INTF_SEL == INTF_UART2) || ((TEST_INTF_SEL == INTF_UART2) && IODM_TEST_EN))
  215. vh_packet_t *p = &vh_packet;
  216. u8 parse_done = bsp_uart_packet_parse(p,data);
  217. if (parse_done) {
  218. #if (CHARGE_BOX_INTF_SEL == INTF_UART2)
  219. charge_box_packet_recv();
  220. #endif
  221. #if IODM_TEST_EN && (TEST_INTF_SEL == INTF_UART2)
  222. iodm_packet_recv();
  223. #endif
  224. }
  225. #endif
  226. #if QTEST_EN && (TEST_INTF_SEL == INTF_UART2)
  227. if(QTEST_IS_ENABLE()) {
  228. qtest_packet_uart_recv(data);
  229. }
  230. #endif
  231. }
  232. AT(.com_text.uart2.isr)
  233. static void uart2_isr(void)
  234. {
  235. u8 data;
  236. if(UART2CON & BIT(9)) {
  237. data = UART2DATA;
  238. UART2CPND = BIT(9);
  239. if(tick_check_expire(uart2_cb.ticks,50)){
  240. uart2_cb.w_cnt = uart2_cb.r_cnt = 0;
  241. }
  242. uart2_cb.ticks = tick_get();
  243. uart2_cb.buf[uart2_cb.w_cnt & 0x3f] = data;
  244. uart2_cb.w_cnt++;
  245. uart2_rx_done_cb(data);
  246. }
  247. }
  248. AT(.com_text.uart)
  249. u8 bsp_uart2_get(u8 *ch)
  250. {
  251. if (uart2_cb.r_cnt != uart2_cb.w_cnt) {
  252. *ch = uart2_cb.buf[uart2_cb.r_cnt & 0x3f];
  253. uart2_cb.r_cnt++;
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. void bsp_uart2_init(u32 baudrate)
  259. {
  260. memset(&uart2_cb, 0, sizeof(uart2_cb));
  261. memset(&vh_packet, 0, sizeof(vh_packet));
  262. uart2_cb.buf = uart2_rx_buf;
  263. u32 baud = 0;
  264. if (xcfg_cb.uart2_sel == UART2_TR_PB2) {
  265. if (UART0_PRINTF_SEL == PRINTF_PB2) {
  266. FUNCMCON0 = 0x0f << 8;
  267. }
  268. GPIOBDE |= BIT(2);
  269. GPIOBFEN |= BIT(2);
  270. GPIOBDIR |= BIT(2);
  271. }else if(xcfg_cb.uart2_sel == UART2_TR_VUSB){
  272. if(!sys_cb.vusb_uart_flag){
  273. if (UART0_PRINTF_SEL == PRINTF_VUSB) {
  274. FUNCMCON0 = 0x0f << 8;
  275. }
  276. PWRCON0 |= BIT(30); //Enable VUSB GPIO
  277. #if (CHARGE_BOX_INTF_SEL == INTF_UART2) || (QTEST_EN && (TEST_INTF_SEL == INTF_UART2))
  278. baudrate = 9600;
  279. #endif
  280. xcfg_cb.chg_inbox_pwrdwn_en = 0; //入仓耳机关机
  281. sys_cb.vusb_uart_flag = 1;
  282. }else{
  283. return;
  284. }
  285. } else if (xcfg_cb.uart2_sel == UART2_TR_PE7){
  286. GPIOEDE |= BIT(7);
  287. GPIOEFEN |= BIT(7);
  288. GPIOEDIR |= BIT(7);
  289. }
  290. baud = ((get_sysclk_nhz() + (baudrate / 2)) / baudrate) - 1;
  291. CLKGAT0 |= BIT(8); //enable uart2 clk
  292. UART2CON = 0;
  293. FUNCMCON2 = (0x03 << 12) | ((xcfg_cb.uart2_sel + 1) << 8); //when RX = 0X3, TX RX共用
  294. UART2BAUD = (baud << 16) | baud;
  295. UART2CON = BIT(7) | BIT(6) | BIT(5) | BIT(4) | BIT(0); //RX EN, One line, fix baud, 2 Stop bits, Uart EN
  296. UART2CON |= 0xaaa << 16; //KEY RESET MODE
  297. UART2CPND = BIT(8) | BIT(9);
  298. UART2CPND |= BIT(10) | BIT(15) ;
  299. uart2_register_isr(uart2_isr);
  300. }
  301. #endif
  302. void set_vusb_uart_flag(u8 flag)
  303. {
  304. sys_cb.vusb_uart_flag = flag;
  305. }
  306. void bsp_vusb_uart_dis(void)
  307. {
  308. PWRCON0 &= ~BIT(30); //disable VUSB GPIO
  309. // UART1CON = 0;
  310. CLKGAT0 &= ~BIT(7); //disable uart1 clk
  311. sys_cb.vusb_uart_flag = 0;
  312. }