I wanted to create a dummy FlexRay frame in order to test a decoder. Given that that FlexRay is 10 Mbaud I would need a fast running mcu. The fastest thing I have is a PI PICO 2.
Yhe following crude and ugly code worked and I did my tests.
#include <stdio.h>
#include "pico/stdlib.h"
// A dumb way for about 100 nsec delay
inline void delay_100ns() {__asm volatile ("nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n");}
int main(){
gpio_init(3);
gpio_set_dir(3, GPIO_OUT);
gpio_put(3, 1);
sleep_ms(1000);
while (true) {
gpio_put(3, 0);
delay_100ns();
delay_100ns();
gpio_put(3, 1);
delay_100ns();
delay_100ns();
gpio_put(3, 0);
delay_100ns();
gpio_put(3, 1);
delay_100ns();
delay_100ns();
...
...
...
}
}
The thing is that the first time the main loop runs (or if it runs only once like the following code) the time the GPIO toggles is order of magnitude bigger. Instead of 100 ns is around 2 usec.
#include <stdio.h>
#include "pico/stdlib.h"
// A dumb way for about 100 nsec delay
inline void delay_100ns() {__asm volatile ("nop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\nnop\n");}
int main(){
gpio_init(3);
gpio_set_dir(3, GPIO_OUT);
gpio_put(3, 1);
sleep_ms(1000);
while (true) {
gpio_put(3, 0);
delay_100ns();
delay_100ns();
gpio_put(3, 1);
delay_100ns();
delay_100ns();
gpio_put(3, 0);
delay_100ns();
gpio_put(3, 1);
delay_100ns();
delay_100ns();
...
...
...
while (1) {
};
}
}
Why is that happening?
Alexander.