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

Public functions to interact with the USART peripheral. More...

Collaboration diagram for USART Public API:

Functions

void usart_init (USART_CFG_ts *usart_cfg)
 Initializes the USART peripheral with the given configuration.
void usart_deinit (USART_REGDEF_ts const *usart_instance)
 Deinitializes the USART peripheral, disables its NVIC interrupt, and disables its clock.
void usart_send (USART_REGDEF_ts *usart_instance, uint8_t *tx_buffer, uint32_t len)
 Blocking USART transmit. Sends len bytes from tx_buffer.
void usart_receive (USART_REGDEF_ts const *usart_instance, uint8_t *rx_buffer, uint32_t len)
 Blocking USART receive. Reads len bytes into rx_buffer.
void usart_set_transmission (USART_REGDEF_ts *usart_instance, EN_STATUS_te en_status)
 Enables or disables the USART transmitter (TE bit).
void usart_set_reception (USART_REGDEF_ts *usart_instance, EN_STATUS_te en_status)
 Enables or disables the USART receiver (RE bit).
void usart_get_name (USART_REGDEF_ts const *usart_instance, char *name)
 Returns the name string of a USART peripheral instance (e.g. "USART1").
void usart1_irq_data_recv_callback (uint8_t data)
 RXNE callback for USART1. Called by USART1_IRQHandler on each received byte.
void usart6_irq_data_recv_callback (uint8_t data)
 RXNE callback for USART6. Called by USART6_IRQHandler on each received byte.

Detailed Description

Public functions to interact with the USART peripheral.

Function Documentation

◆ usart_init()

void usart_init ( USART_CFG_ts * usart_cfg)

Initializes the USART peripheral with the given configuration.

Enables the peripheral clock, configures the frame format, baud rate, oversampling, parity, hardware flow control, sample method, and (if requested) the RXNE interrupt and NVIC line. Enables the peripheral (UE = 1) at the end. TX and RX must be enabled separately via usart_set_transmission and usart_set_reception.

Parameters
[in]usart_cfgPointer to the USART configuration structure.
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.

Parameters
[in]usart_instancePointer to the USART instance to deinitialize.
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. Sends len bytes from tx_buffer.

Polls TXE before writing each byte, then waits for TC (transmission complete) after the last byte before returning.

Parameters
[in]usart_instancePointer to the USART peripheral instance.
[in]tx_bufferPointer to the transmit data buffer.
[in]lenNumber of bytes to transmit.
Note
Transmission must be enabled via usart_set_transmission before calling this function.

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. Reads len bytes into rx_buffer.

Polls RXNE before reading each byte from the data register. Do not use alongside interrupt-driven reception on the same instance, as both compete to read USART_DR.

Parameters
[in]usart_instancePointer to the USART peripheral instance.
[out]rx_bufferPointer to the receive data buffer.
[in]lenNumber of bytes to 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 (TE bit).

Parameters
[in]usart_instancePointer to the USART peripheral instance.
[in]en_statusENABLE to enable TX, DISABLE to disable it.

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 (RE bit).

Parameters
[in]usart_instancePointer to the USART peripheral instance.
[in]en_statusENABLE to enable RX, DISABLE to disable it.

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 (e.g. "USART1").

The caller must ensure name points to a buffer of at least USART_NAME_LEN + 1 bytes.

Parameters
[in]usart_instancePointer to the USART peripheral instance.
[out]namePointer to the destination buffer.

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:

◆ usart1_irq_data_recv_callback()

void usart1_irq_data_recv_callback ( uint8_t data)

RXNE callback for USART1. Called by USART1_IRQHandler on each received byte.

Defined as a weak alias. Override in application code to handle received bytes (e.g. write to a circular buffer as done in console.c). The default implementation spins in an infinite loop.

Note
Must not be called directly from application code.
Parameters
[in]dataThe byte received from USART1_DR.

RXNE callback for USART1. Called by USART1_IRQHandler on each received byte.

Spins in an infinite loop if no application-level override is provided. Override this function in application code to handle received bytes.

RXNE callback for USART1. Called by USART1_IRQHandler on each received byte.

Called automatically by the USART1 interrupt handler on each received byte. Writes data directly into the internal USART circular buffer for later processing by console_run.

Note
This function must not be called directly from application code.
Parameters
[in]dataThe byte received from the USART RX register.

Definition at line 226 of file console.c.

226 {
227 cbuf_write(&internal_state.usart_data_recv_cbuf, &data, 1);
228}
static struct internal_state_s internal_state
Singleton instance of the SysTick driver internal state.
ERR_te cbuf_write(CBUF_HANDLE_ts *cbuf_handle, uint8_t *input_buf, uint32_t input_len)
Writes data from an input buffer into the circular buffer.
Definition cbuf.c:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart6_irq_data_recv_callback()

void usart6_irq_data_recv_callback ( uint8_t data)

RXNE callback for USART6. Called by USART6_IRQHandler on each received byte.

Defined as a weak alias. Override in application code to handle received bytes (e.g. write to a circular buffer as done in neo6.c). The default implementation spins in an infinite loop.

Note
Must not be called directly from application code.
Parameters
[in]dataThe byte received from USART6_DR.

RXNE callback for USART6. Called by USART6_IRQHandler on each received byte.

Spins in an infinite loop if no application-level override is provided. Override this function in application code to handle received bytes.

RXNE callback for USART6. Called by USART6_IRQHandler on each received byte.

Called automatically by the USART6 interrupt handler on each received byte. Writes data directly into the internal USART circular buffer for later processing by neo6_run.

Note
This function must not be called directly from application code.
Parameters
[in]dataThe byte received from the USART RX register.

Definition at line 908 of file neo6.c.

908 {
909 cbuf_write(&internal_state.usart_data_recv_cbuf, &data, 1);
910}
Here is the call graph for this function:
Here is the caller graph for this function: