将结构(缓冲区)传递给 FREERTOS 中的多个函数和任务

问题描述 投票:0回答:1

请帮忙。 在 C 中,我有一个带有缓冲区的结构。我需要这个结构缓冲区的 12 个副本(总共约 4600B),需要将其传递给多个任务和函数,每个任务和函数都会修改内容。 我还有一个回调,它位于一个单独的 FREERTOS 任务中,它也需要访问缓冲区(很难传递稍后才创建的结构)。 – 请注意,到目前为止我还没有传递给此任务的任何参数。 但想避免全局固定缓冲区。我可以将全局缓冲区设置为 **指针 **(这样就不会浪费固定内存),然后在第一个函数中进行 malloc 吗?所以我可以稍后完成后使用 FREE() 吗? 我是否需要在每个任务中为同一个 ota_buf 分配内存? 我需要将 ota_buf 结构传递给 task2 吗?或者因为全局就可以了?

void task2( void *parameter) {
ota_buff_t *ota_buf = malloc(sizeof(ota_buff_t)*12);
…
(ota_buf)[d1_ptr].Nsdu[0] = 0xD1;
(ota_buf)[d1_ptr].position  = 1;
…
set_buf1(&ota_buf);
….
Free(ota_buf)
}//end task 1

void task1 (void *parameter) {
While(1) {
//Do stuff
If (the malloc happens and data set) set_buf2(&ota_buf);
}//while
}//end task2

set_buf1 (ota_buff_t  **ota_buf) {
(ota_buf)[d1_ptr].Nsdu[0] = 0xD2;
(ota_buf)[d1_ptr].position  = 2;
} //end check_buf1()

set_buf2(ota_buff_t  **ota_buf) {
(ota_buf)[d1_ptr].Nsdu[0] = 0xD3;
(ota_buf)[d1_ptr].position  = 3;
} //end check_buf2()

In helper:
typedef struct {
  uint8_t  Nsdu[400];
  uint16_t  position;
} ota_buff_t;

Global: ?
ota_buff_t *ota_buf = NULL;
int d1_ptr = 0;

Main() {
//create task1, loops forever
xTaskCreate(task1, "T1", 1024*8, NULL, 1, NULL);
….some time later: a one-time task
xTaskCreate(task2, "T2", 1024 * 8, NULL, 2, NULL);
}

那么我是否需要将结构传递给task1(即使它被调用的task2还没有malloc?)或者作为全局指针,如果在malloc之后不使用也可以?

更改为:

void task1( void *parameter) {
ota_buff_t ota_buf = *(ota_buff_t *) parameter;
While(1) {
//Do stuff
If (the malloc happens and data set) set_buf2(&ota_buf);
}//while
}//end task2


Main() {
//create task1, loops forever
xTaskCreate(task1, "T1", 1024*8, &ota_buf, 1, NULL);
….some time later: a one-time task
xTaskCreate(task2, "T2", 1024 * 8, NULL, 2, NULL);
}

c struct parameter-passing buffer freertos
1个回答
0
投票

我想我通过反复试验回答了我自己的问题:这是不可能的。当缓冲区初始化(malloc)时,地址从初始的NULL指针开始变化。 相反,使用 FLAG,然后返回带有缓冲区的函数,因此不需要将其放在多个位置。

© www.soinside.com 2019 - 2024. All rights reserved.