The compile I'm using: V6.4.6 With Optimization 2.
This code is from Intraspin Lab13b:
The Call:
obj->pwmHandle[0] = PWM_init((void *)PWM_ePWM1_BASE_ADDR,sizeof(PWM_Obj));
The function:
PWM_Handle PWM_init(void *pMemory,const size_t numBytes)
{
PWM_Handle pwmHandle;
if(numBytes < sizeof(PWM_Obj))
return((PWM_Handle)NULL);
// assign the handle
pwmHandle = (PWM_Handle)pMemory;
return(pwmHandle);
} // end of PWM_init() function
The disassembler:
PWM_init():
3e87c1: 1EA6 MOVL @XAR6, ACC
332 return((PWM_Handle)NULL);
3e87c2: 023A MOVB ACC, #58
3e87c3: 0FA6 CMPL ACC, @XAR6
3e87c4: 56C90003 BF C$L1, LOS
3e87c6: D400 MOVB XAR4, #0x0
C$L1:
3e87c7: 0006 LRETR
The disassembler with Optimization off:
PWM_init():
3e81ba: FE06 ADDB SP, #6
3e81bb: 1E44 MOVL *-SP[4], ACC
3e81bc: A842 MOVL *-SP[2], XAR4
331 if(numBytes < sizeof(PWM_Obj))
3e81bd: 023A MOVB ACC, #58
3e81be: 0F44 CMPL ACC, *-SP[4]
3e81bf: 56C90004 BF C$L1, LOS
332 return((PWM_Handle)NULL);
3e81c1: D400 MOVB XAR4, #0x0
3e81c2: 6F03 SB C$L2, UNC
336 pwmHandle = (PWM_Handle)pMemory;
C$L1:
3e81c3: 0642 MOVL ACC, *-SP[2]
3e81c4: 1E46 MOVL *-SP[6], ACC
339 } // end of PWM_init() function
C$L2:
3e81c5: FE86 SUBB SP, #6
3e81c6: 0006 LRETR
And works correctly.
The output of the compiler optimization is not constant if I play with the order of the code the result will change.
So can you please tell me how to solve this problem. Or better how to disable void pointer optimization.
Thanks