|
在freertos中加入uart不定长例程,发现在发送函数中的while卡住,怀疑是中断等级太低,后面我升到0级还是会有这问题,总的问题还是发送的标志位不会在回调中置1,以下为代码,麻烦大佬们帮我解惑解惑,或者提出建议
#include "new_thread0.h"
#include <stdio.h>
/* New Thread entry function */
void printf_usart(void);
fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
uint8_t RxBuff[1]; //进入中断接收数据的数组
uint8_t DataBuff[1000]; //保存接收到的数据的数组
int RxLine=0; //接收到的数据长度
int Rx_flag=0; //接受到数据标志
int Rx_flag_finish=0; //接受完成或者时间溢出
void user_uart_callback (uart_callback_args_t * p_args)
{
if(p_args->event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
if(p_args->event == UART_EVENT_RX_CHAR)
{
RxBuff[0] = p_args->data;
RxLine++; //每接收到一个数据,进入回调数据长度加1
DataBuff[RxLine-1]=RxBuff[0]; //把每次接收到的数据保存到缓存数组
Rx_flag=1;
if(RxBuff[0]==0xff) //接收结束标志位,这个数据可以自定义,根据实际需求,这里只做示例使用,不一定是0xff
{
Rx_flag_finish=1;
}
RxBuff[0]=0;
err = R_GPT_Reset(&g_timer0_ctrl);
assert(FSP_SUCCESS == err);
}
}
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
void HAL_UART_Transmit(uint8_t data)
{
R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&data, 1);
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i<size;i++)
{
__io_putchar(*pBuffer++);
}
return size;
}
/* Callback function */
void timer0_callback(timer_callback_args_t *p_args)
{
/* TODO: add your own code here */
if (TIMER_EVENT_CYCLE_END == p_args->event)
{
if(Rx_flag==1)
{
printf_usart();
Rx_flag=0;
}
}
}
/* pvParameters contains TaskHandle_t */
void new_thread0_entry(void *pvParameters)
{
FSP_PARAMETER_NOT_USED (pvParameters);
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
/* Initializes the module. */
err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
/* Start the timer. */
(void) R_GPT_Start(&g_timer0_ctrl);
/* TODO: add your own code here */
while (1)
{
vTaskDelay (100);
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT100->160
if(Rx_flag_finish==1)//如果定时器计时完毕
{
printf_usart();
}
}
}
void printf_usart(void)
{
HAL_UART_Transmit(0xff);//如果接受到数据就发送0xff
memset(DataBuff,0,sizeof(DataBuff)); //清空缓存数组
//memset()作用:可以方便的清空一个结构类型的变量或数组。
//例句:memset(aTxbuffer,0,sizeof(aTxbuffer)) 用memset清空aTxbuffer。
RxLine=0; //清空接收长度
Rx_flag_finish=0;
Rx_flag = 0;
}
|
|