STM32F103RBT6 USART1 Initialisation - Page 1
Forum section: electronics / general

author: noname4me
author_rank: Regular Contributor
timestamp: January 01, 2018, 11:07:29 pm
content: Hi,I am trying to initialise the USART1 peripheral by programming the registers directly.I have a board with the uC and have connected to PA9 and PA10 (Tx and Rx) using a serial to USB converter.Code:void USART1_ENABLE(void){RCC -> APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN | RCC_APB2ENR_IOPAEN;// USART1 ->CR1 = 0;// USART1 ->CR2 = 0;// USART1 ->CR3 = 0;USART1 ->BRR = 0x1d4C; // Set baud rate to 9600 with 72MHz clock and over8 enabledUSART1 ->CR1 = USART_CR1_TE | USART_CR1_RE; //Tx and Rx enableUSART1 ->CR1 |= USART_CR1_RXNEIE; //USART1->CR1 = 0x2C; //Rx interrupt enableUSART1 ->CR1 |= USART_CR1_UE; //USART1 enable}Initially I generated code using the STM32CubeMX tool and using that I can get the USART1 to transmit and receive properly using interrupts.I then disable theCode:MX_USART1_UART_Init();function, and use my above function in its place, and it won't work.I have tried changing the baud rate of the terminal program that I'm using (teraterm) but this does not do anything.I have tried looking at the 0x40013800 memory location in the debugger, and the registers contain the same values in both cases. I suspect that there is a clock that I haven't enabled, but I don't know how to find out what it is.Can anyone give any tips on what I am missing?Thanks

author: trevwhite
author_rank: Frequent Contributor
timestamp: January 01, 2018, 11:18:22 pm
content: Have you configured the port pins including enabling the port clock and setting the pins to alternate use?ThanksTrev

author: noname4me
author_rank: Regular Contributor
timestamp: January 01, 2018, 11:59:17 pm
content: HiI thought that was handled by the cube generated gpio init function?Also... doesn't the first line enable the ahpb2, alterate function clocks which are connected to the usart1 peripheral?

author: ajb
author_rank: Super Contributor
timestamp: January 02, 2018, 06:11:56 am
content: Cube should generate the IO initialization functions . . . somewhere, but it might do that in the USART init function you're trying to replace. The RCC->APB2ENR... line does enable the GPIOA, USART, and AFIO clocks, but does NOT set IO pin configuration. You'll need to do that in the GPIOA_CRL/CRH registers, and you might also need to deal with the alternate function remap settings in AFIO_MAPR. Oh, and some of the STM32 peripherals need a second "slow" clock sometimes, but I don't think that's the case for the STM32F1 USARTs.If you're using interrupts, you will also need to make sure you enable those interrupts specifically and interrupts generally:Code:NVIC_EnableIRQ(USART1_IRQn);NVIC_SetPriority(USART1_IRQn, 2); /* setting priority is optional */__enable_irq();

author: newbrain
author_rank: Super Contributor
timestamp: January 02, 2018, 06:13:07 pm
content: Quote from: noname4me on January 01, 2018, 11:59:17 pmNot exactly.As ajb says Cube Mx will genereate the GPIO initialization code.For a peripheral namedPPP, it will be contained in the HAL_PPP_MspInit() function.The MspInit function is called by thelibraryHAL_PPP_Init(), which is in turn called inside thegeneratedMX_Inst_PPP_Init() function, whereInstis the specific instance ofPPP.So, either you need to rewrite the code inside the MspInit() or simply call it in your main() code.The GPIO init code will contain only definitions for pins used as GPIO.

author: noname4me
author_rank: Regular Contributor
timestamp: January 02, 2018, 06:20:21 pm
content: Thanks very much...i added the code to enable the gpio pins and it now works.I found it very hard to understand how the usart hal functions interacts with the registersSent from my SM-G935F using Tapatalk