I can't make bare metal LPUART work in L496
Hello all,
I've recently started experimenting with bare metal programming in L496, I made LED and push button I/Os working, interrupts etc, but I cannot figure out the exact steps to make LPUART1 work and I cannot find any references/examples of LPUART initialization in L4. This is my code:
RCC -> APB1ENR1 |= (RCC_APB1ENR1_PWREN);
// Enable GPIOG clock
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOGEN;
// Enable LPUART1 clock
RCC -> APB1ENR2 |= RCC_APB1ENR2_LPUART1EN;
// Configure PG7 and PG8 as Alternate function
GPIOG->MODER &= ~(GPIO_MODER_MODE7_Msk | GPIO_MODER_MODE8_Msk);
GPIOG->MODER |= (0x02 <<GPIO_MODER_MODE7_Pos) | (0x02 << GPIO_MODER_MODE8_Pos);
// 1000: AF8
GPIOG->AFR[0] &= ~(0xFFFFFFFF);
GPIOG->AFR[1] &= ~(0xFFFFFFFF);
GPIOG->AFR[0] |= (0x8 << GPIO_AFRL_AFSEL7_Pos);
GPIOG->AFR[1] |= (0x8 << GPIO_AFRH_AFSEL8_Pos);
// 11: Very high speed
GPIOG->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED7_Msk);
GPIOG->OSPEEDR &= ~(GPIO_OSPEEDR_OSPEED8_Msk);
GPIOG->OSPEEDR |= (0x03 << GPIO_OSPEEDR_OSPEED7_Pos);
GPIOG->OSPEEDR |= (0x03 << GPIO_OSPEEDR_OSPEED8_Pos);
// 8-bit, 1 start, 1 stop, CTS/RTS disabled
LPUART1->CR1 = 0x00000000;
LPUART1->CR2 = 0x00000000;
LPUART1->CR3 = 0x00000000;
// Select PCLK1 (APB1) as clock source
// PCLK -> 48 MHz
RCC->CCIPR &= ~(RCC_CCIPR_LPUART1SEL_Msk);
// LPUART1->BRR = 833;
LPUART1->BRR = 0x2B672;
// Enable Transmitter and Receiver and USART
LPUART1->CR1 |= USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
and in main:
for(i=0; i<500000; i++);
LPUART1->TDR = 'e';
I have even tried setting up LPUART with LL (it works) and then copy pasting the same register values as an experiment, but with no success:
// clock config
RCC->PLLCFGR = 0x1000;
RCC->PLLSAI1CFGR = 0x1000;
RCC->PLLSAI2CFGR = 0x1000;
RCC->ICSCR = 0x408b0088;
RCC->CR = 0x63;
while (!(RCC->CR & RCC_CR_PLLRDY)) {};
RCC->CFGR = 0x0;
while (!(RCC->CFGR & RCC_CFGR_SWS_PLL)) {};
//Clear LPUART registers
LPUART1->CR1 = 0x0;
LPUART1->CR2 = 0x0;
LPUART1->CR3 = 0x0;
LPUART1->BRR = 0x0;
LPUART1->ISR = 0xc0;
LPUART1->RDR = 0x0;
LPUART1->TDR = 0x0;
RCC->APB1ENR1 = 0x100;
RCC->APB2ENR = 0x1;
while (!(RCC->APB2ENR & RCC_APB2ENR_SYSCFGEN)) {};
RCC -> APB1ENR1 = 0x10000400;
while (!(RCC->APB1ENR1 & RCC_APB1ENR1_PWREN)) {};
RCC -> APB1ENR2 = 0x1;
while (!(RCC->APB1ENR2 & RCC_APB1ENR2_LPUART1EN)) {};
RCC -> AHB2ENR = 0x40;
while (!(RCC->AHB2ENR & RCC_AHB2ENR_GPIOGEN)) {};
RCC->CCIPR = 0x0;
PWR->CR1 = 0x200;
PWR->CR2 = 0x200;
GPIOG->OSPEEDR = 0x3c000;
GPIOG->AFR[0] = 0x80000000;
GPIOG->AFR[1] = 0x8;
GPIOG->MODER = 0xffffffff;
LPUART1->CR1 = 0xc;
//This register can only be written when the LPUART is disabled (UE=0).
LPUART1->BRR = 0x22b9;
LPUART1->ISR = 0xc0;
LPUART1->CR1 = 0xd;
LPUART1->ISR = 0x6000d0;
Any idea what is the problem? I would greatly appreciate your help!