RP2350 (PI PICO 2) fast GPIO toggle question - Page 1
Forum section: electronics / general

author: firewalker
author_rank: Super Contributor
timestamp: December 30, 2024, 03:43:16 pm
content: 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.Code:#include <stdio.h>#include "pico/stdlib.h"// A dumb way for about 100 nsec delayinline 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.Code:#include <stdio.h>#include "pico/stdlib.h"// A dumb way for about 100 nsec delayinline 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.

author: xvr
author_rank: Frequent Contributor
timestamp: December 30, 2024, 04:46:25 pm
content: Use PIO - it's MUCH faster just bitbang IOhttps://hackspace.raspberrypi.com/articles/what-is-programmable-i-o-on-raspberry-pi-pico

author: firewalker
author_rank: Super Contributor
timestamp: December 30, 2024, 05:08:38 pm
content: Using PIO was an overkill for this. And I should learn how to use it.Thee question is why the GPIO toggle speed changes. Is there an interrupt that only affects the first iteration of the loop?

author: xvr
author_rank: Frequent Contributor
timestamp: December 30, 2024, 05:18:36 pm
content: gpio_put will take some time, may be much more than 100ns of delayAs for interrupt - try to disable it.

author: rteodor
author_rank: Frequent Contributor
timestamp: December 30, 2024, 05:33:10 pm
content: This sounds like caching: fetching code from QSPI (if that is the case on this RP, I do not know) takes time.I am too lazy to check but if I would really like to find out then I would try an execution with XIP caching disabled (executing from non cached addressXIP_NOCACHE_BASEXIP_NOCACHE_NOALLOC_BASE)For RP2040:Code:XIP_BASE 0x10000000XIP_NOALLOC_BASE 0x11000000XIP_NOCACHE_BASE 0x12000000XIP_NOCACHE_NOALLOC_BASE 0x13000000XIP_CTRL_BASE 0x14000000XIP_SRAM_BASE 0x15000000XIP_SRAM_END 0x15004000XIP_SSI_BASE 0x18000000LE: in RP2040 DS, ch 2.6.3.1:Quote

author: SiliconWizard
author_rank: Super Contributor
timestamp: December 30, 2024, 09:59:33 pm
content: No, the PIO is not overkill, it's made just for this kind of thing, just learn how to use it. It's one of the main strong points of the RP2040 and RP2350.