Skip to main content
Visitor II
October 17, 2024
Question

Writes to ARF doesn't happen on STM32L552

  • October 17, 2024
  • 0 replies
  • 594 views

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.

krook_0-1729175643434.png

My code (in this order)

  • Selects the system clock to be used for LPUART1 (which is 4MHz by default)
  • Turns on the LPUART1
  • Turns on power to GPIOG through AHB2ENR (since GPIOG is where we find PG7 and PG8)
  • Sets PG7 and PG8 to function in alternate mode
  • The datasheet says that the alternate function for these pins are alternate function 8. I don't quite know what this means, but I don't think this is a problem.
  • Writes to BRR. Since my clock frequency should be 4MHz, and I've randomly chosen 9600 as my baudrate (does this matter?). The reference manual says that BRR is computed for LPUART as (256 * clockfreq) / baudrate. (256 * 4000000) / 9600 = 106666.66..., which in HEX (and rounded) is 0x1A0AA
  • Enables the usart and transmission

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?

    This topic has been closed for replies.