Skip to main content
Associate II
April 11, 2024
Question

UART configuration directly with registers, NUCLEO-H563ZI board

I am using the NUCLEO-H563ZI board and I am trying to configure the UART2 using only registers.

I started by creating a fresh and simple new code with STM32CubeIDE for UART transmit because I wanted to use a simple uart_transmit function but I had trouble making it work until I activated the USB as Host_Only from the .ioc

Pinout & Configuration and only then was I able to use the HAL_UART_Transmit to send a text to a terminal and I would like to know why that is.

 

Then I want to be able to configure the UART2 directly with the registers but then the code breaks how can I make this work? Thank you.

 

 

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file : main.c
 * @brief : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "app_usbx_host.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart2;

HCD_HandleTypeDef hhcd_USB_DRD_FS;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ICACHE_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_USB_HCD_Init(void);
/* USER CODE BEGIN PFP */
uint8_t data[] = "HELLO WORLD \r\n";
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	HAL_UART_Transmit (&huart2, data, sizeof (data), 1000);
}
/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
int main(void)
{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_ICACHE_Init();
 MX_USART2_UART_Init();
 MX_USB_HCD_Init();
 MX_USBX_Host_Init();
 /* USER CODE BEGIN 2 */
 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */


	 HAL_UART_Transmit (&huart2, data, sizeof (data), 1000);
	 HAL_Delay (1000);

 }
 /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage
 */
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_CSI;
 RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
 RCC_OscInitStruct.CSIState = RCC_CSI_ON;
 RCC_OscInitStruct.CSICalibrationValue = RCC_CSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLL1_SOURCE_CSI;
 RCC_OscInitStruct.PLL.PLLM = 1;
 RCC_OscInitStruct.PLL.PLLN = 125;
 RCC_OscInitStruct.PLL.PLLP = 2;
 RCC_OscInitStruct.PLL.PLLQ = 2;
 RCC_OscInitStruct.PLL.PLLR = 2;
 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1_VCIRANGE_2;
 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1_VCORANGE_WIDE;
 RCC_OscInitStruct.PLL.PLLFRACN = 0;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
 |RCC_CLOCKTYPE_PCLK3;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
 {
 Error_Handler();
 }
}

/**
 * @brief ICACHE Initialization Function
 * None
 * @retval None
 */
