Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:50 19 Nov 2026 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 : Stumped again, this time with PIO

Author Message
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 1066
Posted: 05:27am 19 Nov 2026
Copy link to clipboard 
Print this post

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
 
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 2026