Warning: gcc optim issue with some cmsis drivers

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by fariouche on Sun Sep 02 08:50:28 MST 2012
Hi all,  I was debugging the CAN (I'm using nxp's cmsis implementation lpc17xx_can.c)  I'm compiling with Os optimization flag. This triggers reordering of code for optimizations... This is fine most of the time, but in some case this can lead to a disaster (or almost)   __attribute__((optimize("O0"))) should be used in front of some functions. This is the case for the can_setBaudrate function!  WHat happens is that the optimizer puts "CANx->MOD = 0;" to return to normal mode before actually writting to the BTR register (the line just above, CANx->BTR  = ) From a pure C perspective, this is perfectly fine, gcc has no idea that the order here is important!  I've forced the optim flag to O0 for this function and the CAN init function (just to be sure, don't need optimized init functions)  Most likely this must be used in other files! (all functions for example that disables/enables interrupts)  Hope this helps