Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:29 21 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PIO control - I'm confused.

Author Message
George H
Newbie

Joined: 03/07/2020
Location: United States
Posts: 33
Posted: 02:04am 09 Aug 2025
Copy link to clipboard 
Print this post

I have searched the forum, and I have tried about every combination I can think of for the PIO() command.

I'm trying to use the PIO system to set up a 4 mhz clock signal amd a timer.  Currently using GP2/GP3 for the output.  I've got an LED on each.  I know I won't see 4 MHz but it should illuminate about half bright - right?  I get nothing. I can turn them on and off with DOUT though.

The current manual still shows using a variable for the pin, I'm using a literal "GP2" in the command.  Anyway, here's some code:

' Simple Dual Clock Generator
' Outputs 4MHz on GPIO2 (pin 4) and 100kHz on GPIO3 (pin 5)
' Uses PIO state machines for precise timing

Print "Simple Dual Clock Generator"
Print "=========================="
Print "4MHz clock on GPIO2 (physical pin 4)"
Print "100kHz clock on GPIO3 (physical pin 5)"
Print

' Set CPU frequency (420MHz for this Pico)
Dim cpu_freq = 420000000
Print "CPU Frequency: "; cpu_freq; " Hz"

' Reserve pins for PIO use
SetPin 4, PIO1
SetPin 5, PIO1
Print "Pins reserved for PIO"

' Program PIO state machine 0 for 4MHz clock
Print "Programming 4MHz clock generator..."
PIO ASSEMBLE 1, ".program clock_4mhz"
PIO ASSEMBLE 1, ".line 0"
PIO ASSEMBLE 1, "SET PINDIRS, 1"
PIO ASSEMBLE 1, "loop1:"
PIO ASSEMBLE 1, "SET PINS, 1"
PIO ASSEMBLE 1, "SET PINS, 0"
PIO ASSEMBLE 1, "JMP loop1"
PIO ASSEMBLE 1, ".end program"

' Program PIO state machine 1 for 100kHz clock
Print "Programming 100kHz clock generator..."
PIO ASSEMBLE 1, ".program clock_100khz"
PIO ASSEMBLE 1, ".line 8"
PIO ASSEMBLE 1, "SET PINDIRS, 1"
PIO ASSEMBLE 1, "loop2:"
PIO ASSEMBLE 1, "SET PINS, 1"
PIO ASSEMBLE 1, "SET PINS, 0"
PIO ASSEMBLE 1, "JMP loop2"
PIO ASSEMBLE 1, ".end program"

' Calculate PIO clock frequencies (2x output frequency for high+low cycle)
Dim pio_freq_4mhz = 4000000 * 2
Dim pio_freq_100khz = 100000 * 2

Print "4MHz PIO frequency: "; pio_freq_4mhz
Print "100kHz PIO frequency: "; pio_freq_100khz

' Configure and start 4MHz clock on GPIO2
Dim config_4mhz = Pio(pinctrl 0,1,,,,GP2,)
PIO INIT MACHINE 1, 0, pio_freq_4mhz, config_4mhz, 0, 0
PIO START 1, 0

' Configure and start 100kHz clock on GPIO3
Dim config_100khz = Pio(pinctrl 0,1,,,,GP3,)
PIO INIT MACHINE 1, 1, pio_freq_100khz, config_100khz, 0, 8
PIO START 1, 1

' Report final frequencies
Print
Print "Clock outputs started:"
Print "GPIO2: 4MHz clock ("; pio_freq_4mhz/2; " Hz output)"
Print "GPIO3: 100kHz clock ("; pio_freq_100khz/2; " Hz output)"
Print
Print "Clocks are running. Press CTRL+C to stop."

' Keep program running
Do
   Pause 1000
   Loop



And:

> option list
PicoMite MMBasic RP2040 Edition V6.00.03
OPTION COLOURCODE ON
OPTION CPUSPEED (KHz) 420000
>

Any insight is appreciated!!

-George
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5156
Posted: 09:33am 09 Aug 2025
Copy link to clipboard 
Print this post

I haven't tested the code yet.
But I have 3 things I suggest to look at.

1/ I have never tested the PIO at a CPUSPEED of 420MHz. The ARM may be capable of doing it, but maybe the PIO can't.

