cancel
Showing results for 
Search instead for 
Did you mean: 

SMT32L053 Cube USART ( Nucleo )

dan2399
Associate
Posted on September 21, 2015 at 16:02

Hi,

I'm trying to send data via USART2 but I can't receive anything.

Here is my source code: /* Includes ------------------------------------------------------------------*/

&sharpinclude ''stm32l0xx_hal.h''

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

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

USART_HandleTypeDef husart2;

/* USER CODE BEGIN PV */

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

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_Init(void);

void sendOk(void);

void sendError(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

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();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_USART2_Init();

  /* USER CODE BEGIN 2 */

 char data[] = ''ABC'';

 

  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);

  HAL_Delay(3000);

  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */

 

 

  if( HAL_USART_Transmit( &husart2, (uint8_t *) data, 3, 5000 ) != HAL_OK )

  {

    sendError();

  }

  else

  {

    sendOk();

  }

 

  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);

  HAL_Delay(3000);

 

  /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */

}

void sendOk()

{

    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);

   HAL_Delay(2000);

   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);

}

void sendError()

{

 

   for(int i=0; i<3; i++)

   {

     HAL_Delay(500);

     HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);

   }

   

   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_PeriphCLKInitTypeDef PeriphClkInit;

  __PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;

  RCC_OscInitStruct.MSIState = RCC_MSI_ON;

  RCC_OscInitStruct.MSICalibrationValue = 0;

  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;

  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

}

/* USART2 init function */

void MX_USART2_Init(void)

{

    GPIO_InitTypeDef GPIO_InitStruct;

    GPIO_InitStruct.Pin = GPIO_PIN_2;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

    //GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_3;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

    //GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  husart2.Instance = USART2;

  husart2.Init.BaudRate = 9600;

  husart2.Init.WordLength = USART_WORDLENGTH_8B;

  husart2.Init.StopBits = USART_STOPBITS_1;

  husart2.Init.Parity = USART_PARITY_NONE;

  husart2.Init.Mode = USART_MODE_TX_RX;

  husart2.Init.CLKPolarity = USART_POLARITY_LOW;

  husart2.Init.CLKPhase = USART_PHASE_1EDGE;

  husart2.Init.CLKLastBit = USART_LASTBIT_DISABLE;

  HAL_USART_Init(&husart2);

}

/** Pinout Configuration

*/

void MX_GPIO_Init(void)

{

  /* GPIO Ports Clock Enable */

  __GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.Pin = GPIO_PIN_5;

  GPIO_InitStructure.Speed = GPIO_SPEED_FAST;

  GPIO_InitStructure.Pull = GPIO_PULLUP;

  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;

 

  HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

&sharpifdef USE_FULL_ASSERT

/**

   * @brief Reports the name of the source file and the source line number

   * where the assert_param error has occurred.

   * @param file: pointer to the source file name

   * @param 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 */

}

&sharpendif

/**

  * @}

  */

/**

  * @}

*/

I checked twice the terminal baud rate, parity bits, port. Every thin is configured Ok but I don't receive any data.

#stm32l0-usart-cube-nucle
2 REPLIES 2
Nesrine M_O
Lead II
Posted on September 22, 2015 at 12:33

Hi,

I'd highly recommend you to start from UART examples under STM32Cube L0 package: 

STM32Cube_FW_L0_V1.1.0\Projects\STM32L053R8-Nucleo\Examples\UART, it can be very useful.

-Syrine –
dan2399
Associate
Posted on September 23, 2015 at 12:42

I started with examples but I can't understand how to configure USART1 to send data over the on board USB connector.