News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Positron8/16 USB Stack

Started by trastikata, Nov 30, 2026, 02:27 PM

Previous topic - Next topic

trastikata

Hello all,

as mentioned earlier in the other topic I've been working (with the help of an AI model) on a broader USB stack for Positron8 and Positron16.

The new USB stack is user oriented and provides a configuration tool to help you with the basic set-up for the USB Stack as well as the basic PIC MCU set-up it self.

The USB Library currently has USB Device mode only and will be expanded to Host and OTG later when I finish the new Host and OTG development boards.

The USB Library includes several tested examples for some PIC18F, PIC24 and dsPIC33 devices. To keep the USB connection alive, interrupt and poll modes are supported. There is also included the source code for VS2026 WPF PC Host application templates for HID and CDC, note VS2026 has a free version which I used.

Supported USB Device modes are:

- HID data - send and receive data as general HID device

HID_HA.jpg

- HID input devices - keyboard, mouse, joystick or media control


- CDC - an emulated COM-port

CDC_HA.jpg

- MSC - a virtual drive and files


- RNDIS - an emulated network adapter with web server




If you need help or have questions, I'd be happy to help.

P.s. Currently the Positron8/16 USB Stack is a standalone implementation but the road map is:
* USB Host stack
* USB OTG stack
** Merging the Positron8/16 USB Stack with the FAT32 SD card stack and TFT Library 2

top204

Many thanks Dyanko.

A brilliant piece of coding, and it is such a pleasure that I can see the compilers being put to such good, creative, work. It means my years of work on them has a meaningful purpose.

Best regards
Les

trastikata

#2
Hello all,

Positron8/16 USB Stack has been updated to USB Device Library 0.21.

- added ISR for RNDIS to keep the USB connection alive in the background. The web browser still needs regular in-code servicing.
- added mDNS for RNDIS - the virtual PIC server now supports named addressing with the *.local extension like: http://positron.local


Meanwhile to show the capability of Positron Compilers - this is a real project I made for free as a favour to some fellow RC and rocket modelists (my other hobbies). You might think the development cost was high, but that project cost me less than 100$ in total for assembled PCBs with 16 devices - the really inexpensive but powerful Positron license made it possible.

A flight recorder with 9 DoF sensors, barometric altitude sensor, 64 Mbit FLASH memory, built-in LiPo battery and LiPo charger, USB interface - all less than 1 cm3, weighting only 2 grams and using the old PIC18F25J50 plus Positron8.

Now the firmware and software adapted under the new USB stack, made the HID interface faster and more stable which allows real time 9 DoF orientation and automatic calibration through the PC Host Software. For the demo I changed the titles on purpose for Positron promotion.

MAV7TOP.JPG MAV7BOT.JPG MAV7WG.JPG


JonW

Thats a dinky little board.  Nice work dude. 

What about a USBTMC?  Its a test and measurement class.  Great for doing instumentation or test that VISA libs can control. PYVISA and Free Keysight IO Libs can talk directly to it.  I use this a lot in C, as it's supported by Tiny USB. It's really powerful for control via SCPI, etc., and I've added a full Lib to the PBC for USB-to-ETH with an SCPI parser.  Be a cool addition to a great Lib


J

Gamboa

Thank you for sharing your USB library. A great addition to Positron, with thorough and clear documentation too.

Regards
Gamboa
Long live for you

trastikata

#5
Quote from: JonW on Dec 02, 2026, 09:00 PMWhat about a USBTMC?  Its a test and measurement class.  Great for doing instumentation or test that VISA libs can control. PYVISA and Free Keysight IO Libs can talk directly to it.  I use this a lot in C, as it's supported by Tiny USB. It's really powerful for control via SCPI, etc., and I've added a full Lib to the PBC for USB-to-ETH with an SCPI parser.  Be a cool addition to a great Lib


Hi Jon,

USBTMC is now part of the USB Library.

