在使用数据流的时候,发现当发送数据量已经超过触发值,接收函数还是没把数据打印出来,得是发送再发一次后才会与发送的打印数据一起打印出来,但是我用win32运行freertos跑同样的代码没有这个问题,不知道是否是printf函数的问题
/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
task1 and task2 alternate printing
*/
#include "debug.h"
#include "FreeRTOS.h"
#include "task.h"
#include "string.h"
#include "queue.h"
#include "timers.h"
#include "stdlib.h"
#include "string.h"
#include "semphr.h"
#include "event_groups.h"
#include "stream_buffer.h"
/* Global define */
#define TASK1_TASK_PRIO 3
#define TASK1_STK_SIZE 512
#define TASK2_TASK_PRIO 1
#define TASK2_STK_SIZE 512
#define TASK3_TASK_PRIO 1
#define TASK3_STK_SIZE 512
/* Global Variable */
TaskHandle_t Task2Handle = NULL;
TaskHandle_t Task3Handle = NULL;
StreamBufferHandle_t StreamBufferHandle = NULL;
/* SemaphoreHandle */
SemaphoreHandle_t printfSemaphoreHandle = NULL;
//#define MYPRINT(format, ...) vTaskSuspendAll();portENTER_CRITICAL();printf(format, ##__VA_ARGS__);portEXIT_CRITICAL();xTaskResumeAll()
//#define MYPRINT(format, ...) vTaskSuspendAll();printf(format, ##__VA_ARGS__);xTaskResumeAll()
#define MYPRINT(format, ...) xSemaphoreTake(printfSemaphoreHandle,portMAX_DELAY);printf(format, ##__VA_ARGS__);xSemaphoreGive(printfSemaphoreHandle)
/*********************************************************************
* @fn GPIO_Toggle_INIT
*
* @brief Initializes GPIOA.0/1
*
* @return none
*/
void GPIO_Toggle_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*********************************************************************
* @fn task1_task
*
* @brief task1 program.
*
* @param *pvParameters - Parameters point of task1
*
* @return none
*/
void task1_task(void *pvParameters)
{
TickType_t xLastWakeTime = 0;
xLastWakeTime = xTaskGetTickCount();
while(1)
{
if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
}
vTaskDelayUntil( &xLastWakeTime, pdMS_TO_TICKS(1000));
}
}
void task2_task(void *pvParameters)
{
unsigned char StrTable[50] = {0};
int16_t StrLength = 0;
int16_t SendLength = 0;
int16_t Count = 0;
while(1)
{
vTaskDelay(pdMS_TO_TICKS(3000));
Count++;
StrLength = sprintf(StrTable,"StreamBuffer Test Count = %d",Count);
SendLength = xStreamBufferSend(StreamBufferHandle,(void *)StrTable,StrLength,portMAX_DELAY);
MYPRINT("Send: StrLength = %d SendLength = %d\r\n",StrLength,SendLength);
}
}
void task3_task(void *pvParameters)
{
unsigned char StrTable[50] = {0};
int16_t ReadLength = 0;
while(1)
{
memset(StrTable,0x00,sizeof(StrTable));
ReadLength = xStreamBufferReceive(StreamBufferHandle,(void *)StrTable,(sizeof(StrTable) - 1),portMAX_DELAY);
MYPRINT("Read: ReadLength = %d String = %s\r",ReadLength,StrTable);
}
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n",SystemCoreClock);
printf("FreeRTOS Kernel Version:%s\r\n",tskKERNEL_VERSION_NUMBER);
GPIO_Toggle_INIT();
/* create two task */
printf("uint8_t = %d , uint16_t = %d , size_t = %d \r\n",sizeof(uint8_t),sizeof(uint16_t),sizeof(size_t));
StreamBufferHandle = xStreamBufferCreate(200,100); //最大缓冲区为1000,大于50字节触发
if(StreamBufferHandle != NULL)
{
printf("Creat Success \r\n");
}
printfSemaphoreHandle = xSemaphoreCreateBinary();
xSemaphoreGive(printfSemaphoreHandle);
xTaskCreate((TaskFunction_t )task1_task,
(const char* )"task1",
(uint16_t )TASK1_STK_SIZE,
(void* )NULL,
(UBaseType_t )TASK1_TASK_PRIO,
(TaskHandle_t* )NULL);
xTaskCreate((TaskFunction_t )task2_task,
(const char* )"task2",
(uint16_t )TASK2_STK_SIZE,
(void* )NULL,
(UBaseType_t )TASK2_TASK_PRIO,
(TaskHandle_t* )&Task2Handle);
xTaskCreate((TaskFunction_t )task3_task,
(const char* )"task3",
(uint16_t )TASK3_STK_SIZE,
(void* )NULL,
(UBaseType_t )TASK3_TASK_PRIO,
(TaskHandle_t* )NULL);
vTaskStartScheduler();
while(1)
{
printf("shouldn't run at here!!\n");
}
}
您好,可在每个打印函数打印时加个换行符(\)或在每个打印函数后面加个fflush(stdout)函数试一下
感谢3楼加上fflush(stdout)后确实可以了