Passing volatile as a constant argument to a function

I develop and implement code for the Cortex-M0 MC, where I declare a variable as volatile char TOS_Mins_Char[3]; to store some values ​​during ISR that will change periodically. I want to convert these characters to an integer using a function, atoi() but it atoi()has its own argument type as a pointer to a constant string: int atoi(const char *);This gives me an error if I don't avoid the keyword volatilein the variable declaration. (A similar situation is observed in other std functions)

  • Is there any solution for this other than writing a user-defined function?
  • If I use const char TOS_Mins_Char[3];, will it be a problem?
  • Is it mandatory to use a keyword volatile, what is its use in contrast with ARM MC?
+4
source share
1 answer

The keyword volatilemust be reported to the compiler in order to reload characters from memory for each access.

If you know that the array will not be modified during the conversion, you can use a throw to disable the warning:

int value = atoi((const char*)TOS_Mins_Char);

, , atoi(), , . , . , :

char buf[sizeof TOS_Mins_Char];
CLI;   // whatever macro use to disable interrupts
memcpy(buf, TOS_Mins_Char, sizeof TOS_Mins_Char);
STI;   // enable interrupts
int value = atoi(buf);

/: , , , , .

:

int value, last = atoi((const char*)TOS_Mins_Char);
while ((value = atoi((const char*)TOS_Mins_Char)) != last) {
    last = value;
}

ISR , . , ISR, , , .

+3

Source: https://habr.com/ru/post/1655058/


All Articles