core_debug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 (4080)
  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. uint32_t Critical;
  302. j = 0;
  303. if (!prvDBGCtrl.AppMode) return;
  304. if (!len)
  305. {
  306. return ;
  307. }
  308. uart_buf = OS_Zalloc(len * 3 + 2);
  309. if (!uart_buf)
  310. {
  311. return;
  312. }
  313. for (i = 0; i < len; i++)
  314. {
  315. uart_buf[j++] = ByteToAsciiTable[(data[i] & 0xf0) >> 4];
  316. uart_buf[j++] = ByteToAsciiTable[data[i] & 0x0f];
  317. uart_buf[j++] = ' ';
  318. }
  319. uart_buf[j++] = '\r';
  320. uart_buf[j++] = '\n';
  321. prvDBGCtrl.TxFun(uart_buf, len * 3 + 2);
  322. OS_Free(uart_buf);
  323. }
  324. void DBG_Printf(const char* format, ...)
  325. {
  326. char *buf = NULL;
  327. char isr_buf[256];
  328. va_list ap;
  329. if (!prvDBGCtrl.AppMode) return;
  330. va_start(ap, format);
  331. if (OS_CheckInIrq())
  332. {
  333. buf = isr_buf;
  334. vsnprintf_(buf, 255, format, ap);
  335. }
  336. else
  337. {
  338. buf = OS_Zalloc(1024);
  339. vsnprintf_(buf, 1023, format, ap);
  340. }
  341. va_end(ap);
  342. prvDBGCtrl.TxFun( buf, strlen(buf));
  343. if (!OS_CheckInIrq())
  344. {
  345. OS_Free(buf);
  346. }
  347. }
  348. void DBG_DirectOut(void *Data, uint32_t Len)
  349. {
  350. add_printf_data(Data, Len);
  351. }
  352. void DBG_Send(void)
  353. {
  354. uint32_t Critical;
  355. uint32_t TxLen;
  356. if (prvDBGCtrl.TxBusy || !prvDBGCtrl.InitDone || !prvDBGCtrl.AppMode)
  357. {
  358. return;
  359. }
  360. Critical = OS_EnterCritical();
  361. prvDBG_Out();
  362. OS_ExitCritical(Critical);
  363. }
  364. #endif
  365. int32_t DBG_UartIrqCB(void *pData, void *pParam)
  366. {
  367. uint32_t Length;
  368. uint32_t State = (uint32_t)pParam;
  369. uint8_t LastRxSn;
  370. switch (State)
  371. {
  372. case UART_CB_TX_BUFFER_DONE:
  373. case UART_CB_TX_ALL_DONE:
  374. #ifdef __BUILD_APP__
  375. case DMA_CB_DONE:
  376. {
  377. DMA_ClearStreamFlag(DBG_UART_TX_DMA_STREAM);
  378. prvDBGCtrl.TxBusy = 0;
  379. if (!prvDBGCtrl.InsertBusy)
  380. {
  381. DBG_Send();
  382. }
  383. }
  384. #endif
  385. break;
  386. case UART_CB_RX_BUFFER_FULL:
  387. case UART_CB_RX_TIMEOUT:
  388. #ifdef __BUILD_APP__
  389. Uart_RxBufferCB(DBG_UART_ID, prvDBGCtrl.Fun);
  390. break;
  391. #endif
  392. case UART_CB_RX_NEW:
  393. #ifndef __BUILD_APP__
  394. case DMA_CB_DONE:
  395. DMA_StopStream(DBG_UART_RX_DMA_STREAM);
  396. Length = DMA_GetDataLength(DBG_UART_RX_DMA_STREAM, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0]);
  397. Length += Uart_FifoRead(DBG_UART_ID, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][Length]);
  398. LastRxSn = prvDBGCtrl.RxDMASn;
  399. prvDBGCtrl.RxDMASn = (prvDBGCtrl.RxDMASn + 1)%DBG_RXBUF_BAND;
  400. Uart_DMARx(DBG_UART_ID, DBG_UART_RX_DMA_STREAM, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0], DBG_RXBUF_SIZE);
  401. if (Length)
  402. {
  403. prvDBGCtrl.Fun(&prvDBGCtrl.RxDMABuf[LastRxSn][0], Length);
  404. }
  405. #else
  406. // Length = Uart_FifoRead(DBG_UART_ID, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0]);
  407. // LastRxSn = prvDBGCtrl.RxDMASn;
  408. // prvDBGCtrl.RxDMASn = (prvDBGCtrl.RxDMASn + 1)%DBG_RXBUF_BAND;
  409. #endif
  410. break;
  411. case UART_CB_ERROR:
  412. // DBG("%x", pParam);
  413. break;
  414. }
  415. }
  416. void DBG_Response(uint8_t Cmd, uint8_t Result, uint8_t *Data, uint8_t Len)
  417. {
  418. uint8_t Temp[260];
  419. uint16_t TxLen, Check;
  420. Temp[0] = Cmd;
  421. Temp[1] = Result;
  422. Temp[2] = Len;
  423. memcpy(Temp + 3, Data, Len);
  424. Check = CRC16Cal(Temp, Len + 3, CRC16_CCITT_SEED, CRC16_CCITT_GEN, 0);
  425. BytesPutLe16(&Temp[Len + 3], Check);
  426. TxLen = TransferPack(JTT_PACK_FLAG, JTT_PACK_CODE, JTT_PACK_CODE_F1, JTT_PACK_CODE_F2, Temp, Len + 5, prvDBGCtrl.TxBuf);
  427. Uart_BufferTx(DBG_UART_ID, prvDBGCtrl.TxBuf, TxLen);
  428. }
  429. static int32_t DBG_RebootTimeoutCB(void *pData, void *pParam)
  430. {
  431. NVIC_SystemReset();
  432. }
  433. static void DBG_Reboot(uint8_t *Data, uint32_t Len)
  434. {
  435. #ifdef __BUILD_APP__
  436. timer_t *Timer = Timer_Create(DBG_RebootTimeoutCB, NULL, NULL);
  437. Timer_StartMS(Timer, 100, 1);
  438. Uart_DeInit(DBG_UART_ID);
  439. prvDBGCtrl.AppMode = 0;
  440. #else
  441. NVIC_SystemReset();
  442. #endif
  443. }
  444. static void DBG_FlashDownloadStart(uint8_t *Data, uint32_t Len)
  445. {
  446. #ifdef __BUILD_APP__
  447. #else
  448. if (Len < 12)
  449. {
  450. DBG_Response(DBG_CMD_FLASHDOWNLOADSTART, ERROR_PARAM_INVALID, &Len, 4);
  451. return;
  452. }
  453. uint32_t StartAddress = BytesGetLe32(Data);
  454. uint32_t TotalLen = BytesGetLe32(Data + 4);
  455. uint32_t CRC32 = BytesGetLe32(Data + 8);
  456. if (BL_StartNewDownload(StartAddress, TotalLen, CRC32))
  457. {
  458. DBG_Response(DBG_CMD_FLASHDOWNLOADSTART, ERROR_PERMISSION_DENIED, &Len, 4);
  459. return;
  460. }
  461. else
  462. {
  463. DBG_Response(DBG_CMD_FLASHDOWNLOADSTART, ERROR_NONE, &Len, 4);
  464. return;
  465. }
  466. #endif
  467. }
  468. static void DBG_FlashDownload(uint8_t *Data, uint32_t Len)
  469. {
  470. #ifdef __BUILD_APP__
  471. #else
  472. if (Len < 7)
  473. {
  474. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_PARAM_INVALID, &Len, 4);
  475. return;
  476. }
  477. uint32_t PacketSn = BytesGetLe32(Data);
  478. uint32_t NextPacketSn;
  479. int Result;
  480. uint16_t PacketLen = BytesGetLe32(Data + 4);
  481. if ( (PacketLen + 6) != Len )
  482. {
  483. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_PARAM_INVALID, &Len, 4);
  484. return;
  485. }
  486. else
  487. {
  488. Result = BL_DownloadAddData(PacketSn, Data + 6, PacketLen, &NextPacketSn);
  489. if (Result >= 0)
  490. {
  491. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_NONE, &Result, 4);
  492. }
  493. else
  494. {
  495. DBG_Response(DBG_CMD_FLASHDOWNLOAD, ERROR_PARAM_OVERFLOW, &NextPacketSn, 4);
  496. }
  497. return;
  498. }
  499. #endif
  500. }
  501. static void DBG_FlashDownloadEnd(uint8_t *Data, uint32_t Len)
  502. {
  503. #ifdef __BUILD_APP__
  504. #else
  505. uint8_t Result = BL_DownloadEnd();
  506. DBG_Response(DBG_CMD_FLASHDOWNLOADEND, Result, &Result, 1);
  507. #endif
  508. }
  509. static void DBG_RunApp(uint8_t *Data, uint32_t Len)
  510. {
  511. #ifdef __BUILD_APP__
  512. #else
  513. uint8_t Result = BL_RunAPP();
  514. DBG_Response(DBG_CMD_RUNAPP, Result, &Result, 1);
  515. #endif
  516. }
  517. static const CBDataFun_t DBG_ProtoclFunlist[] =
  518. {
  519. DBG_Reboot,
  520. DBG_FlashDownloadStart,
  521. DBG_FlashDownload,
  522. DBG_FlashDownloadEnd,
  523. DBG_RunApp,
  524. };
  525. static int32_t DBG_DummyRx(void *pData, void *pParam)
  526. {
  527. uint32_t DelLen;
  528. uint32_t i, DealLen;
  529. uint16_t Check, Check2;
  530. //Buffer_Struct Buffer;
  531. uint8_t FindHead = 0;
  532. uint8_t FindEnd = 0;
  533. uint8_t NoMoreData = 0;
  534. int32_t Result;
  535. Buffer_Struct *RxBuf = &prvDBGCtrl.RxBuffer;
  536. DBG("%u", pParam);
  537. if (pParam)
  538. {
  539. #ifdef __BUILD_APP__
  540. OS_BufferWrite(&prvDBGCtrl.RxBuffer, (uint8_t *)pData, (uint32_t)pParam);
  541. #else
  542. Buffer_StaticWrite(&prvDBGCtrl.RxBuffer, (uint8_t *)pData, (uint32_t)pParam);
  543. #endif
  544. while(!NoMoreData)
  545. {
  546. if (!RxBuf->Pos)
  547. {
  548. // DBG("no more net rx data");
  549. break;
  550. }
  551. if (!FindHead)
  552. {
  553. for(i = 0; i < RxBuf->Pos; i++)
  554. {
  555. if (JTT_PACK_FLAG == RxBuf->Data[i])
  556. {
  557. OS_BufferRemove(RxBuf, i);
  558. FindHead = 1;
  559. break;
  560. }
  561. }
  562. if (!FindHead)
  563. {
  564. NoMoreData = 1;
  565. RxBuf->Pos = 0;
  566. continue;
  567. }
  568. }
  569. FindEnd = 0;
  570. DealLen = 0;
  571. DelLen = 0;
  572. for(i = 1; i < RxBuf->Pos; i++)
  573. {
  574. if (JTT_PACK_FLAG == RxBuf->Data[i])
  575. {
  576. DelLen = i + 1;
  577. // DBG("msg rec len %dbyte", DelLen);
  578. DealLen = i + 1;
  579. FindEnd = 1;
  580. break;
  581. }
  582. }
  583. if (!FindEnd)
  584. {
  585. // DBG("rest %dbyte", RxBuf->Pos);
  586. NoMoreData = 1;
  587. continue;
  588. }
  589. if (2 == DealLen) //连续2个0x7E,去除第一个,第二个作为头开始重新搜索
  590. {
  591. // DBG("!");
  592. OS_BufferRemove(RxBuf, 1);
  593. FindHead = 1;
  594. continue;
  595. }
  596. if (FindEnd)
  597. {
  598. FindHead = 0;
  599. if (DelLen > DBG_BUF_SIZE)
  600. {
  601. DBG("msg too long, %u", DelLen);
  602. OS_BufferRemove(RxBuf, DelLen);
  603. continue;
  604. }
  605. DealLen = TransferUnpack(JTT_PACK_FLAG, JTT_PACK_CODE, JTT_PACK_CODE_F1, JTT_PACK_CODE_F2,
  606. &RxBuf->Data[1], DealLen - 2, prvDBGCtrl.RxData);
  607. OS_BufferRemove(RxBuf, DelLen);
  608. if (DealLen >= 5)
  609. {
  610. Check = CRC16Cal(prvDBGCtrl.RxData, DealLen - 2, CRC16_CCITT_SEED, CRC16_CCITT_GEN, 0);
  611. Check2 = BytesGetLe16(&prvDBGCtrl.RxData[DealLen - 2]);
  612. if (Check != Check2)
  613. {
  614. // DBG_BlHexPrintf(Data, Len);
  615. DBG("check error %04x %04x", Check, Check2);
  616. DBG_Response(prvDBGCtrl.RxData[0], ERROR_PROTOCL, &Result, 4);
  617. RxBuf->Pos = 0;
  618. return 0;
  619. }
  620. else
  621. {
  622. DealLen -= 2;
  623. if (prvDBGCtrl.RxData[0] < sizeof(DBG_ProtoclFunlist)/sizeof(CBDataFun_t))
  624. {
  625. DBG_ProtoclFunlist[prvDBGCtrl.RxData[0]](prvDBGCtrl.RxData + 1, DealLen - 1);
  626. }
  627. }
  628. }
  629. else
  630. {
  631. DBG("%d", DealLen);
  632. }
  633. }
  634. }
  635. }
  636. return 0;
  637. }
  638. void DBG_Init(uint8_t AppMode)
  639. {
  640. prvDBGCtrl.AppMode = AppMode;
  641. GPIO_Iomux(DBG_UART_TX_PIN, DBG_UART_TX_AF);
  642. GPIO_Iomux(DBG_UART_RX_PIN, DBG_UART_RX_AF);
  643. #ifdef __BUILD_APP__
  644. if (AppMode)
  645. {
  646. OS_InitBuffer(&prvDBGCtrl.LogBuffer, DBG_BUF_SIZE * 4);
  647. OS_InitBuffer(&prvDBGCtrl.RxBuffer, DBG_BUF_SIZE);
  648. //Buffer_StaticInit(&prvDBGCtrl.LogBuffer, prvDBGCtrl.Data, sizeof(prvDBGCtrl.Data));
  649. Uart_BaseInit(DBG_UART_ID, DBG_UART_BR, 1, UART_DATA_BIT8, UART_PARITY_NONE, UART_STOP_BIT1, DBG_UartIrqCB);
  650. Uart_EnableRxIrq(DBG_UART_ID);
  651. DMA_TakeStream(DBG_UART_TX_DMA_STREAM);
  652. Uart_DMATxInit(DBG_UART_ID, DBG_UART_TX_DMA_STREAM, DBG_UART_TX_DMA_CHANNEL);
  653. prvDBGCtrl.TxFun = add_printf_data;
  654. }
  655. else
  656. #endif
  657. {
  658. Buffer_StaticInit(&prvDBGCtrl.LogBuffer, prvDBGCtrl.Data, sizeof(prvDBGCtrl.Data));
  659. Buffer_StaticInit(&prvDBGCtrl.RxBuffer, prvDBGCtrl.CacheData, sizeof(prvDBGCtrl.CacheData));
  660. Uart_BaseInit(DBG_UART_ID, BL_UART_BR, 0, UART_DATA_BIT8, UART_PARITY_NONE, UART_STOP_BIT1, DBG_UartIrqCB);
  661. DMA_TakeStream(DBG_UART_RX_DMA_STREAM);
  662. Uart_DMARxInit(DBG_UART_ID, DBG_UART_RX_DMA_STREAM, DBG_UART_RX_DMA_CHANNEL);
  663. Uart_DMARx(DBG_UART_ID, DBG_UART_RX_DMA_STREAM, &prvDBGCtrl.RxDMABuf[prvDBGCtrl.RxDMASn][0], DBG_RXBUF_SIZE);
  664. }
  665. prvDBGCtrl.InitDone = 1;
  666. prvDBGCtrl.Fun = DBG_DummyRx;
  667. }
  668. void DBG_SetRxCB(CBFuncEx_t cb)
  669. {
  670. prvDBGCtrl.Fun = cb;
  671. }
  672. void DBG_SetTxCB(CBDataFun_t cb)
  673. {
  674. prvDBGCtrl.TxFun = cb;
  675. }