Menu
JAQForum Ver 26.09

Forum Index : Microcontroller and PC projects : Stumped again, this time with PIO

Posted: 05:27am
19 Nov 2026
Copy link to clipboard
OA47
Guru


I have written code with MMBasic version 6.2 so a pi pico 2350 can generate a TTL serial stream from 300 baud to 921600 baud with options of parity, bit length and stop bits and output on GP0.

I wanted to be able to sniff a running stream and hopefully analyse the stream and derive the baud rate and bits. I tried using straight basic but it was not quick enough to identify the higher speeds. Not having any expertise with PIO programming I asked AI for some help to write a subroutine using PIO to hopefully achieve identifying the stream. AI supplied some code which needed some modification (sometimes it confused PIO and PIN commands and it did not realise that PIO 1 cant be used on the 2350)

Here is the code:
Dim K$
' Ensure GP1 is configured for PIO0 before calling:

SetPin GP1, PIO0
Print "Press any key to start detection"
Do
K$=Inkey$
If K$ <> "" Then AnalyzeSerialGP1
Loop

Sub AnalyzeSerialGP1()
 Local INTEGER pio_clk = 10000000 ' 10 MHz PIO clock (100 ns resolution, handle
s 300 to 921600 baud)
 Local INTEGER i, count, raw_val, min_ticks = 99999999
 Local INTEGER samples(127), state(127), bits(512), bit_idx = 0
 Local FLOAT bit_ticks, baud_calc

 ' PIO0 SM0 Assembly (16-bit hex opcodes):
 ' Measures Low pulse width then High pulse width on JMP pin (GP1)
 Dim INTEGER pio_prog(7) = (&HA02B00C3, &H0041A0C1, &H8000A02B, &H00C80009, &H0
046A0C1, &H80000000, 0, 0)

 PIO STOP 0, 0
 PIO CLEAR 0
 PIO PROGRAM 0, pio_prog()

 ' Configure PIO0 SM0: clock = 10MHz, IN pin = GP1 (1), JMP pin = GP1 (1)
 PIO INIT MACHINE 0, 0, pio_clk, Pio(PINCTRL 0, 0, 0, GP1,GP1 ), , , 0

 Print "Waiting for serial traffic on GP1..."
 ' Wait for line to be idle (High) first, then start SM0
 Do While Pin(GP1) = 0 : Loop
 PIO START 0, 0

 ' Collect 64 pulse durations (32 Low/High pairs) with a 5-second timeout
 Timer = 0
 count = 0
 Do While count < 64 And Timer < 5000

  If Pio(FLEVEL 0, 0, RX) > 0 Then
     PIO READ 0, 0, 1, raw_val

     ' Each loop iteration in PIO takes 2 clock cycles
     samples(count) = (&HFFFFFFFF - raw_val) * 2
     state(count) = count Mod 2 ' 0 = Low pulse, 1 = High pulse
     If samples(count) > 0 And samples(count) < min_ticks Then
       min_ticks = samples(count)
     EndIf
     count = count + 1'  :Print count
   EndIf
 Loop
 PIO STOP 0, 0

 If count < 10 Then
   Print "Timeout: Insufficient serial data detected on GP1."
   Exit Sub
 EndIf

 ' 1. Calculate and snap Baud Rate
 baud_calc = pio_clk / min_ticks
 Local INTEGER std_bauds(11) = (300, 1200, 2400, 4800, 9600, 19200, 38400, 5760
0, 115200, 230400, 460800, 921600)
 Local INTEGER best_baud = 0, b
 Local FLOAT min_err = 9999999.0, err

 For b = 0 To 11
   err = Abs(baud_calc - std_bauds(b)) / std_bauds(b)
   If err < min_err Then
     min_err = err
     best_baud = std_bauds(b)
   EndIf
 Next b

 bit_ticks = pio_clk / best_baud

 ' 2. Reconstruct raw bitstream from pulse durations
 For i = 0 To count - 1
   Local INTEGER num_bits = Cint(samples(i) / bit_ticks)
   If num_bits > 30 Then num_bits = 30 ' Cap inter-character idle gaps
   Local INTEGER k
   For k = 1 To num_bits
     If bit_idx <= 512 Then
       bits(bit_idx) = state(i)
       bit_idx = bit_idx + 1
     EndIf
   Next k
 Next i

 ' 3. Analyze frame spacing (Distance between Start bits)
 ' Search for a long idle High (>= 11 bits) followed by a Start bit (0)
 Local INTEGER frame_len = 0, ones_run = 0
 For i = 0 To bit_idx - 1
   If bits(i) = 1 Then
     ones_run = ones_run + 1
   Else
     If ones_run >= 11 Then
       ' Found clean Start bit at index i; measure until idle returns
       Local INTEGER j, ones_after = 0
       For j = i + 1 To MIN(bit_idx - 1, i + 15)
         If bits(j) = 1 Then
           ones_after = ones_after + 1
           If ones_after >= 3 Then
             ' Frame ended; subtract extra idle stop bits beyond 1
             frame_len = (j - i) - (ones_after - 1)
             Exit For
           EndIf
         Else
           ones_after = 0
         EndIf
       Next j
       If frame_len > 0 Then Exit For
     EndIf
     ones_run = 0
   EndIf
 Next i

 ' 4. Evaluate Parity across captured frames
 Local INTEGER even_ok = 1, odd_ok = 1, frames_checked = 0
 If frame_len >= 10 And frame_len <= 12 Then
   i = 0
   Do While i + frame_len < bit_idx
     If bits(i) = 0 And bits(i + frame_len - 1) = 1 Then
       ' Valid Start (0) and Stop (1) framing
       Local INTEGER parity_sum = 0, d_end = i + frame_len - 3
       For j = i + 1 To d_end
         parity_sum = parity_sum Xor bits(j)
       Next j
       Local INTEGER p_bit = bits(d_end + 1)
       If (parity_sum Xor p_bit) <> 0 Then even_ok = 0
       If (parity_sum Xor p_bit) <> 1 Then odd_ok = 0
       frames_checked = frames_checked + 1
       i = i + frame_len
     Else
       i = i + 1
     EndIf
   Loop
 EndIf

 ' 5. Report Derived Parameters
 Print "--- GP1 Serial Analysis (PIO0) ---"
 Print "Measured Baud : "; Cint(baud_calc); " -> Standard: "; best_baud; " bps"
 Print "Frame Width   : "; frame_len; " bits (inc. Start & 1 Stop)"

 If frames_checked >= 2 And (even_ok = 1 Or odd_ok = 1) Then
   If even_ok = 1 Then Print "Parity        : EVEN"
   If odd_ok = 1 Then Print "Parity        : ODD"
   Print "Data / Stop   : "; frame_len - 3; " data bits, 1 stop bit"
 Else
   Print "Parity        : NONE"
   If frame_len = 11 Then
     Print "Data / Stop   : 8 data bits, 2 stop bits (or 8-bit + Parity)"
   Else
     Print "Data / Stop   : "; frame_len - 2; " data bits, 1 stop bit"
   EndIf
 EndIf
