Skip to main content
Toby _
Associate
June 5, 2018
Question

configuration of ''USART'' without CubeMX

  • June 5, 2018
  • 4 replies
  • 3822 views
Posted on June 05, 2018 at 14:48

Hello,

i use the stm nucleo 64 board and the stm/eclipse environment and i want to send string with usart1. Furthermore, i don't want to use cubemx and cmsis. Because it makes the code very unreadable... and I'm familiar with freertos syntax.

The project generated with cubemx, the usart1 transmit works.

But in my project i doesnt work. I think I have implemented all configurations and inits correct.

Here is the code:

#include 'stm32f3xx.h'
#include 'stm32f3xx_nucleo.h'
#include 'FreeRTOS.h'
#include 'task.h'
#include 'queue.h'
#include 'semphr.h'
#define LD2_Pin GPIO_PIN_5
#define LD2_GPIO_Port GPIOA
#define Error_Handler() _Error_Handler(__FILE__, __LINE__)
/*
 PA10USART1_RX
 PA09USART1_TX
 */
UART_HandleTypeDef huart1;
uint8_t Test[] = 'ÿÿÿn0.val=249ÿÿÿ'; //Data to send
uint8_t page2[] = 'ÿÿÿpage 2ÿÿÿ'; //Data to send
uint8_t graph1[] = 'ÿÿÿva2.val=50ÿÿÿ'; //Data to send
uint8_t Data[4];
void SystemClock_Config(void);
static void mainProcess(void *pvParameters);
static void dispTask(void *pvParameters);
static void MX_USART1_UART_Init(void);
void _Error_Handler(char *, int);
int main(void)
{
SystemInit();
HAL_Init();
 /* Configure the system clock */
 SystemClock_Config();

 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOC_CLK_ENABLE();
 __HAL_RCC_GPIOF_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 __HAL_RCC_GPIOB_CLK_ENABLE();
BSP_LED_Init(LED2);
MX_USART1_UART_Init();
while (1) {
HAL_Delay(150);
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
 HAL_UART_Transmit(&huart1, graph1 ,sizeof(graph1), 50);
 HAL_UART_Receive(&huart1, Data ,4, 500);
 HAL_Delay(250);
}
xTaskCreate(mainProcess, 'mainP', (configMINIMAL_STACK_SIZE + 80), NULL, (tskIDLE_PRIORITY + 1), NULL);
xTaskCreate(dispTask, 'dispTask', (configMINIMAL_STACK_SIZE + 80), NULL, (tskIDLE_PRIORITY + 1), NULL);
vTaskStartScheduler();
for(;;);
}
static void mainProcess(__attribute__ ((unused)) void *pvParameters)
{
}
static void dispTask(__attribute__ ((unused)) void *pvParameters)
{
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct;
 RCC_ClkInitTypeDef RCC_ClkInitStruct;
 RCC_PeriphCLKInitTypeDef PeriphClkInit;
 /**Initializes the CPU, AHB and APB busses clocks
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = 16;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
 RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 /**Initializes the CPU, AHB and APB busses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_USART1
 |RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1;
 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
 PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_HSI;
 PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
 PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL;
 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 /**Configure the Systick interrupt time
 */
 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 /**Configure the Systick
 */
 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 /* SysTick_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
}
/* USART1 init function */
static void MX_USART1_UART_Init(void)
{
 huart1.Instance = USART1;
 huart1.Init.BaudRate = 9600;
 huart1.Init.WordLength = UART_WORDLENGTH_8B;
 huart1.Init.StopBits = UART_STOPBITS_1;
 huart1.Init.Parity = UART_PARITY_NONE;
 huart1.Init.Mode = UART_MODE_TX_RX;
 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 if (HAL_UART_Init(&huart1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
 /* USER CODE BEGIN Callback 0 */
 /* USER CODE END Callback 0 */
 if (htim->Instance == TIM1) {
 HAL_IncTick();
 }
 /* USER CODE BEGIN Callback 1 */
 /* USER CODE END Callback 1 */
}
/**
 * @brief This function is executed in case of error occurrence.
 * @param file: The file name as string.
 * @param line: The line in file as a number.
 * @retval None
 */
void _Error_Handler(char *file, int line)
{
 /* USER CODE BEGIN Error_Handler_Debug */
 /* User can add his own implementation to report the HAL error return state */
 while(1)
 {
 }
 /* USER CODE END Error_Handler_Debug */
}
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

What else I have to implement, enable or config ?

Best regards

    This topic has been closed for replies.

    4 replies

    ST Technical Moderator
    June 5, 2018
    Posted on June 05, 2018 at 15:33

    Hello

    frog

    ,

    Which ST board Nucleo are you using ?

    May be you should check your pin configuration and setting.

    Have you enabled theUSART clock: __HAL_RCC_USART1_CLK_ENABLE();

    As a start point, you may find and use the USART example

    available in the Cube firmware package relevant to the STM32 device you are using.

    With Regards,

    Imen

    In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
    Toby _
    Toby _Author
    Associate
    June 5, 2018
    Posted on June 05, 2018 at 15:56

    Dear Imen D,

    - I use the Nucleo F303RE

    - the pin configuration and setting I copied from a example project created with cubemx, so it should be correct, but i could miss something

    - __HAL_RCC_USART1_CLK_ENABLE();       I haven't enabled, but I tried it now and i doesnt work yet

    - where I can find the Cube firmware package's example ?

    With Regards

    AvaTar
    Senior III
    June 5, 2018
    Posted on June 05, 2018 at 15:37

    ... and I'm familiar with freertos syntax.

    What is this ?

    #include 'stm32f3xx.h'

    And this is a CMSIS-conformant header ...

    Either follow the USART section in the reference manual (you will need to read the RCC and GPIO section as well) and implement it manually.

    Or, dissect Cube or SPL code, and copy the sequence of the USART initialization. For most F3 variants, a SPL is still available, including code examples for most peripherals.

    Toby _
    Toby _Author
    Associate
    June 5, 2018
    Posted on June 05, 2018 at 16:10

    AvaTar, thank you for your answer !

    What is this ?

    I my study I used FreeRTOS, I know the basic methods. That is what I want to say.

    And this is a CMSIS-conformant header ...

    Ok, I don't know this, what say this to me or what should I use instead?

    Thank you.

    AvaTar
    Senior III
    June 5, 2018
    Posted on June 05, 2018 at 16:27

    Ok, I don't know this, what say this to me or what should I use instead?

    No, it's ok, to use this header. Just wanted to point out this is already CMSIS ...

    I don't use CubeMX either, too bloated and rigid for me.

    For the F303, there is a SPL (Standard Peripheral Library) available. Or use the STM32F3 Discovery firmware, albeit the GPIOs / pins will probably not match.

    The examples are much more readable, and straight forward.

    Build and run a matching UART/USART example, and/or try to understand it.

    And copy the sequence of initialization steps to your code.

    Tesla DeLorean
    Guru
    June 5, 2018
    Posted on June 05, 2018 at 15:41

    >>What else I have to implement, enable or config ?

    The pins?

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Toby _
    Toby _Author
    Associate
    June 5, 2018
    Posted on June 05, 2018 at 16:16

    Dear Clive One,

    in the

    example project created with cubemx, which is working, I can't find anything where the pins of USART1 are explicit initialized. I think this is done with 'MX_USART1_UART_Init()' ?

    Tesla DeLorean
    Guru
    June 5, 2018
    Posted on June 05, 2018 at 19:12

    Likely an MSP file, like stm32f3xx_hal_msp.c

    CubeMX hides crap everywhere, I prefer having code in plain sight, but you do need to initialize the pins for functionality to occur.

    I code like this, you know what is happening and the order it occurs in.

    https://community.st.com/0D70X000006T12rSAC

     
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    henry.dick
    Associate II
    June 7, 2018
    Posted on June 07, 2018 at 14:51

    '

    Furthermore, i don't want to use  cubemx and cmsis.'

    for non-cube routines, see examples here, for F0/F1 and F4 chips: 

    https://github.com/dannyf00/My-MCU-Libraries---2nd-try

     

    they do use cmsis.

    if you want to abandon CMSIS, it will be considerably more difficult, and incredibly foolish in my view, as you may have to recreate the header files and start-up files.

    It might be better to pick a chip where cmsis is non-existent to start. Like older ARM7 or Luminary / TI chips - though TI does provide CMSIS support for some of its newer chips - you just need to find them.

    Not using CMSIS makes zero sense, in my view.

    Tesla DeLorean
    Guru
    June 7, 2018
    Posted on June 07, 2018 at 15:11

    And at it's lowest level CMSIS just creates some uniformity in the way vendors provide stm32f4xx.h type files describing the registers, and using structures, to allow easy access, portability and reuse.

    If you choose to discard this, then you really need to own your decisions and carry your own weight.

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