cancel
Showing results for 
Search instead for 
Did you mean: 

Why does the first debug-usart-init work and not the second?

RLind.2
Associate II
#include "stm32f4xx_hal.h"
static UART_HandleTypeDef UartHandle;
 
void Setup( void ) {
   GPIO_InitTypeDef  GPIO_InitStruct;
 
 
   /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
   */
   HAL_Init();
 
   SystemClock_Config(); /* Configure the system clock to 180 MHz */
 
#if 0
   // **** THIS WORKS ****
   // Nucleo DEBUG = PA2/AF7/USART2
   __HAL_RCC_GPIOA_CLK_ENABLE();
   GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
   GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // PA2 = TxD2, PA3 = RxD2, AF7
 
   UartHandle.Instance          = USART2;
   UartHandle.Init.BaudRate     = 38400;
   UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
   UartHandle.Init.StopBits     = UART_STOPBITS_1;
   UartHandle.Init.Parity       = UART_PARITY_NONE;
   UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
   UartHandle.Init.Mode         = UART_MODE_TX_RX;
   UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
   if(HAL_UART_Init(&UartHandle) != HAL_OK) Error_Handler();
#else
   // **** This does NOT work ****
   // Display debug = PC10/AF7/USART3
   __HAL_RCC_GPIOC_CLK_ENABLE();
   GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
   GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // PC10 = TxD3, PC11 = RxD3, AF7
 
   UartHandle.Instance          = USART3;
   UartHandle.Init.BaudRate     = 38400;
   UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
   UartHandle.Init.StopBits     = UART_STOPBITS_1;
   UartHandle.Init.Parity       = UART_PARITY_NONE;
   UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
   UartHandle.Init.Mode         = UART_MODE_TX_RX;
   UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
   if(HAL_UART_Init(&UartHandle) != HAL_OK) Error_Handler();
#endif
}

1 REPLY 1

You enable the USART clocks somewhere?

Check the registers, RCC, GPIO and USART..

Still not working check the board, scope the pins.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..