hal_uart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_uart.c
  3. /// @file hal_uart.c
  4. /// @author AE TEAM
  5. /// @brief THIS FILE PROVIDES ALL THE UART FIRMWARE FUNCTIONS.
  6. ////////////////////////////////////////////////////////////////////////////////
  7. /// @attention
  8. ///
  9. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  10. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  11. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  12. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  13. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  14. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  15. ///
  16. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // Define to prevent recursive inclusion
  19. #define _HAL_UART_C_
  20. // Files includes
  21. #include "hal_rcc.h"
  22. #include "hal_uart.h"
  23. #include "hal_gpio.h"
  24. #include "hal_dma.h"
  25. ////////////////////////////////////////////////////////////////////////////////
  26. /// @addtogroup MM32_Hardware_Abstract_Layer
  27. /// @{
  28. ////////////////////////////////////////////////////////////////////////////////
  29. ///@addtogroup UART_HAL
  30. ///@{
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /// @addtogroup UART_Exported_Functions
  33. /// @{
  34. ////////////////////////////////////////////////////////////////////////////////
  35. /// @brief Deinitializes the uart peripheral registers to their
  36. /// default reset values.
  37. /// @param uart: Select the UART or the UART peripheral.
  38. /// @retval None.
  39. ////////////////////////////////////////////////////////////////////////////////
  40. void UART_DeInit(UART_TypeDef* uart)
  41. {
  42. if(UART2 == uart) {
  43. exRCC_APB1PeriphReset(RCC_APB1ENR_UART2);
  44. }
  45. if(UART1 == uart) {
  46. exRCC_APB2PeriphReset(RCC_APB2ENR_UART1);
  47. }
  48. if(UART3 == uart) {
  49. exRCC_APB1PeriphReset(RCC_APB1ENR_UART3);
  50. }
  51. if(UART4 == uart) {
  52. exRCC_APB1PeriphReset(RCC_APB1ENR_UART4);
  53. }
  54. if(UART5 == uart) {
  55. exRCC_APB1PeriphReset(RCC_APB1ENR_UART5);
  56. }
  57. if(UART6 == uart) {
  58. exRCC_APB2PeriphReset(RCC_APB2ENR_UART6);
  59. }
  60. if(UART7 == uart) {
  61. exRCC_APB1PeriphReset(RCC_APB1ENR_UART7);
  62. }
  63. if(UART8 == uart) {
  64. exRCC_APB1PeriphReset(RCC_APB1ENR_UART8);
  65. }
  66. }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. /// @brief Initializes the uart peripheral according to the specified
  69. /// parameters in the UART_InitStruct .
  70. /// @param uart: Select the UART or the UART peripheral.
  71. /// @param init_struct: pointer to a UART_InitTypeDef structure
  72. /// that contains the configuration information for the
  73. /// specified UART peripheral.
  74. /// @retval None.
  75. ////////////////////////////////////////////////////////////////////////////////
  76. void UART_Init(UART_TypeDef* uart, UART_InitTypeDef* init_struct)
  77. {
  78. u32 apbclock = 0x00;
  79. // UART CCR Configuration
  80. MODIFY_REG(uart->CCR, UART_CCR_CHAR, init_struct->WordLength);
  81. MODIFY_REG(uart->CCR, (UART_CCR_SPB0 | UART_CCR_SPB1), init_struct->StopBits);
  82. MODIFY_REG(uart->CCR, (UART_CCR_PEN | UART_CCR_PSEL), init_struct->Parity);
  83. // UART GCR Configuration
  84. MODIFY_REG(uart->GCR, (UART_GCR_TX | UART_GCR_RX), init_struct->Mode);
  85. MODIFY_REG(uart->GCR, UART_GCR_AUTOFLOW, init_struct->HWFlowControl);
  86. //UART BRR Configuration
  87. //Configure the UART Baud Rate
  88. if (uart == UART1) {
  89. apbclock = RCC_GetPCLK2Freq();
  90. }
  91. else {
  92. apbclock = RCC_GetPCLK1Freq();
  93. }
  94. // Determine the UART_baud
  95. uart->BRR = (apbclock / init_struct->BaudRate) / 16;
  96. uart->FRA = (apbclock / init_struct->BaudRate) % 16;
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////
  99. /// @brief Fills each UART_InitStruct member with its default value.
  100. /// @param init_struct: pointer to a UART_InitTypeDef structure
  101. /// which will be initialized.
  102. /// @retval None.
  103. ////////////////////////////////////////////////////////////////////////////////
  104. void UART_StructInit(UART_InitTypeDef* init_struct)
  105. {
  106. // UART_InitStruct members default value
  107. init_struct->BaudRate = 9600;
  108. init_struct->WordLength = UART_WordLength_8b;
  109. init_struct->StopBits = UART_StopBits_1;
  110. init_struct->Parity = UART_Parity_No;
  111. init_struct->Mode = UART_GCR_RX | UART_GCR_TX;
  112. init_struct->HWFlowControl = UART_HWFlowControl_None;
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////
  115. /// @brief Enables or disables the specified UART peripheral.
  116. /// @param uart: Select the UART or the UART peripheral.
  117. /// @param state: new state of the uart peripheral.
  118. /// This parameter can be: ENABLE or DISABLE.
  119. /// @retval None.
  120. ////////////////////////////////////////////////////////////////////////////////
  121. void UART_Cmd(UART_TypeDef* uart, FunctionalState state)
  122. {
  123. MODIFY_REG(uart->GCR, UART_GCR_UART, state << UART_GCR_UART_Pos);
  124. }
  125. ////////////////////////////////////////////////////////////////////////////////
  126. /// @brief Enables or disables the specified UART interrupts.
  127. /// @param uart: Select the UART or the UART peripheral.
  128. /// @param it: specifies the UART interrupt sources to be
  129. /// enabled or disabled.
  130. /// This parameter can be one of the following values:
  131. /// @arg UART_IT_ERR: Error interrupt(Frame error,)
  132. /// @arg UART_IT_PE: Parity Error interrupt
  133. /// @arg UART_OVER_ERR: overrun Error interrupt
  134. /// @arg UART_IT_RXIEN: Receive Data register interrupt
  135. /// @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
  136. ///
  137. /// @param state: new state of the specified uart interrupts.
  138. /// This parameter can be: ENABLE or DISABLE.
  139. /// @retval None.
  140. ////////////////////////////////////////////////////////////////////////////////
  141. void UART_ITConfig(UART_TypeDef* uart, u16 it, FunctionalState state)
  142. {
  143. (state) ? (uart->IER |= it) : (uart->IER &= ~it);
  144. }
  145. ////////////////////////////////////////////////////////////////////////////////
  146. /// @brief Enables or disables the UART DMA interface.
  147. /// @param uart: Select the UART or the UART peripheral.
  148. /// @param dma_request: specifies the DMA request.
  149. /// This parameter can be any combination of the following values:
  150. /// @arg UART_DMAReq_EN: UART DMA transmit request
  151. ///
  152. /// @param state: new state of the DMA Request sources.
  153. /// This parameter can be: ENABLE or DISABLE.
  154. /// @retval None.
  155. ////////////////////////////////////////////////////////////////////////////////
  156. void UART_DMACmd(UART_TypeDef* uart, u16 dma_request, FunctionalState state)
  157. {
  158. MODIFY_REG(uart->GCR, UART_GCR_DMA, state << UART_GCR_DMA_Pos);
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////
  161. /// @brief Transmits single data through the uart peripheral.
  162. /// @param uart: Select the UART or the UART peripheral.
  163. /// @param Data: the data to transmit.
  164. /// @retval None.
  165. ////////////////////////////////////////////////////////////////////////////////
  166. void UART_SendData(UART_TypeDef* uart, u16 value)
  167. {
  168. // Transmit Data
  169. WRITE_REG(uart->TDR, (value & 0xFFU));
  170. }
  171. ////////////////////////////////////////////////////////////////////////////////
  172. /// @brief Returns the most recent received data by the uart peripheral.
  173. /// @param uart: Select the UART or the UART peripheral.
  174. /// @retval The received data.
  175. ////////////////////////////////////////////////////////////////////////////////
  176. u16 UART_ReceiveData(UART_TypeDef* uart)
  177. {
  178. // Receive Data
  179. return (u16)(uart->RDR & 0xFFU);
  180. }
  181. ////////////////////////////////////////////////////////////////////////////////
  182. /// @brief Checks whether the specified UART flag is set or not.
  183. /// @param uart: Select the UART or the UART peripheral.
  184. /// @param flag: specifies the flag to check.
  185. /// This parameter can be one of the following values:
  186. /// @arg UART_FLAG_TXEMPTY: Transmit data register empty flag
  187. /// @arg UART_FLAG_TXFULL: Transmit data buffer full
  188. /// @arg UART_FLAG_RXAVL: RX Buffer has a byte flag
  189. /// @arg UART_FLAG_TXEPT: tx and shifter are emptys flag
  190. /// @retval The new state of UART_FLAG (SET or RESET).
  191. ////////////////////////////////////////////////////////////////////////////////
  192. FlagStatus UART_GetFlagStatus(UART_TypeDef* uart, u16 flag)
  193. {
  194. return (uart->CSR & flag) ? SET : RESET;
  195. }
  196. ////////////////////////////////////////////////////////////////////////////////
  197. /// @brief Checks whether the specified UART interrupt has occurred or not.
  198. /// @param uart: Select the UART or the UART peripheral.
  199. /// @param it: specifies the UART interrupt source to check.
  200. /// This parameter can be one of the following values:
  201. /// @arg UART_IT_ERR: Error interrupt(Frame error,)
  202. /// @arg UART_IT_PE: Parity Error interrupt
  203. /// @arg UART_OVER_ERR: overrun Error interrupt
  204. /// @arg UART_IT_RXIEN: Receive Data register interrupt
  205. /// @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
  206. /// @retval The new state of UART_IT (SET or RESET).
  207. ////////////////////////////////////////////////////////////////////////////////
  208. ITStatus UART_GetITStatus(UART_TypeDef* uart, u16 it)
  209. {
  210. return (uart->ISR & it) ? SET : RESET;
  211. }
  212. ////////////////////////////////////////////////////////////////////////////////
  213. /// @brief Clears the uart interrupt pending bits.
  214. /// @param uart: Select the UART or the UART peripheral.
  215. /// @param it: specifies the interrupt pending bit to clear.
  216. /// This parameter can be one of the following values:
  217. /// @arg UART_IT_ERR: Error interrupt(Frame error,)
  218. /// @arg UART_IT_PE: Parity Error interrupt
  219. /// @arg UART_OVER_ERR: overrun Error interrupt
  220. /// @arg UART_IT_RXIEN: Receive Data register interrupt
  221. /// @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
  222. /// @retval None.
  223. ////////////////////////////////////////////////////////////////////////////////
  224. void UART_ClearITPendingBit(UART_TypeDef* uart, u16 it)
  225. {
  226. //clear UART_IT pendings bit
  227. uart->ICR = it;
  228. }
  229. ////////////////////////////////////////////////////////////////////////////////
  230. /// @brief Selects the UART WakeUp method.
  231. /// @param uart: Select the UART or the UART peripheral.
  232. /// @param mode: specifies the UART wakeup method.
  233. /// @retval None.
  234. ////////////////////////////////////////////////////////////////////////////////
  235. void UART_WakeUpConfig(UART_TypeDef* uart, UART_WakeUp_TypeDef mode)
  236. {
  237. MODIFY_REG(uart->CCR, UART_CCR_WAKE, mode);
  238. }
  239. ////////////////////////////////////////////////////////////////////////////////
  240. /// @brief Determines if the UART is in mute mode or not.
  241. /// @param uart: Select the UART or the UART peripheral.
  242. /// @param state: new state of the UART mute mode.
  243. /// @retval None.
  244. ////////////////////////////////////////////////////////////////////////////////
  245. void UART_ReceiverWakeUpCmd(UART_TypeDef* uart, FunctionalState state)
  246. {
  247. MODIFY_REG(uart->CCR, UART_CCR_RWU, state << UART_CCR_RWU_Pos);
  248. }
  249. ////////////////////////////////////////////////////////////////////////////////
  250. /// @brief Sets the address of the UART Rx Address.
  251. /// @param uart: Select the UART or the UART peripheral.
  252. /// @param address: Indicates the address of the UART Rx Address.
  253. /// @retval None.
  254. ////////////////////////////////////////////////////////////////////////////////
  255. void UART_SetRXAddress(UART_TypeDef* uart, u8 address)
  256. {
  257. MODIFY_REG(uart->RXAR, UART_RXAR_ADDR, address);
  258. }
  259. ////////////////////////////////////////////////////////////////////////////////
  260. /// @brief Sets the address of the UART Rx MASK.
  261. /// @param uart: Select the UART or the UART peripheral.
  262. /// @param address: Indicates the address of the UART Rx MASK.
  263. /// @retval None.
  264. ////////////////////////////////////////////////////////////////////////////////
  265. void UART_SetRXMASK(UART_TypeDef* uart, u8 address)
  266. {
  267. MODIFY_REG(uart->RXMR, UART_RXMR_MASK, address);
  268. }
  269. ////////////////////////////////////////////////////////////////////////////////
  270. /// @brief ENBALE or DISABLE the UART's 9bit.
  271. /// @param uart: Select the UART or the UART peripheral.
  272. /// @param state: new state of the UART 9 bit.
  273. /// @retval None.
  274. ////////////////////////////////////////////////////////////////////////////////
  275. void UART_Enable9bit(UART_TypeDef* uart, FunctionalState state)
  276. {
  277. MODIFY_REG(uart->CCR, UART_CCR_B8EN, state << UART_CCR_B8EN_Pos);
  278. }
  279. ////////////////////////////////////////////////////////////////////////////////
  280. /// @brief Set the UART's 9bit Level.
  281. /// @param uart: Select the UART or the UART peripheral.
  282. /// @param state: new state of the UART 9 bit.
  283. /// @retval None.
  284. ////////////////////////////////////////////////////////////////////////////////
  285. void UART_Set9bitLevel(UART_TypeDef* uart, FunctionalState state)
  286. {
  287. MODIFY_REG(uart->CCR, UART_CCR_B8TXD, state << UART_CCR_B8TXD_Pos);
  288. }
  289. ////////////////////////////////////////////////////////////////////////////////
  290. /// @brief Set the UART's 9bit Polarity.
  291. /// @param uart: Select the UART or the UART peripheral.
  292. /// @param polarity: new state of the UART 9 bit Polarity.
  293. /// @retval None.
  294. ////////////////////////////////////////////////////////////////////////////////
  295. void UART_Set9bitPolarity(UART_TypeDef* uart, UART_9bit_Polarity_TypeDef polarity)
  296. {
  297. MODIFY_REG(uart->CCR, UART_CCR_B8POL, polarity);
  298. }
  299. ////////////////////////////////////////////////////////////////////////////////
  300. /// @brief Set the UART's 9bit Automatic Toggle.
  301. /// @param uart: Select the UART or the UART peripheral.
  302. /// @param state: new state of the UART 9 bit Automatic Toggle.
  303. /// @retval None.
  304. ////////////////////////////////////////////////////////////////////////////////
  305. void UART_Set9bitAutomaticToggle(UART_TypeDef* uart, FunctionalState state)
  306. {
  307. MODIFY_REG(uart->CCR, UART_CCR_B8TOG, state << UART_CCR_B8TOG_Pos);
  308. }
  309. ////////////////////////////////////////////////////////////////////////////////
  310. /// @brief Enables or disables the UART Half Duplex communication.
  311. /// @param uart: Select the UART or the UART peripheral.
  312. /// @param state: new state of the UART Communication.
  313. /// @retval None.
  314. ////////////////////////////////////////////////////////////////////////////////
  315. void UART_HalfDuplexCmd(UART_TypeDef* uart, FunctionalState state)
  316. {
  317. MODIFY_REG(uart->SCR, UART_SCR_HDSEL, state << UART_SCR_HDSEL_Pos);
  318. }
  319. ////////////////////////////////////////////////////////////////////////////////
  320. /// @brief Sets the specified UART guard time.
  321. /// @param uart: Select the UART or the UART peripheral.
  322. /// @param guard_time: specifies the guard time.
  323. /// @retval None.
  324. ////////////////////////////////////////////////////////////////////////////////
  325. void UART_SetGuardTime(UART_TypeDef* uart, u8 guard_time)
  326. {
  327. MODIFY_REG(uart->SCR, UART_SCR_SCFCNT, guard_time << UART_SCR_SCFCNT_Pos);
  328. // Clear the UART Guard time
  329. // uart->SCR &= SCR_SCFCNT_Mask;
  330. // Set the UART guard time
  331. // uart->SCR |= (u16)((u16)guard_time << 0x04);
  332. }
  333. ////////////////////////////////////////////////////////////////////////////////
  334. /// @brief Enables or disables the UART's Smart Card mode.
  335. /// @param uart: Select the UART or the UART peripheral.
  336. /// @param state: new state of the Smart Card mode.
  337. /// @retval None.
  338. ////////////////////////////////////////////////////////////////////////////////
  339. void UART_SmartCardCmd(UART_TypeDef* uart, FunctionalState state)
  340. {
  341. MODIFY_REG(uart->SCR, UART_SCR_SCEN, state << UART_SCR_SCEN_Pos);
  342. }
  343. ////////////////////////////////////////////////////////////////////////////////
  344. /// @brief Enables or disables NACK transmission.
  345. /// @param uart: Select the UART or the UART peripheral.
  346. /// @param state: new state of the NACK transmission.
  347. /// @retval None.
  348. ////////////////////////////////////////////////////////////////////////////////
  349. void UART_SmartCardNACKCmd(UART_TypeDef* uart, FunctionalState state)
  350. {
  351. MODIFY_REG(uart->SCR, UART_SCR_SCARB, state << UART_SCR_SCARB_Pos);
  352. }
  353. ////////////////////////////////////////////////////////////////////////////////
  354. /// @brief Transmits break characters.
  355. /// @param uart: Select the UART or the UART peripheral.
  356. /// @retval None.
  357. ////////////////////////////////////////////////////////////////////////////////
  358. void UART_SendBreak(UART_TypeDef* uart)
  359. {
  360. SET_BIT(uart->CCR, UART_CCR_BRK);
  361. }
  362. ////////////////////////////////////////////////////////////////////////////////
  363. /// @brief Enable or Disable Auto Baud-Rate Detection
  364. /// @param uart: Select the UART or the UART peripheral.
  365. /// @param state: new state of the UART AutoBaudRate Detection.
  366. /// @retval None.
  367. ////////////////////////////////////////////////////////////////////////////////
  368. void UART_AutoBaudRateCmd(UART_TypeDef* uart, FunctionalState state)
  369. {
  370. state ? SET_BIT(uart->ABRCR, UART_ABRCR_ABREN) : CLEAR_BIT(uart->ABRCR, UART_ABRCR_ABREN) ;
  371. }
  372. ////////////////////////////////////////////////////////////////////////////////
  373. /// @brief AutoBaudRate.
  374. /// @param uart: Select the UART or the UART peripheral.
  375. /// value: special character.
  376. /// state: ENABLE/DISABLE.
  377. /// @retval None.
  378. ////////////////////////////////////////////////////////////////////////////////
  379. void UART_AutoBaudRateSet(UART_TypeDef* uart, UART_AutoBaud_TypeDef value, FunctionalState state)
  380. {
  381. CLEAR_BIT(uart->ABRCR, UART_ABRCR_ABREN);
  382. //This bit field can only be written when ABREN = 0 or the UART is disabled (UARTEN=0).
  383. if ((value == ABRMODE_FALLING_TO_RISINGEDGE1BIT) || (value == ABRMODE_STARTBIT) || (value == ABRMODE_VALUE0XFF)) {
  384. //UART measures the duration of the start bit (falling edge) to first rising edge
  385. //FORMER edge = 0 LATTER edge= 1, from fist falling edge to rising edge = one bit
  386. //___ _ _______
  387. // |_|1 x x x x x x x| = Bxxxx xxx1 F to U = 1 start bit
  388. //
  389. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  390. UART_ABRCR_LATTER | UART_ABRCR_BITCNT_MODE0 );
  391. }
  392. else if((value == ABRMODE_FALLING_TO_RISINGEDGE2BIT) || (value == Data_FE)) {
  393. //UART measures the duration of the start bit (falling edge) to first rising edge
  394. //FORMER edge = 0 LATTER edge= 1, from fist falling edge to rising edge = two bit
  395. //___ _ _______
  396. // |_ _|1 x x x x x x| = Bxxxx xx10 F to U = 2
  397. //
  398. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  399. UART_ABRCR_LATTER | UART_ABRCR_BITCNT_MODE1);
  400. }
  401. else if((value == ABRMODE_FALLING_TO_RISINGEDGE4BIT) || (value == Data_F8)) {
  402. //UART measures the duration of the start bit (falling edge) to first rising edge
  403. //FORMER edge = 0 LATTER edge= 1, from fist falling edge to rising edge = four bit
  404. //___ _ _______
  405. // |_ _ _ _|1 x x x x| = Bxxxx 1000 F to U = 4
  406. //
  407. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  408. UART_ABRCR_LATTER | UART_ABRCR_BITCNT_MODE2);
  409. }
  410. else if((value == ABRMODE_FALLING_TO_RISINGEDGE8BIT) || (value == ABRMODE_VALUE0X80)) {
  411. //UART measures the duration of the start bit (falling edge) to first rising edge
  412. //FORMER edge = 0 LATTER edge= 1, from fist falling edge to rising edge = eight bit
  413. //___ _ ______
  414. // |_ _ _ _ _ _ _ _|1 = B1000 0000 F to U = 8
  415. //
  416. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  417. UART_ABRCR_LATTER | UART_ABRCR_BITCNT_MODE3);
  418. }
  419. else if((value == ABRMODE_FALLING_TO_FALLINGEDGE2BIT) || (value == ABRMODE_VALUE0X55)) {
  420. //UART measures the duration of the start bit (falling edge) to next falling edge
  421. //FORMER edge = 0 LATTER edge= 0, from fist falling edge to next falling edge = two bit
  422. //___ _ ______
  423. // |_|1|_|x x x x x x| = Bxxxx xx01 F to F = 2 0x55 and Falling to Falling
  424. //
  425. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  426. UART_ABRCR_BITCNT_MODE1);
  427. }
  428. else if((value == ABRMODE_FALLING_TO_FALLINGEDGE4BIT) || (value == ABRMODE_VALUE0XF7)) {
  429. //UART measures the duration of the start bit (falling edge) to next falling edge
  430. //FORMER edge = 0 LATTER edge= 0, from fist falling edge to next falling edge = four bit
  431. //___ _ _ _ ______
  432. // |_|1 1 1|_|x x x x| = Bxxxx 0111 F to F = 4
  433. //
  434. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  435. UART_ABRCR_BITCNT_MODE2);
  436. }
  437. else if((value == ABRMODE_FALLING_TO_FALLINGEDGE8BIT) || (value == ABRMODE_VALUE0x7F)) {
  438. //UART measures the duration of the start bit (falling edge) to next falling edge
  439. //FORMER edge = 0 LATTER edge= 0, from fist falling edge to next falling edge = eight bit
  440. //___ _ _ _ _ _ _ _ ______
  441. // |_|1 1 1 1 1 1 1|_| = B0111 1111 F to F = 8 0x7F
  442. //
  443. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  444. UART_ABRCR_BITCNT_MODE3);
  445. }
  446. else {
  447. //UART measures the duration of the start bit (falling edge) to next falling edge
  448. //FORMER edge = 0 LATTER edge= 0, from fist falling edge to next falling edge = eight bit
  449. //___ _ _ _ _ _ _ _ ______
  450. // |_|1 1 1 1 1 1 1|_| = B0111 1111 F to F = 8 0x7F
  451. //
  452. MODIFY_REG(uart->ABRCR, UART_ABRCR_LATTER | UART_ABRCR_FORMER | UART_ABRCR_BITCNT, \
  453. UART_ABRCR_BITCNT_MODE3);
  454. }
  455. if(state == ENABLE) {
  456. SET_BIT(uart->ABRCR, UART_ABRCR_ABREN);
  457. }
  458. }
  459. /// @}
  460. /// @}
  461. /// @}