pthreads linux simulator port

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

pthreads linux simulator port

Postby faisal » Wed Dec 27, 2023 5:33 am

https://github.com/faisal-shah/ChibiOS. ... or-pthread
Check out demos/various/RT-Posix-Simulator-Pthread

I wrote a pthreads based simulator port. This has 2 benefits:
1. No more limitation on integer only code being safe (as 'user space' threading was used in the existing SIMIA32 port with manual context switching which did not manage the FPU).
2. More portable - can run on any cpu architecture. Previous port was for x86 specifically.
3. Can be extended to be preemptive (using posix signals and alarms).

How it works:
Each ChibiOS thread is a pthread.
All threads are pinned to the same CPU.
Only one thread executing at a time, and all other pthreads are sleeping (waiting on cond var). As a result, ChibiOS scheduler is deciding which thread is running at any given time.

The port_context struct contains synchronization objects (mutex and conditional variable) for each pthread.
The context switch consists of waking up incoming thread by signaling its cond var, and putting the outgoing thread to sleep by waiting on its cond var.
When a thread exits, the context switch function detects that the state of the outgoing thread is CH_STATE_FINAL - and it signals the incoming thread to cancel the outgoing pthread when it wakes up.

I also modified hal_lld.c _sim_check_for_interrupts to sleep between ticks to dramatically reduce the cpu consumption to almost nothing (before it was pinned at 100%).

https://github.com/faisal-shah/ChibiOS. ... or-pthread

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: pthreads linux simulator port

Postby faisal » Wed Dec 27, 2023 4:38 pm

Maybe this would be a good time to flesh out the simulator with more low level drivers. There didn't seem to be much appetite in the past.

We wrote the following additional simulator low level drivers, and are currently being used on ChibiOS 20.x . Let me know and I can post updated versions here.

ADC
CAN
EFL
GPT
NAND (to use with the community hal_nand)
RTC
SERIAL (existing sim driver uses telnet - this one you can select to use a PTY interface instead)
UART (using PTY)


Some old threads:
viewtopic.php?t=4861
viewtopic.php?f=38&t=6149

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: pthreads linux simulator port

Postby faisal » Thu Dec 28, 2023 4:30 am

Added a pthread serial-tty demo. The hal_serial_lld simulator driver still has support for TCP, and is enabled by default. If you want to use PTY, you have to define USE_SIM_SERIAL_TTY in mcuconf.h .
https://github.com/faisal-shah/ChibiOS. ... or-pthread

From the testhal/PTHREAD/SERIAL-TTY/readme.txt:
*****************************************************************************
** ChibiOS/RT port for x86 into a Posix process **
*****************************************************************************

** TARGET **

The demo runs under any Posix IA32 system as an application program. The serial
I/O is simulated over TTY device.

** The Demo **

A thread is started that serves a small command shell. The command shell can be
exited with the exit command. A new command shell thread is then spawned. The
demo shows how to create/terminate threads at runtime, how to listen to events,
how to work with serial ports, how to use the messages. You can develop your
ChibiOS/RT application using this demo as a simulator then you can recompile it
for a different architecture. See demo.c for details.

** Build Procedure **

The demo was built using GCC.

** Connect to the demo **

1. Create a PTY pair using socat

$ socat -d -d pty,raw,echo=0 pty,raw,echo=0 &
2023/12/27 21:19:17 socat[539] N PTY is /dev/pts/2
2023/12/27 21:19:17 socat[539] N PTY is /dev/pts/3
2023/12/27 21:19:17 socat[539] N starting data transfer loop with FDs [6,6] and [9,9]

2. Use screen to connect to one end of the PTY pair.

$ screen /dev/pts/3 # may have to use sudo

3. In another terminal, use the other end of the PTY pair as a command line
argument to the demo.

$ ch /dev/pts/2

4. You should see the following in the screen program (below 'help' was entered
at the first prompt).

ChibiOS/RT Shell
ch> help
Commands: help exit info echo systime mem threads test
ch>

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: pthreads linux simulator port

Postby faisal » Thu Dec 28, 2023 5:55 pm

Added simulator ports and demos for GPT and ADC.
https://github.com/faisal-shah/ChibiOS. ... or-pthread

