使用局部变量,该变量具有将指针在Freertos中取指针的功能

发布于 2025-01-23 00:59:30 字数 843 浏览 2 评论 0原文

当我需要将它们传递给其他功能作为指示时,我需要一些解释/澄清。

例如,我具有一些在指针“数据”下修改数据的功能。

void modify_data(int * data){
    *data = 10; 
}

我可以这样使用吗?

void some_function(void){
    int d;  // local variable
    modify_data(&d);
}

也许我应该使全球变量?

int d;
void some_function(void){
    modify_data(&d);
}

还是静态变量?

void some_function(void){
    static int d;
    modify_data(&d);
}

我的问题通常是:

如何使用(或替换)局部变量使用freertos中的指针?

编辑:

此刻,我对此的理解是:

  • 函数中的本地变量已有如果我想将他们的指针传递到另一个功能(或用指向这些变量的指针做任何事情),因为任务切换会导致存储位置的更改

  • 如果我想将其指针传递到另一个函数(或用指向这些变量的指针执行任何操作) , 如果我想用他们的指针做任何事情,则为静态或全球
  • 这有点烦人,因为我的大程序中的许多变量必须在全球范围内声明,并且将指针传递给全球数据没有意义,除非对于代码的可读性

的STM32微控制器。

I need some explanation/clarification about using local variables in FreeRTOS when I need to pass them to another functions as pointers.

For example I have some function that modifies data under pointer 'data'.

void modify_data(int * data){
    *data = 10; 
}

Can I use it like this?

void some_function(void){
    int d;  // local variable
    modify_data(&d);
}

Or maybe I should make global variable?

int d;
void some_function(void){
    modify_data(&d);
}

Or static variable?

void some_function(void){
    static int d;
    modify_data(&d);
}

My question in general is:

How to use (or replace) local variables with functions that take pointers in FreeRTOS?

Edit:

At this moment my understanding of this is:

  • local variables within a function have no use if I want to pass their pointers to another function (or do anything with pointers pointing these variables) because task switching can cause change of memory location where local variable is stored

  • I have to declare variables as static or global if I want to do anything with their pointers

  • this is a bit annoying, because a lot of variables in my big program must be declared globally and passing pointers to global data makes no sense except for the readability of the code

I'm using FreeRTOS 10.2.1, CMSIS 1.02 and code runs on STM32 microcontroller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉薄对峙 2025-01-30 00:59:30

首先,此陈述

*data = (*data)++; 

调用不确定的行为。

至于您的问题,要更改函数中的变量,您需要通过引用将其传递给该函数,该引用是通过指向其指示的。例如

void f( int *px )
{
    *px = 10;
}

void g( void )
{
    static int x;
    f( &x );
}

For starters this statement

*data = (*data)++; 

invokes undefined behavior.

As for your question then to change a variable within a function you need to pass it to the function by reference that is indirectly through a pointer to it. For example

void f( int *px )
{
    *px = 10;
}

void g( void )
{
    static int x;
    f( &x );
}
嘴硬脾气大 2025-01-30 00:59:30

取决于您想做什么。如果要在该函数之外使用d,则需要将其定义为全局,如果您仅在函数中使用它将其声明为本地。

Depends on what you want to do. If you want to use d outside of that function you need to define it as a global, if you are only gonna use it inside the function declare it as local.

聊慰 2025-01-30 00:59:30

因此,在评论和一些研究中进行了一些讨论之后,我找到了解决问题的答案。

局部变量可以以任何方式使用,只有在 main函数中声明的局部变量(在MAIN调用的功能中)(在启动Freertos调度程序之前)。

我担心的来源是,我已经在一些教程中读到,在“主要上下文”中创建的本地功能可能会混乱或不存在。尚未清楚地说,它适用于“主要C函数”,我错过了所有认为“主要上下文”与主堆栈指针相关的事物,而不仅仅是“主函数”。

FREERTOS网站上的FAQ 说:

freertos调度程序有Main()的上下文不存在
从那时起,只有RTOS任务和中断才有一个
语境。最大化弗雷托斯可用的RAM量
应用,按C标准作为Main()的上下文允许
不再存在,有些弗雷托端口重新使用分配给的堆栈
主要作为系统或中断堆栈。因此永远不会分配
所需的变量或缓冲区或以任何方式访问
Main()使用的堆栈上的Freertos应用程序,因为它们是
可能被覆盖。

因此,这个简化的示例还可以:

int x;

int main(void)
{
    x = 10;
    createTasks();
    vTaskStartScheduler();
}

但是类似的事情将在弗雷托斯中不起作用

int * px; // this pointer will be not valid after vTaskStartScheduler()

int main(void)
{
    int x = 10;
    px = &x;
    createTasks();
    vTaskStartScheduler();
}

有人可能会问为什么我在MAIN中使用了本地变量,并希望在应用程序的其余部分中访问它,而无需声明它作为全局变量。我之所以这样做,是因为我开发了将代码连接到CMSIS/STM32CUBE生成的代码(迫使程序员在“用户代码区域”中编写的代码)的特定/怪异方法,直到我开始使用FreErtos。

So after some discussion in comments and some research I found the answer for my concerns.

Local variables can be used in any way, JUST EXCEPT local variables declared in main function (and in functions called by main) (before FreeRTOS scheduler is started).

Source of my concerns was that I have read in some tutorial, that local functions created in "main context" may be messed up or not exist. It was not explained clearly that it applies to "main c function" and I missunderstood everything thinking that "main context" is context related with Main Stack Pointer, not just "main function".

FAQ on FreeRTOS website says:

The context of main() does not exist after the FreeRTOS scheduler has
started as, from that point, only RTOS tasks and interrupts have a
context. To maximise the amount of RAM available to the FreeRTOS
application, and as allowed by the C standard as the context of main()
no longer exists, some FreeRTOS ports re-use the stack allocated to
main as the system or interrupt stack. Therefore never allocate
variables or buffers that are needed or in any way accessed by the
FreeRTOS application on the stack used by main() because they are
likely to get overwritten.

So this simplified example would be OK:

int x;

int main(void)
{
    x = 10;
    createTasks();
    vTaskStartScheduler();
}

But something like this will not work in FreeRTOS:

int * px; // this pointer will be not valid after vTaskStartScheduler()

int main(void)
{
    int x = 10;
    px = &x;
    createTasks();
    vTaskStartScheduler();
}

Someone might ask why I used a local variable in main and want to access it in the rest of the application without declaring it as a global variable. I was doing this because I have developed specyfic/weird way of attaching my code to CMSIS/STM32Cube generated code (which forces the programmer to write in "user code regions") that was working until I started using FreeRTOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文