Author Topic: RP2350 (PI PICO 2) fast GPIO toggle question  (Read 1096 times)

0 Members and 1 Guest are viewing this topic.

Offline firewalkerTopic starter

  • Super Contributor
  • ***
  • Posts: 2454
  • Country: gr
RP2350 (PI PICO 2) fast GPIO toggle question
« on: December 30, 2024, 03:43:16 pm »
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: [Select]
#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.

Code: [Select]
#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.
Become a realist, stay a dreamer.

 

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 561
  • Country: ie
    • LinkedIn
Re: RP2350 (PI PICO 2) fast GPIO toggle question
« Reply #1 on: December 30, 2024, 04:46:25 pm »
 

Offline firewalkerTopic starter

  • Super Contributor
  • ***
  • Posts: 2454
  • Country: gr
Re: RP2350 (PI PICO 2) fast GPIO toggle question
« Reply #2 on: December 30, 2024, 05:08:38 pm »
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?
Become a realist, stay a dreamer.

 

Offline xvr

  • Frequent Contributor
  • **
  • Posts: 561
  • Country: ie
    • LinkedIn
Re: RP2350 (PI PICO 2) fast GPIO toggle question
« Reply #3 on: December 30, 2024, 05:18:36 pm »
gpio_put will take some time, may be much more than 100ns of delay :(
As for interrupt - try to disable it.
 

Offline rteodor

  • Regular Contributor
  • *
  • Posts: 214
  • Country: ro
Re: RP2350 (PI PICO 2) fast GPIO toggle question
« Reply #4 on: December 30, 2024, 05:33:10 pm »
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 address XIP_NOCACHE_BASE XIP_NOCACHE_NOALLOC_BASE)

For RP2040:
Code: [Select]
XIP_BASE                        0x10000000
XIP_NOALLOC_BASE                0x11000000
XIP_NOCACHE_BASE                0x12000000
XIP_NOCACHE_NOALLOC_BASE        0x13000000
XIP_CTRL_BASE                   0x14000000
XIP_SRAM_BASE                   0x15000000
XIP_SRAM_END                    0x15004000
XIP_SSI_BASE                    0x18000000

LE: in RP2040 DS, ch 2.6.3.1:
Quote
If the cache is disabled, via the CTRL.EN register bit, then all four of the XIP aliases (0x10 to 0x13) will bypass the cache,
and access the flash directly. This has a significant impact on XIP code execution performance.
« Last Edit: December 30, 2024, 05:40:18 pm by rteodor »
 
The following users thanked this post: firewalker

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15901
  • Country: fr
Re: RP2350 (PI PICO 2) fast GPIO toggle question
« Reply #5 on: December 30, 2024, 09:59:33 pm »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf