core_debug.c 16 KB

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