Do you have any application that you can share or advise so I can test the USBTMC protocol with external software?

Best regards,
D






JonW

Hi Dyanko,

Great news on USBTMC, that is a really useful addition to the library.

For testing against external software, these are what I would recommend:

1. NI-VISA + NI MAX (Windows, easiest start)
Install NI-VISA (free). It includes the USBTMC driver, so the device should appear in NI MAX under Devices and Interfaces. Right-click > Open VISA Test Panel lets you send *IDN? and read the reply, and exercise viClear and viReadSTB without writing any code.

2. Keysight IO Libraries Suite (alternative) is what I normally use; it's free from Keysight.
Connection Expert / Interactive IO does the same job. Worth testing with both, as the two vendors' drivers are not equally forgiving about protocol details.

3. Linux
The kernel has a built-in usbtmc driver, so no install is needed. The device appears as /dev/usbtmc0:
  echo "*IDN?" > /dev/usbtmc0 ; cat /dev/usbtmc0
That is a good independent check that it is not just working with one vendor's driver.

4. Test script
Attached is a small standalone test script, usbtmc_test.py. It needs Python, pyvisa (pip install pyvisa) and NI-VISA or Keysight IO Libraries for the driver.

Only last for 3 days and is safe HERE

To run it, open a command prompt in the folder with the script and type:

  py usbtmc_test.py

It lists every USB instrument VISA can see, tries each one, and tests the first that answers *IDN?:
[1] *IDN?
[2] 200 x *IDN? for throughput / latency (progress every 50)
[3] reply lengths 1..14000 bytes around the 64-byte packet boundaries (multi-packet, EOM, ZLP)
[4] a read with no reply -> timeout -> VISA sends ABORT_BULK_IN -> checks the device recovers
[5] viClear (INITIATE_CLEAR / CHECK_CLEAR_STATUS)
[6] viReadSTB (READ_STATUS_BYTE)

If several instruments are connected and you want to test a specific one, copy its resource string from the list the script prints (or from NI MAX) and pass it in quotes, e.g.:

  py usbtmc_test.py "USB0::0x1234::0x5678::123456::INSTR"

The resource string is made up of your VID, PID and serial number. Use exactly what VISA shows for your device.

Firmware side - three test commands
Tests 3 and 4 need three test commands in the PIC:

  *IDN?    -> ID string, LF terminated
  DATA? n  -> exactly n bytes of test pattern, then LF (details below)
  NOREPLY  -> accepted, no reply (used to force a timeout and abort)

Without them, test 3 times out and fails, but the rest of the results are still valid. That is exactly what I get running it through my USB-LAN adapter to a scope.

DATA? n
The PC sends the command DATA? followed by a space and a decimal number, n, of up to 14000 bytes. For example:

  DATA? 5
  DATA? 64
  DATA? 14000

The device must reply with exactly n bytes of data, followed by a single LF (0x0A) terminator. The data is the alphabet repeated: A, B, C ... Z, then back to A, and so on. Byte number i (counting from 0) is:

  65 + (i MOD 26)      ' 65 = "A"

So:

  DATA? 1    ->  A<LF>                              (2 bytes total)
  DATA? 5    ->  ABCDE<LF>                          (6 bytes total)
  DATA? 30  ->  ABCDEFGHIJKLMNOPQRSTUVWXYZABCD<LF> (31 bytes total)

The script works out the same pattern on the PC and compares it byte for byte, so any dropped, duplicated or reordered byte shows as a FAIL along with the number of bytes actually received.

Why these sizes: full-speed USB bulk packets carry 64 bytes, and USBTMC adds a 12-byte header to the first packet of each transfer. The script deliberately asks for lengths just below, on and just above the packet boundaries:

  1, 62, 63, 64, 65, 127, 128, 129, 511, 512, 513, 4096, 14000