2/ The loop is 3 instructions (the JMP also consumes 1 cycle)

3/ The INIT MACHINE is incorrect.
PIO INIT MACHINE 1, 1, pio_freq_100khz, config_100khz, 0, 8

should be
PIO INIT MACHINE 1, 1, pio_freq_100khz, config_100khz,,, 8


both shiftcontrol and executecontrol registers are left at default. You are writing 0 in execute control, and 8 in shiftcontrol, and have no start address (=0).


Volhout

P.S. since the 2 programs are identical, and the difference is in the PIO clock, and in the GPIO pin, you can run both clocks from the same program. Use start address 0 for both clock generators.

P.P.S. if you need 50% duty cycles, you can either
a/ introduce 1 wait state in the "SET PINS, 1" as "SET PINS, 1[1]". The loop is 4 clocks.
b/ use the WRAP function (look at example program 3 in the user manual appendix F). The loop is 2 clocks.
Edited 2025-08-09 19:37 by Volhout
PicomiteVGA PETSCII ROBOTS
 
George H
Newbie

Joined: 03/07/2020
Location: United States
Posts: 33
Posted: 01:47pm 09 Aug 2025
Copy link to clipboard 
Print this post

@ Volhout  Thanks!  It was the init machine statement. Once I corrected that it came to life.  I'll look at WRAP and try to wrap ... my head around it.  Pio is fun, and it's amazing to me to have a BASIC to run it from.  

-G
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1445
Posted: 08:35am 12 Aug 2025
Copy link to clipboard 
Print this post

  George H said  @ Volhout  Thanks!  It was the init machine statement. Once I corrected that it came to life.  I'll look at WRAP and try to wrap ... my head around it.  Pio is fun, and it's amazing to me to have a BASIC to run it from.  

-G



Yes we have a high-level interpreted language but with realtime deterministic capabilities.....Incredible but try to get others to realize the potential here is like banging your head against the proverbial brick wall.
I hang-out at PLCs.net where there are so many problems that would be a trivial issue for the PicoMite, I find myself almost screaming at my monitor  

"Nah, we'd prefer to spend $3K and struggle to make it work"

We need a biggie product to get some recognition.

There used to be a "MicroMite Companion" where the MX170 was coupled to a Propeller-1.

I have spent a few hours coupling the PicoMite to the Propeller-2...What a power-house of a combination  

We have the MMBasic Interpreter + PIO and as a slave, eight 32bit parallel processors and 64 "smartpins". These smartpins are like having another 64 processors that perform all kinds of functions with zero impact on the CPUs. I have just been experimenting with the 16bit (real) DACs and they seem to be pretty darned good.

Imagine; 60+ 16bit DACs (or ADCs) on a $20 chip  
 
AlbertR
Newbie

Joined: 29/05/2025
Location: Germany
Posts: 23
Posted: 07:00am 18 Aug 2025
Copy link to clipboard 
Print this post

May it be, that since version V6.00.02, PIO programs no longer work from line 0.
I test the first PIO-example from the manual with V6.00.03

'CpuSpeed 126000
'program pio1
pio program line 1,0,&hE081
pio program line 1,1,&hE101
pio program line 1,2,&hE000
pio program line 1,3,&h0001
'configure pio1
p=Pio(pinctrl 0,1,,,,gp0,)
f=2000 'Hz
PIO init machine 1,0,f,p,,,0
PIO start 1,0

I read the output back through an other pin and and it did not work.
Then I used some older firmware-Versions and V6.00.01 works as expected, V6.00.02 did not.
When I changed the startline of the statemaschine to "1" it works.

'CpuSpeed 126000
'program pio1
pio program line 1,1,&hE081
pio program line 1,2,&hE101
pio program line 1,3,&hE000
pio program line 1,4,&h0001
'configure pio1
p=Pio(pinctrl 0,1,,,,gp0,)
f=2000 'Hz
PIO init machine 1,0,f,p,,,1
PIO start 1,0


Albert
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5156
Posted: 09:26am 18 Aug 2025
Copy link to clipboard 
Print this post

Hi Albert,

You are not assigning GP0 to the PIO1

'CpuSpeed 126000

SETPIN GP0,PIO1

