AnsweredAssumed Answered

KE02Z64 project with ARM Compiler -Os Size Optimization flag cause CPU reset

Question asked by sys@allmatic.com on Nov 2, 2016
Latest reply on Nov 2, 2016 by sys@allmatic.com

Hi all,

we are having problem with a MKE02Z64VLH4 project.

We are compiling the project using the "Cross ARM GCC" Toolchain (arm-none-eabi-gcc). For code-size matter, we need to use the "-Os" compilation flag, which means the compiler will compile with Optimization level set to "Optimize size". IDE is KDS 3.1.0.

 

With this settings, everything looks ok, except for this function:

uint8_t CheckCode(uint8_t *Code) {
     if (*((uint32_t*)(Code)) == epmID) {
          return 1;
     }
     return 0;
}

when this function is called, che CPU hangs, resetting itself. After some analysis with step-by-step debug, we found that the problem is in the equal comparison. We found 2 solutions, one is this:

uint8_t CheckCode(uint8_t *Code) {
     uint32_t Cod;
     Cod = *((uint32_t*)(Code));
     Cod |= ((*(Code + 1)) << 8);
     Cod |= ((*(Code + 2)) << 16);
     Cod |= ((*(Code + 3)) << 24);
     if (Cod == epmID) {
          return 1;
     }
     return 0;
}

As you can see, using a support variable (local), everything is fine now.

Another way is to replace the function calling directly with the equal comparison:

//... some code
if (albCmdRx1 == BTC_CONNECT) {
    if (!epmfCANCONNECT) {
     Error(ERR_CONN_DISABLED);o
//    } else if (CheckCode(albParamsRx1)) { // THIS CAUSE THE CPU RESET
      } else if ((*((uint32_t*)albParamsRx1)) == epmID) { // THIS *DONT* CAUSE THE CPU RESET

//... some other code

The calling function is use directly in the main.c file. "albParamsRx1" is an array of uint8_t.

It seems is a problem with *(uint32_t*) cast in the if comparison mixed with the fact that there is a function calling.

 

We would like to know if this is a known problem, how it works exactly and how to be sure that there is no other point in code that can cause the problem, considering that there is no warnings or compiling errors. We resize the stack size, without luck.

We think it's a problem similar to this one , but there is no real solution or reason that explain the problem.

 

Thanks in advance for help,

Sandro

Outcomes