Content originally posted in LPCWare by fjarnjak on Tue Dec 10 02:01:22 MST 2013
Quote: capiman
When the (systick) timer fires, you set a flag and wait, wait, wait till someone works on your flag,
why do you use a interrupt at all? Just query the timer in your main loop. But for your problem, it is useless...
In this case either way can work. But as the system grows, it would be difficult to maintain and improve the code, as you add more features to it.
Setting the flag approach is usually used, even in RTOS kernel implementations.
But if I count the timer in main() or use the flag, UsbdCdc_IO_Buffer_Sync_Task(); needs to be called regularly to maintain the USB stack and its connectivity to PC.
I think this function call sometimes ends quicker and sometimes takes longer to finish. Meaning I do not reach if() with fixed or somewhat fixed delay (depending on compiler optimization used that delay would vary but would essentially be fixed). And due to this, I handle the flag if it is set at varying intervals - or I would check the current timer value also at varying intervals.
So if I count delays: I would have a fixed delay (calling ISR, exiting from ISR, for(;;) loop, if statement) then I would have varying delay of USB stack function, lastly there is sensor data acquisition (2us) and sending the data to PC ~2.4ms. Calling ISR, few if statements and increments and all this fixed would be measured in us....but let's take it to be 1ms. So 1ms + 2.4ms = 3.4ms. I still have 6.6ms before next sensor acquisition. Seems to me UsbdCdc_IO_Buffer_Sync_Task(); sometimes needs even more than 6.6ms to finish.
Perhaps I can call it only if flag is not set that should also help a bit....
Quote:
Why don't you start getting sensor data in systick timer?
Why don't you set the flag when sensor data is available? (You can also use an interrupt again, even multiple time for I2C or whatever you use...)
In case my I2C sensor is broken or disconnected (wire is cut), my ISR would halt for a while...disrupting other services (again when the code would grow and more features are added - LCD, keypad, encoders, etc.).