static void MX_ICACHE_Init(void)
{

 /* USER CODE BEGIN ICACHE_Init 0 */

 /* USER CODE END ICACHE_Init 0 */

 /* USER CODE BEGIN ICACHE_Init 1 */

 /* USER CODE END ICACHE_Init 1 */

 /** Enable instruction cache (default 2-ways set associative cache)
 */
 if (HAL_ICACHE_Enable() != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN ICACHE_Init 2 */

 /* USER CODE END ICACHE_Init 2 */

}

/**
 * @brief USART2 Initialization Function
 * None
 * @retval None
 */
static void MX_USART2_UART_Init(void)
{

 /* USER CODE BEGIN USART2_Init 0 */
 RCC->AHB2ENR |= RCC_AHB2ENR_GPIODEN; // ENABLE GPIO PORT D CLOCK (BIT 0)
 RCC->APB1LENR |= RCC_APB1LENR_USART2EN; // ENABLE UART2 CLOCK (BIT 14)

///*
 GPIOD->MODER &= ~((3u << 10) // CLEAR PD5
 |(3u << 12)); // CLEAR PD6

 // SET PIN MODES
 GPIOD->MODER |= ((2u << 10) // SET PD5 TO AF
 |(2u << 12)); // SET PD6 TO AF

 GPIOD->AFR[2] &= ~((15u << 20) // CLEAR PD5 AF
 |(15u << 24)); // CLEAR PD6 AF

 // SET ALTERNATE FUNCTION
 GPIOD->AFR[2] |= ((7u << 20) // SET PD5 AF
 |(7u << 24)); // SET PD6 AF
//*/




 USART2->CR1 &= ~(USART_CR1_M1 // CLEAR M1 FOR 1 START BIT AND 8 DATA BITS (28)
 |(0x03 << 26) // INHIBIT INTERRUPTS AT BITS 26 AND 27 (27/26)
 |USART_CR1_OVER8 // OVERSAMPLING BY 16 (15)
 |USART_CR1_CMIE // INHIBIT CHARACTER MATCH INTERRUPT (14)
 |USART_CR1_MME // DON'T ENABLE MUTE MODE (13)
 |USART_CR1_M0 // CLEAR M0 FOR 1 START BIT AND 8 DATA BITS (12)
 |USART_CR1_PCE // NOT IMPLEMENTING PARITY CONTROL (10)
 |(0x1F << 3) // INHIBIT INTERRUPTS AT BITS 4 TO 8 (4-8)
 |USART_CR1_TE // DON'T ENABLE TRANSMITTER JUST YET (3)
 |USART_CR1_RE // DON'T ENABLE RECEIVER JUST YET (2)
 |USART_CR1_UE); // DON'T ENABLE UART2 JUST YET (0)

 // CONFIGURE USART CR2 REGISTER
 // CLEAR BITS
 USART2->CR2 &= ~(USART_CR2_RTOEN // DISABLE RECEIVER TIMEOUT (23)
 |USART_CR2_ABREN // NO AUTOMATIC BAUD RATE DETECTION (20)
 |USART_CR2_MSBFIRST // TRANSMIT/RECEIVE LSB FIRST (19)
 |(0x03 << 16) // IDLE STATE HIGH FOR RX/TX PINS (17/16)
 |USART_CR2_SWAP // DON'T SWAP FUNCTION OF RX/TX PINS (15)
 |USART_CR2_LINEN // NO LIN MODE (14)
 |(0x03 << 12) // 1 STOP BIT (13/12)
 |USART_CR2_CLKEN // DON'T USE CLOCK WITH UART (11)
 |USART_CR2_LBDIE); // NO LIN BREAK DETECTION INTERRUPT (6)

 // CONFIGURE USART CR3 REGISTER
 // CLEAR BITS
 USART2->CR3 &= ~(USART_CR3_TCBGTIE // NO TRANSMISSION COMPLETE BEFORE GUART TIME INTERRUPT (24)
 |USART_CR3_DEM // NO DRIVER ENABLE MODE (14)
 |(0x7F << 3) // DISABLE VARIOUS IRRELEVANT MODES (9-3)
 |USART_CR3_IREN // NO IrDA MODE (1)
 |USART_CR3_EIE); // INHIBIT ERROR INTERRUPT (0)

 // SET BITS
 USART2->CR3 |= (USART_CR3_OVRDIS // DISABLE OVERRUN FUNCTIONALITY (12)
 |USART_CR3_ONEBIT); // USE ONE SAMPLE BIT METHOD (11)

 // SET BAUD RATE IN BRR REGISTER
 USART2->BRR = 2170;
 // VALUE THAT SETS BAUD RATE TO 115,200 AT INPUT FREQUENCY OF 4MHZ

 // ENABLE UART
 USART2->CR1 |= (USART_CR1_TE // ENABLE TRANSMITTER (3)
 |USART_CR1_RE // ENABLE RECEIVER (2)
 |USART_CR1_UE); // ENABLE UART2 (0)



 /*
	USART2->CR1 = 0x00; // Clear ALL
	USART2->CR1 |= (1<<13); // UE = 1... Enable USART
	USART2->BRR = 2170; // Baud rate of 115200, PCLK1 at 45MHz

	USART2->CR1 |= (1<<2); // RE=1.. Enable the Receiver
	USART2->CR1 |= (1<<3);
 */
 /* USER CODE END USART2_Init 0 */

 /* USER CODE BEGIN USART2_Init 1 */

 /* USER CODE END USART2_Init 1 */

	/*
 huart2.Instance = USART2;
 huart2.Init.BaudRate = 115200;
 huart2.Init.WordLength = UART_WORDLENGTH_8B;
 huart2.Init.StopBits = UART_STOPBITS_1;
 huart2.Init.Parity = UART_PARITY_NONE;
 huart2.Init.Mode = UART_MODE_TX_RX;
 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 */
 if (HAL_UART_Init(&huart2) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
 {
 Error_Handler();
 }


 /* USER CODE BEGIN USART2_Init 2 */

 /* USER CODE END USART2_Init 2 */

}

/**
 * @brief USB Initialization Function
 * None
 * @retval None
 */
static void MX_USB_HCD_Init(void)
{

 /* USER CODE BEGIN USB_Init 0 */

 /* USER CODE END USB_Init 0 */

 /* USER CODE BEGIN USB_Init 1 */

 /* USER CODE END USB_Init 1 */
 hhcd_USB_DRD_FS.Instance = USB_DRD_FS;
 hhcd_USB_DRD_FS.Init.dev_endpoints = 8;
 hhcd_USB_DRD_FS.Init.Host_channels = 8;
 hhcd_USB_DRD_FS.Init.speed = USBD_FS_SPEED;
 hhcd_USB_DRD_FS.Init.phy_itface = HCD_PHY_EMBEDDED;
 hhcd_USB_DRD_FS.Init.Sof_enable = DISABLE;
 hhcd_USB_DRD_FS.Init.low_power_enable = DISABLE;
 hhcd_USB_DRD_FS.Init.vbus_sensing_enable = DISABLE;
 hhcd_USB_DRD_FS.Init.bulk_doublebuffer_enable = DISABLE;
 hhcd_USB_DRD_FS.Init.iso_singlebuffer_enable = DISABLE;
 if (HAL_HCD_Init(&hhcd_USB_DRD_FS) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN USB_Init 2 */

 /* USER CODE END USB_Init 2 */

}

/**
 * @brief GPIO Initialization Function
 * None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOH_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 __HAL_RCC_GPIOD_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
 /* User can add his own implementation to report the HAL error return state */
 __disable_irq();
 while (1)
 {
 }
 /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * file: pointer to the source file name
 * line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
 /* USER CODE BEGIN 6 */
 /* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

 

This topic has been closed for replies.

5 replies

gbm
Super User
April 11, 2024

There is no AFR[2] register. The bits you want to set are in AFR[0].

 

Don't do logic operations on USART CRx registers. Simply write the proper values to them. For basic setup. you only need to set BRR and CR1 - two assignments are enough.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
G-PouliosAuthor
Associate II
April 12, 2024

Thank you but it still doesn't work

 

static void MX_USART2_UART_Init(void)
{

 /* USER CODE BEGIN USART2_Init 0 */

 RCC->AHB2ENR |= RCC_AHB2ENR_GPIODEN; // ENABLE GPIO PORT D CLOCK (BIT 0)
 RCC->APB1LENR |= RCC_APB1LENR_USART2EN; // ENABLE UART2 CLOCK (BIT 14)

///*
 GPIOD->MODER &= ~((3u << 10) // CLEAR PD5
 |(3u << 12)); // CLEAR PD6

 // SET PIN MODES
 GPIOD->MODER |= ((2u << 10) // SET PD5 TO AF
 |(2u << 12)); // SET PD6 TO AF

 GPIOD->AFR[0] &= ~((15u << 20) // CLEAR PD5 AF
 |(15u << 24)); // CLEAR PD6 AF

 // SET ALTERNATE FUNCTION
 GPIOD->AFR[0] |= ((7u << 20) // SET PD5 AF
 |(7u << 24)); // SET PD6 AF






 USART2->CR1 &= ~(USART_CR1_M1 // CLEAR M1 FOR 1 START BIT AND 8 DATA BITS (28)
 |(0x03 << 26) // INHIBIT INTERRUPTS AT BITS 26 AND 27 (27/26)
 |USART_CR1_OVER8 // OVERSAMPLING BY 16 (15)
 |USART_CR1_CMIE // INHIBIT CHARACTER MATCH INTERRUPT (14)
 |USART_CR1_MME // DON'T ENABLE MUTE MODE (13)
 |USART_CR1_M0 // CLEAR M0 FOR 1 START BIT AND 8 DATA BITS (12)
 |USART_CR1_PCE // NOT IMPLEMENTING PARITY CONTROL (10)
 |(0x1F << 3) // INHIBIT INTERRUPTS AT BITS 4 TO 8 (4-8)
 |USART_CR1_TE // DON'T ENABLE TRANSMITTER JUST YET (3)
 |USART_CR1_RE // DON'T ENABLE RECEIVER JUST YET (2)
 |USART_CR1_UE); // DON'T ENABLE UART2 JUST YET (0)

 // SET BAUD RATE IN BRR REGISTER
 USART2->BRR = 2170;

 // VALUE THAT SETS BAUD RATE TO 115,200 AT INPUT FREQUENCY OF 250MHZ

 // ENABLE UART
 USART2->CR1 |= (USART_CR1_TE // ENABLE TRANSMITTER (3)
 |USART_CR1_RE // ENABLE RECEIVER (2)
 |USART_CR1_UE); // ENABLE UART2 (0)



 /* USER CODE END USART2_Init 0 */

 /* USER CODE BEGIN USART2_Init 1 */

 /* USER CODE END USART2_Init 1 */
 */
 if (HAL_UART_Init(&huart2) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN USART2_Init 2 */

 /* USER CODE END USART2_Init 2 */

}

 

also I see the clock config for the UART2 and it's 250MHz from the PCLK1, should I change that?

GPoulios_0-1712908160197.png

 

 

waclawek.jan
Super User
April 12, 2024

> it still doesn't work

What does this mean? What are the symptoms and how are they different from the expectations?

You still don't directly write values to the UART registers, as @gbm said above.

You are calling HAL_UART_Init() and other HAL stuff. Don't you think that overrides the registers you've just written?

Read out and check/post content of UART and relevant GPIO registers.

JW

 

G-PouliosAuthor
Associate II
April 15, 2024

The expectation is the

HAL_UART_Transmit (&huart2, data, sizeof (data), 1000);

 to transmit/write to a terminal 

HAL_UART_Transmit (&huart2, data, sizeof (data), 1000);

And as for the HAL overwriting the changes, well that's what I am asking, how can I deviate from the HAL libraries and start using exclusively the the registers(that's the end goal), but for now I am trying to do it bit by bit to debug cause I haven't found almost any examples for stm32h563zi specifically or any that I could make them work for my application.

waclawek.jan
Super User
April 15, 2024

Cube/HAL is open source, you can have a look what does it do. Alternatively, just don't use it at all.

Observe UART and GPIO registers content.

Observe the UART Tx pin using oscilloscope/logic analyzer.

JW