| BlueNRG-1 Peripherals drivers APIs

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

BlueNRG1_uart.c
Go to the documentation of this file.
1
22 /* Includes ------------------------------------------------------------------*/
23 #include "BlueNRG1_uart.h"
24
44 #define UART_CLOCK (16000000)
45
85 void UART_Cmd(FunctionalState NewState)
86 {
87 /* Check the parameters */
88 assert_param(IS_FUNCTIONAL_STATE(NewState));
89
90 if (NewState != DISABLE) {
91 /* Enable the selected UART by setting the UARTEN bit in the UARTCR register */
92 UART->CR_b.EN = SET;
93 }
94 else {
95 /* Disable the selected UART by clearing the UARTEN bit in the UARTCR register */
96 UART->CR_b.EN = RESET;
97 }
98 }
99
100
107 void UART_DeInit(void)
108 {
109 UART->TIMEOUT = 0x000001FF;
110 UART->LCRH_RX = 0x00000000;
111 UART->IBRD = 0x00000000;
112 UART->FBRD = 0x00000000;
113 UART->LCRH_TX = 0x00000000;
114 UART->CR = 0x00040300;
115 UART->IFLS = 0x00000012;
116 UART->IMSC = 0x00000000;
117 UART->ICR = 0x00000000;
118 UART->DMACR = 0x00000000;
119 UART->XFCR = 0x00000000;
120 UART->XON1 = 0x00000000;
121 UART->XON2 = 0x00000000;
122 UART->XOFF1 = 0x00000000;
123 UART->XOFF2 = 0x00000000;
124 }
125
126
134 void UART_Init(UART_InitType* UART_InitStruct)
135 {
136 uint32_t divider;
137 uint16_t ibrd, fbrd;
138
139 /* Check the parameters */
140 assert_param(IS_UART_BAUDRATE(UART_InitStruct->UART_BaudRate));
141 assert_param(IS_UART_WORD_LENGTH(UART_InitStruct->UART_WordLengthTransmit));
142 assert_param(IS_UART_WORD_LENGTH(UART_InitStruct->UART_WordLengthReceive));
143 assert_param(IS_UART_STOPBITS(UART_InitStruct->UART_StopBits));
144 assert_param(IS_UART_PARITY(UART_InitStruct->UART_Parity));
145 assert_param(IS_UART_MODE(UART_InitStruct->UART_Mode));
146 assert_param(IS_UART_HW_FLOW_CONTROL(UART_InitStruct->UART_HardwareFlowControl));
147 assert_param(IS_FUNCTIONAL_STATE(UART_InitStruct->UART_FifoEnable));
148
149 /*---------------------------- UART BaudRate Configuration -----------------------*/
150
151 divider = (UART_CLOCK<<7) / (16 * UART_InitStruct->UART_BaudRate);
152
153 ibrd = divider >> 7;
154 UART->IBRD = ibrd;
155 fbrd = ((divider - (ibrd <<7) + 1) >> 1);
156 if (fbrd > 0x3f) {
157 ibrd++;
158 fbrd = (fbrd - 0x3F) & 0x3F;
159 }
160 UART->FBRD = fbrd;
161
162 /*---------------------------- UART Word Length Configuration -----------------------*/
163 UART->LCRH_TX_b.WLEN_TX = UART_InitStruct->UART_WordLengthTransmit;
164 UART->LCRH_RX_b.WLEN_RX = UART_InitStruct->UART_WordLengthReceive;
165
166 /*---------------------------- UART Stop Bits Configuration -----------------------*/
167 UART->LCRH_TX_b.STP2_TX = UART_InitStruct->UART_StopBits;
168 UART->LCRH_RX_b.STP2_RX = UART_InitStruct->UART_StopBits;
169
170 /*---------------------------- UART Parity Configuration -----------------------*/
171 if(UART_InitStruct->UART_Parity == UART_Parity_No) {
172 UART->LCRH_TX_b.PEN_TX = RESET;
173 UART->LCRH_RX_b.PEN_RX = RESET;
174 }
175 else if(UART_InitStruct->UART_Parity == UART_Parity_Odd) {
176 UART->LCRH_TX_b.PEN_TX = SET;
177 UART->LCRH_TX_b.EPS_TX = RESET;
178 UART->LCRH_RX_b.PEN_RX = SET;
179 UART->LCRH_RX_b.EPS_RX = RESET;
180 }
181 else {
182 UART->LCRH_TX_b.PEN_TX = SET;
183 UART->LCRH_TX_b.EPS_TX = SET;
184 UART->LCRH_RX_b.PEN_RX = SET;
185 UART->LCRH_RX_b.EPS_RX = SET;
186 }
187
188 /*---------------------------- UART Mode Configuration -----------------------*/
189 UART->CR_b.RXE = UART_InitStruct->UART_Mode&1;
190 UART->CR_b.TXE = (UART_InitStruct->UART_Mode>>1)&1;
191
192 /*---------------------------- UART Hardware flow control Configuration -----------------------*/
193 UART->CR_b.RTSEN = UART_InitStruct->UART_HardwareFlowControl&1;
194 UART->CR_b.CTSEN = (UART_InitStruct->UART_HardwareFlowControl>>1)&1;
195
196 /*---------------------------- UART Fifo Configuration -----------------------*/
197 UART->LCRH_TX_b.FEN_TX = (uint8_t)UART_InitStruct->UART_FifoEnable;
198 UART->LCRH_RX_b.FEN_RX = (uint8_t)UART_InitStruct->UART_FifoEnable;
199
200 }
201
208 void UART_StructInit(UART_InitType* UART_InitStruct)
209 {
210 /* UART_InitStruct members default value */
211 UART_InitStruct->UART_BaudRate = 115200;
212 UART_InitStruct->UART_WordLengthTransmit = UART_WordLength_8b;
213 UART_InitStruct->UART_WordLengthReceive = UART_WordLength_8b;
214 UART_InitStruct->UART_StopBits = UART_StopBits_1;
215 UART_InitStruct->UART_Parity = UART_Parity_No;
216 UART_InitStruct->UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
217 UART_InitStruct->UART_HardwareFlowControl = UART_HardwareFlowControl_None;
218 UART_InitStruct->UART_FifoEnable = DISABLE;
219 }
220
239 void UART_ITConfig(uint16_t UART_IT, FunctionalState NewState)
240 {
241 /* Check the parameters */
242 assert_param(IS_UART_CONFIG_IT(UART_IT));
243 assert_param(IS_FUNCTIONAL_STATE(NewState));
244
245 if (NewState != DISABLE) {
246 /* Enable specified interrupts */
247 SET_BIT(UART->IMSC, UART_IT);
248 }
249 else {
250 /* Disable specified interrupts */
251 CLEAR_BIT(UART->IMSC, UART_IT);
252 }
253 }
254
260 void UART_SendData(uint16_t Data)
261 {
262 /* Check the parameters */
263 assert_param(IS_UART_DATA(Data));
264
265 /* Transmit Data */
266 UART->DR = (Data & (uint16_t)0x01FF);
267 }
268
274 uint16_t UART_ReceiveData(void)
275 {
276 /* Check the parameters */
277
278 /* Receive Data */
279 return (uint16_t)(UART->DR & 0x00FF);
280 }
281
288 void UART_BreakCmd(FunctionalState NewState)
289 {
290 /* Check the parameters */
291 assert_param(IS_FUNCTIONAL_STATE(NewState));
292
293 UART->LCRH_TX_b.BRK = (uint8_t)NewState;
294 }
295
302 void UART_RequestToSendCmd(FunctionalState NewState)
303 {
304 /* Check the parameters */
305 assert_param(IS_FUNCTIONAL_STATE(NewState));
306
307 UART->CR_b.RTS = (uint8_t)NewState;
308 }
309
310
328 FlagStatus UART_GetFlagStatus(uint32_t UART_FLAG)
329 {
330 /* Check the parameters */
331 assert_param(IS_UART_FLAG(UART_FLAG));
332
333 if (UART_FLAG & 0x80000000) {
334 if (UART->RSR & UART_FLAG) {
335 return SET;
336 }
337 else {
338 return RESET;
339 }
340 }
341 else {
342 if (UART->FR & UART_FLAG) {
343 return SET;
344 }
345 else {
346 return RESET;
347 }
348 }
349
350 }
351
362 void UART_ClearFlag(uint32_t UART_FLAG)
363 {
364 /* Check the parameters */
365 assert_param(IS_UART_CLEAR_FLAG(UART_FLAG));
366
367 UART->ECR = UART_FLAG;
368 }
369
387 ITStatus UART_GetITStatus(uint16_t UART_IT)
388 {
389 /* Check the parameters */
390 assert_param(IS_UART_CONFIG_IT(UART_IT));
391
392 if ((UART->RIS & UART_IT) != (uint16_t)RESET) {
393 return SET;
394 }
395 else {
396 return RESET;
397 }
398 }
399
417 void UART_ClearITPendingBit(uint16_t UART_IT)
418 {
419 /* Check the parameters */
420 assert_param(IS_UART_CONFIG_IT(UART_IT));
421
422 UART->ICR = UART_IT;
423 }
424
425
437 void UART_TxFifoIrqLevelConfig(uint8_t UART_TxFifo_Level)
438 {
439 /* Check the parameters */
440 assert_param(IS_FIFO_LEV(UART_TxFifo_Level));
441
442 UART->IFLS_b.TXIFLSEL = UART_TxFifo_Level;
443 }
444
456 void UART_RxFifoIrqLevelConfig(uint8_t UART_RxFifo_Level)
457 {
458 /* Check the parameters */
459 assert_param(IS_FIFO_LEV(UART_RxFifo_Level));
460
461 UART->IFLS_b.RXIFLSEL = UART_RxFifo_Level;
462 }
463
474 void UART_TimeoutConfig(uint32_t UART_Timeout)
475 {
476 /* Check the parameters */
477 assert_param(IS_UART_TIMEOUT(UART_Timeout));
478
479 /* Set the timeout value */
480 UART->TIMEOUT = UART_Timeout;
481 }
482
483
484
495 void UART_DMACmd(uint8_t UART_DMAReq, FunctionalState NewState)
496 {
497 /* Check the parameters */
498 assert_param(IS_UART_DMAREQ(UART_DMAReq));
499 assert_param(IS_FUNCTIONAL_STATE(NewState));
500
501 if (NewState != DISABLE)
502 {
503 /* Enable the DMA transfer for selected requests by setting the DMAT and/or
504 DMAR bits in the UART CR3 register */
505 UART->DMACR |= UART_DMAReq;
506 }
507 else
508 {
509 /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
510 DMAR bits in the UART CR3 register */
511 UART->DMACR &= (uint8_t)~UART_DMAReq;
512 }
513 }
514
515
516
523 void UART_SwFlowControl(FunctionalState NewState)
524 {
525 /* Check the parameters */
526 assert_param(IS_FUNCTIONAL_STATE(NewState));
527
528 if (NewState != DISABLE) {
529 /* Enable UART software flow control */
530 UART->XFCR_b.SFEN = SET;
531 }
532 else {
533 /* Disable UART software flow control */
534 UART->XFCR_b.SFEN = RESET;
535 }
536 }
537
538
549 void UART_RxSwFlowControlMode(uint8_t UART_RxSwFlowCtrlMode)
550 {
551 /* Check the parameters */
552 assert_param(IS_SW_FLOW_CTRL(UART_RxSwFlowCtrlMode));
553
554 UART->XFCR_b.SFRMOD = UART_RxSwFlowCtrlMode;
555 }
556
567 void UART_TxSwFlowControlMode(uint8_t UART_TxSwFlowCtrlMode)
568 {
569 /* Check the parameters */
570 assert_param(IS_SW_FLOW_CTRL(UART_TxSwFlowCtrlMode));
571
572 UART->XFCR_b.SFTMOD = UART_TxSwFlowCtrlMode;
573 }
574
575
582 void UART_XonAnyBit(FunctionalState NewState)
583 {
584 /* Check the parameters */
585 assert_param(IS_FUNCTIONAL_STATE(NewState));
586
587 if (NewState != DISABLE) {
588 /* Enable the UART XON any bit */
589 UART->XFCR_b.XONANY = SET;
590 }
591 else {
592 /* Disable the UART XON any bit */
593 UART->XFCR_b.XONANY = RESET;
594 }
595 }
596
597
604 void UART_SpecialCharDetect(FunctionalState NewState)
605 {
606 /* Check the parameters */
607 assert_param(IS_FUNCTIONAL_STATE(NewState));
608
609 if (NewState != DISABLE) {
610 /* Enable the UART special character detection */
611 UART->XFCR_b.SPECHAR = SET;
612 }
613 else {
614 /* Disable the UART special character detection */
615 UART->XFCR_b.SPECHAR = RESET;
616 }
617 }
618
619
625 void UART_Xon1Char(uint8_t UART_XonChar)
626 {
627 /* Check the parameters */
628 assert_param(IS_SW_FLOW_CTRL(UART_XonChar));
629
630 UART->XON1 = UART_XonChar;
631 }
632
638 void UART_Xon2Char(uint8_t UART_XonChar)
639 {
640 /* Check the parameters */
641 assert_param(IS_SW_FLOW_CTRL(UART_XonChar));
642
643 UART->XON2 = UART_XonChar;
644 }
645
651 void UART_Xoff1Char(uint8_t UART_XoffChar)
652 {
653 /* Check the parameters */
654 assert_param(IS_SW_FLOW_CTRL(UART_XoffChar));
655
656 UART->XOFF1 = UART_XoffChar;
657 }
658
664 void UART_Xoff2Char(uint8_t UART_XoffChar)
665 {
666 /* Check the parameters */
667 assert_param(IS_SW_FLOW_CTRL(UART_XoffChar));
668
669 UART->XOFF2 = UART_XoffChar;
670 }
671
684 /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
UART_ClearFlag
void UART_ClearFlag(uint32_t UART_FLAG)
Clears the UART pending flags.
Definition: BlueNRG1_uart.c:362
UART_RequestToSendCmd
void UART_RequestToSendCmd(FunctionalState NewState)
Enables or disables the request to send command.
Definition: BlueNRG1_uart.c:302
UART_Xon1Char
void UART_Xon1Char(uint8_t UART_XonChar)
Sets the UART flow control mode for transmission.
Definition: BlueNRG1_uart.c:625
UART_SpecialCharDetect
void UART_SpecialCharDetect(FunctionalState NewState)
Enables or disables the UART special character detection.
Definition: BlueNRG1_uart.c:604
UART_ClearITPendingBit
void UART_ClearITPendingBit(uint16_t UART_IT)
Clears the UART interrupt pending bits.
Definition: BlueNRG1_uart.c:417
UART_TxSwFlowControlMode
void UART_TxSwFlowControlMode(uint8_t UART_TxSwFlowCtrlMode)
Sets the UART flow control mode for transmission.
Definition: BlueNRG1_uart.c:567
IS_SW_FLOW_CTRL
#define IS_SW_FLOW_CTRL(SW_FLOW_CTRL)
Definition: BlueNRG1_uart.h:255
UART_CLOCK
#define UART_CLOCK
Definition: BlueNRG1_uart.c:44
UART_SendData
void UART_SendData(uint16_t Data)
Transmits single data through the UART peripheral.
Definition: BlueNRG1_uart.c:260
UART_GetITStatus
ITStatus UART_GetITStatus(uint16_t UART_IT)
Checks whether the specified UART interrupt has occurred or not.
Definition: BlueNRG1_uart.c:387
UART_InitType::UART_Parity
uint8_t UART_Parity
Definition: BlueNRG1_uart.h:62
UART_BreakCmd
void UART_BreakCmd(FunctionalState NewState)
Enables or disables the break command.
Definition: BlueNRG1_uart.c:288
IS_UART_CLEAR_FLAG
#define IS_UART_CLEAR_FLAG(FLAG)
Definition: BlueNRG1_uart.h:212
IS_UART_BAUDRATE
#define IS_UART_BAUDRATE(BAUDRATE)
Definition: BlueNRG1_uart.h:216
IS_UART_CONFIG_IT
#define IS_UART_CONFIG_IT(IT)
Definition: BlueNRG1_uart.h:179
UART_Cmd
void UART_Cmd(FunctionalState NewState)
Enables or disables the UART peripheral.
Definition: BlueNRG1_uart.c:85
UART_SwFlowControl
void UART_SwFlowControl(FunctionalState NewState)
Enables or disables the UART software flow control.
Definition: BlueNRG1_uart.c:523
IS_UART_HW_FLOW_CONTROL
#define IS_UART_HW_FLOW_CONTROL(CONTROL)
Definition: BlueNRG1_uart.h:156
UART_Parity_Odd
#define UART_Parity_Odd
Definition: BlueNRG1_uart.h:123
UART_TxFifoIrqLevelConfig
void UART_TxFifoIrqLevelConfig(uint8_t UART_TxFifo_Level)
Sets the UART interrupt FIFO level.
Definition: BlueNRG1_uart.c:437
UART_Xoff1Char
void UART_Xoff1Char(uint8_t UART_XoffChar)
Sets the UART flow control mode for transmission.
Definition: BlueNRG1_uart.c:651
IS_FIFO_LEV
#define IS_FIFO_LEV(FIFO_LEV)
Definition: BlueNRG1_uart.h:236
UART_StopBits_1
#define UART_StopBits_1
Definition: BlueNRG1_uart.h:107
UART_InitType::UART_BaudRate
uint32_t UART_BaudRate
Definition: BlueNRG1_uart.h:51
IS_UART_WORD_LENGTH
#define IS_UART_WORD_LENGTH(LENGTH)
Definition: BlueNRG1_uart.h:95
UART_HardwareFlowControl_None
#define UART_HardwareFlowControl_None
Definition: BlueNRG1_uart.h:150
UART_XonAnyBit
void UART_XonAnyBit(FunctionalState NewState)
Enables or disables the UART XON any bit.
Definition: BlueNRG1_uart.c:582
UART_InitType::UART_WordLengthReceive
uint8_t UART_WordLengthReceive
Definition: BlueNRG1_uart.h:56
UART_ITConfig
void UART_ITConfig(uint16_t UART_IT, FunctionalState NewState)
Enables or disables the specified UART interrupts.
Definition: BlueNRG1_uart.c:239
UART_StructInit
void UART_StructInit(UART_InitType *UART_InitStruct)
Fills each UART_InitStruct member with its default value.
Definition: BlueNRG1_uart.c:208
UART_RxSwFlowControlMode
void UART_RxSwFlowControlMode(uint8_t UART_RxSwFlowCtrlMode)
Sets the UART flow control mode for reception.
Definition: BlueNRG1_uart.c:549
UART_InitType::UART_StopBits
uint8_t UART_StopBits
Definition: BlueNRG1_uart.h:59
BlueNRG1_uart.h
This file contains all the functions prototypes for the UART firmware library.
IS_UART_FLAG
#define IS_UART_FLAG(FLAG)
Definition: BlueNRG1_uart.h:205
UART_Mode_Rx
#define UART_Mode_Rx
Definition: BlueNRG1_uart.h:137
UART_ReceiveData
uint16_t UART_ReceiveData(void)
Returns the most recent received data by the UART peripheral.
Definition: BlueNRG1_uart.c:274
IS_UART_DMAREQ
#define IS_UART_DMAREQ(DMAREQ)
Definition: BlueNRG1_uart.h:271
UART_DMACmd
void UART_DMACmd(uint8_t UART_DMAReq, FunctionalState NewState)
Enables or disables the UART DMA interface.
Definition: BlueNRG1_uart.c:495
UART_InitType::UART_HardwareFlowControl
uint8_t UART_HardwareFlowControl
Definition: BlueNRG1_uart.h:68
IS_UART_STOPBITS
#define IS_UART_STOPBITS(STOPBITS)
Definition: BlueNRG1_uart.h:111
UART_DeInit
void UART_DeInit(void)
Deinitializes the UART peripheral registers to their default reset values.
Definition: BlueNRG1_uart.c:107
UART_RxFifoIrqLevelConfig
void UART_RxFifoIrqLevelConfig(uint8_t UART_RxFifo_Level)
Sets the UART interrupt FIFO level.
Definition: BlueNRG1_uart.c:456
UART_Mode_Tx
#define UART_Mode_Tx
Definition: BlueNRG1_uart.h:138
IS_UART_DATA
#define IS_UART_DATA(DATA)
Definition: BlueNRG1_uart.h:219
UART_Xoff2Char
void UART_Xoff2Char(uint8_t UART_XoffChar)
Sets the UART flow control mode for transmission.
Definition: BlueNRG1_uart.c:664
UART_InitType
Structure definition of UART initialization.
Definition: BlueNRG1_uart.h:49
UART_GetFlagStatus
FlagStatus UART_GetFlagStatus(uint32_t UART_FLAG)
Checks whether the specified UART flag is set or not.
Definition: BlueNRG1_uart.c:328
UART_Init
void UART_Init(UART_InitType *UART_InitStruct)
Initializes the UART peripheral according to the specified parameters in the UART_InitStruct.
Definition: BlueNRG1_uart.c:134
UART_InitType::UART_FifoEnable
FunctionalState UART_FifoEnable
Definition: BlueNRG1_uart.h:72
UART_Parity_No
#define UART_Parity_No
Definition: BlueNRG1_uart.h:121
UART_WordLength_8b
#define UART_WordLength_8b
Definition: BlueNRG1_uart.h:92
UART_Xon2Char
void UART_Xon2Char(uint8_t UART_XonChar)
Sets the UART flow control mode for transmission.
Definition: BlueNRG1_uart.c:638
IS_UART_TIMEOUT
#define IS_UART_TIMEOUT(VAL)
Definition: BlueNRG1_uart.h:282
UART_InitType::UART_WordLengthTransmit
uint8_t UART_WordLengthTransmit
Definition: BlueNRG1_uart.h:53
IS_UART_MODE
#define IS_UART_MODE(MODE)
Definition: BlueNRG1_uart.h:141
IS_UART_PARITY
#define IS_UART_PARITY(PARITY)
Definition: BlueNRG1_uart.h:126
UART_InitType::UART_Mode
uint8_t UART_Mode
Definition: BlueNRG1_uart.h:65
UART_TimeoutConfig
void UART_TimeoutConfig(uint32_t UART_Timeout)
UART timeout value for the interrupt. The receive timeout interrupt is asserted when the RX FIFO is n...
Definition: BlueNRG1_uart.c:474