uart_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. ******************************************************************************
  3. * @file uart_driver.c
  4. * @author
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief uart driver function.
  8. ******************************************************************************
  9. *
  10. * COPYRIGHT(c) 2015 STMicroelectronics
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. * 1. Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "uart_driver.h"
  38. #include "hwsetup.h"
  39. /* Private variables ---------------鎺ユ敹缂撳瓨鍖�------------------------------------------*/
  40. //uint8_t UART_RxBuff1[UART_BUFF_SIZE];
  41. //USART_Buf_TypeDef UART_RxBuff_Struct1 = {UART_BUFF_SIZE,0,0,0,0,UART_RxBuff1,UART_BAT_INST};
  42. //uint8_t UART_TxBuff1[UART_BUFF_SIZE];
  43. //USART_Buf_TypeDef UART_TxBuff_Struct1 = {UART_BUFF_SIZE,0,0,0,0,UART_TxBuff1,UART_BAT_INST};
  44. uint8_t UART_RxBuff2[UART_BUFF_SIZE];
  45. USART_Buf_TypeDef UART_RxBuff_Struct2 = {UART_BUFF_SIZE,0,0,0,0,UART_RxBuff2,UART_HMI_INST};
  46. uint8_t UART_TxBuff2[UART_BUFF_SIZE];
  47. USART_Buf_TypeDef UART_TxBuff_Struct2 = {UART_BUFF_SIZE,0,0,0,0,UART_TxBuff2,UART_HMI_INST};
  48. /* Private variables ---------------------------------------------------------*/
  49. /* Private function prototypes -----------------------------------------------*/
  50. void USARTx_RxTX_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct,USART_Buf_TypeDef *USARTx_RxBuf_Struct);
  51. uint8_t UART_ReadChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  52. {
  53. uint8_t ucData;
  54. uint16_t i;
  55. i = ucNum;
  56. if ((*ptRx).ucBufCnt >= ucNum)
  57. {
  58. i += (*ptRx).ucBufRdInde;
  59. if (i >= (*ptRx).ucBufSize)
  60. {
  61. i -=((*ptRx).ucBufSize);
  62. }
  63. }
  64. else
  65. {
  66. i=0;
  67. }
  68. ucData = *((*ptRx).pcBufAddr + i);
  69. return ucData;
  70. }
  71. void UART_DelChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  72. {
  73. uint16_t i;
  74. if ((*ptRx).ucBufCnt >= ucNum)
  75. {
  76. // usart_interrupt_enable(ptRx->usart_periph, USART_RDBF_INT, FALSE);
  77. DL_UART_Main_disableInterrupt(ptRx->usart_periph, DL_UART_MAIN_INTERRUPT_RX);
  78. (*ptRx).ucBufCnt -= ucNum;
  79. // usart_interrupt_enable(ptRx->usart_periph, USART_RDBF_INT, TRUE);
  80. DL_UART_Main_enableInterrupt(ptRx->usart_periph, DL_UART_MAIN_INTERRUPT_RX);
  81. i = ucNum;
  82. i += (*ptRx).ucBufRdInde;
  83. if (i >= (*ptRx).ucBufSize)
  84. {
  85. i -= (*ptRx).ucBufSize;
  86. }
  87. (*ptRx).ucBufRdInde = i;
  88. }
  89. else
  90. {
  91. // usart_interrupt_enable(ptRx->usart_periph, USART_RDBF_INT, FALSE);
  92. DL_UART_Main_disableInterrupt(ptRx->usart_periph, DL_UART_MAIN_INTERRUPT_RX);
  93. i = (*ptRx).ucBufCnt;
  94. (*ptRx).ucBufCnt = 0;
  95. // usart_interrupt_enable(ptRx->usart_periph, USART_RDBF_INT, TRUE);
  96. DL_UART_Main_enableInterrupt(ptRx->usart_periph, DL_UART_MAIN_INTERRUPT_RX);
  97. i += (*ptRx).ucBufRdInde;
  98. if (i >= (*ptRx).ucBufSize)
  99. {
  100. i -= (*ptRx).ucBufSize;
  101. }
  102. (*ptRx).ucBufRdInde = i;
  103. }
  104. }
  105. void UART_PutChar(USART_Buf_TypeDef * ptTx, uint8_t ucData)
  106. {
  107. while ((*ptTx).ucBufCnt == (*ptTx).ucBufSize);
  108. // usart_interrupt_enable(ptTx->usart_periph, USART_TDBE_INT, FALSE);
  109. DL_UART_Main_disableInterrupt(ptTx->usart_periph, DL_UART_MAIN_INTERRUPT_EOT_DONE);
  110. if((*ptTx).ucBufCnt == 0)
  111. {
  112. // if(usart_flag_get(ptTx->usart_periph, USART_TDBE_FLAG) == SET) //check if transmit data register full or not
  113. // if( (DL_UART_Main_getPendingInterrupt(ptTx->usart_periph)) ==DL_UART_MAIN_IIDX_EOT_DONE)
  114. if(DL_UART_isTXFIFOEmpty(ptTx->usart_periph) == true)
  115. {
  116. // usart_data_transmit(ptTx->usart_periph, ucData);
  117. DL_UART_Main_transmitData(ptTx->usart_periph, ucData);
  118. // usart_interrupt_enable(ptTx->usart_periph, USART_TDBE_INT, TRUE);
  119. DL_UART_Main_enableInterrupt(ptTx->usart_periph, DL_UART_MAIN_INTERRUPT_EOT_DONE);
  120. return;
  121. }
  122. }
  123. *((*ptTx).pcBufAddr + (*ptTx).ucBufWrInde) = ucData;
  124. if(++(*ptTx).ucBufWrInde == (*ptTx).ucBufSize)
  125. {
  126. (*ptTx).ucBufWrInde = 0;
  127. }
  128. ++(*ptTx).ucBufCnt;
  129. // usart_interrupt_enable(ptTx->usart_periph, USART_TDBE_INT, TRUE);
  130. DL_UART_Main_enableInterrupt(ptTx->usart_periph, DL_UART_MAIN_INTERRUPT_EOT_DONE);
  131. }
  132. void UART_SendData(USART_Buf_TypeDef * ptTx, uint8_t* Data, uint8_t Length)
  133. {
  134. uint8_t i;
  135. for(i=0; i<Length; i++)
  136. {
  137. UART_PutChar(ptTx, Data[i]);
  138. }
  139. }
  140. /*******************************************************************************
  141. * Function:void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  142. * Discrible:
  143. * Input:
  144. * Output:
  145. * Return:
  146. * Others:
  147. * date version author record
  148. * -----------------------------------------------
  149. * 20151107 V1.0 VINCENT Create
  150. *********************************************************************************/
  151. void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  152. {
  153. uint8_t ucData;
  154. // if(USARTx_RxBuf_Struct->usart_periph->ctrl1_bit.rdbfien != RESET)
  155. if(1) //TODO
  156. {
  157. // if(RESET != usart_flag_get(USARTx_RxBuf_Struct->usart_periph, USART_RDBF_FLAG))
  158. if( (DL_UART_Main_getPendingInterrupt(USARTx_RxBuf_Struct->usart_periph)) ==DL_UART_MAIN_IIDX_RX)
  159. {
  160. // ucData = (uint8_t)usart_data_receive(USARTx_RxBuf_Struct->usart_periph);
  161. ucData = DL_UART_Main_receiveDataBlocking(USARTx_RxBuf_Struct->usart_periph);
  162. //if(((USARTx_RxBuf_Struct->ptUartHandel->Instance->SR) & 0x0000) == 0)
  163. {
  164. *((* USARTx_RxBuf_Struct).pcBufAddr + (* USARTx_RxBuf_Struct).ucBufWrInde) = ucData;
  165. if(++(* USARTx_RxBuf_Struct).ucBufWrInde >= (* USARTx_RxBuf_Struct).ucBufSize)
  166. {
  167. (* USARTx_RxBuf_Struct).ucBufWrInde = 0;
  168. }
  169. if(++(* USARTx_RxBuf_Struct).ucBufCnt > (* USARTx_RxBuf_Struct).ucBufSize)
  170. {
  171. (* USARTx_RxBuf_Struct).ucBufCnt = (* USARTx_RxBuf_Struct).ucBufSize;
  172. (* USARTx_RxBuf_Struct).ucBufOvf = 1;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. void USARTx_Tx_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct)
  179. {
  180. // if(USARTx_TxBuf_Struct->usart_periph->ctrl1_bit.tdbeien != RESET)
  181. if(1) //TODO
  182. {
  183. // if(RESET != usart_flag_get(USARTx_TxBuf_Struct->usart_periph, USART_TDBE_FLAG))
  184. if( (DL_UART_Main_getPendingInterrupt(USARTx_TxBuf_Struct->usart_periph)) ==DL_UART_MAIN_IIDX_EOT_DONE)
  185. {
  186. //usart_interrupt_enable(USART1, USART_TDBE_INT, FALSE);
  187. if((* USARTx_TxBuf_Struct).ucBufCnt)
  188. {
  189. --(* USARTx_TxBuf_Struct).ucBufCnt;
  190. /* write one byte to the transmit data register */
  191. // usart_data_transmit(USARTx_TxBuf_Struct->usart_periph, *(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde));
  192. DL_UART_Main_transmitData(USARTx_TxBuf_Struct->usart_periph,*(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde));
  193. if(++(* USARTx_TxBuf_Struct).ucBufRdInde >= (* USARTx_TxBuf_Struct).ucBufSize)
  194. {
  195. (* USARTx_TxBuf_Struct).ucBufRdInde = 0;
  196. }
  197. }
  198. else
  199. {
  200. /* Disable the UART Transmit Data Register Empty Interrupt */
  201. // usart_interrupt_enable(USARTx_TxBuf_Struct->usart_periph, USART_TDBE_INT, FALSE);
  202. DL_UART_Main_disableInterrupt(USARTx_TxBuf_Struct->usart_periph, DL_UART_MAIN_INTERRUPT_EOT_DONE);
  203. }
  204. }
  205. }
  206. }
  207. void USARTx_RxTX_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct,USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  208. {
  209. uint8_t ucData;
  210. switch (DL_UART_Main_getPendingInterrupt(USARTx_TxBuf_Struct->usart_periph))
  211. {
  212. case DL_UART_MAIN_IIDX_RX:
  213. // ucData = (uint8_t)usart_data_receive(USARTx_RxBuf_Struct->usart_periph);
  214. ucData = DL_UART_Main_receiveDataBlocking(USARTx_RxBuf_Struct->usart_periph);
  215. //if(((USARTx_RxBuf_Struct->ptUartHandel->Instance->SR) & 0x0000) == 0)
  216. {
  217. *((* USARTx_RxBuf_Struct).pcBufAddr + (* USARTx_RxBuf_Struct).ucBufWrInde) = ucData;
  218. if(++(* USARTx_RxBuf_Struct).ucBufWrInde >= (* USARTx_RxBuf_Struct).ucBufSize)
  219. {
  220. (* USARTx_RxBuf_Struct).ucBufWrInde = 0;
  221. }
  222. if(++(* USARTx_RxBuf_Struct).ucBufCnt > (* USARTx_RxBuf_Struct).ucBufSize)
  223. {
  224. (* USARTx_RxBuf_Struct).ucBufCnt = (* USARTx_RxBuf_Struct).ucBufSize;
  225. (* USARTx_RxBuf_Struct).ucBufOvf = 1;
  226. }
  227. }
  228. break;
  229. case DL_UART_MAIN_IIDX_EOT_DONE:
  230. //usart_interrupt_enable(USART1, USART_TDBE_INT, FALSE);
  231. if((* USARTx_TxBuf_Struct).ucBufCnt)
  232. {
  233. --(* USARTx_TxBuf_Struct).ucBufCnt;
  234. /* write one byte to the transmit data register */
  235. // usart_data_transmit(USARTx_TxBuf_Struct->usart_periph, *(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde));
  236. DL_UART_Main_transmitData(USARTx_TxBuf_Struct->usart_periph,*(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde));
  237. if(++(* USARTx_TxBuf_Struct).ucBufRdInde >= (* USARTx_TxBuf_Struct).ucBufSize)
  238. {
  239. (* USARTx_TxBuf_Struct).ucBufRdInde = 0;
  240. }
  241. }
  242. else
  243. {
  244. /* Disable the UART Transmit Data Register Empty Interrupt */
  245. // usart_interrupt_enable(USARTx_TxBuf_Struct->usart_periph, USART_TDBE_INT, FALSE);
  246. DL_UART_Main_disableInterrupt(USARTx_TxBuf_Struct->usart_periph, DL_UART_MAIN_INTERRUPT_EOT_DONE);
  247. }
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. //void USART1_IRQHandler(void)
  254. void UART_BAT_INST_IRQHandler(void)//UART1_IRQHandler
  255. {
  256. // USARTx_Rx_IRQ(&UART_RxBuff_Struct1);
  257. // USARTx_Tx_IRQ(&UART_TxBuff_Struct1);
  258. // USARTx_RxTX_IRQ(&UART_TxBuff_Struct1,&UART_RxBuff_Struct1);
  259. }
  260. //void USART2_IRQHandler(void)
  261. void UART_HMI_INST_IRQHandler(void) //UART0_IRQHandler
  262. {
  263. // USARTx_Rx_IRQ(&UART_RxBuff_Struct2);
  264. // USARTx_Tx_IRQ(&UART_TxBuff_Struct2);
  265. USARTx_RxTX_IRQ(&UART_TxBuff_Struct2,&UART_RxBuff_Struct2);
  266. }
  267. /************************ (C) END OF FILE *********************************/