GPS Device
Loading...
Searching...
No Matches
USART Public APIs

Functions

void usart_init (USART_CFG_ts *usart_cfg)
 Initializes the USART peripheral with the given configuration.
void usart_deinit (USART_REGDEF_ts const *instance)
 Deinitializes the USART peripheral, disables its NVIC interrupt, and disables its clock.
void usart_send (USART_REGDEF_ts *instance, uint8_t *tx_buffer, uint32_t len)
 Blocking USART transmit.
void usart_receive (USART_REGDEF_ts const *instance, uint8_t *rx_buffer, uint32_t len)
 Blocking USART receive.
void usart_set_transmission (USART_REGDEF_ts *instance, EN_STATUS_te en_status)
 Enables or disables the USART transmitter.
void usart_set_reception (USART_REGDEF_ts *instance, EN_STATUS_te en_status)
 Enables or disables the USART receiver.
void usart_get_name (USART_REGDEF_ts const *instance, char *name)
 Returns the name string of a USART peripheral instance.

Detailed Description

Function Documentation

◆ usart_init()

void usart_init ( USART_CFG_ts * usart_cfg)

Initializes the USART peripheral with the given configuration.

See also
usart_init

Definition at line 28 of file stm32f401re_usart.c.

28 {
29 // Enable peripheral clock
30 usart_set_pclk(usart_cfg->instance, ENABLE);
31
32 // Configure data word length (8 or 9 bit)
33 usart_cfg->instance->USART_CR1 &= ~(0x1 << USART_CR1_M);
34 usart_cfg->instance->USART_CR1 |= (usart_cfg->frame_data_bits << USART_CR1_M);
35
36 // Configure stop bit count
37 usart_cfg->instance->USART_CR2 &= ~(0x3 << USART_CR2_STOP);
38 usart_cfg->instance->USART_CR2 |= (usart_cfg->frame_stop_bits << USART_CR2_STOP);
39
40 // Compute and write BRR from peripheral clock and baud rate
41 usart_set_baud_rate(usart_cfg);
42
43 // Configure oversampling ratio
44 usart_cfg->instance->USART_CR1 &= ~(0x1 << USART_CR1_OVER8);
45 usart_cfg->instance->USART_CR1 |= (usart_cfg->oversampling << USART_CR1_OVER8);
46
47 // Configure parity
48 if(usart_cfg->parity != USART_PARITY_DISABLED) {
49 usart_cfg->instance->USART_CR1 |= (0x1 << USART_CR1_PCE);
50
51 if(usart_cfg->parity == USART_PARITY_EVEN) {
52 usart_cfg->instance->USART_CR1 &= ~(0x1 << USART_CR1_PS);
53 }
54 else if(usart_cfg->parity == USART_PARITY_ODD) {
55 usart_cfg->instance->USART_CR1 |= (0x1 << USART_CR1_PS);
56 }
57 }
58
59 // Configure hardware flow control (RTS + CTS)
61 usart_cfg->instance->USART_CR3 |= (0x1 << USART_CR3_CTSE);
62 usart_cfg->instance->USART_CR3 |= (0x1 << USART_CR3_RTSE);
63 }
64
65 // Configure sample bit method
66 usart_cfg->instance->USART_CR3 &= ~(0x1 << USART_CR3_ONEBIT);
67 usart_cfg->instance->USART_CR3 |= (usart_cfg->sample_bit << USART_CR3_ONEBIT);
68
69 if(usart_cfg->mode == USART_MODE_SYNC) {
70 // LBCL, CPHA, CPOL, CLKEN — not yet implemented
71 }
72
73 // Enable RXNE interrupt and NVIC line if requested
74 if(usart_cfg->interrupt_en == USART_INTERRUPT_EN_TRUE) {
75 usart_cfg->instance->USART_CR1 |= (0x1 << USART_CR1_RXNEIE);
76
77 if(usart_cfg->instance == USART1) {
79 }
80 else if(usart_cfg->instance == USART2) {
82 }
83 else if(usart_cfg->instance == USART6) {
85 }
86 }
87
88 // Enable the USART peripheral
89 usart_cfg->instance->USART_CR1 |= (0x1 << USART_CR1_UE);
90}
@ ENABLE
Definition common.h:100
void nvic_set_interrupt(IRQn_te interrupt_line, EN_STATUS_te en_status)
Enables or disables an interrupt line in the NVIC.
static void usart_set_baud_rate(USART_CFG_ts *usart_cfg)
Computes and writes the BRR register for the configured baud rate.
static void usart_set_pclk(USART_REGDEF_ts const *instance, EN_STATUS_te en_status)
Enables or disables the peripheral clock for a USART instance.
@ USART_MODE_SYNC
@ USART_HW_FLOW_CONTROL_ENABLED
@ USART_PARITY_ODD
@ USART_PARITY_DISABLED
@ USART_PARITY_EVEN
@ USART_INTERRUPT_EN_TRUE
#define USART6
#define USART1
#define USART2
@ USART_CR2_STOP
@ USART_CR3_ONEBIT
@ USART_CR3_CTSE
@ USART_CR3_RTSE
@ USART_CR1_PCE
@ USART_CR1_PS
@ USART_CR1_OVER8
@ USART_CR1_UE
@ USART_CR1_M
@ USART_CR1_RXNEIE
@ USART2_IRQn
@ USART6_IRQn
@ USART1_IRQn
USART_OVERSAMPLING_te oversampling
USART_SAMPLE_BIT_te sample_bit
USART_REGDEF_ts * instance
USART_FRAME_STOP_BITS_te frame_stop_bits
USART_HW_FLOW_CONTROL_te hw_flow_control
USART_FRAME_DATA_BITS_te frame_data_bits
USART_INTERRUPT_EN_te interrupt_en
USART_MODE_te mode
USART_PARITY_te parity
uint32_t volatile USART_CR3
uint32_t volatile USART_CR1
uint32_t volatile USART_CR2
Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_deinit()

