RTK.GPIO protocol

The RTK.GPIO is a microprocessor system with a serial interface, and a RPi 40 pin GIO connector.

USB – CH340 UART – STM cpu – RPi GPIO 40 pin
– power 3.3V via voltage regulator for CH340, STM and RPi GPIO
– power 5V to RPi GPIO

The RPi is electrical identical to the RPi (all signals at 3.3V I/O).

Of the RPi connectors the following is supported:
– GPIO connector, layout identical to Raspberry
– GND, +5V, +3.3V
– GPIO 0-27 I/O set for read, write, pullup pulldown
– I2C on GPIO 2 and 3 100kHZ smbus subset
– No SPI, no ALT functions, no PWM, no edge triggered interrupts, no UART/T etc
– I2C also in separate 4 pin connector with +5V, unsure if 3.3V or 5V I2C signals
– UAR/T connector 4 pin undocumented
– PROG connector undocumented

Serial protocol (based upon studying the firmware source main.cpp version RTk-2016-01-05-FINAL)
A simple and not very robust protocol, no ACK’s, just one error message E1, no complete error checking,
version command returns string without CRLF

– ASCII character based
– The CH340 and STM communicate serial over 230400 baud, no hardware or software handshake
– no block I/O, all GPIO via separate commands
– just one error message ‘E 1 CR LF’
– all can be tested with a serial terminal emulator like Putty etc for GPIO, all is readable ASCII
– I2C commands are readable ASCII, but the data characters are interpreted as 8 bit numbers
(no software handshake allowed!)

– GPIO numbers are the numbers on the RPi I/O connector.
– I2C is on GPIO2, GPIO3 with pullup resistors

After power on
– the RTK.GPIO sends the string ‘RTk.GPIO Ready’ (no CR LF!)
– all GPIO are Digital InOut

The RTK.GPIO accepts the following commands.

A command is one character followed by one or more characters as arguments, everything is in ASCII.


main flow 

  if command char in [A..Z] then 
    read (param_char)
    do_command(command_char, param_char)
  else if command_char in [a..|] then
    gpio(command_char, gpio_char)
  else if command_char in {CR, LF)
    do nothing
    send 'E' '1' CR LF
  until forever
do_commmand(command_char, param_char)

    V : send 'RTk.GPIO Ready' (note V requires a second char, can be any!)
	G : read(gpio_char)
	    do_GPIO(param_Char, GPIO_char)

    I : do_I2C(param_char)

gpio_command(pinc, cmdch)

GPIO Commands are two characters.
  <pinch> <cmdch>