End Sub


The code times out without seeing any pulses and times out after 5 seconds with another Pi Pico generating a continuous stream.
As I have mentioned I do not have a grasp of PIO programming and I was wondering if the brainier people on the forum could see where the code is going wrong.

OA47
 
Posted: 04:08pm
19 Nov 2026
Copy link to clipboard
Volhout
Guru


Hi OA,

As AI to disassemble this:
Dim INTEGER pio_prog(7) = (&HA02B00C3, &H0041A0C1, &H8000A02B, &H00C80009, &H0
046A0C1, &H80000000, 0, 0)


Then we can help you....

Volhout
 
Posted: 10:22pm
19 Nov 2026
Copy link to clipboard
phil99
Guru


Could DEVICE SERIALRX do what you want?
  Quote  DEVICE SERIALRX pinno, baudrate, istring$, timeout_in_ms, status% [,nbr] [,terminators$]

Inputs serial data on ‘pinno’. ‘baudrate’ can be between 110 and 230400 (230400 may need CPU to be overclocked).
‘status%’ returns:
  -1 = timeout (Note: use LEN(istring$) to see number of chars received)
  2 = number of characters requested satisfied
  3 = terminating character satisfied
‘nbr’ specifies the number of characters to be received before the command returns.
‘terminators$’ specifies one or more single characters that can be used to terminate reception.
The program will halt and interrupts ignored while this command is executing.
 
