in C51, if I use overlap optimize, my project have problem.( routine R1 changed routine R2's auto variable). if I do not use overlap optimize. the problem gone. Is this Keil's problem? I guess so. How dose Keil do overlap optimize?
I am sure it's overlay problem. I tried use a static variable as a copy of R1's auto variable, then worked around. or I turned off overlay optimise option, the problem gone. From this I make conclusion. It dose is Keil's problem. BTW, I did not make any assumption for auto variable. the R1 routine is following. enum { FILTER_FR, FILTER_COLOR }; f_rotate_motor() function will be block for 300ms. so, R2 get to run, R2 has a pointer variable use the same address as FilterPos. When R1 resumed, FilterPos changed. bit f_set_filter( byte FilterPos ) { bit success; ASSERT ( FilterPos == FILTER_FR || FilterPos == FILTER_COLOR ); if ( FilterPos == FILTER_COLOR ) { if ( f_current.filter_position == FILTER_COLOR ) { success = TRUE; motor_color_flt++; } else if ( f_current.filter_position == FILTER_FR ) { success = f_rotate_motor ( FLUOR_DEGREE, COLOR_DEGREE, 1 ); ASSERT ( FilterPos == FILTER_FR || FilterPos == FILTER_COLOR ); } else ASSERT( FALSE ); ASSERT ( FilterPos == FILTER_FR || FilterPos == FILTER_COLOR ); } else if ( FilterPos == FILTER_FR ) { if ( f_current.filter_position == FILTER_COLOR ) { success = f_rotate_motor ( COLOR_DEGREE, FLUOR_DEGREE, 1 ); ASSERT ( success == TRUE ); } else if ( f_current.filter_position == FILTER_FR ) { success = TRUE; } else ASSERT( FALSE ); ASSERT ( FilterPos == FILTER_FR || FilterPos == FILTER_COLOR ); } else { ASSERT_W( FALSE ); } if ( success == TRUE ) { ASSERT ( FilterPos == FILTER_FR || FilterPos == FILTER_COLOR ); f_current.filter_position = FilterPos; } return success; }
_rotate_motor() function will be block for 300ms. so, R2 get to run This line sounds like you're using some sort of RTOS to switch between your tasks. If you have two different tasks that can preempt each other, then you need to manually adjust the overlay tree to be sure each task has its own "stack". Each independent thread needs to be an independent root of an overlay tree. See chapter 9 of the linker manual. Turning overlaying off entirely will of course avoid the problem, at the cost of using up a lot of memory.