2023-07-22 09:42 AM
Hello,
I have been following a tutorial which is supposed to transmit a string of characters from the board to the realterm terminal on my computer across the USB st-link, however when I run it I do not see anything in the realterm terminal
This is the code:
#include "stm32l4xx.h"
#include <stdint.h>
#define SYS_FREQ 4000000
#define APB1_CLK SYS_FREQ
#define GPIOBEN (1U<<1)// 0b 0000 0000 0000 0000 0000 0000 0000 0010
#define LPUART1EN (1U<<0)
#define CR1_TE (1U<<3)
#define CR1_UE (1U<<0)
#define ISR_TXE (1U<<7)
static uint16_t compute_lpuart_div(uint32_t PeriphClk, uint32_t Baudrate);
static void lpuart_set_baudrate(uint32_t PeriphClk, uint32_t Baudrate);
static void lpuart1_write(uint8_t ch);
void lpuart1_tx_init(void);
int main(void){
lpuart1_tx_init();
while(1){
lpuart1_write('y');
}
}
void lpuart1_tx_init(void){
//Configure the lpuart pin
//Enable clock access to GPIOB as TX line
RCC->AHB2ENR |= GPIOBEN;
//Set PB11 as alternate function mode
GPIOB->MODER &= ~(1U<<22);
GPIOB->MODER |= (1U<<23);
//Set alternate function type to lpuart
GPIOB->AFR[1] &= ~(1U<<12);
GPIOB->AFR[1] &= ~(1U<<13);
GPIOB->AFR[1] &= ~(1U<<14);
GPIOB->AFR[1] |= (1U<<15);
//Configure the lpuart module
//Enable clock access to lpuart
RCC->APB1ENR2 |= LPUART1EN;
//Configure baudrate
lpuart_set_baudrate(APB1_CLK, 115200);
//Configure transfer direction
LPUART1->CR1 = CR1_TE;
//Enable lpuart module
LPUART1->CR1 |= CR1_UE;
}
static void lpuart1_write(uint8_t ch){
while(!(LPUART1-> ISR & ISR_TXE)){}
LPUART1->TDR = (ch & 0xFF);
}
static void lpuart_set_baudrate(uint32_t PeriphClk, uint32_t Baudrate){
LPUART1->BRR = compute_lpuart_div(PeriphClk, Baudrate);
}
static uint16_t compute_lpuart_div(uint32_t PeriphClk, uint32_t Baudrate){
return ((PeriphClk + (Baudrate/2))/Baudrate);
}
Are there any mistakes I am making here? I have checked the pins etc over many times and believe it to be correct.
Any help would be greatly appreciated. Thank you.
2023-07-22 11:32 AM
While waiting for more useful answers: generate a little project with Cube. Of course, make sure that correct pins are assigned to the LPUART.
2023-07-23 04:55 AM - edited 2023-07-23 05:15 AM
APB1_CLK is actually 4MHz?*
I don't have the patience to check all your defines to redefine register bits. It would be better to use the values from the stm32l476xx.h file
Je n'ai pas la patience de verifier tous vos define pour redéfinir les bits des registres. Il vaudrait mieux utiliser les valeurs du fichier stm32l476xx.h
2023-07-23 05:12 AM
According to the RM the BBR formula is:
Tx/Rx baud = (256 × fCK) / LPUARTDIV
So for 4MHz and 115200 : BBR = (256 * 4000000) / 115200
2023-07-23 06:09 AM
Hi, thanks for the reply, I have updated the formula as you have show but am still not getting any output to the realterm terminal. Could there be any other potential issues?
I am fairly sure about the register bits as I have done these over multiple times.
Thanks
2023-07-23 07:12 AM
What do you see in the debugger:
Are the bits in the UART registers OK?
Does the ISR_TXE bit alive ? Are you stuck in the while?
Are the bits in the PORTB registers OK?
2023-07-24 03:21 AM
@Nikita91 wrote:Are the bits in the UART registers OK?
The TDR register appears to be successfully filled when passing through the loop,
TC: Transmission complete and TXE: Transmit data register empty are the only active bits in the ISR register.
In CR1 just the UE: LPUART enable and the TE: Transmitter enable bits are active.
@Nikita91 wrote:Are you stuck in the while?
The debugger appears to be successfully passing in and out of the loop, I do not see it getting stuck anywhere.
@Nikita91 wrote:Are the bits in the PORTB registers OK?
the RCC looks good, AFR looks good so does MODER.