STM32CubeIDE V1.7.0, modulo arithmetic => Compiler Error?
- October 1, 2021
- 4 replies
- 4603 views
Since 25 years I use functions or macros like
#define usTimeRead() ((int16_t) (SysTick->VAL >> 1))
#define usTimePlus(TP, US) ((int16_t) TP - ((int16_t) US * (int16_t) 85))
#define usTimePast(TP) (((int16_t) TP - usTimeRead()) > (int16_t) 0)to create timeouts or to do periodical jobs on 16bit MCUs.
Porting them to STM32G4, the following code should result in a 100us square wave generated at pin B6.
int main(void) {
sysConfigMCU();
/* Loop */
int16_t tp = usTimeRead();
for(;;) {
GPIOB->BSRR = ((uint16_t)0x0040);
tp = usTimePlus(tp, 100);
for ( ; !usTimePast(tp); );
GPIOB->BRR = ((uint16_t)0x0040);
tp = usTimePlus(tp, 100);
for ( ; !usTimePast(tp); );
}
}This doesn't work - I have to change the macro usTimePast(TP) assigning the difference to temporary variable to get the program running correctly.
#define usTimePast(TP) ((tmp = (int16_t) TP - usTimeRead()) > (int16_t) 0)I've simulated this behaviour with TDM-GCC with the same result - for values like
usTimeRead() == -32703 and tp == 32735 the compare in usTimePast(TP) will produce differnt results depending on weather the difference of the left side of compare will be assinged to a temporary variable or not!
Using Microsoft Visual Studio will prodece in both cases (with and without aasigning the difference to a temporary variable) the behaviour i know from 16bit MCUs and their compilers.