from testhal/PTHREAD/ADC-GPT/readme.txt:

*****************************************************************************
** ChibiOS/RT port for x86 into a Posix process **
*****************************************************************************

** TARGET **

The demo runs under any Posix IA32 system as an application program.

** The Demo **

A GPT is used to trigger the ADC conversions.

This demonstrates the usage of the simulator GPT, and ADC.

The simulator GPT has a secondary callback function, which can serve to
trigger other peripherals behind the scenes - as you would expect a timer
peripheral on an MCU to do. In this demo, it is triggering the ADC conversions
every 100ms.

The simulator ADC has a few unique features:
- A low level function `sim_adc_lld_trigger_conversion`, which can be used to
manually trigger a conversion. It will return non-zero if a conversion is
currently in process - otherwise 0.
- A `channels_default` field in the ADCConversionGroup which serves as the
initial ADC values for each channel.
- A `start_conv_cb` which is called on the start of a conversion. Here you
can prepare ADC channel data to be simulated.
- A `conv_cb` which is called when the conversion is finished. Here you can
copy the simulated channel data into ADCDriver::channel_data

** Build Procedure **

The demo was built using GCC.

** Sample output **

ChibiOS/RT simulator (Linux)

triggered = 0
Samples: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
triggered = 0
Samples: 2 3 4 5 6 7 8 9 10 11 2 3 4 5 6 7 8 9 10 11
triggered = 0
Samples: 3 4 5 6 7 8 9 10 11 12 3 4 5 6 7 8 9 10 11 12
triggered = 0
Samples: 4 5 6 7 8 9 10 11 12 13 4 5 6 7 8 9 10 11 12 13
triggered = 0
Samples: 5 6 7 8 9 10 11 12 13 14 5 6 7 8 9 10 11 12 13 14
triggered = 0
Samples: 6 7 8 9 10 11 12 13 14 15 6 7 8 9 10 11 12 13 14 15
triggered = 0
Samples: 7 8 9 10 11 12 13 14 15 16 7 8 9 10 11 12 13 14 15 16
triggered = 0
Samples: 8 9 10 11 12 13 14 15 16 17 8 9 10 11 12 13 14 15 16 17
triggered = 0
Samples: 9 10 11 12 13 14 15 16 17 18 9 10 11 12 13 14 15 16 17 18
triggered = 0
Samples: 10 11 12 13 14 15 16 17 18 19 10 11 12 13 14 15 16 17 18 19

faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Re: pthreads linux simulator port

Postby faisal » Tue Jan 02, 2024 11:37 pm

Added simulator ports and demo for HAL_CAN

From testhal/PTHREAD/CAN/readme.txt

*****************************************************************************
** ChibiOS/RT port for x86 into a Posix process **
*****************************************************************************

** TARGET **

The demo runs under Linux with socketCAN support as an application program.

** The Demo **

The demo sniffs the socketCAN bus and prints out the CAN frames.
It also transmits CAN frames with incrementing data every second with ID
0x80085.

This demonstrates the usage of the simulator CAN driver.

** Build Procedure **

The demo was built using GCC.

** Sample output **

$ # Create virtual CAN device (vcan0)
$ sudo ip link add type vcan vcan0
$ # Activate vcan0 device
$ sudo ip link set dev vcan0 up
$ # generate CAN frames periodically and put it in the background
$ cangen vcan0 &
$ ./build/ch vcan0
ChibiOS/RT simulator (Linux)

(002.826174) 000001CF#6FBBD96BFD02E84D
(003.028118) 000002BC#95853C
(003.228187) 000002B7#7E
(003.428095) 000003E7#7AE1C05AB8F3B04D
(003.628094) 000003A7#14D70A
(003.828155) 000005B3#F8
(004.029206) 0000039F#D020272D46CE0F42
(004.229184) 00000505#F82E4B32D5AEA9
(004.429135) 00000094#D2B1911651B372
(004.629132) 0000055B#A1676D5D126010
(004.829096) 00000168#FDD12000509DFC7C
(005.029097) 0000053F#0891AD4AE60813
(005.230131) 0000059B#709CF4744EF32A19
(005.430101) 0000031E#6814B86DBDEED878


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 5 guests