2024-10-17 07:42 AM
Hi, I am trying to configure and use the LPUART on my STM32L552 board. From the user manual I deduce that the LPUART is connected to the ST-LINK by default, through a VCP.
My code (in this order)
Here is the code:
void enable_lpuart1(void) {
volatile uint32_t dummy;
RCC->CCIPR1 |= (1 << RCC_CCIPR1_LPUART1SEL_Pos); // selects system clock for LPUART1, which runs at 4MHz
// turn on the LPUART
RCC->APB1ENR2 |= (1 << RCC_APB1ENR2_LPUART1EN_Pos);
dummy = RCC->APB1ENR2;
dummy = RCC->APB1ENR2;
// turn on GPIOG, since this is where we will find the G-pins. PG7 = TX pin, PG8 = RX pin
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOGEN_Msk;
dummy = RCC->AHB2ENR;
dummy = RCC->AHB2ENR;
// set PG7 and PG8 to alternate mode
GPIOG->MODER &= ~(GPIO_MODER_MODE7_Msk | GPIO_MODER_MODE8_Msk);
GPIOG->MODER |= (0b10 << GPIO_MODER_MODE7_Pos) | (0b10 << GPIO_MODER_MODE8_Pos); // write the code for alternate mode (0x2)
// the datasheet sets these to alternate function 8. I am not sure what this means
GPIOG->AFR[0] &= ~GPIO_AFRL_AFSEL7;
GPIOG->AFR[1] &= ~GPIO_AFRH_AFSEL8;
GPIOG->AFR[0] |= (8 << GPIO_AFRL_AFSEL7_Pos);
GPIOG->AFR[1] |= (8 << GPIO_AFRH_AFSEL8_Pos);
// NOTE: BRR = (256 * clockfreq) / baud, clock should be 4MHz, and I set baud to 9600
// (256 * 4000000) / 9600 = 106666.666..., which rounded, and in hex, is 0x1A0AA
LPUART1->BRR = 0x1A0AA;
// UE enables the lpuart, and te enables transmission
LPUART1->CR1 |= USART_CR1_UE | USART_CR1_TE;
}
My function to write to the LPUART1 is quite short (I was inspired by code I found online)
void lpuart1_write(char c) {
LPUART1->TDR = c;
while(!(LPUART1->ISR & USART_ISR_TC));
}
I connect to the ST-LINK from my laptop via
minicom -b 9600 -D /dev/ttyACM0
Now, my problem is that the writes to the AFR register never happens. When I inspect all relevant registers in GDB they appear to receive the right value. However, the reset value for the AFR register is 0x00000000, and it remains that way even after I've 'written' to it. I am not sure why this is.
Does anyone have any guess?