'program pio1
pio program line 1,0,&hE081
pio program line 1,1,&hE101
pio program line 1,2,&hE000
pio program line 1,3,&h0001

'configure pio1
p=Pio(pinctrl 0,1,,,,gp0,)
f=2000 'Hz
PIO init machine 1,0,f,p,,,0

PIO start 1,0


Volhout

P.S. be aware that
- PicoMite can use PIO1 and PIO0 (except when using I2S audio).
- PicoMiteVGA can only use PIO1 (PIO0 is used for VGA)
- Webmite can only use PIO0 (PIO1 is used for WIFI)

P.P.S. Please note that firware 6.00.03 default to a CPUSPEED = 200MHz. In that case the frequency f=2000'Hz will cause an error. Use f=4000.
Edited 2025-08-18 20:11 by Volhout
PicomiteVGA PETSCII ROBOTS
 
AlbertR
Newbie

Joined: 29/05/2025
Location: Germany
Posts: 23
Posted: 10:49am 18 Aug 2025
Copy link to clipboard 
Print this post

Hi Volhout,

sorry I did not show the complete program, only the diffent parts.
I use the example from your "PIO explained PICOMITE"-thread

The firmware is plain PicoMite, no vga,hdmi or web!
cpuspeed is as in the comment "126000kHz"
Also if the selected frequency is to high there is an errormessage.

The differnet is, the program works with 6.00.01 but not with 6.00.02 or 6.00.03 from line 0

Albert
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5156
Posted: 12:03pm 18 Aug 2025
Copy link to clipboard 
Print this post

Hi Albert,

I can confirm
The bug is passed to the thread about 6.01.00

Volhout
PicomiteVGA PETSCII ROBOTS
 
AlbertR
Newbie

Joined: 29/05/2025
Location: Germany
Posts: 23
Posted: 12:52pm 18 Aug 2025
Copy link to clipboard 
Print this post

Hi Volhout,

It may help, I discover:
If you progam the PIO from line 0 and start it from there no output will be generated.
If you start the same PIO-program ones again now from line 1 an output is made.
After a power down and only a start from line 1 than fails.

It looks like there is an EXIT after line 0.

Albert

@ Peter

same with inbuilt assembler


PIO ASSEMBLE 1,".program test"'a program has to have a name
PIO ASSEMBLE 1,".line 0"      'start the program at line 0
PIO ASSEMBLE 1,"SET PINDIRS,1"'SET the GPIO line to output
PIO ASSEMBLE 1,"label:"       'define a label called "label"
PIO ASSEMBLE 1,"SET PINS,1 [1]" 'SET the GPIO pin high
PIO ASSEMBLE 1,"SET PINS,0"   'SET the GPIO pin low
PIO ASSEMBLE 1,"JMP label"    'JuMP to "label"
PIO ASSEMBLE 1,".end program list"  'end program, list=show result

Edited 2025-08-18 23:05 by AlbertR
 
AlbertR
Newbie

Joined: 29/05/2025
Location: Germany
Posts: 23
Posted: 04:20pm 18 Aug 2025
Copy link to clipboard 
Print this post

Hi Volhout,

after the post of Peter with
  Quote  PIO Configure 1, 0, 4000, 0,,,,gp0,1

where the 'jmppin' and  'wraptarget' is defined, I do this with e=Pio(execctrl GP0,1,0) and
  Quote  PIO init machine 1,0,f,p,e,,0
and it works from line 0.

Possible it helps!

Albert
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5156
Posted: 04:33pm 18 Aug 2025
Copy link to clipboard 
Print this post

I am glad you have a solution for your project. I am not so happy since I do have several older projects that for an unknown reason work but we are not sure why.And tomorrow maybe they don’t.
I just tested my Logic analyser and it works. But why? It has 0 as starting address and its first instruction is also a SET PINDIRS,1. Totally confused.

Volhout
PicomiteVGA PETSCII ROBOTS
 
AlbertR
Newbie

Joined: 29/05/2025
Location: Germany
Posts: 23
Posted: 04:56pm 18 Aug 2025
Copy link to clipboard 
Print this post

Hi Volhout,

thanks for caring. I currently has no project with PIO but I find it very interesting.
It was a first step and it was a bit bumpy for a 'newbie'.

Greetings
Albert
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025