cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4: UART not transmitting

tyx3gu
Associate II
Posted on June 05, 2014 at 16:16

Hi,

I am currently working on an application that will send/receive time and date data through serial connection via UART. To get a prototype up and running, I modified the RTC calendar example code that comes with STM32Cube, but I'm not getting any output from the serial port. To initialize the UART I simply copied over some initialization code from the serial examples, but nothing seems to be working. Here is some relevant code...

main() function

  HAL_Init();

  SystemClock_Config();

  // LED initialization here

  UartHandle.Instance        = USARTx; // USARTx is defined as USART3 in main.h

  UartHandle.Init.BaudRate   = 9600;

  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;

  if (HAL_UART_Init(&UartHandle) != HAL_OK) {

      BSP_LED_On(LED3); // this LED never comes on so I presume UART init is ok

      while(1) { }

  }

  RtcHandle.Instance = RTC;

  RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;

  RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;

  RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;

  RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;

  RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

  RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

  if  (HAL_RTC_Init(&RtcHandle) != HAL_OK)

  {

    /* Initialization Error */

BSP_LED_On(LED4);

  }

  // RTC Calendar configured here

  // Then, a function writes time and date info to a uint8_t[] with sprintf

  //  I have verified that the data stored in the array is correct w/ debugger

  

  HAL_UART_Transmit_IT(&UartHandle, (uint8_t *) aShowTime, timelength);

  while (HAL_UART_GetState(&UartHandle) != HAL_UART_STATE_READY)

   { }

  HAL_UART_Transmit_IT(&UartHandle, (uint8_t *) aShowDate, datelength);

SystemClock_Config (stock code)

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_OscInitTypeDef RCC_OscInitStruct;

  /* Enable Power Control clock */

  __PWR_CLK_ENABLE();

  /* The voltage scaling allows optimizing the power consumption when the device is 

     clocked below the maximum system frequency, to update the voltage scaling value 

     regarding system frequency refer to product datasheet.  */

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /* Enable HSE Oscillator and activate PLL with HSE as source */

  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 = 25;

  RCC_OscInitStruct.PLL.PLLN = 336;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

  RCC_OscInitStruct.PLL.PLLQ = 7;

  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

  {

    Error_Handler();

  }

  

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 

     clocks dividers */

  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  

  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)

  {

    Error_Handler();

  }

Additionally, I have checked in stm32f4xx_hal_conf.h to ensure that everything that should be defined is defined. Is there anything obviously wrong here?

***UPDATE: I did a full chip erase and re-flashed the chip, and now it's hanging on HAL_UART_Transmit. Weird...

6 REPLIES 6
chen
Associate II
Posted on June 05, 2014 at 18:45

Hi

I do not see any code to configure the IO pins. You need something like this :

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, DISABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, DISABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

You have not said which pins you are using, you need to change the code for the pins you
tyx3gu
Associate II
Posted on June 05, 2014 at 18:55

I saw this type of code before when I was not working with the HAL libraries. However, in the example code provided by ST cube (which uses HAL), no code of that type is used at all.

chen
Associate II
Posted on June 05, 2014 at 19:03

Hi

''I saw this type of code before when I was not working with the HAL libraries. However, in the example code provided by ST cube (which uses HAL), no code of that type is used at all.''

Yes, I understand what you are saying.

However, with the example code provided by ST, I do not see anything that is going to configure the IO pins. There is absolutely no mention of which pins the Uart comes out on!

Have you inspected HAL_UART_Init() to see if it does any of the IO port configuration?

tyx3gu
Associate II
Posted on June 05, 2014 at 19:12

Yeah, my mistake. Pin, clock, and priority initialization is done in HAL_UART_Msp_Init(), which is called on by HAL_UART_Init() if the UART is in reset mode. The code should be correct because it worked before in the UART examples but when I try to port it to the RTC example it no longer works...

chen
Associate II
Posted on June 06, 2014 at 10:11

''The code should be correct because it worked before in the UART examples but when I try to port it to the RTC example it no longer works...''

It sounds like this code was generated by the Stm32Cube program - is that right?

My understanding (because Im too busy to switch over to using it here at work) is that it generates the code.

It MIGHT be that since the RTC demonstration did not need Usart, the generatde code does not have all the code needed to implement the Usart. You may have to go back and generate all the code again.

tyx3gu
Associate II
Posted on June 06, 2014 at 16:43

I figured out last night that this was the issue. I hadn't defined the UART tx/rx callbacks and a few constants which ended up crashing my program. It works now, thanks!