usart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. *
  3. *
  4. *
  5. *
  6. */
  7. #include "string.h"
  8. #include "main.h"
  9. #include "usart.h"
  10. UART_HandleTypeDef huart3;
  11. extern uint8_t esp8266_rxdata;
  12. #define RX_BUF_MAX_LEN 1536
  13. /*typedef struct _frame_state_{
  14. __IO uint16_t FrameHeadIndex;
  15. __IO uint16_t FrameTailIndex;
  16. __IO uint16_t FrameLength;
  17. __IO uint8_t FrameStateFlag;//0,idle; 1: working; 2:done;
  18. __IO uint8_t FrameOverrunFlag;
  19. }stBufferState, *pBufferState;
  20. typedef struct _frame_timeout_{
  21. __IO uint16_t starttime;
  22. __IO uint16_t timeoutConst;//ms
  23. __IO uint16_t timeoutFrame;
  24. __IO uint16_t byteTransTime;//us: 113us@115200, 10us@1382400, 3us@4608000; 13bits/byte=1+9+2+1
  25. }stFrameTimeout, *pFrameTimeout;
  26. typedef struct _struct_frame_
  27. {
  28. char Data_RX_BUF[RX_BUF_MAX_LEN];
  29. stBufferState RX_State;
  30. stFrameTimeout RX_timeout;
  31. char Data_TX_BUF[RX_BUF_MAX_LEN];
  32. stBufferState TX_State;
  33. stFrameTimeout TX_timeout;
  34. }stBufferFrame, *pBufferFrame;*/
  35. #pragma pack(4)
  36. typedef struct _frame_state_{
  37. uint8_t FrameStateFlag;//0,idle; 1: working; 2:done;
  38. uint8_t FrameOverrunFlag;
  39. uint16_t FrameHeadIndex;
  40. uint16_t FrameTailIndex;
  41. uint16_t FrameLength;
  42. }stBufferState, *pBufferState;
  43. #pragma pack()
  44. #pragma pack(4)
  45. typedef struct _frame_timeout_{
  46. uint32_t starttime;
  47. uint16_t timeoutConst;//ms
  48. uint16_t byteTransTime;//us: 113us@115200, 10us@1382400, 3us@4608000; 13bits/byte=1+9+2+1
  49. uint32_t timeoutFrame;
  50. }stFrameTimeout, *pFrameTimeout;
  51. #pragma pack()
  52. #pragma pack(4)
  53. typedef struct _struct_frame_
  54. {
  55. char Data_RX_BUF[RX_BUF_MAX_LEN];
  56. __IO stBufferState RX_State;
  57. stFrameTimeout RX_timeout;
  58. char Data_TX_BUF[RX_BUF_MAX_LEN];
  59. __IO stBufferState TX_State;
  60. stFrameTimeout TX_timeout;
  61. }stBufferFrame, *pBufferFrame;
  62. #pragma pack()
  63. //extern stBufferFrame stWifiFrameRecord;
  64. stBufferFrame stWifiFrameRecord={0};
  65. //__IO uint8_t UartReady = RESET;
  66. UART_HandleTypeDef wifi_UartHandle;
  67. UART_HandleTypeDef huart3;
  68. #if 0
  69. void Delay_ms( unsigned int time_ms )
  70. {
  71. unsigned int temp=0;
  72. unsigned int start = HAL_GetTick();
  73. do
  74. {
  75. temp = HAL_GetTick();
  76. }
  77. while(time_ms > temp - start);
  78. }
  79. #endif
  80. /*
  81. * Init Usart with given baudrate.
  82. * Note:
  83. * Lowest baudrate is 57600!
  84. */
  85. void Wifi_USART_Init(uint32_t baud)
  86. {
  87. uint32_t temp=0;
  88. GPIO_InitTypeDef GPIO_InitStruct;
  89. if(baud < WIFI_LOWEST_BAUDRATE)
  90. {
  91. baud = WIFI_LOWEST_BAUDRATE;
  92. }
  93. else if( baud > WIFI_HIGHEST_BAUDRATE)
  94. {
  95. baud = WIFI_HIGHEST_BAUDRATE;
  96. }
  97. wifi_UartHandle.Instance = WIFI_UART;
  98. wifi_UartHandle.Init.BaudRate = (baud==WIFI_LOWEST_BAUDRATE) ? WIFI_LOWEST_BAUDRATE : WIFI_HIGH_BAUDRATE;
  99. wifi_UartHandle.Init.WordLength = WIFI_UART_WORDLENGTH;
  100. wifi_UartHandle.Init.StopBits = WIFI_UART_STOPBITS;
  101. wifi_UartHandle.Init.Parity = WIFI_UART_PARITY;
  102. wifi_UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  103. wifi_UartHandle.Init.Mode = UART_MODE_TX_RX;
  104. wifi_UartHandle.Init.OverSampling = UART_OVERSAMPLING_8;
  105. HAL_NVIC_SetPriority(UART5_IRQn, 1, 0);
  106. HAL_NVIC_EnableIRQ(UART5_IRQn);
  107. if(HAL_UART_Init(&wifi_UartHandle) != HAL_OK)
  108. {
  109. //Error_Handler();
  110. }
  111. /*##-2- Configure peripheral GPIO ##########################################*/
  112. WIFI_RST_GPIO_CLK_ENABLE();
  113. GPIO_InitStruct.Pin = WIFI_RST_PIN;
  114. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  115. GPIO_InitStruct.Pull = WIFI_RST_PIN_PULL;
  116. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  117. HAL_GPIO_Init(WIFI_RST_GPIO_PORT, &GPIO_InitStruct);
  118. WIFI_ENABLE_GPIO_CLK_ENABLE();
  119. GPIO_InitStruct.Pin = WIFI_ENABLE_PIN;
  120. GPIO_InitStruct.Pull = WIFI_ENABLE_PIN_PULL;
  121. HAL_GPIO_Init(WIFI_ENABLE_GPIO_PORT, &GPIO_InitStruct);
  122. WIFI_CS_GPIO_CLK_ENABLE();
  123. GPIO_InitStruct.Pin = WIFI_CS_PIN;
  124. GPIO_InitStruct.Pull = WIFI_CS_PIN_PULL;
  125. HAL_GPIO_Init(WIFI_CS_GPIO_PORT, &GPIO_InitStruct);
  126. //HAL_UART_Transmit_IT(&wifi_UartHandle,"0123456789\r\n",12);
  127. /*HAL_StatusTypeDef status= HAL_UART_Transmit(&wifi_UartHandle,"0123456789\r\n",12,1000);
  128. status++;*/
  129. //
  130. temp = (uint16_t)(13 * 1000000.0 / wifi_UartHandle.Init.BaudRate + 0.55);
  131. stWifiFrameRecord.RX_timeout.byteTransTime = temp;//us
  132. //stWifiFrameRecord.RX_timeout.timeoutConst = 1500;//1000ms
  133. stWifiFrameRecord.TX_timeout.byteTransTime = temp;//us
  134. //stWifiFrameRecord.TX_timeout.timeoutConst = 200;//200ms
  135. Wifi_Start();
  136. }
  137. void Wifi_Start(void)
  138. {
  139. HAL_GPIO_WritePin(WIFI_RST_GPIO_PORT, WIFI_RST_PIN, GPIO_PIN_SET);
  140. HAL_GPIO_WritePin(WIFI_CS_GPIO_PORT, WIFI_CS_PIN, GPIO_PIN_RESET);
  141. HAL_GPIO_WritePin(WIFI_ENABLE_GPIO_PORT, WIFI_ENABLE_PIN, GPIO_PIN_SET);
  142. }
  143. int Wifi_reset(void)
  144. {
  145. static int rststate=0;
  146. static uint32_t starttime=0;
  147. switch(rststate)
  148. {
  149. case 0:
  150. HAL_GPIO_WritePin(WIFI_RST_GPIO_PORT, WIFI_RST_PIN, GPIO_PIN_RESET);
  151. HAL_GPIO_WritePin(WIFI_CS_GPIO_PORT, WIFI_CS_PIN, GPIO_PIN_SET);
  152. HAL_GPIO_WritePin(WIFI_ENABLE_GPIO_PORT, WIFI_ENABLE_PIN, GPIO_PIN_RESET);
  153. rststate=1;
  154. starttime=HAL_GetTick();
  155. break;
  156. case 1:
  157. if(HAL_GetTick()-starttime<500)
  158. {
  159. break;
  160. }
  161. rststate=2;
  162. case 2:
  163. HAL_GPIO_WritePin(WIFI_CS_GPIO_PORT, WIFI_CS_PIN, GPIO_PIN_RESET);
  164. HAL_GPIO_WritePin(WIFI_ENABLE_GPIO_PORT, WIFI_ENABLE_PIN, GPIO_PIN_SET);
  165. HAL_GPIO_WritePin(WIFI_RST_GPIO_PORT, WIFI_RST_PIN, GPIO_PIN_SET);
  166. starttime=HAL_GetTick();
  167. rststate=3;
  168. break;
  169. case 3:
  170. if(HAL_GetTick()-starttime < 1000)
  171. {
  172. break;
  173. }
  174. default:
  175. rststate=0;
  176. break;
  177. }
  178. return rststate;
  179. }
  180. /*
  181. Tail is the newest byte!!
  182. */
  183. unsigned int Wifi_Uart_DataLen(void)
  184. {
  185. if(!stWifiFrameRecord.RX_State.FrameOverrunFlag)
  186. {
  187. if(stWifiFrameRecord.RX_State.FrameTailIndex<=stWifiFrameRecord.RX_State.FrameHeadIndex)
  188. return (stWifiFrameRecord.RX_State.FrameHeadIndex-stWifiFrameRecord.RX_State.FrameTailIndex);
  189. else
  190. return (RX_BUF_MAX_LEN - stWifiFrameRecord.RX_State.FrameTailIndex
  191. + stWifiFrameRecord.RX_State.FrameHeadIndex);
  192. }
  193. return RX_BUF_MAX_LEN;
  194. }
  195. /*
  196. */
  197. unsigned int Wifi_Uart_ReadData(uint8_t* buf, uint32_t len)
  198. {
  199. uint16_t len1,len2;
  200. uint16_t idxHead = stWifiFrameRecord.RX_State.FrameHeadIndex;
  201. uint16_t idxTail = stWifiFrameRecord.RX_State.FrameTailIndex;
  202. if(idxTail <= idxHead)
  203. {
  204. len1 = idxHead - idxTail;
  205. if(len1 > len)
  206. {
  207. len1 = len;
  208. }
  209. memcpy((void*)buf, (void*)&stWifiFrameRecord.Data_RX_BUF[idxTail], len1);
  210. stWifiFrameRecord.RX_State.FrameTailIndex += len1;
  211. }
  212. else
  213. {
  214. len1 = RX_BUF_MAX_LEN - idxTail;
  215. if(len1 > len)
  216. {
  217. len1 = len;
  218. }
  219. memcpy((void*)buf, (void*)&stWifiFrameRecord.Data_RX_BUF[idxTail], len1);
  220. stWifiFrameRecord.RX_State.FrameTailIndex += len1;
  221. if(stWifiFrameRecord.RX_State.FrameTailIndex >= RX_BUF_MAX_LEN)
  222. {
  223. stWifiFrameRecord.RX_State.FrameTailIndex = 0;
  224. }
  225. len -= len1;
  226. if(len > 0)
  227. {
  228. len2 = idxHead - len;
  229. if(len2 > len)
  230. {
  231. len2 = len;
  232. }
  233. memcpy((void*)(buf+len1), (void*)&stWifiFrameRecord.Data_RX_BUF[idxTail], len2);
  234. stWifiFrameRecord.RX_State.FrameTailIndex += len2;
  235. if(stWifiFrameRecord.RX_State.FrameTailIndex >= RX_BUF_MAX_LEN)
  236. {
  237. stWifiFrameRecord.RX_State.FrameTailIndex = 0;
  238. }
  239. len1 += len2;
  240. }
  241. }
  242. if(len1 > 0)
  243. {
  244. stWifiFrameRecord.RX_State.FrameOverrunFlag = 0;
  245. }
  246. return len1;
  247. }
  248. /**/
  249. void Wifi_Uart_ClearRxBuf(void)
  250. {
  251. memset((void*)&stWifiFrameRecord.RX_State, 0, sizeof(stBufferState));
  252. memset((void*)&stWifiFrameRecord.RX_timeout, 0, sizeof(stFrameTimeout));
  253. }
  254. void Wifi_Uart_ClearTxBuf(void)
  255. {
  256. memset((void*)&stWifiFrameRecord.TX_State, 0, sizeof(stBufferState));
  257. memset((void*)&stWifiFrameRecord.TX_timeout, 0, sizeof(stFrameTimeout));
  258. }
  259. int Wifi_Uart_Send(uint8_t *buf, uint32_t len,uint32_t timeout)
  260. {
  261. #if UART_INF_REG==1
  262. int ret=0;
  263. uint32_t time=0;
  264. pBufferState pstate = (pBufferState)&stWifiFrameRecord.TX_State;
  265. pFrameTimeout ptimeout = &stWifiFrameRecord.TX_timeout;
  266. if(stWifiFrameRecord.TX_State.FrameStateFlag == 0)//idle
  267. {
  268. /*stWifiFrameRecord.TX_State.FrameStateFlag = 1;
  269. stWifiFrameRecord.TX_State.FrameHeadIndex = 0;
  270. stWifiFrameRecord.TX_State.FrameTailIndex = 0;
  271. stWifiFrameRecord.TX_State.FrameLength = len;
  272. memcpy(stWifiFrameRecord.Data_TX_BUF,buf,len);
  273. ret = (stWifiFrameRecord.TX_timeout.byteTransTime * len + 599) / 1000 + 1;
  274. if(timeout < ret) timeout = ret;
  275. stWifiFrameRecord.TX_timeout.timeoutConst = timeout;
  276. ret += stWifiFrameRecord.TX_timeout.timeoutConst;
  277. stWifiFrameRecord.TX_timeout.timeoutFrame = ret;
  278. time = HAL_GetTick();
  279. stWifiFrameRecord.TX_timeout.starttime = time;*/
  280. pstate->FrameStateFlag = 1;
  281. pstate->FrameHeadIndex = 0;
  282. pstate->FrameTailIndex = 0;
  283. pstate->FrameLength = len;
  284. memcpy(stWifiFrameRecord.Data_TX_BUF,buf,len);
  285. ret = (ptimeout->byteTransTime * len + 599) / 1000 + 1;
  286. if(timeout < ret) timeout = ret;
  287. ptimeout->timeoutConst = timeout;
  288. ret += ptimeout->timeoutConst;
  289. ptimeout->timeoutFrame = ret;
  290. time = HAL_GetTick();
  291. ptimeout->starttime = time;
  292. time++;
  293. SET_BIT(WIFI_UART->CR1, USART_CR1_TE);
  294. //CLEAR_BIT(WIFI_UART->CR1, USART_CR1_TCIE);
  295. /* Enable the UART Transmit data register empty Interrupt */
  296. SET_BIT(WIFI_UART->CR1, USART_CR1_TXEIE);
  297. SET_BIT(WIFI_UART->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  298. return 0;
  299. }
  300. else if(stWifiFrameRecord.TX_State.FrameStateFlag == 2)
  301. {
  302. CLEAR_BIT(WIFI_UART->CR1, USART_CR1_TCIE);
  303. CLEAR_BIT(WIFI_UART->CR1, USART_CR1_TXEIE);
  304. stWifiFrameRecord.TX_State.FrameStateFlag = 0;
  305. return stWifiFrameRecord.TX_State.FrameTailIndex;
  306. }
  307. else
  308. {
  309. time = HAL_GetTick();
  310. if(stWifiFrameRecord.TX_timeout.timeoutFrame <=
  311. time - stWifiFrameRecord.TX_timeout.starttime)
  312. {
  313. stWifiFrameRecord.TX_State.FrameStateFlag = 0;
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. #else
  319. UartReady = RESET;
  320. HAL_UART_Transmit_IT(&wifi_UartHandle,buf,len);
  321. while(UartReady == RESET)
  322. {
  323. ;
  324. }
  325. return 0;
  326. #endif
  327. }
  328. int Wifi_Uart_Receive(uint8_t *buf, uint32_t len,uint32_t timeout)
  329. {
  330. #if UART_INF_REG==1
  331. int ret=0;
  332. uint32_t time=0;
  333. if(stWifiFrameRecord.RX_State.FrameStateFlag == 0)//idle
  334. {
  335. if(buf==NULL || len==0)
  336. {
  337. return -1;
  338. }
  339. stWifiFrameRecord.RX_State.FrameStateFlag = 1;
  340. stWifiFrameRecord.RX_State.FrameLength = len;
  341. ret = (stWifiFrameRecord.RX_timeout.byteTransTime * len + 599) / 1000 + 1;
  342. if(timeout < ret) timeout = ret;
  343. stWifiFrameRecord.RX_timeout.timeoutConst = timeout;
  344. ret += stWifiFrameRecord.RX_timeout.timeoutConst;
  345. stWifiFrameRecord.RX_timeout.timeoutFrame = ret;
  346. time = HAL_GetTick();
  347. stWifiFrameRecord.RX_timeout.starttime = time;
  348. SET_BIT(WIFI_UART->CR1, USART_CR1_RE);
  349. // Enable the UART Error Interrupt: (Frame error, noise error, overrun error)
  350. SET_BIT(WIFI_UART->CR3, USART_CR3_EIE);
  351. // Enable the UART Parity Error and Data Register not empty Interrupts
  352. SET_BIT(WIFI_UART->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  353. return 0;
  354. }
  355. else
  356. {
  357. ret = Wifi_Uart_DataLen();
  358. //if(stWifiFrameRecord.RX_State.FrameStateFlag == 3 || ret >= len)
  359. if(ret >= len)
  360. {
  361. stWifiFrameRecord.RX_State.FrameStateFlag = 0;//over
  362. if(len < ret) len = ret;
  363. ret = Wifi_Uart_ReadData(buf,len);
  364. }
  365. else
  366. {
  367. time = HAL_GetTick();
  368. if(stWifiFrameRecord.RX_timeout.timeoutFrame <=
  369. (time - stWifiFrameRecord.RX_timeout.starttime))
  370. {
  371. stWifiFrameRecord.RX_State.FrameStateFlag = 0;//over
  372. //ret = Wifi_Uart_DataLen();
  373. ret = Wifi_Uart_ReadData(buf, ret);
  374. if(ret <= 0)
  375. return -2;
  376. return ret;
  377. }
  378. ret = 0;//wait again
  379. }
  380. return ret;
  381. }
  382. /*if(stWifiFrameRecord.RX_State.FrameStateFlag == 0)//idle
  383. {
  384. stWifiFrameRecord.RX_State.FrameStateFlag = 1;
  385. stWifiFrameRecord.RX_State.FrameHeadIndex = 0;
  386. stWifiFrameRecord.RX_State.FrameTailIndex = 0;
  387. stWifiFrameRecord.RX_State.FrameLength = len;
  388. memset(stWifiFrameRecord.Data_RX_BUF,0,RX_BUF_MAX_LEN);
  389. stWifiFrameRecord.RX_State.FrameOverrunFlag = 0;
  390. stWifiFrameRecord.RX_timeout.timeoutFrame = stWifiFrameRecord.RX_timeout.byteTransTime * len;
  391. stWifiFrameRecord.RX_timeout.timeoutFrame += stWifiFrameRecord.RX_timeout.timeoutConst + 1;
  392. stWifiFrameRecord.RX_timeout.starttime = HAL_GetTick();
  393. SET_BIT(WIFI_UART->CR1, USART_CR1_RE);
  394. // Enable the UART Error Interrupt: (Frame error, noise error, overrun error)
  395. SET_BIT(WIFI_UART->CR3, USART_CR3_EIE);
  396. // Enable the UART Parity Error and Data Register not empty Interrupts
  397. SET_BIT(WIFI_UART->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  398. return 0;
  399. }
  400. else if(stWifiFrameRecord.RX_State.FrameStateFlag >= 2)
  401. {
  402. CLEAR_BIT(WIFI_UART->CR3, USART_CR3_EIE);
  403. CLEAR_BIT(WIFI_UART->CR1, USART_CR1_PEIE);
  404. CLEAR_BIT(WIFI_UART->CR1, USART_CR1_RXNEIE);
  405. memcpy(buf,stWifiFrameRecord.Data_RX_BUF,stWifiFrameRecord.RX_State.FrameLength);
  406. stWifiFrameRecord.RX_State.FrameStateFlag = 0;
  407. return stWifiFrameRecord.RX_State.FrameLength;
  408. }
  409. else
  410. {
  411. if(stWifiFrameRecord.RX_timeout.timeoutFrame <=
  412. (HAL_GetTick() - stWifiFrameRecord.RX_timeout.starttime))
  413. {
  414. stWifiFrameRecord.RX_State.FrameStateFlag = 0;
  415. return -1;
  416. }
  417. if((stWifiFrameRecord.RX_State.FrameTailIndex - stWifiFrameRecord.RX_State.FrameHeadIndex)
  418. >= stWifiFrameRecord.RX_State.FrameLength)
  419. {
  420. memcpy(buf,stWifiFrameRecord.Data_RX_BUF,stWifiFrameRecord.RX_State.FrameLength);
  421. stWifiFrameRecord.RX_State.FrameStateFlag = 0;
  422. return stWifiFrameRecord.RX_State.FrameLength;
  423. }
  424. return 0;
  425. }*/
  426. #else
  427. return HAL_UART_Receive(&wifi_UartHandle,buf,len,3000);
  428. #endif
  429. }
  430. #if UART_INF_REG==1
  431. #define __UART_GET_FLAG(__UART__, __FLAG__) (((__UART__)->SR & (__FLAG__)) == (__FLAG__))
  432. #define __UART_CLEAR_FLAG(__UART__, __FLAG__) ((__UART__)->SR = ~(__FLAG__))
  433. void Wifi_Uart_IRQ_Handler(void)
  434. {
  435. uint32_t sr = WIFI_UART->SR;//1
  436. uint16_t ucCh = (uint16_t)(WIFI_UART->DR);//2
  437. //uint16_t temp = 0;
  438. uint32_t cr1 = WIFI_UART->CR1;
  439. //uint32_t cr3 = WIFI_UART->CR3;
  440. //if( __UART_GET_FLAG(WIFI_UART, UART_FLAG_RXNE))
  441. if((sr&UART_FLAG_RXNE)>0)
  442. {
  443. __UART_CLEAR_FLAG( WIFI_UART, UART_FLAG_RXNE );
  444. /*
  445. #if WIFI_UART_WORDLENGTH==UART_WORDLENGTH_8B
  446. #if (WIFI_UART_PARITY == UART_PARITY_NONE)
  447. ucCh = (uint8_t)(WIFI_UART->DR & 0x00FF);
  448. #else
  449. ucCh = (uint8_t)(WIFI_UART->DR & 0x007F);
  450. #endif
  451. #else //(WIFI_UART_WORDLENGTH == UART_WORDLENGTH_9B)
  452. #if (WIFI_UART_PARITY == UART_PARITY_NONE)
  453. ucCh = (uint16_t)(WIFI_UART->DR & 0x01FF);
  454. len=2;
  455. #else
  456. ucCh = (uint16_t)(WIFI_UART->DR & 0x00FF);
  457. #endif
  458. #endif
  459. if(stWifiFrameRecord.RX_State.FrameStateFlag == 1)
  460. {
  461. if(stWifiFrameRecord.RX_State.FrameTailIndex < ( stWifiFrameRecord.RX_State.FrameLength - 1 ) )
  462. {
  463. stWifiFrameRecord.Data_RX_BUF[stWifiFrameRecord.RX_State.FrameTailIndex++] = ucCh;
  464. }
  465. else
  466. {
  467. stWifiFrameRecord.RX_State.FrameStateFlag = 2;
  468. }
  469. }*/
  470. #if WIFI_UART_WORDLENGTH==UART_WORDLENGTH_8B
  471. #if (WIFI_UART_PARITY == UART_PARITY_NONE)
  472. ucCh = (uint8_t)(ucCh & 0x00FF);
  473. #else
  474. ucCh = (uint8_t)(ucCh & 0x007F);
  475. #endif
  476. #else //(WIFI_UART_WORDLENGTH == UART_WORDLENGTH_9B)
  477. #if (WIFI_UART_PARITY == UART_PARITY_NONE)
  478. ucCh = (uint16_t)(ucCh & 0x01FF);
  479. len=2;
  480. #else
  481. ucCh = (uint16_t)(ucCh & 0x00FF);
  482. #endif
  483. #endif
  484. stWifiFrameRecord.Data_RX_BUF[stWifiFrameRecord.RX_State.FrameHeadIndex++] = ucCh;
  485. if(stWifiFrameRecord.RX_State.FrameHeadIndex >= RX_BUF_MAX_LEN)
  486. {
  487. stWifiFrameRecord.RX_State.FrameHeadIndex = 0;
  488. }
  489. if(stWifiFrameRecord.RX_State.FrameHeadIndex <= stWifiFrameRecord.RX_State.FrameTailIndex)
  490. {
  491. stWifiFrameRecord.RX_State.FrameOverrunFlag = 1;
  492. }
  493. }
  494. //if( __UART_GET_FLAG( WIFI_UART, UART_FLAG_IDLE ))
  495. if((sr&UART_FLAG_IDLE)>0)
  496. {
  497. __UART_CLEAR_FLAG( WIFI_UART, UART_FLAG_IDLE );
  498. /*
  499. if(stWifiFrameRecord.RX_State.FrameStateFlag > 0)//In receiving
  500. {
  501. stWifiFrameRecord.RX_State.FrameStateFlag = 3;
  502. }
  503. #if (WIFI_UART_PARITY == UART_PARITY_NONE)
  504. ucCh = (uint8_t)(WIFI_UART->DR & 0xFF);
  505. #else
  506. ucCh = (uint8_t)(WIFI_UART->DR & 0x7F);
  507. #endif
  508. */
  509. }
  510. /*if((sr&UART_FLAG_ORE)>0)//
  511. {
  512. cr3 = WIFI_UART->SR;
  513. }*/
  514. //if( __UART_GET_FLAG( WIFI_UART, UART_FLAG_TXE )&& 0<(WIFI_UART->CR1& USART_CR1_TXEIE))
  515. if( (sr & UART_FLAG_TXE)>0 && (cr1 & USART_CR1_TXEIE)>0)
  516. {
  517. if(stWifiFrameRecord.TX_State.FrameTailIndex < stWifiFrameRecord.TX_State.FrameLength)
  518. {
  519. #if (WIFI_UART_PARITY == UART_PARITY_NONE)
  520. ucCh = 0xFF & stWifiFrameRecord.Data_TX_BUF[stWifiFrameRecord.TX_State.FrameTailIndex];
  521. #else
  522. ucCh = 0x7F & stWifiFrameRecord.Data_TX_BUF[stWifiFrameRecord.TX_State.FrameTailIndex];
  523. #endif
  524. WIFI_UART->DR = (uint8_t)ucCh;
  525. stWifiFrameRecord.TX_State.FrameTailIndex++;
  526. if(stWifiFrameRecord.TX_State.FrameTailIndex>=RX_BUF_MAX_LEN)
  527. {
  528. stWifiFrameRecord.TX_State.FrameTailIndex = 0;
  529. }
  530. }
  531. if(stWifiFrameRecord.TX_State.FrameTailIndex >= stWifiFrameRecord.TX_State.FrameLength)
  532. {
  533. /* Disable the UART Transmit Complete Interrupt */
  534. CLEAR_BIT(WIFI_UART->CR1, USART_CR1_TXEIE);
  535. /* Enable the UART Transmit Complete Interrupt */
  536. SET_BIT(WIFI_UART->CR1, USART_CR1_TCIE);
  537. }
  538. else
  539. {
  540. /* Enable the UART Transmit Complete Interrupt */
  541. CLEAR_BIT(WIFI_UART->CR1, USART_CR1_TCIE);
  542. /* Disable the UART Transmit Complete Interrupt */
  543. SET_BIT(WIFI_UART->CR1, USART_CR1_TXEIE);
  544. }
  545. sr = WIFI_UART->SR;
  546. cr1 = WIFI_UART->CR1;
  547. }
  548. //if( __UART_GET_FLAG( WIFI_UART, UART_FLAG_TC ) && 0<(WIFI_UART->CR1& USART_CR1_TCIE))
  549. if( (sr & UART_FLAG_TC) > 0 && (cr1 & USART_CR1_TCIE) > 0)
  550. {
  551. __UART_CLEAR_FLAG( WIFI_UART, UART_FLAG_TC );
  552. /* Disable the UART Transmit Complete Interrupt */
  553. CLEAR_BIT(WIFI_UART->CR1, USART_CR1_TCIE);
  554. stWifiFrameRecord.TX_State.FrameStateFlag = 2;
  555. }
  556. }
  557. #else
  558. /**
  559. * @brief Tx Transfer completed callback
  560. * @param UartHandle: UART handle.
  561. * @note This example shows a simple way to report end of IT Tx transfer, and
  562. * you can add your own implementation.
  563. * @retval None
  564. */
  565. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
  566. {
  567. UartReady = SET;
  568. }
  569. /**
  570. * @brief Rx Transfer completed callback
  571. * @param UartHandle: UART handle
  572. * @note This example shows a simple way to report end of IT Rx transfer, and
  573. * you can add your own implementation.
  574. * @retval None
  575. */
  576. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
  577. {
  578. ;
  579. }
  580. /**
  581. * @brief UART error callbacks
  582. * @param UartHandle: UART handle
  583. * @note This example shows a simple way to report transfer error, and you can
  584. * add your own implementation.
  585. * @retval None
  586. */
  587. void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
  588. {
  589. ;
  590. }
  591. #endif