STM32F030C8T6 serial port receives variable length data

Publisher:MindfulBeingLatest update time:2018-07-10 Source: eefocusKeywords:STM32F030C8T6 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

usart.c file code.


#include "usart.h"

#include "gpio.h"


UART_HandleTypeDef huart1;


/* USART1 init function */

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;

    huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

    huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT | UART_ADVFEATURE_DMADISABLEONERROR_INIT;

    huart1.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;

    huart1.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;

    if (HAL_UART_Init(&huart1) != HAL_OK)

    {

        _Error_Handler(__FILE__, __LINE__);

    }


    __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);

    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);

}


void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)

{

    GPIO_InitTypeDef GPIO_InitStruct;


    if(uartHandle->Instance == USART1)

    {

        /* USER CODE BEGIN USART1_MspInit 0 */


        /* USER CODE END USART1_MspInit 0 */

        /* USART1 clock enable */

        __HAL_RCC_USART1_CLK_ENABLE();


        /**USART1 GPIO Configuration

        PA9     ------> USART1_TX

        PA10    ------> USART1_RX

        */

        GPIO_InitStruct.Pin = GPIO_PIN_9;

        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

        GPIO_InitStruct.Pull = GPIO_PULLUP;

        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

        GPIO_InitStruct.Alternate = GPIO_AF1_USART1;

        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);



        GPIO_InitStruct.Pin = GPIO_PIN_10;

        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

        GPIO_InitStruct.Pull = GPIO_PULLUP;

        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

        GPIO_InitStruct.Alternate = GPIO_AF1_USART1;

        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


        /* USART1 interrupt Init */

        HAL_NVIC_SetPriority(USART1_IRQn, 3, 0);

        HAL_NVIC_EnableIRQ(USART1_IRQn);

        /* USER CODE BEGIN USART1_MspInit 1 */


        /* USER CODE END USART1_MspInit 1 */

    }

}


void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)

{


    if(uartHandle->Instance == USART1)

    {

        /* USER CODE BEGIN USART1_MspDeInit 0 */


        /* USER CODE END USART1_MspDeInit 0 */

        /* Peripheral clock disable */

        __HAL_RCC_USART1_CLK_DISABLE();


        /**USART1 GPIO Configuration

        PA9     ------> USART1_TX

        PA10     ------> USART1_RX

        */

        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);


        /* USART1 interrupt Deinit */

        HAL_NVIC_DisableIRQ(USART1_IRQn);

        /* USER CODE BEGIN USART1_MspDeInit 1 */


        /* USER CODE END USART1_MspDeInit 1 */

    }

}


/*

** Rewrite fputc function and make printf function work

**/

int fputc(int ch, FILE* file)

{

    huart1.Instance->TDR = ch & 0xFF;


    while((huart1.Instance->ISR & UART_FLAG_TC) == RESET);


    return ch;

}


/*

** Rewrite fgetc function and make scanf function work

**/

int fgetc(FILE* file)

{

    while((huart1.Instance->ISR & UART_FLAG_RXNE) == RESET);


    return huart1.Instance->RDR;

}


usart.h code:


#ifndef __usart_H

#define __usart_H


#ifdef __cplusplus

    extern "C" {

#endif


/* Includes ------------------------------------------------------------------*/

#include "stm32f0xx_hal.h"

#include


/* Log enable/disable switch */

#define    ENABLE_LOG    1


#if ENABLE_LOG

    #define    Log(format, ...)    printf(format, ##__VA_ARGS__)

#else

    #define    Log(format, ...)

#endif


typedef struct Data

{

    uint8_t Data[256];

    uint16_t Size;

    bool ReceiveFlag;

}ReceiveUARTData_t;



void MX_USART1_UART_Init(void);



#ifdef __cplusplus

}

#endif

/*__usart_H */ #endif /*__usart_H */


Interrupt function code:


uint8_t TempData[260] = {0};

uint16_t TempSize = 0;


void USART1_IRQHandler(void)

{

    /* Handle read data register not empty interruption */

    if((__HAL_UART_GET_IT(&huart1, UART_IT_RXNE) != RESET))

    {

        __HAL_UART_CLEAR_IT(&huart1, UART_IT_RXNE);


        if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)

        {

            __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);


            TempData[TempSize++] = huart1.Instance->RDR;

        }

    }


    /* Handle idle interruption */

    if((__HAL_UART_GET_IT(&huart1, UART_IT_IDLE) != RESET))

    {

        __HAL_UART_CLEAR_IT(&huart1, UART_IT_IDLE);


        if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET)

        {

            __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_IDLE);


            UARTData.Size = TempSize;

            memcpy(UARTData.Data, TempData, UARTData.Size);

            memset(TempData, 0, TempSize);

            TempSize = 0;

            UARTData.ReceiveFlag = true;

        }

    }

}


main.c code:


#include "stm32f0xx_hal.h"

#include "usart.h"

#include "gpio.h"

#include

#include

#include


ReceiveUARTData_t UARTData= {{0}, 0, false};

void CharPrint(const uint8_t* Data, size_t Size);


int main(void)

{

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

    HAL_Init();


    /* Configure the system clock */

    SystemClock_Config();


    /* Initialize all configured peripherals */

    MX_USART1_UART_Init();


    Log("\r\n>>>>>>>>>>>>>>>>>>>> UART data transmit receive test <<<<<<<<<<<<<<<<<<<<\r\n");


    while (1)

    {

        if(UARTData.ReceiveFlag)

        {

             UARTData.ReceiveFlag = false;

             CharPrint(UARTData.Data, UARTData.Size);

             memset(UARTData.Data, 0, UARTData.Size);

             BluetoothData.Size = 0;

        }

    }

}


/*

** Brief: Print char data

**/

void CharPrint(const uint8_t* Data, size_t Size)

{

    size_t DataIndex = 0;


    for(DataIndex = 0; DataIndex < Size; DataIndex++)

    {

        if(sprint(Data[DataIndex]))

        {

            Log("%c", Data[DataIndex]);

        }

    }

}


It can be noticed that when processing interrupts, I used a double buffering mechanism to ensure that the problem of excessive data loss is solved. I tested it myself and it works normally.


Keywords:STM32F030C8T6 Reference address:STM32F030C8T6 serial port receives variable length data

Previous article:STM32 internal flash operation details that must be known
Next article:STM32F030 cannot download FLASH using IAR+JLINK online debugging

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号