void usart_deinit ( USART_REGDEF_ts const * instance)

Deinitializes the USART peripheral, disables its NVIC interrupt, and disables its clock.

See also
usart_deinit

Definition at line 93 of file stm32f401re_usart.c.

93 {
94 if(instance == USART1) {
97 }
98 else if(instance == USART2) {
101 }
102 else if(instance == USART6) {
105 }
106
107 usart_set_pclk(instance, DISABLE);
108}
@ DISABLE
Definition common.h:97
void rcc_reset_periph_apb2(RCC_APB2RSTR_te periph_position)
Resets an APB2 peripheral via RCC_APB2RSTR.
void rcc_reset_periph_apb1(RCC_APB1RSTR_te periph_position)
Resets an APB1 peripheral via RCC_APB1RSTR.
@ RCC_APB2RSTR_USART1RST
@ RCC_APB2RSTR_USART6RST
@ RCC_APB1RSTR_USART2RST
Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_send()

void usart_send ( USART_REGDEF_ts * instance,
uint8_t * tx_buffer,
uint32_t len )

Blocking USART transmit.

Blocking USART transmit. Sends len bytes from tx_buffer.

See also
usart_send

Definition at line 111 of file stm32f401re_usart.c.

111 {
112 while(len != 0) {
113 while(!((instance->USART_SR >> USART_SR_TXE) & 0x1));
114 instance->USART_DR = *tx_buffer;
115 tx_buffer++;
116 len--;
117 }
118 // Wait for TC to confirm the last byte has fully shifted out
119 while(!((instance->USART_SR >> USART_SR_TC) & 0x1));
120}
@ USART_SR_TC
@ USART_SR_TXE
uint32_t volatile USART_DR
uint32_t volatile USART_SR
Here is the caller graph for this function:

◆ usart_receive()

void usart_receive ( USART_REGDEF_ts const * instance,
uint8_t * rx_buffer,
uint32_t len )

Blocking USART receive.

Blocking USART receive. Reads len bytes into rx_buffer.

See also
usart_receive

Definition at line 123 of file stm32f401re_usart.c.

123 {
124 while(len != 0) {
125 while(!((instance->USART_SR >> USART_SR_RXNE) & 0x1));
126 *rx_buffer = instance->USART_DR;
127 rx_buffer++;
128 len--;
129 }
130}
@ USART_SR_RXNE

◆ usart_set_transmission()

void usart_set_transmission ( USART_REGDEF_ts * instance,
EN_STATUS_te en_status )

Enables or disables the USART transmitter.

Enables or disables the USART transmitter (TE bit).

See also
usart_set_transmission

Definition at line 133 of file stm32f401re_usart.c.

133 {
134 instance->USART_CR1 &= ~(0x1 << USART_CR1_TE);
135 instance->USART_CR1 |= (en_status << USART_CR1_TE);
136}
@ USART_CR1_TE
Here is the caller graph for this function:

◆ usart_set_reception()

void usart_set_reception ( USART_REGDEF_ts * instance,
EN_STATUS_te en_status )

Enables or disables the USART receiver.

Enables or disables the USART receiver (RE bit).

See also
usart_set_reception

Definition at line 139 of file stm32f401re_usart.c.

139 {
140 instance->USART_CR1 &= ~(0x1 << USART_CR1_RE);
141 instance->USART_CR1 |= (en_status << USART_CR1_RE);
142}
@ USART_CR1_RE
Here is the caller graph for this function:

◆ usart_get_name()

void usart_get_name ( USART_REGDEF_ts const * instance,
char * name )

Returns the name string of a USART peripheral instance.

Returns the name string of a USART peripheral instance (e.g. "USART1").

See also
usart_get_name

Definition at line 145 of file stm32f401re_usart.c.

145 {
146 const char usart[] = "USART";
147 uint8_t usart_len = get_str_len(usart);
148 uint8_t pos_counter = 0;
149
150 while(pos_counter != usart_len) {
151 name[pos_counter] = usart[pos_counter];
152 pos_counter++;
153 }
154
155 if(instance == USART1) name[pos_counter] = '1';
156 else if(instance == USART2) name[pos_counter] = '2';
157 else if(instance == USART6) name[pos_counter] = '6';
158 pos_counter++;
159
160 name[pos_counter] = '\0';
161}
uint32_t get_str_len(char const *str)
Returns the length of a string, excluding the null terminator.
Definition common.c:22
Here is the call graph for this function: