core_debug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. #define JTT_PACK_FLAG (0x7e)
  23. #define JTT_PACK_CODE (0x7d)
  24. #define JTT_PACK_CODE_F1 (0x02)
  25. #define JTT_PACK_CODE_F2 (0x01)
  26. #ifdef __BUILD_APP__
  27. #define DBG_BUF_SIZE (2048)
  28. #define DBG_TXBUF_SIZE DBG_BUF_SIZE
  29. #define DBG_RXBUF_SIZE (0)
  30. #define DBG_RXBUF_BAND (1)
  31. #else
  32. #define DBG_BUF_SIZE (4090)
  33. #define DBG_TXBUF_SIZE DBG_BUF_SIZE
  34. #define DBG_RXBUF_SIZE DBG_BUF_SIZE
  35. #define DBG_RXBUF_BAND (4)
  36. #endif
  37. typedef struct
  38. {
  39. Buffer_Struct LogBuffer;
  40. Buffer_Struct RxBuffer;
  41. CBFuncEx_t Fun;
  42. CBDataFun_t TxFun;
  43. //Loop_Buffer IrqBuffer;
  44. uint8_t Data[DBG_BUF_SIZE];
  45. uint8_t CacheData[DBG_BUF_SIZE * 2];
  46. uint8_t RxData[DBG_BUF_SIZE * 2];
  47. uint8_t TxBuf[DBG_TXBUF_SIZE];
  48. uint8_t RxDMABuf[DBG_RXBUF_BAND][DBG_RXBUF_SIZE + 16];
  49. uint8_t AppMode;
  50. uint8_t InitDone;
  51. uint8_t InsertBusy;
  52. uint8_t TxBusy;
  53. uint8_t RxDMASn;
  54. }DBG_CtrlStruct;
  55. static DBG_CtrlStruct prvDBGCtrl;
  56. static void prvDBG_Out(void)
  57. {
  58. uint32_t TxLen;
  59. SEND_AGAIN:
  60. if (prvDBGCtrl.LogBuffer.Pos)
  61. {
  62. prvDBGCtrl.TxBusy = 1;
  63. TxLen = (prvDBGCtrl.LogBuffer.Pos > sizeof(prvDBGCtrl.TxBuf))?sizeof(prvDBGCtrl.TxBuf):prvDBGCtrl.LogBuffer.Pos;
  64. memcpy(prvDBGCtrl.TxBuf, prvDBGCtrl.LogBuffer.Data, TxLen);
  65. OS_BufferRemove(&prvDBGCtrl.LogBuffer, TxLen);
  66. Uart_DMATx(DBG_UART_ID, DBG_UART_TX_DMA_STREAM, prvDBGCtrl.TxBuf, TxLen);
  67. // if (!Uart_BufferTx(DBG_UART_ID, prvDBGCtrl.TxBuf, TxLen))
  68. // {
  69. // prvDBGCtrl.TxBusy = 0;
  70. // goto SEND_AGAIN;
  71. // }
  72. }
  73. else
  74. {
  75. if (prvDBGCtrl.LogBuffer.MaxLen > (DBG_BUF_SIZE * 8))
  76. {
  77. OS_ReInitBuffer(&prvDBGCtrl.LogBuffer, (DBG_BUF_SIZE * 4));
  78. }
  79. }
  80. }
  81. static void add_trace_data(uint8_t *data, uint32_t len)
  82. {
  83. Buffer_StaticWrite(&prvDBGCtrl.LogBuffer, data, len);
  84. }
  85. void add_printf_data(uint8_t *data, uint32_t len)
  86. {
  87. #ifdef __BUILD_APP__
  88. uint32_t Critical = OS_EnterCritical();
  89. configASSERT(prvDBGCtrl.InsertBusy == 0);
  90. prvDBGCtrl.InsertBusy = 1;
  91. if ((prvDBGCtrl.LogBuffer.Pos + len) > prvDBGCtrl.LogBuffer.MaxLen)
  92. {
  93. OS_ReSizeBuffer(&prvDBGCtrl.LogBuffer, prvDBGCtrl.LogBuffer.MaxLen * 2);
  94. }
  95. OS_BufferWrite(&prvDBGCtrl.LogBuffer, data, len);
  96. prvDBGCtrl.InsertBusy = 0;
  97. if (prvDBGCtrl.TxBusy || !prvDBGCtrl.InitDone || !prvDBGCtrl.AppMode)
  98. {
  99. OS_ExitCritical(Critical);
  100. return;
  101. }
  102. prvDBG_Out();
  103. OS_ExitCritical(Critical);
  104. #endif
  105. }
  106. extern const uint8_t ByteToAsciiTable[16];
  107. void DBG_Trace(const char* format, ...)
  108. {
  109. #ifndef __RAMRUN__
  110. char c;
  111. char *s = 0;
  112. int d = 0, i = 0, p = 0;
  113. uint32_t slen;
  114. unsigned int ud = 0;
  115. unsigned char ch[36] = {0};
  116. unsigned char int_max;
  117. unsigned char flag;
  118. va_list ap;
  119. #ifdef __BUILD_APP__
  120. __disable_irq();
  121. if (prvDBGCtrl.AppMode)
  122. {
  123. prvDBGCtrl.AppMode = 0;
  124. //关闭掉APP的DMA UART输出
  125. //
  126. DMA_StopStream(DBG_UART_TX_DMA_STREAM);
  127. Buffer_StaticInit(&prvDBGCtrl.LogBuffer, prvDBGCtrl.Data, sizeof(prvDBGCtrl.Data));
  128. }
  129. #endif
  130. va_start(ap, format);
  131. while (* format)
  132. {
  133. if (* format != '%')
  134. {
  135. add_trace_data((uint8_t *)format, 1);
  136. format += 1;
  137. continue;
  138. }
  139. slen = 0;
  140. repeat:
  141. switch (*(++format))
  142. {
  143. case '*':
  144. slen = va_arg(ap, int);
  145. goto repeat;
  146. break;
  147. case 's':
  148. case 'S':
  149. s = va_arg(ap, char *);
  150. if (!slen)
  151. {
  152. for ( ; *s; s++)
  153. {
  154. add_trace_data((uint8_t *) s, 1);
  155. }
  156. }
  157. else
  158. {
  159. add_trace_data((uint8_t *) s, slen);
  160. }
  161. break;
  162. case 'c':
  163. case 'C':
  164. c = va_arg(ap, int);
  165. add_trace_data((uint8_t *)&c, 1);
  166. break;
  167. case 'x':
  168. case 'X':
  169. ud = va_arg(ap, int);
  170. p = 8;
  171. for(i = 0; i < p; i++)
  172. {
  173. ch[i] = ByteToAsciiTable[(unsigned char)(ud&0x0f)];
  174. ud >>= 4;
  175. }
  176. for(i = p; i > 0; i--)
  177. {
  178. add_trace_data(&ch[i -1], 1);
  179. }
  180. break;
  181. case 'u':
  182. case 'U':
  183. ud = va_arg(ap, unsigned int);
  184. int_max = 0;
  185. for(i = 0; i < 16; i++)
  186. {
  187. ch[i] = ud%10 + '0';
  188. ud = ud/10;
  189. int_max++;
  190. if (!ud)
  191. break;
  192. }
  193. for(i = int_max; i > 0; i--)
  194. {
  195. add_trace_data(&ch[i -1], 1);
  196. }
  197. break;
  198. case 'd':
  199. case 'D':
  200. d = va_arg(ap, int);
  201. if (d < 0) {
  202. flag = 1;
  203. } else {
  204. flag = 0;
  205. }
  206. d = abs(d);
  207. int_max = 0;
  208. for(i = 0; i < 16; i++)
  209. {
  210. ch[i] = d%10 + '0';
  211. d = d/10;
  212. int_max++;
  213. if (!d)
  214. break;
  215. }
  216. if (flag)
  217. {
  218. flag = '-';
  219. add_trace_data(&flag, 1);
  220. }
  221. for(i = int_max; i > 0; i--)
  222. {
  223. add_trace_data(&ch[i -1], 1);
  224. }
  225. break;
  226. case 'b':
  227. case 'B':
  228. ud = va_arg(ap, int);
  229. if (ud > 0xffff)
  230. {
  231. p = 32;
  232. }
  233. else if (ud > 0xff)
  234. {
  235. p = 16;
  236. }
  237. else
  238. {
  239. p = 8;
  240. }
  241. for(i = 0; i < p; i++)
  242. {
  243. ch[i] = ByteToAsciiTable[(unsigned char)(ud&0x01)];
  244. ud >>= 1;
  245. }
  246. for(i = p; i > 0; i--)
  247. {
  248. add_trace_data(&ch[i -1], 1);
  249. }
  250. break;
  251. default:
  252. //add_trace_data((uint8_t *)format, 1);
  253. goto repeat;
  254. break;
  255. }
  256. format++;
  257. }
  258. va_end(ap);
  259. add_trace_data("\r\n", 2);
  260. // va_list ap;
  261. // va_start(ap, format);
  262. // prvDBGCtrl.TxPos = vsnprintf(prvDBGCtrl.TxBuf, sizeof(prvDBGCtrl.TxBuf) - 2, format, ap);
  263. // va_end(ap);
  264. // prvDBGCtrl.TxBuf[prvDBGCtrl.TxPos] = '\r';
  265. // prvDBGCtrl.TxBuf[prvDBGCtrl.TxPos + 1] = '\n';
  266. Uart_BlockTx(DBG_UART_ID, prvDBGCtrl.LogBuffer.Data, prvDBGCtrl.LogBuffer.Pos);
  267. prvDBGCtrl.LogBuffer.Pos = 0;
  268. #ifdef __BUILD_APP__
  269. __enable_irq();
  270. #endif
  271. #endif
  272. }
  273. void DBG_BlHexPrintf(void *Data, unsigned int len)
  274. {
  275. uint8_t *data = (uint8_t *)Data;
  276. int8_t uart_buf[128];
  277. uint32_t i,j;
  278. j = 0;
  279. if (!len)
  280. {
  281. return ;
  282. }
  283. for (i = 0; i < len; i++)
  284. {
  285. uart_buf[j++] = ByteToAsciiTable[(data[i] & 0xf0) >> 4];
  286. uart_buf[j++] = ByteToAsciiTable[data[i] & 0x0f];
  287. uart_buf[j++] = ' ';
  288. }
  289. uart_buf[j++] = '\r';
  290. uart_buf[j++] = '\n';
  291. add_trace_data(uart_buf, j);
  292. Uart_BlockTx(DBG_UART_ID, prvDBGCtrl.LogBuffer.Data, prvDBGCtrl.LogBuffer.Pos);
  293. prvDBGCtrl.LogBuffer.Pos = 0;
  294. }
  295. #ifdef __BUILD_OS__
  296. void DBG_HexPrintf(void *Data, unsigned int len)
  297. {
  298. uint8_t *data = (uint8_t *)Data;
  299. int8_t *uart_buf;
  300. uint32_t i,j;
  301. j = 0;
  302. if (!prvDBGCtrl.AppMode) return;
  303. if (!len)
  304. {
  305. return ;
  306. }
  307. uart_buf = OS_Zalloc(len * 3 + 2);
  308. if (!uart_buf)
  309. {
  310. return;
  311. }
  312. for (i = 0; i < len; i++)
  313. {
  314. uart_buf[j++] = ByteToAsciiTable[(data[i] & 0xf0) >> 4];
  315. uart_buf[j++] = ByteToAsciiTable[data[i] & 0x0f];
  316. uart_buf[j++] = ' ';
  317. }
  318. uart_buf[j++] = '\r';
  319. uart_buf[j++] = '\n';
  320. prvDBGCtrl.TxFun(uart_buf, len * 3 + 2);
  321. OS_Free(uart_buf);
  322. }
  323. void DBG_Printf(const char* format, ...)
  324. {
  325. char *buf = NULL;
  326. char isr_buf[256];
  327. va_list ap;
  328. if (!prvDBGCtrl.AppMode) return;
  329. va_start(ap, format);
  330. if (OS_CheckInIrq())
  331. {
  332. buf = isr_buf;
  333. vsnprintf_(buf, 255, format, ap);
  334. }
  335. else
  336. {
  337. buf = OS_Zalloc(1024);
  338. vsnprintf_(buf, 1023, format, ap);
  339. }
  340. va_end(ap);
  341. prvDBGCtrl.TxFun( buf, strlen(buf));
  342. if (!OS_CheckInIrq())
  343. {
  344. OS_Free(buf);
  345. }
  346. }
  347. void DBG_DirectOut(void *Data, uint32_t Len)
  348. {
  349. add_printf_data(Data, Len);
  350. }
  351. void DBG_Send(void)
  352. {
  353. uint32_t Critical;
  354. uint32_t TxLen;
  355. if (prvDBGCtrl.TxBusy || !prvDBGCtrl.InitDone || !prvDBGCtrl.AppMode)
  356. {
  357. return;
  358. }
  359. Critical = OS_EnterCritical();
  360. prvDBG_Out();
  361. OS_ExitCritical(Critical);
  362. }
  363. #endif
  364. int32_t DBG_UartIrqCB(void *pData, void *pParam)
  365. {
  366. uint32_t Length;
  367. uint32_t State = (uint32_t)pParam;
  368. uint8_t LastRxSn;
  369. switch (State)
  370. {
  371. case UART_CB_TX_BUFFER_DONE:
  372. case UART_CB_TX_ALL_DONE:
  373. #ifdef __BUILD_APP__
  374. case DMA_CB_DONE:
  375. {
  376. DMA_ClearStreamFlag(DBG_UART_TX_DMA_STREAM);
  377. prvDBGCtrl.TxBusy = 0;
  378. if (!prvDBGCtrl.InsertBusy)
  379. {
  380. DBG_Send();
  381. }
  382. }
  383. #endif
  384. break;
  385. case UART_CB_RX_BUFFER_FULL:
  386. case UART_CB_RX_TIMEOUT:
  387. #ifdef __BUILD_APP__
  388. Uart_RxBufferCB(DBG_UART_ID, prvDBGCtrl.Fun);
  389. break;
  390. #endif
  391. case UART_CB_RX_NEW:
  392. #ifndef __BUILD_APP__
  393. case DMA_CB_DONE:
  394. DMA_StopStream(DBG_UART_RX_DMA_STREAM);
  395. Length = DMA_GetDataLength(DBG_UART_RX_DMA_STREAM, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0]);
  396. Length += Uart_FifoRead(DBG_UART_ID, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][Length]);
  397. LastRxSn = prvDBGCtrl.RxDMASn;
  398. prvDBGCtrl.RxDMASn = (prvDBGCtrl.RxDMASn + 1)%DBG_RXBUF_BAND;
  399. Uart_DMARx(DBG_UART_ID, DBG_UART_RX_DMA_STREAM, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0], DBG_RXBUF_SIZE);
  400. if (Length)
  401. {
  402. prvDBGCtrl.Fun(&prvDBGCtrl.RxDMABuf[LastRxSn][0], Length);
  403. }
  404. #else
  405. // Length = Uart_FifoRead(DBG_UART_ID, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0]);
  406. // LastRxSn = prvDBGCtrl.RxDMASn;
  407. // prvDBGCtrl.RxDMASn = (prvDBGCtrl.RxDMASn + 1)%DBG_RXBUF_BAND;
  408. #endif
  409. break;
  410. case UART_CB_ERROR:
  411. // DBG("%x", pParam);
  412. break;
  413. }
  414. }
  415. void DBG_Response(uint8_t Cmd, uint8_t Result, uint8_t *Data, uint8_t Len)
  416. {
  417. uint8_t Temp[260];
  418. uint16_t TxLen, Check;
  419. Temp[0] = Cmd;
  420. Temp[1] = Result;
  421. Temp[2] = Len;
  422. memcpy(Temp + 3, Data, Len);
  423. Check = CRC16Cal(Temp, Len + 3, CRC16_CCITT_SEED, CRC16_CCITT_GEN, 0);
  424. BytesPutLe16(&Temp[Len + 3], Check);
  425. TxLen = TransferPack(JTT_PACK_FLAG, JTT_PACK_CODE, JTT_PACK_CODE_F1, JTT_PACK_CODE_F2, Temp, Len + 5, prvDBGCtrl.TxBuf);
  426. Uart_BufferTx(DBG_UART_ID, prvDBGCtrl.TxBuf, TxLen);
  427. }
  428. static int32_t DBG_RebootTimeoutCB(void *pData, void *pParam)
  429. {
  430. NVIC_SystemReset();
  431. }
  432. static void DBG_Reboot(uint8_t *Data, uint32_t Len)
  433. {
  434. #ifdef __BUILD_APP__
  435. timer_t *Timer = Timer_Create(DBG_RebootTimeoutCB, NULL, NULL);
  436. Timer_StartMS(Timer, 100, 1);
  437. Uart_DeInit(DBG_UART_ID);
  438. prvDBGCtrl.AppMode = 0;
  439. #else
  440. NVIC_SystemReset();
  441. #endif
  442. }
  443. static void DBG_FlashDownloadStart(uint8_t *Data, uint32_t Len)
  444. {
  445. #ifdef __BUILD_APP__
  446. #else
  447. if (Len < 12)
  448. {
  449. DBG_Response(DBG_CMD_FLASHDOWNLOADSTART, ERROR_PARAM_INVALID, &Len, 4);
  450. return;
  451. }
  452. uint32_t StartAddress = BytesGetLe32(Data);
  453. uint32_t TotalLen = BytesGetLe32(Data + 4);
  454. uint32_t CRC32 = BytesGetLe32(Data + 8);
  455. if (BL_StartNewDownload(StartAddress, TotalLen, CRC32))
  456. {
  457. DBG_Response(DBG_CMD_FLASHDOWNLOADSTART, ERROR_PERMISSION_DENIED, &Len, 4);
  458. return;
  459. }
  460. else
  461. {
  462. DBG_Response(DBG_CMD_FLASHDOWNLOADSTART, ERROR_NONE, &Len, 4);
  463. return;
  464. }
  465. #endif
  466. }
  467. static void DBG_FlashDownload(uint8_t *Data, uint32_t Len)
  468. {
  469. #ifdef __BUILD_APP__
  470. #else
  471. if (Len < 7)
  472. {
  473. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_PARAM_INVALID, &Len, 4);
  474. return;
  475. }
  476. uint32_t PacketSn = BytesGetLe32(Data);
  477. uint32_t NextPacketSn;
  478. int Result;
  479. uint16_t PacketLen = BytesGetLe32(Data + 4);
  480. if ( (PacketLen + 6) != Len )
  481. {
  482. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_PARAM_INVALID, &Len, 4);
  483. return;
  484. }
  485. else
  486. {
  487. Result = BL_DownloadAddData(PacketSn, Data + 6, PacketLen, &NextPacketSn);
  488. if (Result >= 0)
  489. {
  490. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_NONE, &Result, 4);
  491. }
  492. else
  493. {
  494. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_PARAM_OVERFLOW, &NextPacketSn, 4);
  495. }
  496. return;
  497. }
  498. #endif
  499. }
  500. static void DBG_FlashDownloadEnd(uint8_t *Data, uint32_t Len)
  501. {
  502. #ifdef __BUILD_APP__
  503. #else
  504. uint8_t Result = BL_DownloadEnd();
  505. DBG_Response(DBG_CMD_FLASHDOWNLOADEND, Result, &Result, 1);
  506. #endif
  507. }
  508. static void DBG_RunApp(uint8_t *Data, uint32_t Len)
  509. {
  510. #ifdef __BUILD_APP__
  511. #else
  512. uint32_t Result = BL_RunAPP();
  513. DBG_Response(DBG_CMD_RUNAPP, Result, &Result, 4);
  514. #endif
  515. }
  516. static void DBG_FlashZipBlockEnd(uint8_t *Data, uint32_t Len)
  517. {
  518. #ifdef __BUILD_APP__
  519. #else
  520. uint8_t Result = BL_RunLzmaBlock(Data + 1, Data[0]);
  521. DBG_Response(DBG_CMD_FLASHLZMABLOCKEND, Result, &Result, 1);
  522. #endif
  523. }
  524. static const CBDataFun_t DBG_ProtoclFunlist[] =
  525. {
  526. DBG_Reboot,
  527. DBG_FlashDownloadStart,
  528. DBG_FlashDownload,
  529. DBG_FlashDownloadEnd,
  530. DBG_RunApp,
  531. DBG_FlashZipBlockEnd,
  532. };
  533. static int32_t DBG_DummyRx(void *pData, void *pParam)
  534. {
  535. uint32_t DelLen;
  536. uint32_t i, DealLen;
  537. uint16_t Check, Check2;
  538. //Buffer_Struct Buffer;
  539. uint8_t FindHead = 0;
  540. uint8_t FindEnd = 0;
  541. uint8_t NoMoreData = 0;
  542. int32_t Result;
  543. Buffer_Struct *RxBuf = &prvDBGCtrl.RxBuffer;
  544. DBG("%u", pParam);
  545. if (pParam)
  546. {
  547. #ifdef __BUILD_APP__
  548. OS_BufferWrite(&prvDBGCtrl.RxBuffer, (uint8_t *)pData, (uint32_t)pParam);
  549. #else
  550. Buffer_StaticWrite(&prvDBGCtrl.RxBuffer, (uint8_t *)pData, (uint32_t)pParam);
  551. #endif
  552. while(!NoMoreData)
  553. {
  554. if (!RxBuf->Pos)
  555. {
  556. // DBG("no more net rx data");
  557. break;
  558. }
  559. if (!FindHead)
  560. {
  561. for(i = 0; i < RxBuf->Pos; i++)
  562. {
  563. if (JTT_PACK_FLAG == RxBuf->Data[i])
  564. {
  565. OS_BufferRemove(RxBuf, i);
  566. FindHead = 1;
  567. break;
  568. }
  569. }
  570. if (!FindHead)
  571. {
  572. NoMoreData = 1;
  573. RxBuf->Pos = 0;
  574. continue;
  575. }
  576. }
  577. FindEnd = 0;
  578. DealLen = 0;
  579. DelLen = 0;
  580. for(i = 1; i < RxBuf->Pos; i++)
  581. {
  582. if (JTT_PACK_FLAG == RxBuf->Data[i])
  583. {
  584. DelLen = i + 1;
  585. // DBG("msg rec len %dbyte", DelLen);
  586. DealLen = i + 1;
  587. FindEnd = 1;
  588. break;
  589. }
  590. }
  591. if (!FindEnd)
  592. {
  593. // DBG("rest %dbyte", RxBuf->Pos);
  594. NoMoreData = 1;
  595. continue;
  596. }
  597. if (2 == DealLen) //连续2个0x7E,去除第一个,第二个作为头开始重新搜索
  598. {
  599. // DBG("!");
  600. OS_BufferRemove(RxBuf, 1);
  601. FindHead = 1;
  602. continue;
  603. }
  604. if (FindEnd)
  605. {
  606. FindHead = 0;
  607. if (DelLen > DBG_BUF_SIZE)
  608. {
  609. DBG("msg too long, %u", DelLen);
  610. OS_BufferRemove(RxBuf, DelLen);
  611. continue;
  612. }
  613. DealLen = TransferUnpack(JTT_PACK_FLAG, JTT_PACK_CODE, JTT_PACK_CODE_F1, JTT_PACK_CODE_F2,
  614. &RxBuf->Data[1], DealLen - 2, prvDBGCtrl.RxData);
  615. OS_BufferRemove(RxBuf, DelLen);
  616. if (DealLen >= 5)
  617. {
  618. Check = CRC16Cal(prvDBGCtrl.RxData, DealLen - 2, CRC16_CCITT_SEED, CRC16_CCITT_GEN, 0);
  619. Check2 = BytesGetLe16(&prvDBGCtrl.RxData[DealLen - 2]);
  620. if (Check != Check2)
  621. {
  622. // DBG_BlHexPrintf(Data, Len);
  623. DBG("check error %04x %04x", Check, Check2);
  624. DBG_Response(prvDBGCtrl.RxData[0], ERROR_PROTOCL, &Result, 4);
  625. RxBuf->Pos = 0;
  626. return 0;
  627. }
  628. else
  629. {
  630. DealLen -= 2;
  631. if (prvDBGCtrl.RxData[0] < sizeof(DBG_ProtoclFunlist)/sizeof(CBDataFun_t))
  632. {
  633. DBG_ProtoclFunlist[prvDBGCtrl.RxData[0]](prvDBGCtrl.RxData + 1, DealLen - 1);
  634. }
  635. }
  636. }
  637. else
  638. {
  639. DBG("%d", DealLen);
  640. }
  641. }
  642. }
  643. }
  644. return 0;
  645. }
  646. void DBG_Init(uint8_t AppMode)
  647. {
  648. prvDBGCtrl.AppMode = AppMode;
  649. GPIO_Iomux(DBG_UART_TX_PIN, DBG_UART_TX_AF);
  650. GPIO_Iomux(DBG_UART_RX_PIN, DBG_UART_RX_AF);
  651. #ifdef __BUILD_APP__
  652. if (AppMode)
  653. {
  654. OS_InitBuffer(&prvDBGCtrl.LogBuffer, DBG_BUF_SIZE * 4);
  655. OS_InitBuffer(&prvDBGCtrl.RxBuffer, DBG_BUF_SIZE);
  656. //Buffer_StaticInit(&prvDBGCtrl.LogBuffer, prvDBGCtrl.Data, sizeof(prvDBGCtrl.Data));
  657. Uart_BaseInit(DBG_UART_ID, DBG_UART_BR, 1, UART_DATA_BIT8, UART_PARITY_NONE, UART_STOP_BIT1, DBG_UartIrqCB);
  658. Uart_EnableRxIrq(DBG_UART_ID);
  659. DMA_TakeStream(DBG_UART_TX_DMA_STREAM);
  660. Uart_DMATxInit(DBG_UART_ID, DBG_UART_TX_DMA_STREAM, DBG_UART_TX_DMA_CHANNEL);
  661. prvDBGCtrl.TxFun = add_printf_data;
  662. }
  663. else
  664. #endif
  665. {
  666. Buffer_StaticInit(&prvDBGCtrl.LogBuffer, prvDBGCtrl.Data, sizeof(prvDBGCtrl.Data));
  667. Buffer_StaticInit(&prvDBGCtrl.RxBuffer, prvDBGCtrl.CacheData, sizeof(prvDBGCtrl.CacheData));
  668. Uart_BaseInit(DBG_UART_ID, BL_UART_BR, 0, UART_DATA_BIT8, UART_PARITY_NONE, UART_STOP_BIT1, DBG_UartIrqCB);
  669. DMA_TakeStream(DBG_UART_RX_DMA_STREAM);
  670. Uart_DMARxInit(DBG_UART_ID, DBG_UART_RX_DMA_STREAM, DBG_UART_RX_DMA_CHANNEL);
  671. Uart_DMARx(DBG_UART_ID, DBG_UART_RX_DMA_STREAM, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0], DBG_RXBUF_SIZE);
  672. }
  673. prvDBGCtrl.InitDone = 1;
  674. prvDBGCtrl.Fun = DBG_DummyRx;
  675. PM_SetDriverRunFlag(PM_DRV_DBG, 1);
  676. }
  677. void DBG_SetRxCB(CBFuncEx_t cb)
  678. {
  679. prvDBGCtrl.Fun = cb;
  680. }
  681. void DBG_SetTxCB(CBDataFun_t cb)
  682. {
  683. prvDBGCtrl.TxFun = cb;
  684. }