uart_monitor.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /************************************************************************
  2. Project: Welling FAN Motor Developing platform
  3. Filename: uart_monitor.c
  4. Partner Filename: uart_monitor.h
  5. Description: Partner file of uart_monitor.h
  6. Complier: IAR Embedded Workbench for ARM 7.80, IAR Systems.
  7. CPU TYPE : ST32F0xx
  8. *************************************************************************
  9. Copyright (c) 2019 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. 20191225, by cyf, create this file;
  15. ************************************************************************/
  16. /************************************************************************
  17. Beginning of File, do not put anything above here except notes
  18. Compiler Directives:
  19. *************************************************************************/
  20. #ifndef _UART_MONITOR_C_
  21. #define _UART_MONITOR_C_
  22. #endif
  23. #define FALSE (0U)
  24. #define TRUE (1U)
  25. /************************************************************************
  26. Included File:
  27. *************************************************************************/
  28. #include "uart_monitor.h"
  29. //#include "api.h"
  30. #include "board_config.h"
  31. #include "spdctrFSM.h"
  32. /************************************************************************
  33. Exported Functions:
  34. *************************************************************************/
  35. ULONG TempPara1=11;
  36. ULONG TempPara2=22;
  37. ULONG TempPara3=33;
  38. ULONG TempPara4=44;
  39. /*--------------------------------------------------------------------------------
  40. Parameter group (R/W)
  41. --------------------------------------------------------------------------------*/
  42. UWORD* UART_pParaTable[PARA_TABLE_SIZE]=
  43. {
  44. HIGH_WORD(dummy32) , // 000 P-00(Reserve) Never use!!
  45. LOW_WORD(dummy32) , // 001 P-00(Reserve) Never use!!
  46. /*--------------------------------------------------------------------------------
  47. 32-bits parameters (signed or unsigned)
  48. --------------------------------------------------------------------------------*/
  49. //--------P01----------
  50. HIGH_WORD(TempPara4) , // 002 P-01(high word)
  51. LOW_WORD(TempPara4) , // 003 P-01(low word)
  52. //--------P02----------
  53. HIGH_WORD(TempPara4) , // 004 P-02(high word)
  54. LOW_WORD(TempPara4) , // 005 P-02(low word)
  55. //--------P03----------
  56. HIGH_WORD(TempPara4) , // 006 P-03(high word)
  57. LOW_WORD(TempPara4) , // 007 P-03(low word)
  58. //--------P04----------
  59. HIGH_WORD(TempPara4) , // 008 P-04(high word)
  60. LOW_WORD(TempPara4) , // 009 P-04(low word)
  61. //--------P05----------
  62. HIGH_WORD(dummy32) , // 010 P-05(high word)
  63. LOW_WORD(dummy32) , // 011 P-05(low word)
  64. //--------P06----------
  65. HIGH_WORD(dummy32) , // 012 P-06(high word)
  66. LOW_WORD(dummy32) , // 013 P-06(low word)
  67. /*--------------------------------------------------------------------------------
  68. 16-bits parameters (signed or unsigned)
  69. --------------------------------------------------------------------------------*/
  70. //--------P07----------
  71. &placeholder[0] , // 014 Don't change!
  72. LOW_WORD(dummy32) , // 015 P-07(one word)
  73. //--------P08----------
  74. &placeholder[1] , // 016 Don't change!
  75. LOW_WORD(dummy32) , // 017 P-08(one word)
  76. //--------P09----------
  77. &placeholder[2] , // 018 Don't change!
  78. LOW_WORD(dummy32) , // 019 P-09(one word)
  79. //--------P10----------
  80. &placeholder[3] , // 020 Don't change!
  81. LOW_WORD(dummy32) // 021 P-10(one word)
  82. };
  83. /***************************************************************
  84. Function: uart_voMonitorInit
  85. Description: Initialize hardware
  86. Call by: hwsetup;
  87. Input Variables: N/A
  88. Output/Return Variables: N/A
  89. Subroutine Call: ...;
  90. Reference: N/A
  91. ****************************************************************/
  92. void uart_voMonitorInit(void)
  93. {
  94. UBYTE i = 0;
  95. UART_fpError = (CB_ERROR)CBError_monitor; // Store CallBack function pointers
  96. uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer,uart_voCBDoneRead);
  97. //--- insert stack initialization ---
  98. for(i=0;i<MSG_QUEUE_SIZE;i++)
  99. {
  100. UART_stMsgQueue[i].stMsg.enType = UART_NULL;
  101. if(i==(MSG_QUEUE_SIZE-1))
  102. {
  103. UART_stMsgQueue[i].pNext = &UART_stMsgQueue[0];
  104. }
  105. else
  106. {
  107. UART_stMsgQueue[i].pNext = &UART_stMsgQueue[i+1];
  108. }
  109. }
  110. UART_Config = UART_stConfigLEN;
  111. }
  112. /***************************************************************
  113. Function
  114. Description:
  115. Call by: main;
  116. Input Variables: N/A
  117. Output/Return Variables: N/A
  118. Subroutine Call: ...;
  119. Reference: N/A
  120. ****************************************************************/
  121. void CBDoneRead2(UART_ERR_Type _err, ULONG _NumBytes)
  122. {
  123. switch(_err)
  124. {
  125. case UART_ERR_OK:
  126. uart_DataNum2 = _NumBytes; // copy received data number
  127. break;
  128. case UART_ERR_TIMEOUT:
  129. break;
  130. default:
  131. uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);//ly add
  132. break;
  133. }
  134. }
  135. /***************************************************************
  136. Function
  137. Description: Query flag ,then callback decode function
  138. Call by: main;
  139. Input Variables: N/A
  140. Output/Return Variables: N/A
  141. Subroutine Call: ...;
  142. Reference: N/A
  143. ****************************************************************/
  144. void uart_voMainDec(void)
  145. {
  146. if(UART_uwRxNum)
  147. {
  148. uart_voDecode();
  149. UART_uwRxNum = 0; // clear UART_uwRxNum for the next read !!
  150. uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer,uart_voCBDoneRead);
  151. }
  152. }
  153. /***************************************************************
  154. Function
  155. Description: decode instruction
  156. Call by: UART_voMainDec();
  157. Input Variables: N/A
  158. Output/Return Variables: N/A
  159. Subroutine Call: ...;
  160. Reference: N/A
  161. ****************************************************************/
  162. void uart_voDecode(void)
  163. {
  164. UWORD i;
  165. UBYTE temp_ubCheckCode;
  166. ULONG temp_ulAddr[CHANNEL_NUM];
  167. UART_MSG twmp_stMesg;
  168. //---------------------- Serial-X application -----------------------------
  169. if(UART_uwRxNum == UART_CMD_LENGTH)
  170. {
  171. temp_ubCheckCode = uart_ubCheckXOR(UART_ubReadBuffer,UART_CMD_LENGTH-1);
  172. if(temp_ubCheckCode == UART_ubReadBuffer[UART_CMD_LENGTH-1])
  173. {
  174. //++++++++++++++++++++ decode monitor address ++++++++++++++++++++++++
  175. if((UART_ubReadBuffer[0]==0x11)&&(UART_ubReadBuffer[1]==0x00))
  176. {
  177. if(UART_ubReadBuffer[UART_CMD_LENGTH-2])
  178. {
  179. UART_bMonSwitch = TRUE;
  180. }
  181. else
  182. {
  183. UART_bMonSwitch = FALSE;
  184. }
  185. if(UART_bMonSwitch)// *Application Specific*
  186. {
  187. temp_ulAddr[0] = 0x20000000 + ((ULONG)UART_ubReadBuffer[2] << 16) + ((ULONG)UART_ubReadBuffer[3] << 8) + ((ULONG)UART_ubReadBuffer[4] );
  188. temp_ulAddr[1] = 0x20000000 + ((ULONG)UART_ubReadBuffer[5] << 16) + ((ULONG)UART_ubReadBuffer[6] << 8) + ((ULONG)UART_ubReadBuffer[7] );
  189. temp_ulAddr[2] = 0x20000000 + ((ULONG)UART_ubReadBuffer[8] << 16) + ((ULONG)UART_ubReadBuffer[9] << 8) + ((ULONG)UART_ubReadBuffer[10]);
  190. temp_ulAddr[3] = 0x20000000 + ((ULONG)UART_ubReadBuffer[11] << 16) + ((ULONG)UART_ubReadBuffer[12] << 8) + ((ULONG)UART_ubReadBuffer[13]);
  191. temp_ulAddr[4] = 0x20000000 + ((ULONG)UART_ubReadBuffer[14] << 16) + ((ULONG)UART_ubReadBuffer[15] << 8) + ((ULONG)UART_ubReadBuffer[16]);
  192. temp_ulAddr[5] = 0x20000000 + ((ULONG)UART_ubReadBuffer[17] << 16) + ((ULONG)UART_ubReadBuffer[18] << 8) + ((ULONG)UART_ubReadBuffer[19]);
  193. /* handle address */
  194. for(i=0; i<CHANNEL_NUM; i++)// *Application Specific*
  195. {
  196. if(temp_ulAddr[i] >= 0x20000000 && temp_ulAddr[i] <= HW_RAM_END_ADDRESS) // STM32F03x SRAM range
  197. {
  198. UART_pMonAddr[i] = (unsigned short *)(temp_ulAddr[i]&0xfffffffe);
  199. }
  200. }
  201. }
  202. else
  203. {
  204. UART_uwWriteIndex = 4; // restore initial WriterBufferIndex
  205. }
  206. return;
  207. }
  208. //++++++++++++++++++++ decode R/W Parameter command ++++++++++++++++++++++++
  209. if((UART_ubReadBuffer[0]==0x11)&&(UART_ubReadBuffer[1]==0x10))
  210. {
  211. uart_voHandleParaCmd();
  212. return;
  213. }
  214. //++++++++++++++++++++ decode drying machine command ++++++++++++++++++++++++
  215. if((UART_ubReadBuffer[0]==0x02)&&(UART_ubReadBuffer[1]==0x01))
  216. {
  217. if(UART_ubReadBuffer[2] == 0x11) // query command
  218. {
  219. twmp_stMesg.enType = UART_SEND_STATUS;
  220. twmp_stMesg.pBuf = NULL;
  221. twmp_stMesg.uwNumBytes =0;
  222. uart_voPostMsg(twmp_stMesg);
  223. }
  224. return;
  225. }
  226. }
  227. }
  228. }
  229. /***************************************************************
  230. Function
  231. Description: callback called when error occurs
  232. Call by: ;
  233. Input Variables: N/A
  234. Output/Return Variables: N/A
  235. Subroutine Call: ...;
  236. Reference: N/A
  237. ****************************************************************/
  238. void CBError_monitor(UART_ERR_Type _err)
  239. {
  240. // switch (_err)
  241. // {
  242. // case UART_ERR_TIMEOUT:
  243. // DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN);
  244. // DMA1->IFCR = DMA_ISR_GIF3;
  245. // DMA1_Channel3->CNDTR = 22;
  246. // DMA1_Channel3->CCR |= DMA_CCR_EN;
  247. // break;
  248. // case UART_ERR_NO_BUFFER:
  249. // break;
  250. // default:
  251. // break;
  252. // }
  253. }
  254. /***************************************************************
  255. Function
  256. Description: Callback called when some data received.
  257. Call by: ;
  258. Input Variables: N/A
  259. Output/Return Variables: N/A
  260. Subroutine Call: ...;
  261. Reference: N/A
  262. ****************************************************************/
  263. void uart_voCBDoneRead(UART_ERR_Type _err, ULONG _NumBytes)
  264. {
  265. switch(_err)
  266. {
  267. case UART_ERR_OK:
  268. UART_uwRxNum = _NumBytes; // copy received data number
  269. break;
  270. case UART_ERR_TIMEOUT:
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. /***************************************************************
  277. Function
  278. Description: used to send monitor data out
  279. Call by: tbc_voIsr();
  280. Input Variables: N/A
  281. Output/Return Variables: N/A
  282. Subroutine Call: ...;
  283. Reference: N/A
  284. ****************************************************************/
  285. void uart_voAppMonitor(void)
  286. {
  287. // if(UART_bMonSwitch)
  288. // {
  289. // UBYTE temp_ubBuf[TEMP_BUFFER_LEN];
  290. // UBYTE i;
  291. // //------ load monitor channels data (general) --------//
  292. // for(i=0;i<(TEMP_BUFFER_LEN>>1);i++) // less than half of TEMP_BUFFER_LEN
  293. // {
  294. // temp_ubBuf[i*2] = (*UART_pMonAddr[i])>>8; //Data.hi
  295. // temp_ubBuf[i*2+1] = (*UART_pMonAddr[i])&0xff; //Data.lo
  296. // }
  297. // //------ loading data from tempBuffer to Writebuffer and send out when Writebuffer is full -------//
  298. // for(i = 0; i < TEMP_BUFFER_LEN; i++)
  299. // {
  300. // UART_pWriteBuffer[UART_uwWriteIndex] = temp_ubBuf[i]; // write latest data//data adrress
  301. // if(UART_uwWriteIndex == (S_FRAME_SIZE-1)) //if full, start Write_Async
  302. // {
  303. // if(!UART_stParaStatus.bWriteBusy)
  304. // {
  305. // UART_stParaStatus.bWriteBusy = TRUE;
  306. // if((UART_bInsertPendTx==TRUE)&&(UART_pWriteBuffer == UART_ubWriteBuffer2)) // send writeBuffer and insertBuffer
  307. // {
  308. // UART_stParaStatus.bParaStart = TRUE;
  309. // iUart_WriteAsync(0, UART_pWriteBuffer, S_FRAME_SIZE+16, uart_TxCompleteCallback, uart_TxErrorCallback);
  310. // }
  311. // else
  312. // {
  313. // UART_stParaStatus.bParaStart = FALSE;
  314. // iUart_WriteAsync(0, UART_pWriteBuffer, S_FRAME_SIZE, uart_TxCompleteCallback, uart_TxErrorCallback);
  315. // }
  316. // if(UART_pWriteBuffer == UART_ubWriteBuffer1)
  317. // {
  318. // UART_pWriteBuffer = UART_ubWriteBuffer2;
  319. // } //dual buffer swap.
  320. // else
  321. // {
  322. // UART_pWriteBuffer = UART_ubWriteBuffer1;
  323. // }
  324. // //initialize UART_uwWriteIndex
  325. // UART_uwWriteIndex = 4; // utilize later 60bytes of 64bytes of write-frame
  326. // }
  327. // else
  328. // {
  329. // UART_uwWriteIndex = 4;
  330. // }
  331. // }
  332. // else
  333. // {
  334. // UART_uwWriteIndex++;
  335. // } //index increase to next write position
  336. // }
  337. // }
  338. }
  339. /***************************************************************
  340. Function
  341. Description: post message
  342. Call by: main;
  343. Input Variables: N/A
  344. Output/Return Variables: N/A
  345. Subroutine Call: ...;
  346. Reference: N/A
  347. ****************************************************************/
  348. void uart_voPostMsg(UART_MSG _msg)
  349. {
  350. if((UART_stMsgQ.pSave == UART_stMsgQ.pUse)&&(UART_stMsgQ.bValid == TRUE))
  351. {
  352. return;// if UART_stMsgQueue is full, just return
  353. }
  354. UART_stMsgQ.pSave->stMsg = _msg;
  355. UART_stMsgQ.pSave = UART_stMsgQ.pSave->pNext;
  356. UART_stMsgQ.bValid = TRUE;
  357. }
  358. /***************************************************************
  359. Function
  360. Description: handle parameter related command
  361. Call by: UART_voDecode;
  362. Input Variables: N/A
  363. Output/Return Variables: N/A
  364. Subroutine Call: ...;
  365. Reference: N/A
  366. ****************************************************************/
  367. void uart_voHandleParaCmd(void)
  368. {
  369. UWORD i;
  370. UWORD temp_uwIndex;
  371. UWORD temp_HiWord,temp_LowWord;
  372. UART_MSG temp_stMsg;
  373. if(UART_ubReadBuffer[2] == 0x01) // Read parameters command
  374. {
  375. for(i=2;i<PARA_TABLE_SIZE;i+=2)
  376. {
  377. //----- check 16bits data's sign ------
  378. if(i>=14)
  379. {
  380. if(*UART_pParaTable[i+1]&0x8000)
  381. {
  382. *UART_pParaTable[i] = 0xFFFF;
  383. } // if data16<0
  384. else
  385. {
  386. *UART_pParaTable[i] = 0x0000;
  387. }
  388. }
  389. // ----- load value (little-endian)-----
  390. UART_ubParaBuffer[i*2-4] = (UBYTE)((*UART_pParaTable[i])>>8);
  391. UART_ubParaBuffer[i*2-3] = (UBYTE)((*UART_pParaTable[i])&0x00FF);
  392. UART_ubParaBuffer[i*2-2] = (UBYTE)((*UART_pParaTable[i+1])>>8);
  393. UART_ubParaBuffer[i*2-1] = (UBYTE)((*UART_pParaTable[i+1])&0x00FF);
  394. }
  395. temp_stMsg.enType = UART_SEND_PARA;
  396. temp_stMsg.pBuf = UART_ubParaBuffer;
  397. temp_stMsg.uwNumBytes = PARA_BUFFER_SIZE ;
  398. uart_voPostMsg(temp_stMsg); // post message
  399. }
  400. if(UART_ubReadBuffer[2] == 0x02) // Write Parameters command
  401. {
  402. temp_uwIndex = UART_ubReadBuffer[3];
  403. temp_HiWord = ((UWORD)UART_ubReadBuffer[4]<<8) + ((UWORD)UART_ubReadBuffer[5]);
  404. temp_LowWord = ((UWORD)UART_ubReadBuffer[6]<<8) +((UWORD)UART_ubReadBuffer[7]);
  405. *UART_pParaTable[temp_uwIndex] = temp_HiWord;
  406. *UART_pParaTable[temp_uwIndex+1] = temp_LowWord;
  407. }
  408. }
  409. /***************************************************************
  410. Function
  411. Description: Query if there is a message in msgQueue
  412. Call by: SysTick_Handler;
  413. Input Variables: N/A
  414. Output/Return Variables: N/A
  415. Subroutine Call: ...;
  416. Reference: N/A
  417. ****************************************************************/
  418. void uart_voApplTimer(void)
  419. {
  420. // UBYTE i = 0;
  421. // if(UART_stMsgQ.bValid) // if there is a message in msgQueue
  422. // {
  423. // //------------- fetch message ---------------//
  424. // if(UART_stMsgQ.bHandling == FALSE) // if there isn't any MSG under handling.
  425. // {
  426. // if(UART_stMsgQ.pUse != UART_stMsgQ.pSave)
  427. // {
  428. // UART_stMsgFetched = UART_stMsgQ.pUse->stMsg;// fetch message
  429. // UART_stMsgQ.pUse = UART_stMsgQ.pUse->pNext; // shift to next UART_stMsgQueue position
  430. // UART_stMsgQ.bHandling = TRUE; //set bMsgHandling flag, prevent from fetching new msg from UART_stMsgQueue.
  431. // }
  432. // else
  433. // {
  434. // UART_stMsgQ.bValid = FALSE; // invalid UART_stMsgQueue(all message has been handled, prevent from calling UART_voHandleMsg again)
  435. // return;
  436. // }
  437. // }
  438. // //--------------------------------------------
  439. // if(UART_bInsertPendTx)
  440. // {
  441. // return;// if insertBuffer is pending to be transmitted, just return to wait
  442. // }
  443. // //------------ process message --------------//
  444. // switch(UART_stMsgFetched.enType)
  445. // {
  446. // case UART_NULL: // invalid message
  447. // break;
  448. // case UART_SEND_PARA:
  449. // {
  450. // if((UART_stMsgFetched.uwNumBytes==0)||(UART_stMsgFetched.pBuf==NULL))
  451. // {
  452. // UART_stMsgQ.bHandling = FALSE; // fetched message has been processed over(invalid message)
  453. // return;
  454. // }
  455. // UART_ubInsertBuf[4] = 0x11; // parameter response frame header
  456. // UART_ubInsertBuf[5] = 0x10;
  457. // }
  458. // break;
  459. // case UART_SEND_STATUS:
  460. // {
  461. // if((UART_stMsgFetched.uwNumBytes==0)||(UART_stMsgFetched.pBuf==NULL))
  462. // {
  463. // UART_stMsgQ.bHandling = FALSE; // fetched message has been processed over(invalid message)
  464. // return;
  465. // }
  466. // UART_ubInsertBuf[4]=0x02; // application status response header
  467. // UART_ubInsertBuf[5]=0x01;
  468. // }
  469. // break;
  470. // }
  471. // if(UART_stMsgFetched.uwNumBytes > (INSERT_BUFFER_SIZE-INSERT_BUFFER_HEADER_LEN))
  472. // {
  473. // UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN-1] = 0xFF;
  474. // UART_stMsgFetched.uwNumBytes -= (INSERT_BUFFER_SIZE-INSERT_BUFFER_HEADER_LEN);// bytes left to be sent out in modbus response.
  475. // for(i=0;i<(INSERT_BUFFER_SIZE-INSERT_BUFFER_HEADER_LEN);i++)
  476. // {
  477. // UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN+i] = *UART_stMsgFetched.pBuf;
  478. // UART_stMsgFetched.pBuf++;
  479. // }
  480. // }
  481. // else
  482. // {
  483. // UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN-1] = UART_stMsgFetched.uwNumBytes;
  484. // for(i=0;i<UART_stMsgFetched.uwNumBytes;i++)
  485. // {
  486. // UART_ubInsertBuf[INSERT_BUFFER_HEADER_LEN+i] = *UART_stMsgFetched.pBuf;
  487. // UART_stMsgFetched.pBuf++;
  488. // }
  489. // UART_stMsgQ.bHandling = FALSE;// fetched message has been processed over(new message can be fetched)
  490. // }
  491. // UART_bInsertPendTx = TRUE; // insertBuffer is pending to be transmitted
  492. // //----------- transmit message -------------//
  493. // if(UART_bMonSwitch)//--- Load data in insertBuffer to WriteBuffer2 ---
  494. // {
  495. // for(i=0;i<INSERT_BUFFER_SIZE;i++)
  496. // {
  497. // UART_ubWriteBuffer2[S_FRAME_SIZE+i] = UART_ubInsertBuf[i];
  498. // }
  499. // }
  500. // else
  501. // {
  502. // UART_stParaStatus.bParaStart = TRUE;
  503. // iUart_WriteAsync(0, UART_ubInsertBuf, 16, uart_TxCompleteCallback, uart_TxErrorCallback);
  504. // }
  505. // }
  506. }
  507. /***************************************************************
  508. Function
  509. Description: XOR check when receive data.
  510. Call by: main;
  511. Input Variables: N/A
  512. Output/Return Variables: N/A
  513. Subroutine Call: ...;
  514. Reference: N/A
  515. ****************************************************************/
  516. UBYTE uart_ubCheckXOR(UBYTE* Buf, UBYTE CNT)
  517. {
  518. UWORD temp_Xor = 0;
  519. UWORD i;
  520. for(i=0;i<CNT; i++)
  521. {
  522. temp_Xor ^= Buf[i];
  523. }
  524. return temp_Xor;
  525. }
  526. /***************************************************************
  527. Function
  528. Description: Reconfigure when the data received incomletely in 1s
  529. Call by: SysTick_Handler;
  530. Input Variables: N/A
  531. Output/Return Variables: N/A
  532. Subroutine Call: ...;
  533. Reference: N/A
  534. ****************************************************************/
  535. void uart_voDriTimer(void)
  536. {
  537. // if(DMA1_Channel3->CNDTR != 22)
  538. // {
  539. // ++UART_uwTimeoutCtr;
  540. // if(UART_uwTimeoutCtr == UART_Config.uwTimeout) // Timeout match
  541. // {
  542. // UART_uwTimeoutCtr = 0;
  543. // UART_fpError(UART_ERR_TIMEOUT);
  544. // }
  545. // }
  546. // else
  547. // {
  548. // UART_uwTimeoutCtr = 0;
  549. // }
  550. }
  551. /***************************************************************
  552. Function:
  553. Description:
  554. Call by:
  555. Input Variables:
  556. Output/Return Variables:
  557. Subroutine Call:
  558. Reference: N/A
  559. ****************************************************************/
  560. void uart_Read_Poll2(ULONG _NumBytes,UBYTE* _Buffer,CB_DONE_READ _fpCBDone)
  561. {
  562. UBYTE i;
  563. UBYTE* pBuffer = _Buffer;
  564. //---clear receive data buffer!---
  565. for(i=0;i<_NumBytes;i++)
  566. {
  567. *pBuffer = 0;
  568. pBuffer++;
  569. }
  570. //---Set busy flag---
  571. USART_Read.m_ReadBusy = TRUE; // clear ReadBusy flag
  572. //---setup Read object---
  573. USART_Read.m_BuffSize = _NumBytes;
  574. USART_Read.m_DataBuff.pBuf = _Buffer;
  575. USART_Read.m_fpDone = _fpCBDone;
  576. USART_Read.m_DataBuff.NumBytes = 0;
  577. }
  578. /***************************************************************
  579. Function:
  580. Description:
  581. Call by:
  582. Input Variables:
  583. Output/Return Variables:
  584. Subroutine Call:
  585. Reference: N/A
  586. ****************************************************************/
  587. void ReadFrame_Poll2(void)
  588. {
  589. // UBYTE tempRxData;
  590. // CB_DONE_READ CBTemp;
  591. // if(NULL == USART_Read.m_DataBuff.pBuf)
  592. // {
  593. // return;
  594. // }
  595. // while (iUart_Available(0))
  596. // {
  597. // iUart_Read(0, &tempRxData, 1); // save register data
  598. // UART_blCommErrFlag = FALSE;
  599. // if(uart_frameStartFlag == FALSE) // if the frame header has not been detected.
  600. // {
  601. // if(tempRxData != 0x11)
  602. // {
  603. // return;
  604. // } // 0xB2 is LittleSwan CMD frame header byte.
  605. // else // frame header detected
  606. // {
  607. // if(USART_Read.m_DataBuff.NumBytes==0) // ready to save data
  608. // {
  609. // uart_frameStartFlag = TRUE;
  610. // uart_receivedNum = 0;
  611. // }
  612. // else // not ready to save data
  613. // {
  614. // return; // just return (wait until ready)
  615. // }
  616. // }
  617. // }
  618. // //------------------------ cmd frameheader 0xB2 has been detected -----------------------
  619. // uart_receivedNum++;
  620. // uart_frameLength = 22;
  621. // if(uart_receivedNum == uart_frameLength)
  622. // {
  623. // uart_receivedNum =0;
  624. // uart_frameStartFlag = FALSE;
  625. // }
  626. // if(UART_blCommErrFlag == FALSE)
  627. // {
  628. // if(USART_Read.m_BuffSize > USART_Read.m_DataBuff.NumBytes)
  629. // {
  630. // if(USART_Read.m_DataBuff.pBuf != NULL)
  631. // {
  632. // *USART_Read.m_DataBuff.pBuf = tempRxData;
  633. // USART_Read.m_DataBuff.pBuf++;
  634. // USART_Read.m_DataBuff.NumBytes++;
  635. // }
  636. // if(USART_Read.m_DataBuff.NumBytes == uart_frameLength)
  637. // {
  638. // USART_Read.m_DataBuff.pBuf = NULL;
  639. // USART_Read.m_BuffSize = 0;
  640. // CBTemp = USART_Read.m_fpDone;
  641. // USART_Read.m_fpDone = NULL;
  642. // USART_Read.m_ReadBusy = FALSE; // clear ReadBusy flag !
  643. // if(CBTemp != NULL)
  644. // {
  645. // CBTemp(UART_ERR_OK, uart_frameLength);
  646. // }
  647. // uart_frameLength = 0;
  648. // }
  649. // }
  650. // }
  651. // }
  652. }
  653. /***************************************************************
  654. Function:
  655. Description:
  656. Call by: main;
  657. Input Variables: N/A
  658. Output/Return Variables: N/A
  659. Subroutine Call: ...;
  660. Reference: N/A
  661. ****************************************************************/
  662. void uart_voSwanInit(void)
  663. {
  664. UART_fpError = (CB_ERROR)CBError_Swan;
  665. UART_Config = UART_stConfigLEN;
  666. uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2); // use poll method for littleswan receiving.
  667. }
  668. /***************************************************************
  669. Function
  670. Description:
  671. Call by: main;
  672. Input Variables: N/A
  673. Output/Return Variables: N/A
  674. Subroutine Call: ...;
  675. Reference: N/A
  676. ****************************************************************/
  677. void CBError_Swan(UART_ERR_Type _err)
  678. {
  679. switch (_err)
  680. {
  681. case UART_ERR_TIMEOUT:
  682. uart_DataNum2 = 0; // clear s_DataNum2 for the next read !!
  683. uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);
  684. break;
  685. case UART_ERR_NO_BUFFER:
  686. break;
  687. case UART_ERR_UNKNOWN_CMD:
  688. uart_DataNum2 = 0; // clear s_DataNum2 for the next read !!
  689. uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);
  690. break;
  691. case UART_ERR_OK:
  692. break;
  693. }
  694. }
  695. /***************************************************************
  696. Function:
  697. Description:
  698. Call by:
  699. Input Variables:
  700. Output/Return Variables:
  701. Subroutine Call:
  702. Reference: N/A
  703. ****************************************************************/
  704. //void WriteFrame_Poll2(void)
  705. //{
  706. // BOOL WriteOverFlag = FALSE;
  707. // if(NULL== USART_Write.m_DataBuff.pBuf)
  708. // {
  709. // return;
  710. // }
  711. // if(USART_Write.m_DataBuff.NumBytes != 0)
  712. // {
  713. // USART1->TDR = *USART_Write.m_DataBuff.pBuf;
  714. // USART_Write.m_DataBuff.pBuf++;
  715. // USART_Write.m_DataBuff.NumBytes--;
  716. // if(0 == USART_Write.m_DataBuff.NumBytes)
  717. // {
  718. // WriteOverFlag = TRUE;
  719. // }
  720. // }
  721. // if(TRUE == WriteOverFlag)
  722. // {
  723. // //disable TXEIE(bit7)
  724. // USART1->CR1 &= ~0x00000080;
  725. // WriteOverFlag = FALSE;
  726. // USART_Write.m_DataBuff.pBuf =NULL;
  727. // USART_Write.m_WriteBusy = FALSE;
  728. // }
  729. //}
  730. /***************************************************************
  731. Function:
  732. Description:
  733. Call by:
  734. Input Variables:
  735. Output/Return Variables:
  736. Subroutine Call:
  737. Reference: N/A
  738. ****************************************************************/
  739. void HAL_CancelRead_Poll2(void)
  740. {
  741. USART_Read.m_BuffSize = 0;
  742. uart_receivedNum =0;
  743. uart_frameLength =0;
  744. uart_frameType = 0x00;
  745. uart_frameStartFlag = FALSE;
  746. UART_fpError(UART_ERR_TIMEOUT);
  747. }
  748. /***************************************************************
  749. Function
  750. Description:
  751. Call by: main;
  752. Input Variables: N/A
  753. Output/Return Variables: N/A
  754. Subroutine Call: ...;
  755. Reference: N/A
  756. ****************************************************************/
  757. // void uart_voApplMain(void)
  758. // {
  759. // UBYTE* pBuffer = NULL;
  760. // UWORD numBytes = 0;
  761. // if(uart_DataNum2)
  762. // {
  763. // if(SwanDecoder(uart_pReadBuffer2, uart_DataNum2, &pBuffer, &numBytes))
  764. // {
  765. // uart_Write_Poll2(numBytes, pBuffer);
  766. // }
  767. // uart_DataNum2 = 0;
  768. // uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);
  769. // }
  770. // }
  771. /***************************************************************
  772. Function:
  773. Description:
  774. Call by:
  775. Input Variables:
  776. Output/Return Variables:
  777. Subroutine Call:
  778. Reference: N/A
  779. ****************************************************************/
  780. // void uart_Write_Poll2(ULONG _NumBytes,const UBYTE* _Buffer)
  781. // {
  782. // if(USART_Write.m_WriteBusy == TRUE)
  783. // {
  784. // return;
  785. // }
  786. // USART_Write.m_WriteBusy = TRUE;
  787. // USART_Write.m_DataBuff.pBuf = (UBYTE*)_Buffer;
  788. // USART_Write.m_DataBuff.NumBytes = _NumBytes;
  789. // //enable TXEIE(bit7)
  790. // USART1->CR1 |= 0x00000080;
  791. // }
  792. void uart_TxCompleteCallback()
  793. {
  794. if( UART_stParaStatus.bParaStart)
  795. {
  796. UART_bInsertPendTx = FALSE; // clear insertBuffer pending
  797. UART_stParaStatus.bParaStart = FALSE; //clear parameter status
  798. }
  799. else
  800. {
  801. //do nothing
  802. }
  803. UART_stParaStatus.bWriteBusy = FALSE;
  804. }
  805. void uart_TxErrorCallback()
  806. {
  807. if( UART_stParaStatus.bParaStart)
  808. {
  809. UART_bInsertPendTx = FALSE; // clear insertBuffer pending
  810. UART_stParaStatus.bParaStart = FALSE; //clear parameter status
  811. }
  812. UART_stParaStatus.bWriteBusy = FALSE;
  813. }
  814. /***************************************************************
  815. Function: ;crc_ccitt(polynomial:1021)
  816. Description:
  817. Call by:
  818. Input Variables: N/A
  819. Output/Return Variables: N/A
  820. Subroutine Call: ...;
  821. Reference: N/A
  822. ****************************************************************/
  823. #ifdef CRC_CCITT
  824. UWORD uart_uwCRCCcitt(UBYTE *ptubBuf, UWORD length)
  825. {
  826. UBYTE b = 0;
  827. UWORD crc = 0xffff;
  828. UWORD i, j;
  829. for (i = 0; i < length; i++)
  830. {
  831. for (j = 0; j < 8; j++)
  832. {
  833. b = ((ptubBuf[i] << j) & 0x80) ^ ((crc & 0x8000) >> 8);
  834. crc <<= 1;
  835. if (b != 0)
  836. {
  837. crc ^= 0x1021;
  838. }
  839. }
  840. }
  841. return crc;
  842. }
  843. #endif
  844. /************************************************************************
  845. Local Functions:
  846. *************************************************************************/
  847. #ifdef _UART_MONITOR_C_
  848. #undef _UART_MONITOR_C_
  849. #endif
  850. /************************************************************************
  851. Copyright (c) 2019 Welling Motor Technology(Shanghai) Co. Ltd.
  852. All rights reserved.
  853. *************************************************************************
  854. End of this File (EOF)!
  855. Do not put anything after this part!
  856. *************************************************************************/