News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Positron Arm Compiler

Started by JonW, Nov 27, 2026, 01:04 AM

Previous topic - Next topic

top204

I'm still trying to get my head around it Jon. :-)

Thanks to your help, I now have the ARM toolchain loaded and working, and I have compiled my first example. So now to get a PCB, so I can see things working.

So... My reaction is........ WOW !!!!!!!!!!!!!

Best regards
Les

JonW

Sent you a PM.  Ordered some PICO2W wifi boards and a swd debugger to your house. Ill send the mwave ones next week when im in the lab. The debugger will be useful for a hardware emulator, the software one can do serial, spi,12c, gpio, usb, ethernet using pc sockets, hardware ethernet will use wiz chips.

The full toolchain is not needed and am going to see how we can strip it without breaching the Gnu licensing and redistribution.  We are not changing them so it should be ok as long as we acknowledge and abide by the license.

Ill send you the latest version in the morning with the software emulator.

Glad you like it

Frizie

Jon, consider the following:
HIGH PORTx.x, LOW PORTx.x, and TOGGLE PORTx.x versus PORTx.x = 1, PORTx.x = 0, and PORTx.x = ~PORTx.x

HIGH, LOW and TOGGLE do not just set the state of PORTx.x; they also configure the corresponding port as an output (if it is not already set as such).
The examples using PORTx.x do not do this; they only set the state of PORTx.x.
Therefore, HIGH, LOW and TOGGLE are inefficient commands if you have already defined the ports using TRISx.x.

I am not sure if your compiler behaves this way (though it really should).
Ohm sweet Ohm | www.picbasic.nl

JonW

#23
Hi Frizie

PBC does the same as Positron: High, Low and Toggle also make the pin an output. On the RP2350 it costs almost nothing, and the "state only" version isn't faster. Here are both ways, with the code PBC actually produces.
You need to understand that the ARM GPIO differs from a PIC, so some instructions and commands will not port over directly.
1. High / Low / Toggle

Device = RP2354A
Symbol LED = 21

Output LED                      ' make it an output once

Do
    High LED                    ' set it, and make sure it is an output
    Low LED
    Toggle LED
Loop

