FreeRTOS队列结构C
我不知道如何声明一个结构,我可以使用它在FreeRTOS的两个线程之间发送数据 我有两个线程,一个应该用数据填充结构,另一个应该从结构中读取数据,该结构是通过消息队列发送的 数据可以被复制或通过指针,它不是大量的数据 在main.c文件中,我声明了结构,并声明了队列和队列句柄: 在int main(无效)之前: 我主要创建队列FreeRTOS队列结构C,c,pointers,struct,queue,freertos,C,Pointers,Struct,Queue,Freertos,我不知道如何声明一个结构,我可以使用它在FreeRTOS的两个线程之间发送数据 我有两个线程,一个应该用数据填充结构,另一个应该从结构中读取数据,该结构是通过消息队列发送的 数据可以被复制或通过指针,它不是大量的数据 在main.c文件中,我声明了结构,并声明了队列和队列句柄: 在int main(无效)之前: 我主要创建队列 ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage *)); 在需要向队列发送数据的任务中: stati
ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage *));
在需要向队列发送数据的任务中:
static void StopCompThread(void const *argument)
{
uint32_t count=0;
uint8_t ActiveChannel =0;
uint16_t uartcount =0;
const float period = 0.0085;
static float frequency = 0;
for (;;)
{
struct AMessage txIdrisData;
if(xSemaphoreTake(OscStopSem, portMAX_DELAY)) // Timer 17 Callback 8.5ms
{
HAL_TIM_Base_Stop_IT(&htim17);
__HAL_TIM_SET_COUNTER(&htim17,0);
count = __HAL_TIM_GetCounter(&htim3);
uartcount++;
uint16_t pinstatus = (uint16_t)GPIOB->ODR & 0x2000;
if (pinstatus == 0)
{
ActiveChannel = 0x01;
}
else ActiveChannel = 0x02;
if (uartcount == 525)
{
txIdrisData.channelID = ActiveChannel;
txIdrisData.channelFreq = frequency;
xQueueSend(ChannelFreqQueue, (void *) &txIdrisData,portMAX_DELAY);
}
}
} //FORever
} // StopCompThread
static void IDRISThread(void const *argument)
{
struct AMessage rxIdrisData;
float temp = 0.0;
uint8_t channel = 0;
char IdrisDataBuf[11] = {0}; // 3 Bytes x 4 channels = 12 Bytes
uint8_t IdrisStatusByte = 0;
for (;;)
{
xQueueReceive( ChannelFreqQueue, &( rxIdrisData ), portMAX_DELAY );
temp = rxIdrisData.channelFreq;
channel = rxIdrisData.channelID;
temp = temp * 1000;
snprintf(IdrisDataBuf, 2, "%.0f",temp); // Channel Data - Counts/Frequency
if (channel == 0x00)
{
IdrisDataBuf[2] = 0x00;
}
if (channel == 0x01)
{
IdrisDataBuf[2] = 0x01;
}
uart_send(IdrisDataBuf, 12);
} //FORever
} // IDRISThread
然后是需要从队列接收数据的任务:
static void StopCompThread(void const *argument)
{
uint32_t count=0;
uint8_t ActiveChannel =0;
uint16_t uartcount =0;
const float period = 0.0085;
static float frequency = 0;
for (;;)
{
struct AMessage txIdrisData;
if(xSemaphoreTake(OscStopSem, portMAX_DELAY)) // Timer 17 Callback 8.5ms
{
HAL_TIM_Base_Stop_IT(&htim17);
__HAL_TIM_SET_COUNTER(&htim17,0);
count = __HAL_TIM_GetCounter(&htim3);
uartcount++;
uint16_t pinstatus = (uint16_t)GPIOB->ODR & 0x2000;
if (pinstatus == 0)
{
ActiveChannel = 0x01;
}
else ActiveChannel = 0x02;
if (uartcount == 525)
{
txIdrisData.channelID = ActiveChannel;
txIdrisData.channelFreq = frequency;
xQueueSend(ChannelFreqQueue, (void *) &txIdrisData,portMAX_DELAY);
}
}
} //FORever
} // StopCompThread
static void IDRISThread(void const *argument)
{
struct AMessage rxIdrisData;
float temp = 0.0;
uint8_t channel = 0;
char IdrisDataBuf[11] = {0}; // 3 Bytes x 4 channels = 12 Bytes
uint8_t IdrisStatusByte = 0;
for (;;)
{
xQueueReceive( ChannelFreqQueue, &( rxIdrisData ), portMAX_DELAY );
temp = rxIdrisData.channelFreq;
channel = rxIdrisData.channelID;
temp = temp * 1000;
snprintf(IdrisDataBuf, 2, "%.0f",temp); // Channel Data - Counts/Frequency
if (channel == 0x00)
{
IdrisDataBuf[2] = 0x00;
}
if (channel == 0x01)
{
IdrisDataBuf[2] = 0x01;
}
uart_send(IdrisDataBuf, 12);
} //FORever
} // IDRISThread
我确信我对如何声明和使用结构有误解,而且我把指针和非指针混淆了。我已尝试使用此API文档作为参考:
如果有人能指出我的错误或帮助我理解伪代码,我们将不胜感激。您没有空间在队列中存储元素
ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage *));
创建指向struct AMessage
structures的2个指针元素。
您需要的是struct-AMessage
ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage));
您没有空间在队列中存储元素
ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage *));
创建指向struct AMessage
structures的2个指针元素。
您需要的是struct-AMessage
ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage));
为什么不直接对结构进行排队,而不是指向结构的指针,这样会简单得多。阅读页面上的简介,并在页面的中间部分查看“使用队列集的替代方案”部分。为什么不将结构排入队列,而不是指向结构的指针,这样会简单得多。阅读页面中间的“使用队列集的替代方案”部分中的简介。如果将
struct-AMessage
推入队列,为什么在创建队列时使用sizeof(struct-AMessage*)
?类型不匹配。如果将struct-AMessage
推入队列,为什么在创建队列时使用sizeof(struct-AMessage*)
?类型不匹配。假设您有一个32位系统,您的队列将有2个条目长,每个条目有4个字节。当您向队列发送内容时,只有结构的前4个字节将被复制到队列中。当您从队列接收时,只有4个字节将被复制回您的结构中。假设没有填充,您的结构可能有5个字节长。所以LP上面说的就是您需要做的。假设您有一个32位系统,您的队列将有2个条目长,每个条目有4个字节。当您向队列发送内容时,只有结构的前4个字节将被复制到队列中。当您从队列接收时,只有4个字节将被复制回您的结构中。假设没有填充,您的结构可能有5个字节长。所以LP上面说的就是你需要做的。