These catch the usual bugs:
- off-by-one errors where the header and data split across packets
- a missing zero-length packet when a transfer ends exactly on a 64-byte boundary (the host waits and times out)
- multi-transfer replies where the EOM bit is set too early, too late or not at all
- buffer overruns on long replies

Large replies: the pattern is generated on the fly, so even 14000 bytes needs only a 64-byte buffer in the PIC. But a reply that long spans many USBTMC transfers. The host issues several REQUEST_DEV_DEP_MSG_IN requests, and the device must set EOM only on the final one. If the library does not support multi-transfer replies yet, edit the size list in the script (search for "for size in") to stop at a value it can handle.

Positron outline
I have made up the calls to your USMTMC library; thus the two hooks need mapping to your library's calls:


- sCmd holds the received DEV_DEP_MSG_OUT payload
- TMC_Send(buf, len, eom) queues bytes for the next DEV_DEP_MSG_IN; eom = 1 on the last chunk

  Dim sCmd  As String * 64
  Dim TxBuf[64] As Byte
  Dim wN    As Word
  Dim wSent  As Word
  Dim bChunk As Byte

  Proc HandleCommand()
      If Left$(sCmd, 5) = "*IDN?" Then
          sCmd = "PIC USBTMC TEST,0,0001,1.0" + Chr$(10)
          ' copy sCmd into TxBuf, then:
          TMC_Send(TxBuf, Len(sCmd), 1)

      ElseIf Left$(sCmd, 5) = "DATA?" Then
          wN = Val(Mid$(sCmd, 7, 6), Dec)      ' n after "DATA? "
          wSent = 0
          Repeat
              bChunk = 0
              While bChunk < 64 And wSent < wN
                  TxBuf[bChunk] = 65 + (wSent // 26)  ' A..Z repeating
                  Inc bChunk
                  Inc wSent
              Wend
              If wSent = wN And bChunk < 64 Then        ' room for the LF
                  TxBuf[bChunk] = 10
                  Inc bChunk
                  TMC_Send(TxBuf, bChunk, 1)            ' last chunk, EOM
                  Break
              EndIf
              TMC_Send(TxBuf, bChunk, 0)                ' more to come
              If wSent = wN Then                        ' LF falls in next chunk
                  TxBuf[0]= 10
                  TMC_Send(TxBuf, 1, 1)
                  Break
              EndIf
          Until 0

      ElseIf Left$(sCmd, 7) = "NOREPLY" Then
          ' accepted, deliberately no reply
      EndIf
  Endproc

Things worth checking, as these are where USBTMC stacks usually break:
- Interface descriptor: class 0xFE, subclass 0x03, protocol 0x01 for USB488. If it is wrong, VISA will not bind.
- GET_CAPABILITIES response.
- bTag / bTagInverse checking and incrementing.
- A zero-length packet when a reply is an exact multiple of 64 bytes.
- Abort handling after a VISA timeout.
- viClear from idle as well as after a timeout. My own adapter passes the clear after a timeout but times out on a clear when nothing is pending, so CHECK_CLEAR_STATUS has to complete in both states.
- The interrupt-IN endpoint, if you support SRQ.

The script has only been run against my adapter so far, and the Positron outline is untested, so any feedback is welcome.

Best regards,
Jon


JonW

#7
I also have this ULAN_BENCH App.  It uses the USBTMC to connect to the Eth Pico2-Wiz board and creates a USBTMC SCPI bridge that appears in Visa as an instrument. Basically, it connects to any Ethernet SCPI kit over USB, with no switches, and can be used by any Visa tool.

This tool can assign any command, run scripts and save the data, display the data, screen dump, grab traces etc.  Very powerful tool if you want to script setups and tests.  ULAN_BENCH Help.  I'm also working on an old school fully isolated USB_TMC to GPIB that doesn't cost the earth and is fast, one of the reasons for the compiler as the PICO SDK was doing my head in.

ULAN_BENCH_ADAPTOR.png

▲