pinch is the pin character (a..) where 'a' represents pin 0 (28 I/O's)
character is converted to number (nr = pin character - 'a'
cmdch is the command to perform on that pin:
  I: Set this pin to a digital input,        eg: aI
  O: Set this pin to a digital output,       eg: aO
  0: Write a digital low to this output,     eg: a0
  1: Write a digital high to this output,    eg: a1
  U: Write a pull input high,    eg: aU
  D: Write a pull input down,    eg: aD
  N: write a pull none           eg  aN
  ?: Read the state of this digital input,   eg: a?
     State is returned as pinch + state(0|1) 
     with CR LF added                        eg: a0 CR LF

 error if pin character out of range


in i2c all data chars are interpreted as integers, 
'0' is decimal i2c address 48 for example  
(make sure software handshake is off, otherwise the serial communication eats xon/xoff)

  read(i2c command)
  if i2c command = 'W' then
    read(length of i2c datastring)
    read all i2c datastring into i2c datablock array
    i2c.write(i2caddress, i2c datablock, lenght of i2c datablock)
  else if i2c command = 'R' then
    read(length of i2c datastring)
    read(first datablock data)
    i2c read(i2c addrss, i2c datablock, i2c datablock length)
    for all in i2c datablock do
      write datablock[count] to serial


rpi_hal from https://github.com/rudiratlos/rpi-hal is an incredible rich and uptodate unit for Freepascal to use the I/O of the Raspberry.

From Freepascal programs all works as expected when run as root. Otherwise accessing /dev/mem is causing real problems.

The unit can be used from Lazarus also (avoid the test procedures with writeln’ 😉 ) but also require root access and setting cthreads in the program heading.

For X apps  running as root is not possible with just sudo.
Seems I found a solution here: https://wiki.archlinux.org/index.php/Running_GUI_applications_as_root

I applied method 2:

sudo nano /etc/profile

and added this line as last line

export XAUTHORITY=/home/pi/.Xauthority

Now i can do

$ sudo ./testinit (my simple test program with only the init line of rpi_hal)

and this worked without error, init succeeded.

If you want debugging the app in Lazarus, start Lazarus as root (and ignore all the warnings)

xhost +localhost
sudo /usr/bin/startlazarus

Original text from https://wiki.archlinux.org/index.php/Running_GUI_applications_as_root

Method 2: Globally in /etc/profile
Add the following line to /etc/profile:

export XAUTHORITY=/home/username/.Xauthority
This will permanently allow root to connect to a non-root user’s X server.

Or, merely specify a particular app:

XAUTHORITY=/home/username/.Xauthority appname
where appname is the name of the particular app. (e.g. kwrite

Arduino on Raspberry

Install Arduino on Buster

  1. Arduino 1.8.12 (or higher) from arduino.cc – Software – Arduino ARM 32 bits
  2. Download to e.g. Downloads
  3. cd /Downloads
  4. tar -x -f arduino-1.8.5-linuxarm.tar.xz
  5. cd arduino-1.8.12
  6. sudo ./install.sh
  7. and Arduino appears in Home – Programming desktop

Autostart program like minicom at console at boot

Install minicom

sudo apt-get install minicom

setup your system to auto ;login into a commandline session
[use sudo raspi-config]

then at the end of .bashrc put in

if [ $(tty) == /dev/tty1 ]; then
   minicom -s  ##change this to be the thing you want or even a script [full path to]

then reboot your rpi

Lazarus and Raspberry site


XRDP revisited

Shutdown menu not working:

You can get the Buttons working, just change the line ‘Logout=lxde-pi-shutdown-helper’ to ‘Logout=sudo lxde-pi-shutdown-helper’
in ‘/home/pi/.config/lxpanel/LXDE-pi/config’.

Shutdown Raspbian with a button

he simplest way to set up a “pushbutton to shut down” is to add “dtoverlay=gpio-shutdown” to /boot/config.txt and rig a NO momentary contact switch across pins 5 and 6. Push once to shut down the system. Push again to reboot it.


To avoid I2c being unusable:GPIO26 is physical pin 37

pi@raspberrypi:~ $ dtoverlay -h gpio-shutdown
Name:   gpio-shutdown

Info:   Initiates a shutdown when GPIO pin changes. The given GPIO pin
        is configured as an input key that generates KEY_POWER events.
        This event is handled by systemd-logind by initiating a
        shutdown. Systemd versions older than 225 need an udev rule
        enable listening to the input device:

                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"

        This overlay only handles shutdown. After shutdown, the system
        can be powered up again by driving GPIO3 low. The default
        configuration uses GPIO3 with a pullup, so if you connect a
        button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
        you get a shutdown and power-up button.

Usage:  dtoverlay=gpio-shutdown,=

Params: gpio_pin                GPIO pin to trigger on (default 3)

        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low).

        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up".

                                Note that the default pin (GPIO3) has an
                                external pullup.
The default GPIO is 3 (which appears on pin 5 of the 40-pin header). The pin is configured to pull high by default (which is also the SoC's default), and the GPIO is marked as active low (the opposite to the pull, for obvious reasons).

If you are having trouble, you can debug using raspi-gpio on Raspbian. Assuming for now that the overlay applied correctly (lsmod should show gpio-keys as being loaded), raspi-gpio get 3 will show the pin level (which should be 1) and function (INPUT). Now run it in a loop:

pi@raspberrypi:~ $ while true; do raspi-gpio get 3; done
Attach one end of a patch lead to pin 5 (GPIO3), and touch the other end on pin 6 (GND) - the Pi should shut down immediately, but if not you will see the pin level change to 0.

Ultibo installation





1. Install from distribution on ultibo.org
2. Next rebuild RTL to latest and greatest version
– download sources
– download from github
– extract in {location}
– Lazarus tools Build Ultibo

Raspberry Pi

The next stepts, including the IDE (Lazarus for Ultibo) work file from the RPi console or over a XRDP or VNC connection.

Use the ultibo installer script, initially and to update to the latest and greatest version of Ultibo on github.

1. Copy the installer script over to the Pi, in the user pi home directory.

wget https://github.com/ultibohub/Tools/releases/download/1.0.5/ultiboinstaller.sh

2. Make the script executable

chmod +X ultiboinstaller.sh

2. Install dependencies (only needed on a fresh system)
Do the next steps in superuser mode. Convenient and otherwise it fails on an XRDP session

sudo su
apt-get update
apt-get upgrade

apt-get install -y libgtk2.0-dev libcairo2-dev libpango1.0-dev libgdk-pixbuf2.0-dev libatk1.0-dev libghc-x11-dev
apt-get install -y libx11-dev libgdk-pixbuf2.0-dev  gir1.2-coglpango-1.0  xorg-dev
apt-get install -y binutils-arm-none-eabi 

3. Increase swapfile

Because the Raspberry Pi only has 1024MB of memory at most, installation on Raspbian requires a swap file size of at least 1000MB, anything less may result in an error while building Lazarus.

nano /etc/dphys-swapfile

Edit the CONF_SWAPSIZE value to be 1000 or greater

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
ls -lh /var

And check that the file “swap” is now the size specified above

5. Now run script in user mod, the ‘exit’ in step 3 takes care of that. Takes a while!

6. After completion

Change swap back to 100, see step 3, in superuser mode!

The Lazarus IDE will now show up in Programming on the desktop.

Headless setup Raspbian

From the rapsberri Pi forum written by Hawaiianpi

Most local network use WPA2-PSK (AES), and this is the procedure used here for setting up headless boot with SSH and wireless network. It works with Raspbian Jessie or Stretch, Desktop or Lite, and with the built-in WiFi on the Pi3B(+), Pi Zero W, or a Raspbian compatible USB WiFi dongle on other models. Everything is done before you boot, and can be done on a Windows or Mac computer which only has access to the small FAT32 “boot” partition of a Raspbian imaged SD card.

Grab the latest Raspbian image from https://www.raspberrypi.org/downloads/raspbian/
Grab the Etcher software from https://etcher.io/
Install Etcher and use it to write the Raspbian image to your SD card.
You don’t need to extract the image or format the card prior to writing.
Just run Etcher, choose the Raspbian .zip you downloaded, pick your SD card and write.
If you have trouble, verify the SHA256 checksum of the download.

Writing an image to your card will erase everything previously on it!

Remove and reinsert the SD card so that your Windows or Mac PC can see the small FAT32 partition on the card (labelled “boot”).
If you get a message telling you the card must be formatted, cancel it.

On that small FAT32 partition, create a file with the name ssh (or ssh.txt). It can be empty, the contents don’t matter.

To connect to a wireless network, create another file on the card called wpa_supplicant.conf, which has the following inside:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

ssid="Your network name/SSID"
psk="Your WPA/WPA2 security key"

Edit country=, ssid= and psk= with your information and save the file.
Use the 2 letter country abbreviation in CAPS (without this you will have no WiFi).
Use a pure text editor like Notepad++, not a word processor, to edit the wpa_supplicant.conf file.

Make sure that both files are in the main directory of the small FAT32 partition, not in any folder.

Safely eject the card from your PC and use it to boot the Pi.

If Raspbian finds an ssh file it will enable SSH and delete the file.

If it finds a wpa_supplicant.conf file, it will move it to its correct location and connect to your wireless network. Give your Pi some time to boot and connect to your network (the first boot always takes longer), then you should be able to SSH into the Pi and configure it how you like.

If you have a Zeroconf network service installed (Apple’s iTunes or Bonjour install Zeroconf), you can SSH into pi@raspberrypi.local (provided you don’t have any other Pi computers on your network with the same default hostname). Otherwise you must SSH into your Pi’s IP address, which you can find by logging into your router and checking the list of connected clients, or using a network scanner app (like Fing for smartphones cor Advanedc IP scanner).

To login using SSH from Windows, you can use an app called PuTTY.

In the Host Name (or IP address) field, enter either,
Code: Select all

make sure SSH is selected (it should be by default) and click on Open.

If you have done everything correctly, a terminal window will open and you should see a password request. Although, if it’s the first time you’ve connected to your Pi, you may have to answer “yes” to a security question, and then you’ll get the password prompt. After login you’ll get a warning about having SSH enabled with the default password, so type passwd[Enter] and enter a new password (twice). And now you can do whatever you need to configure your little Raspberry Pi computer.

sudo raspi-config

Will bring up the Raspbian configuration utility. If you have more than one Raspberry Pi computer on your network it’s a good idea to change the hostnames to something unique so that they can easily be identified.

Note: If you have attempted this and failed, then unplugged power to turn off your Pi, you should start over with a freshly imaged card. Improperly powering down the Pi can cause SSH key generation to fail, which will prevent SSH logins (even if everything else is correct).

Idea for a slideshow