STM32G431KB USART Problem - Page 1
Forum section: electronics / general

author: syntax333
author_rank: Regular Contributor
timestamp: March 26, 2020, 01:24:23 pm
content: Hi, I am working with STM32G431 Nucleo board which has STM32G431KB on it. I am trying to send continuous data just to see USART is working. Below is my code.main:Code:#include "main.h"#include "SysClock_Config.h"void Initialization(){RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; //Enable GPIOA ClockRCC->APB2ENR |= RCC_APB2ENR_USART1EN; //Enable USART1 ClockGPIOA->AFR |= (0111 << GPIO_AFRH_AFSEL9_Pos) | (0111 << GPIO_AFRH_AFSEL10_Pos); //Set PA9 and PA10 as USART1GPIOA->OSPEEDR |= (GPIO_OSPEEDR_OSPEED9 | GPIO_OSPEEDER_OSPEEDR10); //Set GPIO Speed HighGPIOA->PUPDR &= ~(GPIO_PUPDR_PUPD9 | GPIO_PUPDR_PUPD10); //Set No Pull Up/DownGPIOA->MODER = (GPIOA->MODER | GPIO_MODER_MODER9_1 | GPIO_MODER_MODER10_1) & ~(GPIO_MODER_MODER9_0 | GPIO_MODER_MODER10_0); //Set PA9/PA10 as Alternate FunctionUSART1->CR1 &= ~(USART_CR1_UE);USART1->PRESC = 0b0101; //USART Clock Divided by 10USART1->BRR = 0x06EA; //Baudrate 17MHz/9600= 1770 = 0x06EAUSART1->CR1 |= USART_CR1_TE; //Enable TransmitUSART1->CR1 |= USART_CR1_UE; //Enable USART}int main(void){HAL_Init();SystemClock_Config();Initialization();while (1){USART1->TDR = 0xAA;while(!(USART1->ISR & USART_ISR_TC));}}System clock configuration:Code:#include "main.h"void Error_Handler(void){}void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Configure the main internal regulator output voltage*/HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);/** Initializes the CPU, AHB and APB busses clocks*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6;RCC_OscInitStruct.PLL.PLLN = 85;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB busses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK){Error_Handler();}SysTick->CTRL &= ~(SysTick_CTRL_ENABLE_Msk);}I checked the PA9 pin with a scope its always low. In debug mode I can confirm that registers can be written since they change as I write to them so I am assuming USART clock is working.What could be the problem?NOTE: I tried just switching GPIOs it was working thus AHB and APB are getting clock. Also I am using STM32CubeIDE.

author: syntax333
author_rank: Regular Contributor
timestamp: March 26, 2020, 02:14:54 pm
content: I found the problem....Its a typo.GPIOA->AFR |= (0111 << GPIO_AFRH_AFSEL9_Pos) | (0111 << GPIO_AFRH_AFSEL10_Pos); //Set PA9 and PA10 as USART1I wrote 0111 instead of 0b0111

author: techman-001
author_rank: Frequent Contributor
timestamp: March 26, 2020, 10:58:23 pm
content: Quote from: syntax333 on March 26, 2020, 02:14:54 pmSo easy to do!I'm a Forth user and the 'stack comment' ( %bbbb -- x addr ) from my auto generated bitfield templates does help to prevent this to a great degree. In Forth "%" = "0b" in C.This is what I call a 'config Word' and I define them first for all the peripheral configs in my program.Code:: GPIOC_AFRH_AFRH9 ( %bbbb -- x addr ) 4 lshift GPIOC_AFRH ; \ GPIOC_AFRH_AFRH9, Alternate function selectionThen in my main code I write something like this to set GPIOA bit 6 to AF3:Code:AF3 GPIOA_AFRL_AFRL6 BIS!"BIS!" just means set the bit(s) and "AF3" is a predefined constant which equals "%0011"