uart_monitor_appl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /************************************************************************
  2. Project:
  3. Filename: uart_appl.h
  4. Partner Filename: N/A
  5. Description:
  6. Complier: IAR Embedded Workbench for ARM 8.40.2, IAR Systems.
  7. CPU TYPE : GD32F30x
  8. *************************************************************************
  9. Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. M0_20170410, by liyue, 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_APPL_H
  21. #define UART_MONITOR_APPL_H
  22. #include "typedefine.h"
  23. #include "uart_driver.h"
  24. /************************************************************************
  25. Compiler Directives (N/A)
  26. *************************************************************************/
  27. #ifdef _UART_MONITOR_APPL_C_
  28. #define _UART_MONITOR_APPL_EXT
  29. #else
  30. #define _UART_MONITOR_APPL_EXT extern
  31. #endif
  32. /************************************************************************
  33. Definitions & Macros
  34. *************************************************************************/
  35. #define INSERT_BUFFER_SIZE 16
  36. #define INSERT_BUFFER_HEADER_LEN 7
  37. #define ONE_SECOND_COUNT 1000
  38. #define CMD_LENGTH 22
  39. #define TX_BUFFER_SIZE 150
  40. #define RX_BUFFER_SIZE 24
  41. #define MSG_QUEUE_SIZE 10
  42. #define PARA_TABLE_SIZE 22
  43. #define PARA_BUFFER_SIZE 40
  44. #define LOW_WORD(var) ((UWORD *)(&(var))) // little-endian
  45. #define HIGH_WORD(var) ((UWORD *)(&(var)) + 1)
  46. /************** data monitor application ***************/
  47. /*******************************************************/
  48. /* Channel Number Time(sample+DMA/sample)(us) */
  49. /* 3 6.4/4.8 */
  50. /* 4 7.6/5.9 */
  51. /* 5 8.9/7.3 */
  52. /* 6 10.1/8.6 */
  53. /*******************************************************/
  54. /* TBC Channels Selection */
  55. /* 5.3k 3/4/5/6 */
  56. /* 8k 3/4/5/6 */
  57. /* 12k 3/4 */
  58. /*******************************************************/
  59. #define CHANNEL_NUM 6 // channel number
  60. #define CHANNEL_1 2 // 1 channel correspond to 2 bytes
  61. #define CHANNEL_2 4
  62. #define CHANNEL_3 6
  63. #define CHANNEL_4 8
  64. #define CHANNEL_5 10
  65. #define CHANNEL_6 12
  66. #define TEMP_BUFFER_LEN CHANNEL_5 // temp buffer length is based on the choice of channel number.
  67. #if TEMP_BUFFER_LEN == CHANNEL_6
  68. #define S_FRAME_SIZE 124
  69. #elif TEMP_BUFFER_LEN == CHANNEL_5
  70. #define S_FRAME_SIZE 104
  71. #elif TEMP_BUFFER_LEN == CHANNEL_4
  72. #define S_FRAME_SIZE 84
  73. #elif TEMP_BUFFER_LEN == CHANNEL_3
  74. #define S_FRAME_SIZE 64
  75. #else
  76. // do nothing!
  77. #endif
  78. #define UART_DEF_TIMEOUT_MS 1000 // default timeout const(ms)
  79. #define UART_DEF_CMD_LEN 0 // default command frame length
  80. //*** define USART Driver configuration objects ***
  81. // **length-mode configuration object**
  82. #define UART_APPL_CONFIG_DEFAULT \
  83. { \
  84. UART_MODE_LEN, UART_DEF_TIMEOUT_MS, CMD_LENGTH, NULL \
  85. }
  86. #define UART_MsgQ_CONFIG_DEFAULT \
  87. { \
  88. UART_stMsgQueue, UART_stMsgQueue, FALSE, FALSE \
  89. }
  90. /************************************************************************
  91. TypeDefs & Structure defines (N/A)
  92. *************************************************************************/
  93. /* USART work mode type */
  94. typedef enum UART_WorkMode
  95. {
  96. UART_MODE_TIME, // time interval framing mode
  97. UART_MODE_LEN, // fixed length framing mode
  98. UART_MODE_CHAR // fixed chars framing mode
  99. } UART_WorkMode_Type;
  100. typedef struct UART_CONFIG
  101. {
  102. UART_WorkMode_Type enWorkMode;
  103. UWORD uwTimeout; // unit:count
  104. UBYTE ubLength; // unit:bytes
  105. UBYTE * pChar;
  106. } UART_CONFIG_Type;
  107. typedef enum MsgType
  108. {
  109. UART_NULL = 0, // NULL message will not be processed
  110. UART_SEND_STATUS = 1, // send out Application status
  111. UART_SEND_PARA = 2 // send out Parameter value
  112. } UART_MsgType;
  113. typedef struct MSG
  114. {
  115. UART_MsgType enType;
  116. UBYTE * pBuf;
  117. UWORD uwNumBytes;
  118. } UART_MSG;
  119. typedef struct MsgQNode
  120. {
  121. UART_MSG stMsg;
  122. struct MsgQNode *pNext;
  123. } UART_MsgQNodeType;
  124. typedef struct MsgQType
  125. {
  126. UART_MsgQNodeType *pSave;
  127. UART_MsgQNodeType *pUse;
  128. BOOL bValid;
  129. BOOL bHandling;
  130. } UART_MsgQType;
  131. typedef struct ParaData
  132. {
  133. BOOL bParaStart;
  134. BOOL bWriteBusy;
  135. } UART_ParaData_Flag;
  136. /************************************************************************
  137. Exported Variables
  138. *************************************************************************/
  139. #ifdef _UART_MONITOR_APPL_C_
  140. _UART_MONITOR_APPL_EXT UWORD placeholder[4] = {0xFFFF};
  141. _UART_MONITOR_APPL_EXT ULONG dummy32 = 0x00000000;
  142. _UART_MONITOR_APPL_EXT UWORD dummy16 = 0x0000;
  143. /* parabuffer contains parameter response frame payload */
  144. _UART_MONITOR_APPL_EXT UBYTE UART_ubParaBuffer[PARA_BUFFER_SIZE] = {0};
  145. /* define Read Buffer and its pointer */
  146. _UART_MONITOR_APPL_EXT UBYTE UART_ubReadBuffer[RX_BUFFER_SIZE];
  147. /* define Write dual Buffer and its pointers */
  148. _UART_MONITOR_APPL_EXT UBYTE UART_ubWriteBuffer1[TX_BUFFER_SIZE] = {0x55, 0xAA, 0x55, 0xAA};
  149. _UART_MONITOR_APPL_EXT UBYTE UART_ubWriteBuffer2[TX_BUFFER_SIZE] = {0x55, 0xAA, 0x55, 0xAA};
  150. _UART_MONITOR_APPL_EXT UBYTE *UART_pWriteBuffer = UART_ubWriteBuffer1;
  151. _UART_MONITOR_APPL_EXT UWORD UART_uwWriteIndex = 4;
  152. /* data bytes recieved in a Read */
  153. _UART_MONITOR_APPL_EXT UWORD volatile UART_uwRxNum = 0;
  154. _UART_MONITOR_APPL_EXT UWORD writeDenyCount = 0; // HAL_Write request deny count(because of S_ERR_BUSY), this value mainly used for debug.
  155. _UART_MONITOR_APPL_EXT UART_CONFIG_Type UART_stConfigLEN = UART_APPL_CONFIG_DEFAULT;
  156. _UART_MONITOR_APPL_EXT BOOL UART_bMonSwitch = FALSE; // control monitor function whether of not write data out.
  157. _UART_MONITOR_APPL_EXT UWORD *UART_pMonAddr[6]; // the address of the variable which user want to monitor.
  158. //*** define insert buffer ***
  159. _UART_MONITOR_APPL_EXT UBYTE UART_ubInsertBuf[100] = {0x55, 0xAA, 0x55,
  160. 0xFF}; // the data in insertBuffer will be inserted into writeBuffer when enabled
  161. _UART_MONITOR_APPL_EXT volatile BOOL UART_bInsertPendTx = FALSE; // flag indicate whether insert buffer is pending to be transmitted.
  162. _UART_MONITOR_APPL_EXT UART_ParaData_Flag UART_stParaStatus = {FALSE};
  163. _UART_MONITOR_APPL_EXT UART_MsgQNodeType UART_stMsgQueue[MSG_QUEUE_SIZE]; // message queue
  164. _UART_MONITOR_APPL_EXT UART_MsgQType UART_stMsgQ = UART_MsgQ_CONFIG_DEFAULT;
  165. #else
  166. _UART_MONITOR_APPL_EXT UWORD placeholder[4];
  167. _UART_MONITOR_APPL_EXT ULONG dummy32;
  168. _UART_MONITOR_APPL_EXT UWORD dummy16;
  169. _UART_MONITOR_APPL_EXT UBYTE UART_ubParaBuffer[PARA_BUFFER_SIZE];
  170. _UART_MONITOR_APPL_EXT UBYTE UART_ubReadBuffer[RX_BUFFER_SIZE];
  171. /* define Write dual Buffer and its pointers */
  172. _UART_MONITOR_APPL_EXT UBYTE UART_ubWriteBuffer1[TX_BUFFER_SIZE];
  173. _UART_MONITOR_APPL_EXT UBYTE UART_ubWriteBuffer2[TX_BUFFER_SIZE];
  174. _UART_MONITOR_APPL_EXT UBYTE *UART_pWriteBuffer;
  175. _UART_MONITOR_APPL_EXT UWORD UART_uwWriteIndex;
  176. /* data bytes recieved in a Read */
  177. _UART_MONITOR_APPL_EXT UWORD volatile UART_uwRxNum;
  178. _UART_MONITOR_APPL_EXT UWORD writeDenyCount; // HAL_Write request deny count(because of S_ERR_BUSY), this value mainly used for debug.
  179. _UART_MONITOR_APPL_EXT UART_CONFIG_Type UART_stConfigLEN;
  180. _UART_MONITOR_APPL_EXT BOOL UART_bMonSwitch; // control monitor function whether of not write data out.
  181. _UART_MONITOR_APPL_EXT UWORD *UART_pMonAddr[6]; // the address of the variable which user want to monitor.
  182. //*** define insert buffer ***
  183. _UART_MONITOR_APPL_EXT UBYTE UART_ubInsertBuf[100]; // the data in insertBuffer will be inserted into writeBuffer when enabled
  184. _UART_MONITOR_APPL_EXT volatile BOOL UART_bInsertPendTx;
  185. _UART_MONITOR_APPL_EXT UART_ParaData_Flag UART_stParaStatus;
  186. _UART_MONITOR_APPL_EXT UART_MsgQNodeType UART_stMsgQueue[MSG_QUEUE_SIZE];
  187. #endif
  188. /************************************************************************
  189. RAM ALLOCATION (N/A)
  190. *************************************************************************/
  191. /************************************************************************
  192. Exported Function Call Prototypes
  193. *************************************************************************/
  194. #ifdef _UART_MONITOR_APPL_C_
  195. _UART_MONITOR_APPL_EXT void uart_voMainDec(void); // used in main.c for checking RxData and insertQueue
  196. _UART_MONITOR_APPL_EXT void UART_voAppMonitor(void); // used to send monitor data out
  197. _UART_MONITOR_APPL_EXT void UART_voApplTimer(void);
  198. _UART_MONITOR_APPL_EXT static void CBError_monitor(UART_ERR_Type _err);
  199. /* handle parameter related command*/
  200. _UART_MONITOR_APPL_EXT static void UART_voHandleParaCmd(void);
  201. //*** command decoder function ***
  202. _UART_MONITOR_APPL_EXT static void UART_voDecode(void);
  203. //*** Callback called when error occurs. ***
  204. _UART_MONITOR_APPL_EXT void CBError(UART_ERR_Type _err);
  205. //*** Callback called when some data received. ***
  206. _UART_MONITOR_APPL_EXT void UART_voCBDoneRead(UART_ERR_Type _err, ULONG _NumBytes);
  207. _UART_MONITOR_APPL_EXT static UBYTE UART_ubCheckXOR(const UBYTE Buf[], UBYTE CNT);
  208. _UART_MONITOR_APPL_EXT UART_MSG UART_stMsgFetched; // message fetched from msgQueue
  209. _UART_MONITOR_APPL_EXT void UART_voInitMsgQueue(void); // MsgQueue initialization
  210. _UART_MONITOR_APPL_EXT static void UART_voPostMsg(UART_MSG _msg); // post message
  211. _UART_MONITOR_APPL_EXT static void UART_voHandleMsg(void); // handle msg according to msg type
  212. #else
  213. _UART_MONITOR_APPL_EXT void uart_voMainDec(void); // used in main.c for checking RxData and insertQueue
  214. _UART_MONITOR_APPL_EXT void UART_voAppMonitor(void); // used to send monitor data out
  215. _UART_MONITOR_APPL_EXT void UART_voApplTimer(void);
  216. //*** Callback called when error occurs. ***
  217. _UART_MONITOR_APPL_EXT void CBError(UART_ERR_Type _err);
  218. //*** Callback called when some data received. ***
  219. _UART_MONITOR_APPL_EXT void UART_voCBDoneRead(UART_ERR_Type _err, ULONG _NumBytes);
  220. _UART_MONITOR_APPL_EXT UART_MSG UART_stMsgFetched; // message fetched from msgQueue
  221. _UART_MONITOR_APPL_EXT void UART_voInitMsgQueue(void); // MsgQueue initialization
  222. #endif
  223. /************************************************************************
  224. Local Function Call Prototypes (N/A)
  225. *************************************************************************/
  226. /************************************************************************
  227. Flag Define (N/A)
  228. *************************************************************************/
  229. #endif
  230. /************************************************************************
  231. Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
  232. All rights reserved.
  233. *************************************************************************
  234. End of this File (EOF)!
  235. Do not put anything after this part!
  236. *************************************************************************/