cancel
Showing results for 
Search instead for 
Did you mean: 

How to transmit UART data on STM32 discovery board?

RLee.2
Associate II

Hi forum,

I'm developing on the stm32f3disovery board with STM32F3-Discovery_FW_V1.1.0 running in IAR Embedded Workbench. I've been able to get the basic demo compiled and running, but I can't seem to get UART data transmission to work. There are no error messages, just no output on the serial terminal when I transmit data. I've made sure to match the serial terminal and UART peripheral settings. Below is the UART initialization and runtime code that I have:

  //Set up UART peripheral 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_7);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_7);
  
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStruct);
  
  USART_InitTypeDef USART1_Struct; 
  USART1_Struct.USART_BaudRate = 38400;
  USART1_Struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART1_Struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART1_Struct.USART_Parity = USART_Parity_No;
  USART1_Struct.USART_StopBits = USART_StopBits_1;
  USART1_Struct.USART_WordLength = USART_WordLength_8b;
  USART_Init(USART1, &USART1_Struct);
  
  USART_Cmd(USART1, ENABLE);
    //Transmit heading value over UART for display 
    while(USART_GetFlagStatus(USART1, USART_FLAG_BUSY)) {};
    USART_SendData(USART1, 15);

Thanks!

4 REPLIES 4
LMI2
Lead

I use STM32CubeMX from ST to set up peripherals. It can be downloaded free from ST.

Then it is easy to find various examples and you get basic io working.

And then the following would make sense. Google finds quite a lot of help about the keywords. Variable names are in finnish, sorry. You have to change the handles h.... like huart1 to your board.

#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>
ADC_HandleTypeDef hadc1;
 
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;
 
/* USER CODE BEGIN PV */
volatile uint32_t atulos, luku, tikit ;
char msg[20];
volatile unsigned int alkukello, kellopulssit, kellopulssit1;
volatile unsigned int *DWT_CYCCNT   = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL  = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *DWT_LAR      = (volatile unsigned int *)0xE0001FB0; //address of the register
volatile unsigned int *SCB_DEMCR    = (volatile unsigned int *)0xE000EDFC; //address of the register
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART3_UART_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* 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_ADC1_Init();
  MX_USART1_UART_Init();
  MX_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
	
 
 
  *DWT_LAR = 0xC5ACCE55; // unlock
  *SCB_DEMCR |= 0x01000000;
  *DWT_LAR = 0xC5ACCE55; // unlock
  *DWT_CYCCNT = 0; // reset the counter
  *DWT_CONTROL |= 1 ; // enable the counter
//alkukello=*DWT_CYCCNT;
  HAL_ADC_Start(&hadc1);
  tikit =HAL_GetTick();
	alkukello=*DWT_CYCCNT;
//	start = *DWT_CYCCNT;
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
		//K15_Pin|K1_Pin|K3_Pin|K16_Pin
		HAL_GPIO_TogglePin(GPIOD,K16_Pin);
//		HAL_GPIO_TogglePin(GPIOD,K15_Pin);  //vilkkuu
		HAL_GPIO_TogglePin(GPIOD,K1_Pin);
		HAL_GPIO_TogglePin(GPIOD,K3_Pin);
		 /*Configure GPIO pins : PD4 PD5 PD6 PD7 */
 // GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
 
	while	(HAL_ADC_PollForConversion(&hadc1,0)!=HAL_OK)
	{
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_4);//spend time	
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_5);//spend time
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_6);//spend time	
luku=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_7);//spend time		
	}
luku = HAL_ADC_GetValue(&hadc1);
//sprintf(msg ,"Welcome %s",msg); 
sprintf(msg ,"ADC %d \n\r",luku);
HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 1000);
 
atulos  =HAL_GetTick();
atulos=atulos-tikit;	
sprintf(msg ,"aika %d \n\r",atulos);
HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 1000);
	
kellopulssit	  = *DWT_CYCCNT;
kellopulssit1=kellopulssit-alkukello ;	
sprintf(msg ,"kellopulssit %d \n\r",kellopulssit1);
HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 1000);
alkukello=	kellopulssit;
HAL_ADC_Stop(&hadc1);
HAL_ADC_Start(&hadc1);
HAL_Delay(1000);
  }
  /* USER CODE END 3 */
}

Piranha
Chief II

Observe the Tx pin using oscilloscope or logic analyzer.

Try without the ADC first, to exclude any additional problem source.

JW

Thanks @LMI2​ , I was able to set up UART using STM32DubeIDE.