uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /******************************************************************************
  2. * @brief providing common UART API.
  3. *
  4. ******************************************************************************/
  5. #include "uart.h"
  6. /******************************************************************************
  7. * Local variables
  8. ******************************************************************************/
  9. UART_CallbackType UART_Callback = NULL;
  10. /******************************************************************************
  11. * Local function prototypes
  12. ******************************************************************************/
  13. /******************************************************************************
  14. * Local functions
  15. *****************************************************************************/
  16. /******************************************************************************
  17. * Global functions
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * define UART APIs
  21. *
  22. *//*! @addtogroup uart_api_list
  23. * @{
  24. *******************************************************************************/
  25. /*****************************************************************************//*!
  26. *
  27. * @brief initialize the UART, interrupts disabled, and no hardware flow-control.
  28. *
  29. * @param[in] pUART base of UART port
  30. * @param[in] pConfig pointer to UART configuration structure
  31. *
  32. * @return none
  33. *
  34. * @ Pass/ Fail criteria: none
  35. *****************************************************************************/
  36. void UART_Init(UART_Type *pUART, UART_ConfigType *pConfig)
  37. {
  38. uint16_t u16Sbr;
  39. uint8_t u8Temp;
  40. uint32_t u32SysClk = pConfig->u32SysClkHz;
  41. uint32_t u32Baud = pConfig->u32Baudrate;
  42. /* Sanity check */
  43. ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
  44. /* Enable the clock to the selected UART */
  45. if (pUART == UART0)
  46. {
  47. SIM->SCGC |= SIM_SCGC_UART0_MASK;
  48. }
  49. #if defined(CPU_NV32) | defined(CPU_NV326)
  50. else if (pUART == UART1)
  51. {
  52. SIM->SCGC |= SIM_SCGC_UART1_MASK;
  53. }
  54. else
  55. {
  56. SIM->SCGC |= SIM_SCGC_UART2_MASK;
  57. }
  58. #endif
  59. /* Make sure that the transmitter and receiver are disabled while we
  60. * change settings.
  61. */
  62. pUART->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
  63. /* Configure the UART for 8-bit mode, no parity */
  64. pUART->C1 = 0;
  65. /* Calculate baud settings */
  66. u16Sbr = (((u32SysClk)>>4) + (u32Baud>>1))/u32Baud;
  67. /* Save off the current value of the UARTx_BDH except for the SBR field */
  68. u8Temp = pUART->BDH & ~(UART_BDH_SBR_MASK);
  69. pUART->BDH = u8Temp | UART_BDH_SBR(u16Sbr >> 8);
  70. pUART->BDL = (uint8_t)(u16Sbr & UART_BDL_SBR_MASK);
  71. /* Enable receiver and transmitter */
  72. pUART->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
  73. }
  74. /*****************************************************************************//*!
  75. *
  76. * @brief receive a character.
  77. *
  78. * @param[in] pUART base of UART port
  79. *
  80. * @return unsigned char
  81. *
  82. *****************************************************************************/
  83. uint8_t UART_GetChar(UART_Type *pUART)
  84. {
  85. /* Sanity check */
  86. ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
  87. /* Wait until character has been received */
  88. while (!(pUART->S1 & UART_S1_RDRF_MASK));
  89. /* Return the 8-bit data from the receiver */
  90. return pUART->D;
  91. }
  92. /*****************************************************************************//*!
  93. *
  94. * @brief send a character.
  95. *
  96. * @param[in] pUART base of UART port
  97. * @param[in] u8Char char to send
  98. *
  99. * @return none
  100. *
  101. *****************************************************************************/
  102. void UART_PutChar(UART_Type *pUART, uint8_t u8Char)
  103. {
  104. /* Wait until space is available in the FIFO */
  105. while (!(pUART->S1 & UART_S1_TDRE_MASK));
  106. /* Send the character */
  107. pUART->D = (uint8_t)u8Char;
  108. }
  109. /*****************************************************************************//*!
  110. *
  111. * @brief set baudrate.
  112. *
  113. * @param[in] pUART base of UART port
  114. * @param[in] pConfig baudrate config parameters
  115. *
  116. * @return none
  117. *
  118. * @ Pass/ Fail criteria:
  119. *****************************************************************************/
  120. void UART_SetBaudrate(UART_Type *pUART, UART_ConfigBaudrateType *pConfig)
  121. {
  122. uint8_t u8Temp;
  123. uint16_t u16Sbr;
  124. uint32_t u32SysClk = pConfig->u32SysClkHz;
  125. uint32_t u32baud = pConfig->u32Baudrate;
  126. /* Sanity check */
  127. ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
  128. /* Calculate baud settings */
  129. u16Sbr = (((u32SysClk)>>4) + (u32baud>>1))/u32baud;
  130. /* Save off the current value of the UARTx_BDH except for the SBR field */
  131. u8Temp = pUART->BDH & ~(UART_BDH_SBR_MASK);
  132. pUART->BDH = u8Temp | UART_BDH_SBR(u16Sbr >> 8);
  133. pUART->BDL = (uint8_t)(u16Sbr & UART_BDL_SBR_MASK);
  134. /* Enable receiver and transmitter */
  135. pUART->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
  136. }
  137. /*****************************************************************************//*!
  138. *
  139. * @brief enable interrupt.
  140. *
  141. * @param[in] pUART base of UART port
  142. * @param[in] InterruptType interrupt type
  143. *
  144. * @return none
  145. *
  146. * @ Pass/ Fail criteria:
  147. *****************************************************************************/
  148. void UART_EnableInterrupt(UART_Type *pUART, UART_InterruptType InterruptType)
  149. {
  150. /* Sanity check */
  151. ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
  152. if (InterruptType == UART_TxBuffEmptyInt)
  153. {
  154. pUART->C2 |= UART_C2_TIE_MASK;
  155. }
  156. else if (InterruptType == UART_TxCompleteInt)
  157. {
  158. pUART->C2 |= UART_C2_TCIE_MASK;
  159. }
  160. else if (InterruptType == UART_RxBuffFullInt)
  161. {
  162. pUART->C2 |= UART_C2_RIE_MASK;
  163. }
  164. else if (InterruptType == UART_IdleLineInt)
  165. {
  166. pUART->C2 |= UART_C2_ILIE_MASK;
  167. }
  168. else if (InterruptType == UART_RxOverrunInt)
  169. {
  170. pUART->C3 |= UART_C3_ORIE_MASK;
  171. }
  172. else if (InterruptType == UART_NoiseErrorInt)
  173. {
  174. pUART->C3 |= UART_C3_NEIE_MASK;
  175. }
  176. else if (InterruptType == UART_FramingErrorInt)
  177. {
  178. pUART->C3 |= UART_C3_FEIE_MASK;
  179. }
  180. else if (InterruptType == UART_ParityErrorInt)
  181. {
  182. pUART->C3 |= UART_C3_FEIE_MASK;
  183. }
  184. else
  185. {
  186. /* un-supported Interrupt type */
  187. }
  188. }
  189. /*****************************************************************************//*!
  190. *
  191. * @brief disable interrupt.
  192. *
  193. * @param[in] pUART base of UART port
  194. * @param[in] InterruptType interrupt type
  195. *
  196. * @return none
  197. *
  198. * @ Pass/ Fail criteria:
  199. *****************************************************************************/
  200. void UART_DisableInterrupt(UART_Type *pUART, UART_InterruptType InterruptType)
  201. {
  202. /* Sanity check */
  203. ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
  204. if (InterruptType == UART_TxBuffEmptyInt)
  205. {
  206. pUART->C2 &= (~UART_C2_TIE_MASK);
  207. }
  208. else if (InterruptType == UART_TxCompleteInt)
  209. {
  210. pUART->C2 &= (~UART_C2_TCIE_MASK);
  211. }
  212. else if (InterruptType == UART_RxBuffFullInt)
  213. {
  214. pUART->C2 &= (~UART_C2_RIE_MASK);
  215. }
  216. else if (InterruptType == UART_IdleLineInt)
  217. {
  218. pUART->C2 &= (~UART_C2_ILIE_MASK);
  219. }
  220. else if (InterruptType == UART_RxOverrunInt)
  221. {
  222. pUART->C3 &= (~UART_C3_ORIE_MASK);
  223. }
  224. else if (InterruptType == UART_NoiseErrorInt)
  225. {
  226. pUART->C3 &= (~UART_C3_NEIE_MASK);
  227. }
  228. else if (InterruptType == UART_FramingErrorInt)
  229. {
  230. pUART->C3 &= (~UART_C3_FEIE_MASK);
  231. }
  232. else if (InterruptType == UART_ParityErrorInt)
  233. {
  234. pUART->C3 &= (~UART_C3_FEIE_MASK);
  235. }
  236. else
  237. {
  238. /* un-supported interrupt type */
  239. }
  240. }
  241. /*****************************************************************************//*!
  242. *
  243. * @brief get flags from 2 UART status registers.
  244. *
  245. * @param[in] pUART base of UART port
  246. *
  247. * @return 16-bit flags
  248. *
  249. * @ Pass/ Fail criteria:
  250. *****************************************************************************/
  251. uint16_t UART_GetFlags(UART_Type *pUART)
  252. {
  253. uint16_t u16StatusFlags = 0;
  254. u16StatusFlags = pUART->S2;
  255. u16StatusFlags = (u16StatusFlags<<8)| pUART->S1;
  256. return u16StatusFlags;
  257. }
  258. /*****************************************************************************//*!
  259. *
  260. * @brief check whether the specified flag is set.
  261. *
  262. * @param[in] pUART base of UART port
  263. * @param[in] FlagType flag type
  264. *
  265. * @return
  266. * 1, flag is set
  267. * 0, flag is clear
  268. *
  269. * @ Pass/ Fail criteria: none
  270. *****************************************************************************/
  271. uint8_t UART_CheckFlag(UART_Type *pUART, UART_FlagType FlagType)
  272. {
  273. uint16_t u16StatusFlags = 0;
  274. u16StatusFlags = UART_GetFlags(pUART);
  275. return (u16StatusFlags & (1<<FlagType));
  276. }
  277. /*****************************************************************************//*!
  278. *
  279. * @brief send a series of charecters using polling mode.
  280. *
  281. * @param[in] pUART base of UART port
  282. * @param[in] pSendBuff pointer of charecters to send
  283. * @param[in] u32Length number of charecters
  284. *
  285. * @return none
  286. *
  287. * @ Pass/ Fail criteria:
  288. *****************************************************************************/
  289. void UART_SendWait(UART_Type *pUART, uint8_t *pSendBuff, uint32_t u32Length)
  290. {
  291. uint8_t u8TxChar;
  292. uint32_t i;
  293. for (i = 0; i < u32Length; i++)
  294. {
  295. u8TxChar = pSendBuff[i];
  296. while (!UART_IsTxBuffEmpty(pUART))
  297. {
  298. #if defined(ENABLE_WDOG)
  299. WDOG_Feed();
  300. #endif
  301. }
  302. UART_WriteDataReg(pUART, u8TxChar);
  303. }
  304. }
  305. /*****************************************************************************//*!
  306. *
  307. * @brief receive a series of charecters using polling mode.
  308. *
  309. * @param[in] pUART base of UART port
  310. * @param[in] pReceiveBuff pointer of charecters to receive
  311. * @param[in] u32Length number of charecters
  312. *
  313. * @return none
  314. *
  315. * @ Pass/ Fail criteria:
  316. *****************************************************************************/
  317. void UART_ReceiveWait(UART_Type *pUART, uint8_t *pReceiveBuff, uint32_t u32Length)
  318. {
  319. uint8_t u8RxChar;
  320. uint32_t i;
  321. for (i = 0; i < u32Length; i++)
  322. {
  323. while (!UART_IsRxBuffFull(pUART))
  324. {
  325. #if defined(ENABLE_WDOG)
  326. WDOG_Feed();
  327. #endif
  328. }
  329. u8RxChar = UART_ReadDataReg(pUART);
  330. pReceiveBuff[i] = u8RxChar;
  331. }
  332. }
  333. /*****************************************************************************//*!
  334. *
  335. * @brief wait tx complete.
  336. *
  337. * @param[in] pUART base of UART port
  338. *
  339. * @return none
  340. *
  341. * @ Pass/ Fail criteria: none*****************************************************************************/
  342. void UART_WaitTxComplete(UART_Type *pUART)
  343. {
  344. while (!UART_IsTxComplete(pUART));
  345. }
  346. /*****************************************************************************//*!
  347. *
  348. * @brief set up UART callback routines to be called by interrupt service routine.
  349. *
  350. * @param[in] pUART pointer to an UART register base
  351. * @param[in] pfnCallback callback routine
  352. *
  353. * @return none
  354. *
  355. * @ Pass/ Fail criteria: none
  356. *****************************************************************************/
  357. void UART_SetCallback(UART_CallbackType pfnCallback)
  358. {
  359. //uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
  360. UART_Callback = pfnCallback;
  361. }
  362. /*! @} End of uart_api_list */
  363. /*****************************************************************************//*!
  364. *
  365. * @brief uart0 interrupt service routine.
  366. *
  367. * @param none
  368. *
  369. * @return none
  370. *
  371. * @ Pass/ Fail criteria:
  372. *****************************************************************************/
  373. void UART0_Isr(void)
  374. {
  375. UART_Callback(UART0);
  376. }
  377. #if defined(CPU_NV32) | defined(CPU_NV326)
  378. /*****************************************************************************//*!
  379. *
  380. * @brief uart1 interrupt service routine.
  381. *
  382. * @param none
  383. *
  384. * @return none
  385. *
  386. * @ Pass/ Fail criteria:
  387. *****************************************************************************/
  388. void UART1_Isr(void)
  389. {
  390. UART_Callback(UART1);
  391. }
  392. /*****************************************************************************//*!
  393. *
  394. * @brief uart2 interrupt service routine.
  395. *
  396. * @param none
  397. *
  398. * @return none
  399. *
  400. * @ Pass/ Fail criteria:
  401. *****************************************************************************/
  402. void UART2_Isr(void)
  403. {
  404. UART_Callback(UART2);
  405. }
  406. #endif