Posted: 10:47pm
19 Nov 2026
Copy link to clipboard
OA47
Guru


Voltout here is the description of the PIO program,

 ' PIO1 SM0 Assembly (16-bit hex opcodes):
 ' Measures Low pulse width then High pulse width on IN pin 0 (GP1)
 ' 0: MOV X, ~NULL  (0xA02B) - Load X with 0xFFFFFFFF
 ' 1: JMP PIN, 3    (0x00C3) - If GP1 is High, Low pulse ended
 ' 2: JMP X--, 1    (0x0041) - Decrement X and loop while Low
 ' 3: MOV ISR, X    (0xA0C1) - Move remaining count to ISR
 ' 4: PUSH noblock  (0x8000) - Push Low count to RX FIFO
 ' 5: MOV X, ~NULL  (0xA02B) - Reset X to 0xFFFFFFFF
 ' 6: JMP PIN, 8    (0x00C8) - If GP1 is still High, keep counting
 ' 7: JMP 9         (0x0009) - GP1 went Low, High pulse ended
 ' 8: JMP X--, 6    (0x0046) - Decrement X and loop while High
 ' 9: MOV ISR, X    (0xA0C1) - Move remaining count to ISR
 ' 10: PUSH noblock (0x8000) - Push High count to RX FIFO
 ' 11: JMP 0        (0x0000) - Wrap to start


OA47
 
Posted: 03:24am
20 Nov 2026
Copy link to clipboard
OA47
Guru


Phil, could you show me a snippet of code that shows the use of DEVICE SERIALRX ?

OA47
 
Posted: 04:18am
20 Nov 2026
Copy link to clipboard
phil99
Guru


A crude test using PWM as a source of serial "UUU" characters and Device SERIALRX as the "serial sniffer"
A better serial source would be another Pico running Device SERIALTX.
> LIST
Dim istring$, timeout_in_ms=2000, status%
SetPin gp3, din 'serial sniffer pin
SetPin gp4, pwm 'link pins GP3 & 4
PWM 2,19200/2,50
do : loop until pin(gp3)=0
do : loop until pin(gp3)=1 'ensure correct start - idle high

Device SERIALRX gp3, 19200, istring$, timeout_in_ms, status% ,8
Pause 999
Print "Status ";status%, "Data ";istring$, Len(istring$)
End
>
> RUN
Status  2       Data UUUUUUUU    8
>


Some time ago Volhout wrote a PIO_UART program that you may be able to adapt.
PIO_UART .


Some time wasting.
Dim istring$, timeout_in_ms=1000, status%
SetPin gp3, din
SetPin gp4, pwm

Sub Rx_test
  Do : Loop Until Pin(gp3)=0
  Do : Loop Until Pin(gp3)=1
  Device SERIALRX gp3, 19200, istring$, timeout_in_ms, status% ,64
  Pause 999
  Print "Status ";status%, "Data ";istring$, Len(istring$)
End Sub

PWM 2, 19200 / 2, 50
Rx_test
PWM 2, 19200 / 4, 75
Rx_test
End

>
> RUN
Status  2       Data UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU    64
Status  2       Data wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww    64
>

Edited 2026-11-20 20:19 by phil99
 


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

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