I answered to your related question why you could only toggle at 4 MHz when you expected 100 MHz:
If the 4 MHz is about 4 MHz, and not exactly 100 MHz/25 then the problem is probably with the C function GPIO_WriteBit
.
For high speed operations and operations which require accurate timing you better code in assembly than in C. If you look at the assembly code created by GPIO_WriteBit
it may be half a page long, depending on what kind of features the function has, and how much the compiler's optimizer can do with it.
You don't say which development toolchain you're using, but many/most C compilers can handle in-line assembly.
So, write the functions in assembly. A function like
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
should take no more than 2 instructions in assembly, while the compiled C code may take 20 times as much. Or more.
Edit:
I found the cause of the spikes.
The BMI055 chip is internally flawed, I am sure of it now.
If you read out the FIFO in Bypass mode (that means only one frame is kept) then you have to expect data errors.
If you read it faster than the sampling rate you receive 10% 0x8000 (smallest possible number) in the mix. Not zero, not the last value .. a MAXIMUM value!
If you read the data a bit slower than the sampling rate, you receive valid data with 1-8 spikes per second.
This happens if you use the FIFO register in 6 or 8 byte burst mode.
Now I got curious, I read the 0x02 register in 6 byte burst and this gives the same data (x,y,z) and I changed absolutely nothing else.
The spikes are reliable gone.
previous text:
It seems that the gyroscope itself is flawed and it is not just that one.
I first expected an error in I2C communication, turned down the speed and exchanged the voltage shifting circuit without any change in results.
Now as I identified the problem to come from random high spikes I could also find many other people with similar issues or reports.
Surprising fact is that there are rarely any answers.
My first test was a moving exponential average filter, but as I expected that ruins many subsequent readings and only dampens the spikes.
The best solution I can think about is to use two gyroscopes, that is what I will do longterm.
They will likely both have spikes and by comparing both frames with each other it should be possible to get a very good result and no spikes.
The simple solution is to filter spikes based on a hardcoded max-change value.
I first considered capturing data at twice the current rate and then always lagging behind one frame.
That way I would know the 'future' and the past, if the 'current' data is way off from future and past it is a spike (or someone hit a hammer on the sensor).
But for gyros with high degree/s resolution and pratical use outside of 2000deg/sec the filtering can be easier.
I played around and was shaking the gyro heavily, the raw values rarely exceeded +10k and no matter what I did they did not have any such sudden changes, even during a drop on the table.
I use this now:
if (abs(old-current)>0x3000) use_value();
So if the result is off by more than 12,000 raw values in comparison to the previous value than the previous value will stay in the variable. Otherwise the variable is updated with the new value.
Now I have a quite low drift which even works for many minutes.
Sidenotes about the used gyro:
a) The BMI055 gyroscope is specified to return ZERO if you sample it too fast, in reality it returns random values.
b) The BMI055 gyroscope crashes the I2C bus if you send a soft_reset to it (the onchip ACC doesn't do that)
c) Sudden spikes are not described in the datasheets of the gyros and still many people notice them. That's quite strange.
I would appreciate a better answer, maybe I am wrong.
However, the spike filtration made my program work quite reliable compared to the unusable results earlier.
Best Answer
This is a trivial software problem. You keep "time to next wake-up" per each sensor. On every wake-up you subtract elapsed time from every value, measure sensors that reached zero (and re-initialize their counters with corresponding sampling periods) and at the same time find the smallest value of the rest. Setup your timer to wake-up after this smallest period and activate sleep again.
You only need one wake-up source for this to work. It can be RTC alarm, low-power timer or even WDT timer, depending on what is available on your MCU in chosen sleep mode. This method will work even if longest possible sleep period is shorter than smallest required delay. In this case you simply subtract elapsed time without polling any sensors and go into next sleep period immediately.