PBC output (ARM Thumb-2):

    ldr  r1, =0x00200000        ; pin mask, 1 << 21   } loaded once,
    ldr  r2, =0xD0000000        ; SIO base address     } before the loop
    str  r1, [r2, #0x38]        ; GPIO_OE_SET  (Output LED)
.L1:
    str  r1, [r2, #0x18]        ; GPIO_OUT_SET  - High
    str  r1, [r2, #0x38]        ; GPIO_OE_SET   - ...and output
    str  r1, [r2, #0x20]        ; GPIO_OUT_CLR  - Low
    str  r1, [r2, #0x38]        ; GPIO_OE_SET
    str  r1, [r2, #0x28]        ; GPIO_OUT_XOR  - Toggle
    str  r1, [r2, #0x38]        ; GPIO_OE_SET
    b    .L1

2. "State only": the equivalent of PORTB.0 = 1

Do
    SIO_GPIO_OUT_SET = 1 << LED
    SIO_GPIO_OUT_CLR = 1 << LED
    SIO_GPIO_OUT_XOR = 1 << LED
Loop

PBC output:

.L1:
    ldr  r0, =0x00200000        ; pin mask
    ldr  r3, =0xD0000018        ; address of GPIO_OUT_SET
    str  r0, [r3]
    ldr  r3, =0xD0000020        ; address of GPIO_OUT_CLR
    str  r0, [r3]
    ldr  r3, =0xD0000028        ; address of GPIO_OUT_XOR
    str  r0, [r3]
    b    .L1

Why High / Low / Toggle win on this chip

No read-modify-write. On a PIC, setting TRIS for one pin is a bcf on the whole TRIS register, often with a bank select. The RP2350 has separate write-only registers for set, clear and toggle, for both the pin state (OUT) and the direction (OE). Writing a 1 changes only that pin, in one store. That's atomic, so an interrupt can't catch it half-done.
Each of those stores is about one clock, roughly 7 ns at 150 MHz. That's because the SIO block sits on the core's own single-cycle bus, not out on the peripheral bus. So the "make it an output" part of High is one extra 7 ns store.
The compiler puts the pin mask and the SIO base address in registers once, before the loop, so every High, Low or Toggle inside it is just two stores. All the stores share one base address and differ only in offset (#0x18, #0x20, #0x28, #0x38).
The "state only" version has to load each register's address before writing to it. That's 8 instructions per loop against 7, and the four loads each take longer than a store. So on this chip, PORTx.x = 1 style is the slower of the two.

So the advice for Positron on a PIC (set TRIS once and write to the port or latch directly) doesn't carry over to PBC on the RP2350. Use High, Low and Toggle.

The instruction counts (SEVEN against EIGHT) are exact. The one-clock store and the timing claims are from the Cortex-M33 and RP2350 documentation; I haven't measured them on the board.

top204

Many thanks Jon.

Sorry for the sparse feedbacks, but I still cannot get my head around the fact that someone is actually helping me so much, and not doing it for a secondary reason.

Throughout my working life, I have been used more often than helped.

If it all works out, I will be using the PBC as the backend of the Positron_Arm compiler, so that backward compatibility is as close to 100% as possible, and I can also integrate the preprocessor and loader and IDE etc...

It also means the Positron_Arm compiler can pick up syntax errors and relay them to the excellent 3rd party IDEs that have been written for them.

Best regards
Les

JonW

Sounds perfect, Les. Glad to help out and it makes me feel good. Plus, you know I have been pushing for an ARM compiler like Positron and some other C-style commands (Structs, unions, string functions, USB, ETH, etc.).

I've sent you the latest version, which includes all the Ethernet and USB test and measurement classes and 60 demos. The IDE is built in C89 and colours the Procs too.  There is a demo or a USBTMC-SCPI-ETHERNET bridge in there.  I did this in C, and it was 90k, and PBC is 23k.  60 lines of BASIC for a full USBTMC SCPI BRIDGE, pretty impressive

Simulator is working too!


trastikata

Les, Jon,

I don't know if it is possible or it would make sense, but the advanced AI that Jon has access to could run the actual Positron8/16 source (or test part of it) and could advise on possible anomalies.

The high end paid AI got so advanced in coding and analysis that I think if there's any potential anomaly it will catch it. It doesn't mean you have to trust it blindly and the pointed possible anomaly should be verified, but in my experience - it is rarely wrong with code analysis - sometimes inefficient but almost never wrong.

This could make the excellent Positron8/16 better even better, just a thought.

Best regards,
D

JonW

Hi Dyanko

This is exactly what I said to Les in a PM: if he can generate enough from this initial version, it could raise the funds to pay for it; I can also share the tools and know-how to get the most out of it, so the cost is as low as possible. Then, as you say, the sky is the limit.  I am 100 per cent convinced that AI can write and test the current compiler with ease.  On the PBC, it runs hundreds of thousands of regressive steps and precompiles, emulating the cores down to the ASM and register level.

J

JonW

#28
Ported a USBTMC Class I did in C (hand-coded before AI) and have added it to PBC as the dev board has an Silabs clk generator on the PCB.  The power is the control, as the device can be identified by any VISA program. PYVISA, LabVIEW, IO LIBS from Keysight, etc or roll your own, which I do.

Here is a 3-ch 2.5 kHz to 225 MHz clock gen which has been pimped to do FM broadcast at decent deviation by overclocking the i2c and PLL.  The LEDs are WS x 12 on the dev board, but they're driven by DMA via PIO, so you can just deliver the request to change the LED string to the DMA, and it processes it to the PIO, which generates the WS pulses.  All this with 0 code blocking on a single core. 

Here is the code
' clockgen.bas - three-output clock generator, 2.5 kHz to 225 MHz (Si5351A)
'
' MWAVE RP2354A board: Si5351A on I2C0 (GP4/GP5, 27 MHz crystal), 12 WS2812
' LEDs on GP22. Settings are kept in flash and come back at power-up.
'
' LEDs, four per clock (1-4 CLK0, 5-8 CLK1, 9-12 CLK2):
'   green  running      red  off      amber  asked for but can't be made
'   all blue: the Si5351 does not answer on I2C
'   sweeping: that clock's four are a position bar, green at the start to
'             orange at the end
'   FM: that clock's four are a live level meter of the deviation (purple)
' The LEDs are fed by DMA (WS_UseDMA): a frame costs the CPU ~15 us, so the
' live LEDs never hold up a sweep or FM.
'
' A USBTMC instrument (VISA: USB0::0x16D0::0x15B0::<chip ID>::INSTR) and,
' on the same cable, a USB COM port - plus the CP2102 COM port (115200).
' The same SCPI commands on all three. The PC program
' examples\clockgen\clockgen_gui.exe uses either VISA or a COM port.
'
'   *IDN?                       MWAVE,PBC Clock Generator,<chip ID>,1.0
'   *RST                        defaults (CLK0 10 MHz on, CLK1 25 MHz, CLK2 1 MHz off)
'   *CLS  *OPC?  *OPC  *WAI  *TST?  *SAV 0  *RCL 0
'   SYSTem:ERRor[:NEXT]?        next error: 0,"No error"  -113,"Undefined header" ...
'   SYSTem:VERSion?             1999.0
'   CLK0:FREQuency 220MHZ       Hz, or with kHz / MHz (K / M), or 2.2E8, 220E6
'   CLK0:FREQuency?             the frequency asked for, Hz
'   CLK0:OUTPut[:STATe] ON|OFF  output on / off  (also 1 / 0)    ...? = 1 / 0
'   CLK0:DRiVe 2|4|6|8          drive strength, mA (also CLK0:DRV)
'   CLK0:ACTual?                the frequency really made, Hz (0 = not running)
'   SET f0,on0,mA0,f1,on1,mA1,f2,on2,mA2    all three at once (one PLL restart)
'   STAT?                       chip;on,Hz asked,Hz made,mA,state,PLL;... (x3)
'                               state 0 off, 1 running, 2 can't be made
'   SAVE  LOAD  DEF             flash: keep / back to the saved / defaults
'
' Fast changes, sweep and FM (no PLL restart: the other clocks don't glitch):
'   CLK0:FREQ also changes a running clock without a restart when it can -
'     within about 1.5 : 1 of where it was (else a full re-plan, as before)
'   CLK0:SWEEP 88MHZ,108MHZ,100KHZ,10MS    start, stop, step, dwell (US / MS / S)
'                               round and round; CLK0:SWEEP OFF  (or SWEEP:STOP)
'   CLK0:SWEEP?                 1 = sweeping
'   CLK0:FM:DEV 5KHZ            FM deviation (default 5 kHz)
'   CLK0:FM:TONE 1KHZ           modulating tone; 0 = audio on ADC0 (GP26,
'                               biased to mid-rail; link LK7 on the board)
'   CLK0:FM ON | OFF            FM on the carrier CLK0:FREQ; CLK0:FM? 1 / 0
'   SYST:RATE?                  sweep / FM updates in the last second
'   SYST:I2C 1000000 | 400000   the Si5351's I2C speed (SYST:I2C? ). 1 MHz is
'                               outside its 400 kHz rating but works on most
'                               chips: about 2.5 x the FM / sweep update rate.
'                               Kept by SAVE.
' Any other setting stops a sweep / FM. FM needs the clock to have a PLL of
' its own (one other clock on, or all three with this one not the sharer).
' Long or short forms (FREQUENCY or FREQ), any case, leading ':' ignored.
' Errors go into the SCPI error queue (SYST:ERR?). On the COM ports every
' setting command also answers OK, or ERR and the reason; on USBTMC only
' queries answer, as usual for an instrument.
Device = RP2354A                ' the chip on the board
Declare Hserial_Baud = 115200   ' CP2102 COM port
Declare USB_Driver = On         ' the USB COM port; F5 reprograms without BOOTSEL
Declare USB_TMC = On            ' and a USBTMC instrument beside it (VISA)
Declare USB_Product = "PBC Clock Generator"
Include "si5351.inc"            ' SI_Init, SI_Write (I2C0 GP4/GP5, $60)
Include "clockgen.inc"          ' CG_Start, CG_Apply, CG_Save ... (the PLL planner)
Include "scpi.inc"              ' SCPI_Split: header / argument
Include "ws2812.inc"            ' the 12 LEDs on GP22

Dim lin As String * 120         ' the line coming in
Dim cmd As String * 120         ' a whole command
Dim src As Byte                 ' where it came from: 0 USB COM, 1 CP2102, 2 USBTMC
Dim eq[8] As String * 100       ' SCPI error queue
Dim eqn As Byte
Dim hdr As String * 60          ' the command's header, short forms (Norm)
Dim qq As String * 1            ' a " character
Dim fmdev As Dword = 5000       ' FM deviation, Hz
Dim fmtone As Dword = 1000      ' FM tone, Hz (0 = ADC0)
Dim lst[3] As Byte              ' LED states last shown (255 = never)
Dim lchip As Byte = 255
Dim lanim As Byte               ' 1 = a sweep / FM was being shown
Dim lt As Dword                 ' when the last live frame went
Dim c As Byte, k As Byte
Dim hz As Dword
Dim t As String * 40

' ---- answers go back the way the command came --------------------------
Proc Say(s As String)
    If src = 0 Then
        USBOut s, 13, 10
    ElseIf src = 1 Then
        HRSOut s, 13, 10
    Else
        TMC_Out s, 10                               ' USBTMC: the reply to the query
    EndIf
EndProc

' a setting command worked: OK on the COM ports, silence on USBTMC
Proc Ok()
    If src < 2 Then Say("OK")
EndProc

' an error: into the queue (SYST:ERR?), and ERR on the COM ports
Proc Err(code As SWord, txt As String)
    If eqn = 8 Then                                 ' full: the last entry says so
        eq[7] = "-350," + qq + "Queue overflow" + qq
    Else
        eq[eqn] = Str$(SDec code) + "," + qq + txt + qq
        Inc eqn
    EndIf
    If src < 2 Then Say("ERR " + txt)
EndProc

' ---- LEDs: four per clock ----------------------------------------------
' one clock's four LEDs as a bar: lvl 0..4096 (4096 = all four full)
Proc Bar(k As Byte, lvl As Word, r As Byte, g As Byte, b As Byte, br As Byte, bg As Byte, bb As Byte)
    Dim i As Byte, v As Word
    For i = 0 To 3
        If lvl >= (i + 1) * 1024 Then
            v = 1024                                ' full
        ElseIf lvl > i * 1024 Then
            v = lvl - i * 1024                      ' the leading LED: part-lit
        Else
            v = 0
        EndIf
        If v Then
            WS_Set(k * 4 + i, (r * v) / 1024 + br, (g * v) / 1024 + bg, (b * v) / 1024 + bb)
        Else
            WS_Set(k * 4 + i, br, bg, bb)            ' background
        EndIf
    Next
EndProc

Proc Leds()
    Dim i As Byte, r As Byte, g As Byte, b As Byte, live As Byte
    Dim span As Dword, d As Dword, lvl As Word
    live = 0
    If CG_SwK < 3 Or CG_FmK < 3 Then live = 1
    If live = 0 And lanim = 0 Then                  ' still: only on a change
        If lchip = CG_Chip And lst[0] = CG_St[0] And lst[1] = CG_St[1] And lst[2] = CG_St[2] Then Exit Proc
    EndIf
    If live And TIMER0_TIMERAWL - lt < 40000 Then Exit Proc   ' live: 25 frames a second
    If WS_Ready() = 0 Then Exit Proc                ' last frame still going out: next pass
    lt = TIMER0_TIMERAWL
    lanim = live
    lchip = CG_Chip
    For i = 0 To 2 : lst[i] = CG_St[i] : Next
    For i = 0 To 11
        k = i / 4
        If CG_Chip = 0 Then
            r = 0 : g = 0 : b = 40                  ' no chip: blue
        ElseIf CG_St[k] = 1 Then
            r = 0 : g = 40 : b = 0                  ' running: green
        ElseIf CG_St[k] = 2 Then
            r = 60 : g = 25 : b = 0                 ' can't be made: amber
        Else
            r = 40 : g = 0 : b = 0                  ' off: red
        EndIf
        WS_Set(i, r, g, b)
    Next
    If CG_SwK < 3 Then                              ' sweep position: 0 at start, 4096 at stop
        If CG_SwB >= CG_SwA Then
            span = CG_SwB - CG_SwA
            d = CG_SwF - CG_SwA
        Else
            span = CG_SwA - CG_SwB
            d = CG_SwA - CG_SwF
        EndIf
        lvl = 4096
        If span Then lvl = MulDiv(d, 4096, span)
        If lvl > 4096 Then lvl = 4096
        Bar(CG_SwK, lvl, lvl / 70, 40, 0, 0, 3, 0)  ' green -> orange as it goes
    EndIf
    If CG_FmK < 3 Then                              ' FM: peak deviation since the last frame
        lvl = CG_FmPk * 2
        CG_FmPk = 0
        Bar(CG_FmK, lvl, 50, 0, 70, 4, 0, 6)        ' purple meter on a dim purple
    EndIf
    WS_Show()
EndProc

' ---- frequency text -> Hz in hz; 1 = understood --------------------------
' 220MHZ  220M  10.7MHz  32.768K  2500  2500HZ  2.2E8  220E6  1.5E+6
Proc ParseHz(s As String), Byte
    Dim i As Byte, d As Byte, dot As Byte, esgn As Byte, ee As Byte, ue As SByte
    Dim nfrac As SByte, ex As SByte, n As Byte
    Result = 0
    t = UCase$(s)
    t = Replace$(t, " ", "")
    If Right$(t, 2) = "HZ" Then t = Left$(t, Len(t) - 2)
    ue = 0
    If Right$(t, 1) = "M" Then ue = 6
    If Right$(t, 1) = "K" Then ue = 3
    If Right$(t, 1) = "G" Then ue = 9
    If ue Then t = Left$(t, Len(t) - 1)
    n = Len(t)
    If n = 0 Then Exit Proc
    hz = 0 : dot = 0 : nfrac = 0 : ex = 0 : ee = 0 : esgn = 0
    i = 0
    While i < n                                     ' mantissa
        d = t[i]
        If d = "." Then
            If dot Then Exit Proc
            dot = 1
        ElseIf d >= "0" And d <= "9" Then
            If hz <= 429496728 Then                 ' room for another digit
                hz = hz * 10 + (d - "0")
                If dot Then Dec nfrac
            ElseIf dot = 0 Then
                Inc ex                              ' too many digits: count them as x10
            EndIf
        ElseIf d = "E" Then
            Break
        Else
            Exit Proc
        EndIf
        Inc i
    Wend
    If i < n Then                                   ' exponent: E, sign, digits
        Inc i
        If i < n Then
            If t[i] = "-" Then
                esgn = 1
                Inc i
            ElseIf t[i] = "+" Then
                Inc i
            EndIf
        EndIf
        If i >= n Then Exit Proc
        While i < n
            d = t[i]
            If d < "0" Or d > "9" Then Exit Proc
            ee = ee * 10 + (d - "0")
            If ee > 30 Then Exit Proc
            Inc i
        Wend
    EndIf
    If esgn Then ex = ex - ee Else ex = ex + ee
    ex = ex + ue + nfrac                            ' value = hz x 10^ex
    While ex > 0
        If hz > 429496729 Then Exit Proc            ' too big
        hz = hz * 10
        Dec ex
    Wend
    While ex < 0
        If ex = -1 Then hz = (hz + 5) / 10 Else hz = hz / 10   ' round to the hertz
        Inc ex
    Wend
    Result = 1
EndProc

' ---- "10MS", "500US", "0.5S", "20" (ms) -> microseconds in hz -------------
Proc ParseUs(s As String), Byte
    Dim u As String * 30
    u = UCase$(s)
    u = Replace$(u, " ", "")
    If Right$(u, 2) = "US" Then
        u = Left$(u, Len(u) - 2)
    ElseIf Right$(u, 2) = "MS" Then
        u = Left$(u, Len(u) - 2) + "K"              ' x 1000
    ElseIf Right$(u, 1) = "S" Then
        u = Left$(u, Len(u) - 1) + "M"              ' x 1000000
    Else
        u = u + "K"                                 ' no unit: ms
    EndIf
    Result = ParseHz(u)
EndProc

' ---- mA <-> drive code --------------------------------------------------
Proc DrvCode(ma As Dword), Byte
    Result = 9
    Select ma
        Case 2 : Result = 0
        Case 4 : Result = 1
        Case 6 : Result = 2
        Case 8 : Result = 3
    EndSelect
EndProc

' after a change: plan, set the chip, LEDs; say why if a clock can't be made
Proc Update()
    CG_SweepStop()
    CG_FmStop()
    CG_Apply()
    Leds()
    For k = 0 To 2
        If CG_St[k] = 2 Then
            Err(-221, "Settings conflict; CLK" + Str$(k) + " can't be made with these frequencies")
            Exit Proc
        EndIf
    Next
    Ok()
EndProc

' SCPI long forms -> the short ones; drops a leading ':'
Proc Norm(hin As String)
    Dim i As Byte, w As String * 20, q As Byte, h As String * 60
    h = hin
    If Left$(h, 1) = ":" Then h = Mid$(h, 2, 60)
    q = 0
    If Right$(h, 1) = "?" Then
        q = 1
        h = Left$(h, Len(h) - 1)
    EndIf
    hdr = ""
    For i = 1 To 5
        w = Field$(h, i, ":")
        If Len(w) = 0 Then Break
        If w = "FREQUENCY" Then w = "FREQ"
        If w = "OUTPUT" Then w = "OUTP"
        If w = "STATE" Then w = "STAT"
        If w = "DRIVE" Or w = "DRIV" Then w = "DRV"
        If w = "ACTUAL" Then w = "ACT"
        If w = "SYSTEM" Then w = "SYST"
        If w = "ERROR" Then w = "ERR"
        If w = "VERSION" Then w = "VERS"
        If w = "DEVIATION" Then w = "DEV"
        If i > 1 Then hdr = hdr + ":"
        hdr = hdr + w
    Next
    If hdr = "SYST:ERR:NEXT" Then hdr = "SYST:ERR"
    If q Then hdr = hdr + "?"
EndProc

' ---- one command ---------------------------------------------------------

Proc Doit(cl As String)
    Dim n As Byte, item As String * 20, sub As String * 20, i As Byte, sw As String * 80
    Dim swv[3] As Dword
    SCPI_Split(cl)
    If Len(SCPI_Hdr) = 0 Then Exit Proc
    Norm(SCPI_Hdr)
    If hdr = "*IDN?" Then
        Say("MWAVE,PBC Clock Generator," + USB_SerialNo$ + ",1.0")
    ElseIf hdr = "*RST" Or hdr = "DEF" Then
        CG_Defaults()
        Update()
    ElseIf hdr = "*CLS" Then
        eqn = 0
        Ok()
    ElseIf hdr = "*OPC?" Then
        Say("1")                                    ' every command is finished when answered
    ElseIf hdr = "*OPC" Or hdr = "*WAI" Then
        Ok()
    ElseIf hdr = "*TST?" Then
        If CG_Chip Then Say("0") Else Say("1")     ' 0 = self-test passed (the chip answers)
    ElseIf hdr = "SYST:ERR?" Then
        If eqn = 0 Then
            Say("0," + qq + "No error" + qq)
        Else
            Say(eq[0])
            For i = 1 To eqn - 1                    ' take the oldest off the queue
                eq[i - 1] = eq[i]
            Next
            Dec eqn
        EndIf
    ElseIf hdr = "SYST:VERS?" Then
        Say("1999.0")
    ElseIf hdr = "SYST:RATE?" Then
        Say(Str$(CG_Rate))
    ElseIf hdr = "SYST:I2C" Then
        If ParseHz(SCPI_Arg) = 0 Then Err(-104, "Data type error; 400000 or 1000000") : Exit Proc
        If hz = 1000000 Then
            CG_SetI2C(1)
        ElseIf hz = 400000 Then
            CG_SetI2C(0)
        Else
            Err(-224, "Illegal parameter value; 400000 or 1000000") : Exit Proc
        EndIf
        Ok()
    ElseIf hdr = "SYST:I2C?" Then
        If CG_I2CF Then Say("1000000") Else Say("400000")
    ElseIf hdr = "STAT?" Then
        cmd = Str$(CG_Chip)
        For k = 0 To 2
            cmd = cmd + ";" + Str$(CG_On[k]) + "," + Str$(CG_F[k]) + "," + Str$(CG_Act[k]) + "," _
                  + Str$(CG_Drv[k] * 2 + 2) + "," + Str$(CG_St[k]) + ","
            If CG_Pll[k] Then cmd = cmd + "B" Else cmd = cmd + "A"
        Next
        Say(cmd)
    ElseIf hdr = "SAVE" Or hdr = "*SAV" Then
        CG_Save()
        Ok()
    ElseIf hdr = "LOAD" Or hdr = "*RCL" Then
        If CG_Load() Then
            Update()
        Else
            Err(-200, "Execution error; nothing saved")
        EndIf
    ElseIf hdr = "SET" Then                         ' f0,on0,mA0,f1,on1,mA1,f2,on2,mA2
        For k = 0 To 2
            item = Field$(SCPI_Arg, k * 3 + 1, ",")
            If ParseHz(item) = 0 Then Err(-104, "Data type error; frequency " + item) : Exit Proc
            CG_F[k] = hz
            CG_On[k] = Val(Field$(SCPI_Arg, k * 3 + 2, ",")) & 1
            i = DrvCode(Val(Field$(SCPI_Arg, k * 3 + 3, ",")))
            If i > 3 Then Err(-224, "Illegal parameter value; drive 2, 4, 6 or 8 mA") : Exit Proc
            CG_Drv[k] = i
        Next
        Update()
    ElseIf Left$(hdr, 3) = "CLK" And Mid$(hdr, 5, 1) = ":" Then
        n = Val(Mid$(hdr, 4, 1))
        If n > 2 Or Mid$(hdr, 4, 1) < "0" Then Err(-114, "Header suffix out of range; CLK0, CLK1 or CLK2") : Exit Proc
        sub = Mid$(hdr, 6, 20)
        If sub = "FREQ?" Then
            Say(Str$(CG_F[n]))
        ElseIf sub = "ACT?" Then
            Say(Str$(CG_Act[n]))
        ElseIf sub = "OUTP?" Or sub = "OUTP:STAT?" Then
            Say(Str$(CG_On[n]))
        ElseIf sub = "DRV?" Then
            Say(Str$(CG_Drv[n] * 2 + 2))
        ElseIf sub = "FREQ" Then
            If ParseHz(SCPI_Arg) = 0 Then Err(-104, "Data type error; frequency") : Exit Proc
            If hz < CG_FMIN Or hz > CG_FMAX Then Err(-222, "Data out of range; 2.5 kHz to 225 MHz") : Exit Proc
            CG_SweepStop()
            CG_FmStop()
            If CG_On[n] And CG_Fast(n, hz) Then     ' no PLL restart
                Ok()
            Else
                CG_F[n] = hz
                Update()
            EndIf
        ElseIf sub = "SWEEP" Then                   ' start,stop,step,dwell  or OFF
            If UCase$(SCPI_Arg) = "OFF" Or SCPI_Arg = "0" Then CG_SweepStop() : Ok() : Exit Proc
            sw = SCPI_Arg
            For i = 0 To 2
                item = Field$(sw, i + 1, ",")
                If ParseHz(item) = 0 Then Err(-104, "Data type error; SWEEP start,stop,step,dwell") : Exit Proc
                swv[i] = hz
            Next
            item = Field$(sw, 4, ",")
            If ParseUs(item) = 0 Then Err(-104, "Data type error; dwell (US, MS, S)") : Exit Proc
            If swv[0] < CG_FMIN Or swv[0] > CG_FMAX Or swv[1] < CG_FMIN Or swv[1] > CG_FMAX Then
                Err(-222, "Data out of range; 2.5 kHz to 225 MHz") : Exit Proc
            EndIf
            If swv[2] = 0 Then Err(-224, "Illegal parameter value; step 0") : Exit Proc
            If CG_SweepStart(n, swv[0], swv[1], swv[2], hz) Then
                Leds()
                Ok()
            Else
                Leds()
                Err(-221, "Settings conflict; CLK" + Str$(n) + " can't be made")
            EndIf
        ElseIf sub = "SWEEP:STOP" Then
            CG_SweepStop()
            Ok()
        ElseIf sub = "SWEEP?" Then
            If CG_SwK = n Then Say("1") Else Say("0")
        ElseIf sub = "FM:DEV" Then
            If ParseHz(SCPI_Arg) = 0 Then Err(-104, "Data type error; deviation") : Exit Proc
            If hz > 200000 Then Err(-222, "Data out of range; deviation up to 200 kHz") : Exit Proc
            fmdev = hz
            If CG_FmK = n Then i = CG_FmStart(n, fmdev, fmtone)
            Ok()
        ElseIf sub = "FM:DEV?" Then
            Say(Str$(fmdev))
        ElseIf sub = "FM:TONE" Then
            If ParseHz(SCPI_Arg) = 0 Then Err(-104, "Data type error; tone") : Exit Proc
            If hz > 20000 Then Err(-222, "Data out of range; tone up to 20 kHz (0 = ADC0)") : Exit Proc
            fmtone = hz
            If CG_FmK = n Then i = CG_FmStart(n, fmdev, fmtone)
            Ok()
        ElseIf sub = "FM:TONE?" Then
            Say(Str$(fmtone))
        ElseIf sub = "FM" Then
            item = UCase$(SCPI_Arg)
            If item = "OFF" Or item = "0" Then
                CG_FmStop()
                Ok()
            ElseIf item = "ON" Or item = "1" Then
                If CG_FmStart(n, fmdev, fmtone) Then
                    Ok()
                Else
                    Err(-221, "Settings conflict; FM needs CLK" + Str$(n) + " on, with a PLL of its own")
                EndIf
            Else
                Err(-224, "Illegal parameter value; ON or OFF")
            EndIf
        ElseIf sub = "FM?" Then
            If CG_FmK = n Then Say("1") Else Say("0")
        ElseIf sub = "OUTP" Or sub = "OUTP:STAT" Then
            item = UCase$(SCPI_Arg)
            If item = "ON" Or item = "1" Then
                CG_On[n] = 1
            ElseIf item = "OFF" Or item = "0" Then
                CG_On[n] = 0
            Else
                Err(-224, "Illegal parameter value; ON or OFF") : Exit Proc
            EndIf
            Update()
        ElseIf sub = "DRV" Then
            i = DrvCode(Val(SCPI_Arg))
            If i > 3 Then Err(-224, "Illegal parameter value; drive 2, 4, 6 or 8 mA") : Exit Proc
            CG_Drv[n] = i
            Update()
        Else
            Err(-113, "Undefined header; " + SCPI_Hdr)
        EndIf
    Else
        Err(-113, "Undefined header; " + SCPI_Hdr)
    EndIf
EndProc

' ---- a byte in: build lines, run each one ------------------------------
Proc Take(b As Byte, from As Byte)
    If b = 13 Or b = 10 Then
        If Len(lin) Then
            src = from
            cmd = lin
            lin = ""
            Doit(cmd)
        EndIf
    ElseIf Len(lin) < 118 Then
        lin = lin + Chr$(b)
    EndIf
EndProc

' ---- start ----------------------------------------------------------------
qq = Chr$(34)
WS_Init(GP22, 12)
WS_UseDMA(11)                   ' frames go out by DMA: no CPU time
WS_Fill(0, 0, 0) : WS_Show()
DelayMs 100                     ' let the Si5351 power up
CG_Start()                      ' chip, saved settings (or defaults), outputs
Leds()
HRSOut "PBC clock generator - Si5351 ", Dec CG_Chip, 13, 10

Do
    While USB_Avail() > 0
        USBIn c
        Take(c, 0)
    Wend
    While HSer_Avail() > 0
        HRSIn c
        Take(c, 1)
    Wend
    If TMC_Avail() > 0 Then                         ' a command over USBTMC (VISA)
        TMC_In {5, NoTmc}, cmd
        src = 2
        Doit(cmd)
    EndIf
NoTmc:
    CG_Run()                                        ' sweep / FM steps
    Leds()                                          ' (only redraws on a change)
Loop


Some pics of the IDE, I have added build progress bar and some new icons. 

PBC0_9_GUI.png

Also building supporting Gui's for control (Wifi, Ethernet, LED demos to show how you can build your own).

I'm working on a Wi-Fi-to-Silabs FM broadcast, so you can stream Wi-Fi audio and rebroadcast it locally on FM.

SILABS.png

There is a built-in VISA server that sends and receives the SCPI commands.  So the clkgen comes up as an instrument to any VISA program.




▲