mapp_uart1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //==================================================================
  2. //
  3. // Filename: app_uart2.c
  4. // Description:
  5. //
  6. //------------------------------------------------------------------
  7. //
  8. // version:
  9. // data: 18:24, 05.Seb.2019, written by dwd
  10. // Description:
  11. //
  12. //------------------------------------------------------------------
  13. //==================================================================
  14. /*------------------------------------------------------------------------------
  15. * I N C L U D E
  16. *------------------------------------------------------------------------------*/
  17. #include <string.h>
  18. #include "stm32f0xx.h"
  19. #include "general.h"
  20. #include "main.h"
  21. #include "mapp_uart1.h"
  22. #include "mapp_pwr.h"
  23. #include "mapp_arm.h"
  24. #include "mapp_gpio.h"
  25. /*------------------------------------------------------------------------------
  26. * D E F I N E
  27. *------------------------------------------------------------------------------*/
  28. /*------------------------------------------------------------------------------
  29. * S T R U C T
  30. *------------------------------------------------------------------------------*/
  31. /*------------------------------------------------------------------------------
  32. * S T A T I C - V A R I A B L E S
  33. *------------------------------------------------------------------------------*/
  34. T_COM_UART1 s_tUart1Cfg = {0}; /* Com for DVD */
  35. /*------------------------------------------------------------------------------
  36. * G L O B A L
  37. *------------------------------------------------------------------------------*/
  38. /*------------------------------------------------------------------------------
  39. * P R O T O T Y P E
  40. *------------------------------------------------------------------------------*/
  41. static void s_Uart1VarInit(void);
  42. static BOOL s_Uart1RxbufEmpty(void);
  43. //static BOOL s_Uart1TxbufEmpty(void);
  44. /*----------------------------------------------------------------------
  45. // Function uses : Uart1CfgInit
  46. // Input parameter :
  47. // Output parameter :
  48. // Use Function : for DVD communication
  49. // Reserve date : 18:24, 05.Seb.2019, written by dwd
  50. ----------------------------------------------------------------------*/
  51. void Uart1CfgInit(void)
  52. {
  53. GPIO_InitTypeDef GPIO_InitStructure;
  54. USART_InitTypeDef USART_InitStructure = {0};
  55. /* Init variables ------------------------------------------------*/
  56. s_Uart1VarInit();
  57. /* Enable peripheral clocks ------------------------------------------------*/
  58. /* Enable USART Clock */
  59. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
  60. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  61. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
  62. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  63. /* Configure pins as AF pushpull */
  64. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//TXRX
  65. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  66. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  67. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  68. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  69. GPIO_Init(GPIOA, &GPIO_InitStructure);
  70. /* USART3 configuration ------------------------------------------------------*/
  71. USART_InitStructure.USART_BaudRate = 115200;
  72. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  73. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  74. USART_InitStructure.USART_Parity = USART_Parity_No;
  75. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  76. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  77. USART_Init(USART1, &USART_InitStructure);
  78. /* Enable the USART */
  79. USART_Cmd(USART1, ENABLE);
  80. /* Enable USART3 Receive and Transmit interrupts */
  81. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  82. USART_ITConfig(USART1, USART_IT_TC, ENABLE);
  83. USART_ClearFlag(USART1, USART_FLAG_TC);
  84. }
  85. /*----------------------------------------------------------------------
  86. // Function uses : UartISRHandler
  87. // Input parameter :
  88. // Output parameter :
  89. // Use Function :
  90. // Reserve date : 18:24, 05.Seb.2019, written by dwd
  91. ----------------------------------------------------------------------*/
  92. void Uart1ISRHandler(void)
  93. {
  94. T_COM_UART1 *pbuf;
  95. U08 rdata = 0;
  96. static U08 rstep = 0;
  97. pbuf = &s_tUart1Cfg;
  98. //------------------------------------------------------------------
  99. // for Tx compelete interrupt routine
  100. // 19:50pm, 11.Oct.2012, modified by Sirius
  101. //------------------------------------------------------------------
  102. if (USART_GetFlagStatus(USART1, USART_FLAG_TC) != RESET) {
  103. if (pbuf->TxCtr > 0) { /* See if buffer is empty */
  104. pbuf->TxCtr--; /* No, decrement character count */
  105. if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  106. pbuf->TxDeque = 0;
  107. }
  108. USART_SendData(USART1, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */
  109. } else {
  110. pbuf->TxDeque = pbuf ->TxEnque;
  111. }
  112. USART_ClearFlag(USART1, USART_FLAG_TC);
  113. }
  114. //------------------------------------------------------------------
  115. // for Rx not empty interrupt routine
  116. // 19:51pm, 11.Oct.2012, modified by Sirius
  117. //------------------------------------------------------------------
  118. if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) {
  119. rdata = USART_ReceiveData(USART1);
  120. #ifdef WDFJKEY_MAC_ADD
  121. switch (rstep) {
  122. case 0:
  123. if (rdata == '$') {
  124. rstep = 1;
  125. // gtArm.ledtestcnt = 10;
  126. }
  127. pbuf->RxCnt = 1;
  128. break;
  129. case 1:
  130. if (rdata == HI_BYTE( gtArm.FJKeymac)) {
  131. rstep = 2 ;
  132. } else {
  133. rstep = 0;
  134. }
  135. break;
  136. case 2:
  137. if (rdata == LO_BYTE( gtArm.FJKeymac)) {
  138. rstep = 3;
  139. } else {
  140. rstep = 0;
  141. }
  142. break;
  143. case 3:
  144. if (rdata == '#') {
  145. // gtArm.ledtestcnt = 30;
  146. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = '#';
  147. pbuf->BufRx[pbuf->RxInPag][0] = pbuf->RxCnt; //Lenght
  148. pbuf->RxInPag++;
  149. if (pbuf->RxInPag >= UART1_RX_PAGE) {
  150. pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */
  151. }
  152. rstep = 0;
  153. } else if (rdata == '$') {
  154. pbuf->RxCnt = 1;
  155. } else {
  156. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata;
  157. pbuf->RxCnt++;
  158. }
  159. break;
  160. default:
  161. rstep = 0;
  162. break;
  163. }
  164. #else
  165. switch (rstep) {
  166. case 0:
  167. if (rdata == '$') {
  168. rstep = 1;
  169. // gtArm.ledtestcnt = 10;
  170. }
  171. pbuf->RxCnt = 1;
  172. break;
  173. case 1:
  174. if (rdata == '#') {
  175. // gtArm.ledtestcnt = 30;
  176. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = '#';
  177. pbuf->BufRx[pbuf->RxInPag][0] = pbuf->RxCnt; //Lenght
  178. pbuf->RxInPag++;
  179. if (pbuf->RxInPag >= UART1_RX_PAGE) {
  180. pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */
  181. }
  182. rstep = 0;
  183. } else if (rdata == '$') {
  184. pbuf->RxCnt = 1;
  185. } else {
  186. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata;
  187. pbuf->RxCnt++;
  188. }
  189. break;
  190. default:
  191. rstep = 0;
  192. break;
  193. }
  194. #endif
  195. USART_ClearFlag(USART1, USART_FLAG_RXNE);
  196. }
  197. }
  198. /*----------------------------------------------------------------------
  199. // Function name : Uart1CfgDisable
  200. // Input parameter :
  201. // Output parameter :
  202. // Use Function :
  203. // Reserve date : 18:24, 05.Seb.2019, written by dwd
  204. ----------------------------------------------------------------------*/
  205. void Uart1CfgDisable(void)
  206. {
  207. /* Enable the USART */
  208. USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
  209. USART_ITConfig(USART1,USART_IT_TC,DISABLE);
  210. USART_ClearFlag(USART1, USART_FLAG_TC);
  211. USART_Cmd(USART1, DISABLE);
  212. }
  213. /*------------------------------------------------------------------------------
  214. - Name: Uart1GetChar
  215. - DESCRIPTION: - To obtain a character from the communications channel. The function
  216. will wait for a character to be received on the serial channel
  217. or until the function times out.
  218. -
  219. - Input: 'to' is the amount of time (in clock ticks) that the calling function is
  220. willing to wait for a character to arrive. If you specify a timeout of 0,
  221. the function will wait forever for a character to arrive.
  222. - Output:
  223. -
  224. - modification history
  225. - --------------------
  226. - 18:24, 05.Seb.2019, written by dwd
  227. * --------------------
  228. -----------------------------------------------------------------------------*/
  229. void Uart1GetChar(U08 *rbuf ,U16 to, U08 *err)
  230. {
  231. U08 i,len;
  232. T_COM_UART1 *pbuf;
  233. pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */
  234. if (!s_Uart1RxbufEmpty()) {
  235. //*rbuf++ = pbuf->BufRx[pbuf->RxOutPag][0]; /* Copy data type */
  236. len = pbuf->BufRx[pbuf->RxOutPag][0];
  237. if (len > (UART1_RX_BUF_SIZE-2) ) {
  238. len = UART1_RX_BUF_SIZE -2;
  239. }
  240. *rbuf++ = len; /* Copy data length */
  241. len ++;
  242. for (i = 0; i < len; i++) { /* Copy data to buffer */
  243. *rbuf++ = pbuf->BufRx[pbuf->RxOutPag][i + 1];
  244. }
  245. pbuf->RxOutPag++;
  246. if (pbuf->RxOutPag >= UART1_RX_PAGE) { /* Ready for next page */
  247. pbuf->RxOutPag = 0;
  248. }
  249. *err = COM_NO_ERR;
  250. } else { /* See if characters received within timeout*/
  251. *err = COM_RX_EMPTY; /* No, return error code */
  252. }
  253. }
  254. U08 Uart1PutChar(U08 *c, U08 len)
  255. {
  256. BOOL newTras;
  257. T_COM_UART1 *pbuf;
  258. pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */
  259. if (len == 0) {
  260. return (COM_TX_EMPTY);
  261. }
  262. //if (len > s_Uart1TxbufLeft()) { /* Txbuffer left is not enough for this transmit */
  263. // return (COM_TX_FULL);
  264. // }
  265. newTras = s_Uart1RxbufEmpty();
  266. while (len--) {
  267. pbuf->TxCtr++; /* No, increment character count */
  268. if (pbuf->TxEnque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  269. pbuf->TxEnque = 0;
  270. }
  271. pbuf->BufTx[pbuf->TxEnque++] = *c++; /* Put character into buffer */
  272. }
  273. /* Tx first character to wake up Usart new transmit process */
  274. if (newTras) {
  275. if (pbuf->TxCtr){
  276. pbuf->TxCtr--; /* No, decrement character count */
  277. }
  278. if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  279. pbuf->TxDeque = 0;
  280. }
  281. USART_SendData(USART1, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */
  282. USART_ClearFlag(USART1, USART_FLAG_ORE); /* Clear TC flag in status register */
  283. }
  284. return (COM_NO_ERR);
  285. }
  286. /*------------------------------------------------------------------------------
  287. - Name: Uart1VarInit
  288. - DESCRIPTION: - Initialize the ring buffer for COMM.
  289. Must call this function before calling any other functions.
  290. -
  291. - Input:
  292. - Output:
  293. -
  294. - modification history
  295. - --------------------
  296. - 18:24, 05.Seb.2019, written by dwd
  297. * --------------------
  298. -----------------------------------------------------------------------------*/
  299. static void s_Uart1VarInit(void)
  300. {
  301. T_COM_UART1 *pbuf;
  302. pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */
  303. pbuf->RxInPag = 0; /* Initialize the ring buffer for COMM port */
  304. pbuf->RxOutPag = 0;
  305. pbuf->RxLen = 0;
  306. pbuf->RxCnt = 0;
  307. }
  308. /*=======================================================
  309. - Name: s_Uart1RxbufEmpty
  310. - DESCRIPTION: - Judge whether rxbuf data valid.
  311. -
  312. - Input:
  313. - Output:
  314. -
  315. - modification history
  316. - --------------------
  317. - 18:24, 05.Seb.2019, written by dwd
  318. * --------------------
  319. =======================================================*/
  320. static BOOL s_Uart1RxbufEmpty(void)
  321. {
  322. return (BOOL)(s_tUart1Cfg.RxInPag == s_tUart1Cfg.RxOutPag);
  323. }
  324. /*------------------------------- E O F ----------------------------------------*/