uart_monitor.c 30 KB

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