0

Device: stm32f407vg

IDE: Keil uvision 5

IDE Version: Latest version

Cubemx Version: Latest version

I want to use the USART communication protocol in my project. Communication is provided but incorrect data is sent (STM> PC).

The "SystemInit ()" function in the compiler is underlined by a gray line. One of the page users commented:

"""""

SystemInit() should be called in the reset handler only

This function is supposed to reset the system clock to a default state, running directly from HSI at 16 MHz.

You are calling it in the middle of main(), when USART1 is already configured assuming a higher clock speed. There is no telling what the peripheral would do when its clock source is changed on the fly.

""""

Codes:

#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "stm32f4xx_hal.h"
#include "defines.h"
#include "tm_stm32_disco.h"
#include "tm_stm32_delay.h"
#include "tm_stm32_lis302dl_lis3dsh.h"
#include "stm32f4xx.h"  
#include "arm_math.h"
#define PID_PARAM_KP        100 
#define PID_PARAM_KI        0.025
#define PID_PARAM_KD        0   

float pid_error;

extern UART_HandleTypeDef huart1;

char* bufftr="Hello\n\r";

void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    TM_LIS302DL_LIS3DSH_t Axes_Data;

    MX_GPIO_Init();
    MX_TIM1_Init();
    MX_SPI1_Init();
    MX_USART1_UART_Init();

    __HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);

    SystemInit();
    TM_DELAY_Init();
    TM_DISCO_LedInit();

    if (TM_LIS302DL_LIS3DSH_Detect() == TM_LIS302DL_LIS3DSH_Device_LIS302DL) {
        TM_DISCO_LedOn(LED_GREEN | LED_RED);
        TM_LIS302DL_LIS3DSH_Init(TM_LIS302DL_Sensitivity_2_3G, TM_LIS302DL_Filter_2Hz);
    } else if (TM_LIS302DL_LIS3DSH_Detect() == TM_LIS302DL_LIS3DSH_Device_LIS3DSH) {
        TM_DISCO_LedOn(LED_BLUE | LED_ORANGE);
        TM_LIS302DL_LIS3DSH_Init(TM_LIS3DSH_Sensitivity_2G, TM_LIS3DSH_Filter_800Hz);
    } else {
        TM_DISCO_LedOn(LED_GREEN | LED_RED | LED_BLUE | LED_ORANGE);
        while (1);
    }   

    Delayms(300);
    TM_DISCO_LedOff(LED_ORANGE);
    TM_DISCO_LedOff(LED_BLUE);

    arm_pid_instance_f32 PID;
    PID.Kp = PID_PARAM_KP;
    PID.Ki = PID_PARAM_KI;
    PID.Kd = PID_PARAM_KD;
    arm_pid_init_f32(&PID, 1);


    TM_GPIO_SetPinLow(GPIOD, GPIO_Pin_12);
    TM_GPIO_SetPinLow(GPIOD, GPIO_Pin_14);  

    while (1){
        HAL_UART_Transmit_IT(&huart1, (uint8_t *)bufftr, 8);
        HAL_Delay(500);
    }

    void SystemClock_Config(void){
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    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 = 4;
    RCC_OscInitStruct.PLL.PLLN = 168;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    RCC_OscInitStruct.PLL.PLLQ = 4;

    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){
        Error_Handler();
    }

    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_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK){
        Error_Handler();
    }
}
static void MX_USART1_UART_Init(void){
    huart1.Instance = USART1;
    huart1.Init.BaudRate = 115200;
    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;

    if (HAL_UART_Init(&huart1) != HAL_OK){
        Error_Handler();
    }
}
static void MX_GPIO_Init(void){
    __HAL_RCC_GPIOH_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
}
void Error_Handler(void){

}
#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line){ 

}
#endif

When I look at the file startup_stm32f407xx.s, the following lines are included in the code.

; Reset handler
Reset_Handler    PROC
                 EXPORT  Reset_Handler             [WEAK]
        IMPORT  SystemInit
        IMPORT  __main

                 LDR     R0, =SystemInit
                 BLX     R0
                 LDR     R0, =__main
                 BX      R0
                 ENDP

; Dummy Exception Handlers (infinite loops which can be modified)

Where do you think the error is? Thanks in advance.

K__A__
  • 1
  • 1
  • I can only repeat myself, the problem is that `SystemInit()` is called in the in the middle of the `main()` function. What purpose does the call to `SystemInit()` serve there, why did you put it in `main()`? – followed Monica to Codidact Jun 17 '19 at 14:05
  • The function call is underlined because it lacks a prototype. If you just want the warning to go away, you should provide a prototype to the function. But this won't change the fact that the peripheral clock speed is changed after the UART is initialized. – followed Monica to Codidact Jun 17 '19 at 14:08
  • Possible duplicate of [Why USART transmits incorrect data as the APB1 frequency changes. (RCC <= 21MHz)](https://stackoverflow.com/questions/56608971/why-usart-transmits-incorrect-data-as-the-apb1-frequency-changes-rcc-21mhz) – followed Monica to Codidact Jun 17 '19 at 14:10

0 Answers0