简体   繁体   English

FreeRTOS队列结构C

[英]FreeRTOS Queue Struct C

I do not understand how to declare a structure that I can use to send data between two threads in FreeRTOS. 我不明白如何声明可用于在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. 数据可以被复制或通过指针,它不是大量数据。

In my main.c file I declare the structure and declare the queue and the queue handle: Before int main(void): 在我的main.c文件中,声明结构并声明队列和队列句柄:在int main(void)之前:

xQueueHandle LED_Queue_Handle, ChannelFreqQueue;

    struct AMessage
{
        uint8_t channelID;
        float channelFreq;
};

In main I create the queue 在主我创建队列

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

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 我试图使用此API文档作为参考: 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 *));

Creates 2 pointer element to struct AMessage structures. 创建2个指针元素以构造struct AMessage结构。 What you need is 2 element array of struct AMessage 您需要的是struct AMessage 2元素数组

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

Why not just queue the structure, rather than a pointer to the structure, it would be much simpler. 为什么不只是将结构排入队列,而不是指向结构的指针,则要简单得多。 Read the blurb on the FreeRTOS Queues documentation page and see the "Alternatives To Using Queue Sets" section half way down the Queue Sets page. 阅读的书籍说明FreeRTOS的队列文档页面 ,看到了“替代品使用队列设置”下的一节半路上队列设置页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM