GPS Device
Loading...
Searching...
No Matches
USART Internal Helpers

Functions

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.
static void usart_set_baud_rate (USART_CFG_ts *usart_cfg)
 Computes and writes the BRR register for the configured baud rate.

Detailed Description

Function Documentation

◆ usart_set_pclk()

void usart_set_pclk ( USART_REGDEF_ts const * instance,
EN_STATUS_te en_status )
static

Enables or disables the peripheral clock for a USART instance.

Routes to the appropriate RCC APB1 or APB2 clock enable/disable call:

  • USART1 and USART6 are on APB2.
  • USART2 is on APB1.

Called by usart_init and usart_deinit.

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

Definition at line 183 of file stm32f401re_usart.c.

183 {
184 if(instance == USART1) {
186 }
187 else if(instance == USART2) {
189 }
190 else if(instance == USART6) {
192 }
193}
void rcc_set_pclk_apb2(RCC_APB2ENR_te periph_position, EN_STATUS_te en_status)
Enables or disables the peripheral clock for an APB2 peripheral.
void rcc_set_pclk_apb1(RCC_APB1ENR_te periph_position, EN_STATUS_te en_status)
Enables or disables the peripheral clock for an APB1 peripheral.
#define USART6
#define USART1
#define USART2
@ RCC_APB2ENR_USART1EN
@ RCC_APB2ENR_USART6EN
@ RCC_APB1ENR_USART2EN
Here is the call graph for this function:
Here is the caller graph for this function:

◆ usart_set_baud_rate()

void usart_set_baud_rate ( USART_CFG_ts * usart_cfg)
static

Computes and writes the BRR register for the configured baud rate.

Uses the fixed-point scaling method from the STM32 reference manual:

  • USARTDIV × 100 = (25 × PCLK) / (multiplier × baud_rate), where multiplier is 2 for OVER8 and 4 for OVER16.
  • The mantissa is USARTDIV / 100.
  • The fraction is rounded and masked to 3 bits (OVER8) or 4 bits (OVER16).

USART1 and USART6 use the APB2 clock; USART2 uses the APB1 clock.

Parameters
[in]usart_cfgPointer to the USART configuration structure.

Definition at line 209 of file stm32f401re_usart.c.

209 {
210 usart_cfg->instance->USART_BRR &= ~(0xFFFF);
211 uint32_t usart_pclk = 0;
212 uint32_t temp_reg = 0;
213 uint32_t usartdiv = 0;
214 uint32_t usartdiv_mantissa;
215 uint32_t usartdiv_fraction;
216
217 if(usart_cfg->instance == USART1 || usart_cfg->instance == USART6) {
218 usart_pclk = rcc_get_apb2_clk();
219 }
220 else if(usart_cfg->instance == USART2) {
221 usart_pclk = rcc_get_apb1_clk();
222 }
223
224 if(usart_cfg->oversampling == USART_OVERSAMPLING_8) {
225 usartdiv = ((25 * usart_pclk) / (2 * usart_cfg->baud_rate));
226 }
227 else if(usart_cfg->oversampling == USART_OVERSAMPLING_16) {
228 usartdiv = ((25 * usart_pclk) / (4 * usart_cfg->baud_rate));
229 }
230
231 usartdiv_mantissa = usartdiv / 100;
232 temp_reg |= usartdiv_mantissa << 4;
233
234 usartdiv_fraction = (usartdiv - (usartdiv_mantissa * 100));
235
236 if(usart_cfg->oversampling == USART_OVERSAMPLING_8) {
237 // Round fraction to 3 bits for OVER8
238 usartdiv_fraction = (((usartdiv_fraction * 8) + 50) / 100) & ((uint8_t)0x07);
239 }
240 else if(usart_cfg->oversampling == USART_OVERSAMPLING_16) {
241 // Round fraction to 4 bits for OVER16
242 usartdiv_fraction = (((usartdiv_fraction * 16) + 50) / 100) & ((uint8_t)0x0F);
243 }
244
245 temp_reg |= usartdiv_fraction;
246 usart_cfg->instance->USART_BRR = temp_reg;
247}
uint32_t rcc_get_apb2_clk(void)
Returns the current APB2 (high-speed) peripheral bus clock frequency in Hz.
uint32_t rcc_get_apb1_clk(void)
Returns the current APB1 (low-speed) peripheral bus clock frequency in Hz.
@ USART_OVERSAMPLING_16
@ USART_OVERSAMPLING_8
USART_OVERSAMPLING_te oversampling
USART_BAUD_RATE_te baud_rate
USART_REGDEF_ts * instance
uint32_t volatile USART_BRR
Here is the call graph for this function:
Here is the caller graph for this function: