FreeRTOS 队列结构 C [英] FreeRTOS Queue Struct C

查看:74
本文介绍了FreeRTOS 队列结构 C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何声明可用于在 FreeRTOS 中的两个线程之间发送数据的结构.

I do not understand how to declare a structure that I can use to send data between two threads in FreeRTOS.

我有两个线程,一个应该用数据填充结构,另一个应该从结构中读取数据,该数据是通过消息队列发送的.

I have two threads, the one should populate the struct with data, and the other one should read the data from the struct, which was sent with a message queue.

数据可以复制或通过指针,数据量不大.

The data can be copied or via pointer, it is not large amounts of data.

在我的 main.c 文件中,我声明了结构并声明了队列和队列句柄:在 int main(void) 之前:

In my main.c file I declare the structure and declare the queue and the queue handle: Before int main(void):

xQueueHandle LED_Queue_Handle, ChannelFreqQueue;

    struct AMessage
{
        uint8_t channelID;
        float channelFreq;
};

在主要我创建队列

ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage *));

在需要向队列发送数据的任务中:

In the task that needs to send data to the queue:

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

然后是需要从队列中接收数据的任务:

And then the task that needs to receive the data from the queue:

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 文档作为参考:http://www.freertos.org/a00118.html

I am sure that I have a misunderstanding of how to declare and use the structure and also that I have pointers and non-pointers mixed up. I have tried to use this API doc as reference: http://www.freertos.org/a00118.html

如果有人能指出我的错误或帮助我理解伪代码,我们将不胜感激.

If someone can point out my mistakes or help with pseudo-code that might help me understand it would be appreciated.

推荐答案

您没有空间在队列中存储元素

You have no room to store elements in your queue

ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage *));

创建 2 个指向 struct AMessage 结构的指针元素.您需要的是 struct AMessage

Creates 2 pointer element to struct AMessage structures. What you need is 2 element array of struct AMessage

ChannelFreqQueue = xQueueCreate(2, sizeof(struct AMessage));

这篇关于FreeRTOS 队列结构 C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