denko-piboard 0.13.2 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -0
  3. data/LICENSE +1 -1
  4. data/README.md +181 -132
  5. data/Rakefile +0 -5
  6. data/board_maps/README.md +59 -0
  7. data/board_maps/le_potato.yml +89 -0
  8. data/board_maps/orange_pi_zero_2w.yml +85 -0
  9. data/board_maps/radxa_zero3.yml +88 -0
  10. data/board_maps/raspberry_pi.yml +95 -0
  11. data/board_maps/raspberry_pi5.yml +95 -0
  12. data/denko_piboard.gemspec +6 -7
  13. data/examples/digital_io/bench_out.rb +22 -0
  14. data/examples/digital_io/rotary_encoder.rb +31 -0
  15. data/examples/display/ssd1306.rb +53 -0
  16. data/examples/i2c/bitbang_aht10.rb +18 -0
  17. data/examples/i2c/bitbang_search.rb +24 -0
  18. data/examples/i2c/bitbang_ssd1306_bench.rb +29 -0
  19. data/examples/i2c/search.rb +24 -0
  20. data/examples/led/blink.rb +10 -0
  21. data/examples/led/fade.rb +22 -0
  22. data/examples/led/ws2812_bounce.rb +36 -0
  23. data/examples/motor/servo.rb +16 -0
  24. data/examples/pi_system_monitor.rb +10 -8
  25. data/examples/pulse_io/buzzer.rb +34 -0
  26. data/examples/pulse_io/infrared.rb +25 -0
  27. data/examples/sensor/aht10.rb +17 -0
  28. data/examples/sensor/dht.rb +24 -0
  29. data/examples/sensor/ds18b20.rb +59 -0
  30. data/examples/sensor/hcsr04.rb +16 -0
  31. data/examples/sensor/neat_tph_readings.rb +32 -0
  32. data/examples/spi/bb_loopback.rb +31 -0
  33. data/examples/spi/loopback.rb +37 -0
  34. data/examples/spi/output_register.rb +38 -0
  35. data/lib/denko/piboard.rb +10 -2
  36. data/lib/denko/piboard_base.rb +21 -63
  37. data/lib/denko/piboard_core.rb +150 -130
  38. data/lib/denko/piboard_core_optimize_lookup.rb +31 -0
  39. data/lib/denko/piboard_hardware_pwm.rb +32 -0
  40. data/lib/denko/piboard_i2c.rb +59 -82
  41. data/lib/denko/piboard_i2c_bb.rb +48 -0
  42. data/lib/denko/piboard_infrared.rb +7 -44
  43. data/lib/denko/piboard_led_array.rb +9 -0
  44. data/lib/denko/piboard_map.rb +125 -38
  45. data/lib/denko/piboard_one_wire.rb +42 -0
  46. data/lib/denko/piboard_pulse.rb +11 -68
  47. data/lib/denko/piboard_spi.rb +47 -73
  48. data/lib/denko/piboard_spi_bb.rb +41 -0
  49. data/lib/denko/piboard_tone.rb +15 -26
  50. data/lib/denko/piboard_version.rb +1 -1
  51. data/scripts/99-denko.rules +9 -0
  52. data/scripts/set_permissions.rb +131 -0
  53. metadata +48 -21
  54. data/ext/gpiod/extconf.rb +0 -9
  55. data/ext/gpiod/gpiod.c +0 -179
  56. data/lib/denko/piboard_servo.rb +0 -18
  57. data/lib/gpiod.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bad42203be8c858270403900ba28c58f7a63eaf616151975e7b5d61a471a472
4
- data.tar.gz: ad86f14e2d91a644ea1ccc8f7d599b4bbfa095d55b5f07fc734dd741447811ea
3
+ metadata.gz: 1e8e869c561c0673bcd8cafbef836a7e0c8fddb369a0b97a0c60a67a99380faf
4
+ data.tar.gz: 7f84e27065a552ad844c0483c8a5ff23e05fd7a4c2a7a8c5be2c92e99bfe9862
5
5
  SHA512:
6
- metadata.gz: aceaac910ced26a348b5a22da0d3729c6cf0b91c66c7bac417d0a96877b531407a2ff56e7fa1aa1c30cde3b8709eea7d9a44b2d07cd1628f315591b0c7477594
7
- data.tar.gz: ea5a92edc491447c75417b3e9787fcd785a86e7f15e27c18b92e97b4969ff30dcb1c913bc4fc10b81c09ecbe079859b7a5cd10d9cd744865680d1fd643b842fe
6
+ metadata.gz: aa4600cdd0cd83070610255636ae318a4a3eff654fc7fce96097558a0d4bd673b30a6e206aadba7f26010a17e1aa3615b11d1f3735a4aef2096024bc9409d687
7
+ data.tar.gz: d9d8d876321f87d4bc47c91913e91159d094a7558353750f16afce38fabe6b5ff50c620cb571c588c715af9dba6f7d469fcc9c665427d488f3f11e85c55c8792
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in denko.gemspec
4
+ gemspec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 denko-rb
3
+ Copyright (c) 2024 denko-rb
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,33 +1,22 @@
1
- # denko-piboard 0.13.1
1
+ # denko-piboard
2
2
 
3
- ### Raspberry Pi GPIO in Ruby
3
+ Use Linux single-board-computer GPIO in Ruby.
4
4
 
5
- This gem adds support for the Raspberry Pi GPIO interface to the [`denko`](https://github.com/denko-rb/denko) gem. Unlike the main gem, which requires an external microcontroller, this lets you to connect peripherals directly to the Pi.
6
-
7
- `Denko::PiBoard` is a drop-in replacement for `Denko::Board`, which would represent a connected micrcontroller. Everything maps to the Pi's built-in GPIO pins instead, and Ruby runs on the Pi itself.
8
-
9
- **Note:** This is not for the Raspberry Pi Pico (W) / RP2040. That microcontroller works with the main gem.
10
-
11
- ## Example
12
5
  ```ruby
6
+ # Turn LED on when a button is held down, off when released.
13
7
  require 'denko/piboard'
14
8
 
15
- # Board instance for the Pi.
16
- board = Denko::PiBoard.new
17
-
18
- # LED connected to GPIO4.
19
- led = Denko::LED.new(board: board, pin: 4)
9
+ board = Denko::PiBoard.new
10
+ led = Denko::LED.new(board: board, pin: 260)
11
+ button = Denko::DigitalIO::Button.new(board: board, pin: 259, mode: :input_pullup)
20
12
 
21
- # Momentary button connected to GPIO17, using internal pullup.
22
- button = Denko::DigitalIO::Button.new(board: board, pin: 17, pullup: true)
23
-
24
- # Callback runs when button is down (0)
13
+ # Callback fires when button is down (0)
25
14
  button.down do
26
15
  puts "Button down"
27
16
  led.on
28
17
  end
29
18
 
30
- # Callback runs when button is up (1)
19
+ # Callback fires when button is up (1)
31
20
  button.up do
32
21
  puts "Button up"
33
22
  led.off
@@ -37,145 +26,205 @@ end
37
26
  sleep
38
27
  ```
39
28
 
40
- #### More Examples
41
- Pi-specific examples are in this gem's [examples](examples) folder, but examples from the [main gem](https://github.com/denko-rb/denko/tree/master/examples) can be modified to work on the Pi:
42
-
43
- 1. Replace setup code:
44
- ```ruby
45
- # Replace this:
46
- require 'bundler/setup'
47
- require 'denko'
48
- # With this:
49
- require 'denko/piboard'
50
-
51
- # Replace this:
52
- connection = Denko::Connection::Serial.new()
53
- board = Denko::Board.new()
54
- # With this:
55
- board = Denko::PiBoard.new
56
- ```
57
-
58
- 2. Update GPIO/pin numbers as needed. Raspberry Pi pinouts can be found [here](https://pinout.xyz/).
59
-
60
- **Note:** Not all features from all examples are implemented yet, nor can be implemented. See [Features](#features) below.
29
+ ## Linux GPIO Features
30
+ - [x] Internal Pull-Up/Down
31
+ - [x] Open Drain/Source
32
+ - [x] Digital Read/Write
33
+ - [x] Digital Listen (Alerts)
34
+ - Interrupt driven, unlike `denko` microcontroller implementation 1ms polling
35
+ - Built in software debounce (1us by default)
36
+ - Alerts are read from a FIFO queue up to 65,536. Oldest alerts are lost first.
37
+ - [ ] Analog Read (ADC)
38
+ - [ ] Analog Write (DAC)
39
+ - [x] Software PWM Out (any pin)
40
+ - [x] Hardware PWM Out (specific pins per board)
41
+ - [x] Tone Out (via Software PWM or Hardware PWM)
42
+ - [x] Servo (via Hardware PWM)
43
+ - [x] Infrared Out (via Hardware PWM)
44
+ - [x] Hardware I2C
45
+ - [x] Multiple interfaces
46
+ - [x] Hardware SPI
47
+ - [x] Multiple interfaces
48
+ - [x] WS2812 addressable LED via SPI MOSI
49
+ - [ ] SPI Listeners from `denko`
50
+ - [ ] UART
51
+ - [x] Ultrasonic Input (for HC-SR04 and similar)
52
+ - [x] Pulse Sequence Input (for DHT enviro sensors and similar)
53
+ - [x] Bit-Bang I2C
54
+ - [x] Bit-Bang SPI
55
+ - [x] Bit-Bang 1-Wire
61
56
 
62
57
  ## Support
63
58
 
64
59
  #### Hardware
65
60
 
66
- :green_heart: Support verified
67
- :question: Should work, but not verified
61
+ In theory, this should work on any SBC, running Linux, with drivers for the relevant GPIO/I2C/SPI/PWM subsystems available on the system-on-chip (SoC). This is just a list of chips and products confirmed working.
62
+
63
+ :green_heart: Known working
64
+ :heart: Awaiting testing
65
+ :question: Might work. No hardware
68
66
 
69
- | Chip | Status | Products | Notes |
70
- | :-------- | :------: | :--------------- |------ |
71
- | BCM2835 | :green_heart: | Pi 1, Pi Zero (W) |
72
- | BCM2836/7 | :question: | Pi 2 |
73
- | BCM2837A0/B0 | :green_heart: | Pi 3 |
74
- | BCM2711 | :green_heart: | Pi 4, Pi 400 |
75
- | BCM2710A1 | :question: | Pi Zero 2W |
67
+ | Chip | Status | Products | Notes |
68
+ | :-------- | :------: | :---------------------- |------ |
69
+ | Allwinner H618 | :green_heart: | Orange Pi Zero 2W | DietPi
70
+ | Rockchip RK3566 | :green_heart: | Radxa Zero 3W/E, Radxa Rock 3C | Armbian
71
+ | BCM2835 | :green_heart: | Raspberry Pi 1, Raspberry Pi Zero (W) | Raspberry Pi OS
72
+ | BCM2836/7 | :question: | Raspberry Pi 2 |
73
+ | BCM2837A0/B0 | :green_heart: | Raspberry Pi 3 | Raspberry Pi OS
74
+ | BCM2711 | :green_heart: | Raspberry Pi 4, Raspberry Pi 400 | Raspberry Pi OS
75
+ | BCM2710A1 | :green_heart: | Raspberry Pi Zero 2W | Raspberry Pi OS
76
+ | BCM2712 | :question: | Raspberry Pi 5 |
77
+ | AML-S905X-CC | :green_heart: | Libre Computer Le Potato | Libre Computer Debian 12
76
78
 
77
79
  #### Software
78
80
 
79
81
  - Operating Systems:
80
- - Raspberry Pi OS
81
- - DietPi
82
-
83
- Note: Both with kernel version 6.1 or higher.
82
+ - DietPi (Bookworm)
83
+ - Armbian (Bookworm)
84
+ - Raspberry Pi OS (Bookworm)
84
85
 
85
86
  - Rubies:
86
- - Ruby 2.7.4 (system Ruby on some Raspberry Pi OS installs)
87
- - Ruby 3.2.2 (with and without YJIT)
88
- - TruffleRuby 22.3.1 :man_shrugging: (Not available on ARMv6 Pis: Zero W, Pi 1. Not recommended in general)
87
+ - Ruby 3.3.5 +YJIT
88
+
89
+ ## Install
90
+
91
+ #### Install the lg C library
92
+ ```console
93
+ # Requirements to install lgpio C
94
+ sudo apt install swig python3-dev python3-setuptools gcc make
95
+
96
+ # Temporary fork of: wget https://github.com/joan2937/lg
97
+ wget https://github.com/vickash/lg/archive/refs/heads/master.zip
98
+
99
+ # Install lgpio C
100
+ unzip master.zip
101
+ cd lg-master
102
+ make
103
+ sudo make install
104
+ ```
105
+
106
+ #### Install the denko-piboard gem
107
+ ```console
108
+ # Latest Ruby + YJIT from rbenv works best, but Ruby 3 from apt works too.
109
+ # Install system Ruby anyway to automate permissions startup script.
110
+ sudo apt install ruby ruby-dev
111
+
112
+ sudo gem install denko-piboard
113
+ ```
114
+ **Note:** `sudo` may be needed before `gem install` if using the system Ruby.
89
115
 
90
- #### Dependencies
116
+ ## Enable Hardware
91
117
 
92
- - [pigpio](https://github.com/joan2937/pigpio)
93
- - [libgpiod](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git)
94
- - [pigpio gem](https://github.com/nak1114/ruby-extension-pigpio) (Ruby bindings for pigpio)
95
- - [denko](https://github.com/denko-rb/denko) (peripheral implementations from main gem)
118
+ **Note:** These are simplified instructions for a few SBCs, to get you going quickly. denko-piboard uses standard Linux interfaces, so should work for many. See [the board maps readme](board_maps/README.md).
96
119
 
97
- ## Installation
120
+ ### Orange Pi Zero 2W
121
+ - For DietPi OS specifically
122
+ - 2 PWMs on GPIO 226 and 227 (not matching the docs) are enabled without any setup.
123
+ - Save the [default map](board_maps/orange_pi_zero_2w.yml) as `~/.denko_piboard_map.yml` on your Zero2W.
124
+ - Add/edit the lines below in `/boot/dietpiEnv.txt`, and reboot.
98
125
 
99
- #### 1. Install pigpio and libgpiod packages
100
- ```shell
101
- sudo apt install pigpio libgpiod-dev
126
+ ```
127
+ # /dev/i2c-3 (I2C3) @ 100 kHz
128
+ # /dev/spidev1.0 (SPI1) with first chip select (CS0) enabled
129
+ overlay_prefix=sun50i-h616
130
+ overlays=i2c1-pi spidev1_0
102
131
  ```
103
132
 
104
- #### 2. Install denko-piboard gem
105
- ```shell
106
- gem install denko-piboard
133
+ ### Radxa Zero3W/E
134
+ - For Armbian OS specifically. Newer and performs better than latest Radxa OS or DietPi available.
135
+ - Unfortunately, Armbian does not package all device tree overlays for the RK3566. I built binaries for this default config, on kernel `6.1.75-vendor-rk35xx`, and made them available [here](https://github.com/vickash/linux-sbc-overlays/tree/master/radxa/rockchip). To use, download the `.dtbo` files into `/boot/dtb/rockchip/overlay` on your Zero3. Make sure your kernel version matches.
136
+
137
+ If you rather build the overlays yourself, that repo contains the script too. On the Zero3:
138
+ ```console
139
+ sudo apt install device-tree-compiler
140
+ ruby rk3568_denko_overlay_install.rb
141
+ ```
142
+
143
+ - Save the [default map](board_maps/radxa_zero3.yml) as `~/.denko_piboard_map.yml` on your Zero3.
144
+ - Add/edit the lines below in `/boot/armbianEnv.txt`, and reboot.
145
+
146
+ ```
147
+ # /dev/i2c-3 (I2C3) @ 100 kHz
148
+ # /dev/spidev3.0 (SPI3) with first chip select (CS0) enabled
149
+ # 2 PWMs on GPIO 105 and 106
150
+ overlay_prefix=rk3568
151
+ overlays=i2c3-m0 spi3-m1-cs0-spidev pwm8-m0 pwm9-m0
107
152
  ```
108
- This automatically installs dependency gems: `denko` and `pigpio`.
109
153
 
110
- **Note:** `sudo` may be needed before `gem install` if using the Pi's system ruby.
154
+ **Note:** The Radxa docs are missing `I2C3_SDA_M0` and `I2C3_SCL_M0` in the function columns for GPIOs 32 and 33 respectively. This is an error. I2C3 works on these pins.
111
155
 
112
- ## Pi Setup
156
+ ### Raspberry Pi 4 and Lower
157
+ - For Raspberry Pi OS specifically
158
+ - Save the [default map](board_maps/raspberry_pi.yml) as `~/.denko_piboard_map.yml` on your Raspberry Pi.
159
+ - Add the lines below to `/boot/config.txt`, and reboot.
113
160
 
114
- #### pigpiod
115
- The `pigpio` package installs `pigpiod`, which must run in the background (as root) for Ruby scripts to work. You should only need to start it once per boot. Automate it, or start manually with:
116
- ```shell
117
- sudo pigpiod -s 10
118
161
  ```
119
- **Note:** `-s 10` sets tick interval to 10 microseconds, lowering CPU use. Valid values are: 1, 2, 4, 5, 8, 10 (5 default).
162
+ # 2 PWMS on GPIO 12 and 13
163
+ dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4
164
+ # /dev/i2c-1 (I2C1) @ 400 kHz
165
+ dtparam=i2c_arm=on
166
+ dtparam=i2c_arm_baudrate=400000
167
+ # /dev/spidev0.0 (SPI0) with first chip select (CS0) enabled
168
+ dtoverlay=spi0-1cs
169
+ ```
120
170
 
121
- #### libgpiod
122
- Depending on your Pi and OS, `libgpiod` may limit GPIO access. If this happens, some scripts will fail. It is only used for digital read/write operations, so test with a simple script like blinking an LED. To get permission, add your user account to the `gpio` group:
171
+ ### Raspberry Pi 5
172
+ - Instructions are same as Pi <= 4, but use the [Pi 5 map](board_maps/raspberry_pi5.yml) instead.
173
+ - NOTE: This is based on Raspberry Pi docs, but untested in hardware. If you notice anything that should be fixed, please open a PR.
174
+
175
+ ### Libre Computer Le Potato
176
+ - Use [Libre Computer Debian 12](https://distro.libre.computer/ci/debian/12/debian-12-base-arm64%2Baml-s905x-cc.img.xz)
177
+ - Save the [default map](board_maps/le_potato.yml) as `~/.denko_piboard_map.yml` on your Le Potato.
178
+ - Fix invalid signatures for apt:
179
+ ```console
180
+ wget https://deb.libre.computer/repo/pool/main/libr/libretech-keyring/libretech-keyring_2024.05.19_all.deb
181
+ sudo dpkg -i libretech-keyring_2024.05.19_all.deb
182
+ sudo apt update
183
+ ```
184
+ - Install the Libre Computer Wiring Tool:
185
+ ```console
186
+ sudo apt install libretech-gpio libretech-dtoverlay
123
187
  ```
124
- sudo usermod -a -G gpio $(whoami)
188
+ - Enable the default overlays for PWM, I2C and SPI:
189
+ ```console
190
+ # 1 PWM on GPIO 95
191
+ # 1 I2C (/dev/i2c-0) on GPIO 4 (SCL) and GPIO 5 (SDA)
192
+ # 1 SPI (/dev/spidev0.0) on GPIO 90 (CLK), GPIO 87 (MOSI), GPIO 88 (MISO), GPIO 98 (CS)
193
+ sudo ldto merge pwm-e i2c-ao spicc spicc-spidev
125
194
  ```
195
+ - Reboot to enable overlays
126
196
 
127
- #### Enable Features
128
- I2C, SPI and the hardware UART may be disabled on the Pi by default. Enable them with the built-in utility:
129
- ```shell
130
- # On Raspberry Pi OS:
131
- sudo raspi-config
197
+ ## Get Permissions
198
+ By default, only the Linux `root` user can use GPIO / I2C / SPI / PWM. If you have a default board map at `~/.denko_piboard_map.yml`, save [this script](scripts/set_permissions.rb) to your SBC, then run it:
132
199
 
133
- # On DietPi:
134
- sudo dietpi-config
200
+ ```console
201
+ ruby set_permissions.rb
135
202
  ```
136
- Select "Interfacing Options" (Raspberry Pi OS), or "Advanced Options" (DietPi) and enable features as needed.
137
-
138
- ## Features
139
-
140
- ### Already Implemented
141
- - Internal Pull Down/Up
142
- - Digital Out
143
- - Digital In
144
- - Listeners are polled in a thread, similar to a microcontroller, but always at 1ms.
145
- - `pigpio` supports even faster polling (1-10 microseconds), but events are not received in a consistent order across pins. Won't work for MultiPin components, but may implement for SinglePin.
146
- - PWM Out
147
- - Servo
148
- - Tone Out
149
- - Infrared Out
150
- - DHT Class Temperature + Humidity Sensors
151
- - I2C
152
- - Always uses I2C1 interface.
153
- - Must enable before use. Instructions [here](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c).
154
- - I2C clock cannot be set dynamically. It is set at boot time from `/boot/config.txt`. Default is 100 kHz. 400 kHz is recommended if higher data rate is needed, eg. using OLED screen. Instructions [here](https://www.raspberrypi-spy.co.uk/2018/02/change-raspberry-pi-i2c-bus-speed/).
155
-
156
- ### Partially Implemented
157
- - SPI
158
- - Always uses SPI1 interface.
159
- - Must enable before use. Instructions [here](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-spi).
160
- - Does not bind CE pins according to GPIO pinout. Any pin can be used for chip enable.
161
- - SPI modes 1 and 3 may not work.
162
- - No listeners yet.
163
-
164
- ### Feature Exclusivity
165
- - PWM Out / Servo Out
166
- - Using either of these on **any** pin disables the Pi's PCM audio output globally.
167
-
168
- - Tone Out / Infrared Out
169
- - Both of these use pigpio's wave interface, which only has a single instance. Calling either on **any** pin automatically stops any running instance that exists. Both features can co-exist in a script, but cannot happen at the same time.
170
-
171
- ### To Be Implemented
172
- - OneWire
173
- - Hardware UART
174
- - BitBang I2C
175
- - BitBang SPI
176
- - BitBang UART
177
- - WS2812
178
-
179
- ### Incompatible
180
- - EEPROM (Use the filesystem for persistence instead)
181
- - Analog IO (No analog pins on Raspberry Pi. Use ADC or DAC over I2C or SPI)
203
+
204
+ It will load load the default board map, then:
205
+ - Create any necessary Linux groups
206
+ - Add your user to the relevant groups
207
+ - Change ownership and permissions for devices in the map, so you can read/write them
208
+
209
+ **Note:** `sudo` is required.
210
+
211
+ **Note:** If you automate this script to run at boot (recommended), it will run as root. Set the `USERNAME` constant to your Linux user's name as a String literal. This ensures the map loads from your home, and changes are applied to your user, not root.
212
+
213
+ ## Modifying Examples From Main Gem
214
+ Some [examples](examples) are included in this gem, but the [main denko gem](https://github.com/denko-rb/denko/tree/master/examples) is more comprehensive. Those are written for connected microcontrollers, `Denko::Board`, but can be modified for `Denko::PiBoard`:
215
+
216
+ 1. Replace setup code:
217
+ ```ruby
218
+ # Replace this:
219
+ require 'bundler/setup'
220
+ require 'denko'
221
+ # With this:
222
+ require 'denko/piboard'
223
+
224
+ # Replace this:
225
+ board = Denko::Board.new(Denko::Connection::Serial.new)
226
+ # With this:
227
+ board = Denko::PiBoard.new
228
+ ```
229
+
230
+ 2. Change pin numbers and I2C/SPI device indices as needed.
data/Rakefile CHANGED
@@ -1,5 +0,0 @@
1
- require "rake/extensiontask"
2
-
3
- Rake::ExtensionTask.new "gpiod" do |ext|
4
- ext.lib_dir = "lib/gpiod"
5
- end
@@ -0,0 +1,59 @@
1
+ # Creating Custom Board Maps
2
+
3
+ You will need:
4
+ - Your SBC, running a recent (5.10 kernel or later) Linux distribution
5
+ - A working understanding of how to enable/disable device tree overlays in Linux
6
+ - Documentation for your board, specifically:
7
+ - Its pinout, with "friendly" GPIO numbers
8
+ - Which GPIO numbers correspond to which device tree overlays
9
+
10
+ ## Overlays
11
+
12
+ The physical pins on your SBC can internally connect to diffent hardware devices within the SoC. The default is regular GPIO. To use hardware PWM, I2C and SPI, Linux device tree overlays tell the kernel to reserve pins, disconnected them from GPIO, and connect them to the other hardware instead.
13
+
14
+ This happens at boot, so there are side-effects:
15
+ - Pins reserved by I2C or SPI can't be used for GPIO, unless the interface is disabled, and the SBC rebooted.
16
+ - With some asbraction tricks, a PWM can work as a regular GPIO, but **for digital output only**, and it's slower.
17
+ - I2C frequency can only be changed at boot time, not on a per-transmission basis.
18
+
19
+ There are a couple other issues too:
20
+ - One SoC may have GPIOs on multiple `/dev/gpiochip*`s, with non-unque line numbers.
21
+ - PWM is called by its `pwmchip*` and `pwm*` channel, not GPIO number.
22
+ - I2C and SPI are called by index (N) from `/dev/i2c-N` or `/dev/spidev-N.0`, not GPIO numbers.
23
+
24
+ To deal with this complexity, and standardize the user interface, denko-piboard uses board maps.
25
+
26
+ ## Board Maps
27
+
28
+ A board map is a YAML file, defining all the GPIO, PWM, I2C and SPI resources enabled on the SBC.
29
+
30
+ It follows these conventons:
31
+ - `Denko::PiBoard.new` will raise unless it finds a board map
32
+ - It accepts a board map file path as its only argument
33
+ - If that isn't given, it looks for `.denko_piboard_map.yml`, in the user's home directory.
34
+ - In the map, individual GPIOs are referred to (keyed by) their "friendly" or human-readable" numbers
35
+ - Theoretically arbitrary, but rule of thumb is: "unique numbers from the SBC's documentation"
36
+ - These are NOT physical pin numbers from the pinout
37
+ - These might match, but are NOT necessarily `/dev/gpiochip*` line numbers. Those can be non-unique, if multiple gpiochips.
38
+ - Each GPIO number declares its:
39
+ - Linux gpiochip
40
+ - Line on that gpiochip
41
+ - Physical number on the SBC's pinout (optional)
42
+ - Each PWM declares:
43
+ - Which GPIO number it reserves when enabled (this is its key)
44
+ - Its Linux pwmchip
45
+ - Its pwm channel on that chip
46
+ - Each I2C or SPI interface declares:
47
+ - Its index, N from `/dev/i2c-N` or `/dev/spidev-N.0` (this is its key)
48
+ - All the GPIO numbers it reserves, keyed to their names. Eg. `miso:`, `sda:` etc.
49
+
50
+ This information enables denko-piboard to:
51
+ - Refer to GPIOs by a single, user-friendly, unique number (rather than a tuple of gpiochip and line)
52
+ - Refer to PWMs by the GPIO number they multiplex with (rather than a tuple of pwmchip and channel)
53
+ - Refer to I2C and SPI by their Linux device indices
54
+ - Raise errors if you try to use any reserved pin. This fails silently otherwise, which is confusing.
55
+
56
+ There are pre-made maps for some boards [here](./), which can be followed as templates. In general, these enable:
57
+ - 2 PWM pins
58
+ - 1 I2C interface (on physical pins 3,5 when possible)
59
+ - 1 SPI interface (on physical pins 19,21,23 when possible)
@@ -0,0 +1,89 @@
1
+ #
2
+ # Tested on:
3
+ # - Libre Computer Le Potato AML-S905X-CC, using Libre Computing's official Debian 12 (2024-01-25), with kernel 6.1.92-15907-gf36fd2695db3
4
+ #
5
+ # Based on https://docs.google.com/spreadsheets/d/1U3z0Gb8HUEfCIMkvqzmhMpJfzRqjPXq7mFLC-hvbKlE
6
+ # Uses "Linux" GPIO numbers from that table
7
+ #
8
+ ---
9
+ pins:
10
+ # Left side (odd numbered physical pins)
11
+ # PIN 1 is 3V3
12
+ 5: { phy: 3, chip: 0, line: 5 }
13
+ 4: { phy: 5, chip: 0, line: 4 }
14
+ 98: { phy: 7, chip: 1, line: 98 }
15
+ # PIN 9 is GND
16
+ 8: { phy: 11, chip: 0, line: 8 }
17
+ 9: { phy: 13, chip: 0, line: 9 }
18
+ 10: { phy: 15, chip: 0, line: 10 }
19
+ # PIN 17 is 3V3
20
+ 87: { phy: 19, chip: 1, line: 87 }
21
+ 88: { phy: 21, chip: 1, line: 88 }
22
+ 90: { phy: 23, chip: 1, line: 90 }
23
+ # PIN 25 is GND
24
+ 75: { phy: 27, chip: 1, line: 75 }
25
+ 96: { phy: 29, chip: 1, line: 96 }
26
+ 97: { phy: 31, chip: 1, line: 97 }
27
+ 85: { phy: 33, chip: 1, line: 85 }
28
+ 86: { phy: 35, chip: 1, line: 86 }
29
+ 84: { phy: 37, chip: 1, line: 84 }
30
+ # PIN 39 is GND
31
+
32
+ # Right side (even numbered physical pins)
33
+ # PIN 2 is 5V
34
+ # PIN 4 is 5V
35
+ # PIN 6 is GND
36
+ 91: { phy: 8, chip: 1, line: 91 }
37
+ 92: { phy: 10, chip: 1, line: 92 }
38
+ 6: { phy: 12, chip: 0, line: 6 }
39
+ # PIN 14 is GND
40
+ 93: { phy: 16, chip: 1, line: 93 }
41
+ 94: { phy: 18, chip: 1, line: 94 }
42
+ # PIN 20 is GND
43
+ 79: { phy: 22, chip: 1, line: 79 }
44
+ 89: { phy: 24, chip: 1, line: 89 }
45
+ 80: { phy: 26, chip: 1, line: 80 }
46
+ 76: { phy: 28, chip: 1, line: 76 }
47
+ # PIN 30 is GND
48
+ 95: { phy: 32, chip: 1, line: 95 }
49
+ # PIN 34 is GND
50
+ 81: { phy: 36, chip: 1, line: 81 }
51
+ 82: { phy: 38, chip: 1, line: 82 }
52
+ 83: { phy: 40, chip: 1, line: 83 }
53
+
54
+ pwms:
55
+ # Overlay: pwm-e
56
+ # Enable on boot: sudo ldto merge pwm-e
57
+ # Device: /sys/class/pwm/pwmchip0/pwm0
58
+ #
59
+ 95:
60
+ pwmchip: 0
61
+ channel: 0
62
+
63
+ i2cs:
64
+ # Overlay: i2c-ao
65
+ # Enable on boot: sudo ldto merge i2c-ao
66
+ # Device: /dev/i2c-0
67
+ #
68
+ 0:
69
+ scl: 4
70
+ sda: 5
71
+
72
+ # Overlay: i2c-b
73
+ # Enable on boot: sudo ldto merge i2c-b
74
+ # Device: /dev/i2c-0 (if enabled alone), /dev/i2c-1 (if enabled with i2c-ao)
75
+ #
76
+ # 1:
77
+ # scl: 76
78
+ # sda: 75
79
+
80
+ spis:
81
+ # Overlays: spicc, spicc-spidev
82
+ # Enable on boot: sudo ldto merge spicc spicc-spidev
83
+ # Device: /dev/spidev0.0
84
+ #
85
+ 0:
86
+ clk: 90
87
+ mosi: 87
88
+ miso: 88
89
+ cs0: 89
@@ -0,0 +1,85 @@
1
+ #
2
+ # Tested on Orange Pi Zero 2W, running DietPi, Kernel 6.6.31-current-sunxi64
3
+ # Note: PWM channels 1 and 2 work without an overlay.
4
+ #
5
+ ---
6
+ pins:
7
+ # Left side (odd numbered)
8
+ # PIN 1 is 3V3
9
+ 264: { phy: 3, chip: 0, line: 264 }
10
+ 263: { phy: 5, chip: 0, line: 263 }
11
+ 269: { phy: 7, chip: 0, line: 269 }
12
+ # PIN 9 is GND
13
+ 226: { phy: 11, chip: 0, line: 226 }
14
+ 227: { phy: 13, chip: 0, line: 227 }
15
+ 261: { phy: 15, chip: 0, line: 261 }
16
+ # PIN 17 is 3V3
17
+ 231: { phy: 19, chip: 0, line: 231 }
18
+ 232: { phy: 21, chip: 0, line: 232 }
19
+ 230: { phy: 23, chip: 0, line: 230 }
20
+ # PIN 25 is GND
21
+ 266: { phy: 27, chip: 0, line: 266 }
22
+ 256: { phy: 29, chip: 0, line: 256 }
23
+ 271: { phy: 31, chip: 0, line: 271 }
24
+ 268: { phy: 33, chip: 0, line: 268 }
25
+ 258: { phy: 35, chip: 0, line: 258 }
26
+ 272: { phy: 37, chip: 0, line: 272 }
27
+ # PIN 39 is GND
28
+
29
+ # Right side (even numbered)
30
+ # PIN 2 is 5V
31
+ # PIN 4 is 5V
32
+ # PIN 6 is GND
33
+ 224: { phy: 8, chip: 0, line: 224 }
34
+ 225: { phy: 10, chip: 0, line: 225 }
35
+ 257: { phy: 12, chip: 0, line: 257 }
36
+ # PIN 14 is GND
37
+ 270: { phy: 16, chip: 0, line: 270 }
38
+ 228: { phy: 18, chip: 0, line: 228 }
39
+ # PIN 20 is GND
40
+ 262: { phy: 22, chip: 0, line: 262 }
41
+ 229: { phy: 24, chip: 0, line: 229 }
42
+ 233: { phy: 6, chip: 0, line: 233 }
43
+ 265: { phy: 28, chip: 0, line: 265 }
44
+ # PIN 30 is GND
45
+ 267: { phy: 32, chip: 0, line: 267 }
46
+ # PIN 34 is GND
47
+ 76: { phy: 36, chip: 0, line: 76 }
48
+ 260: { phy: 38, chip: 0, line: 260 }
49
+ 259: { phy: 40, chip: 0, line: 259 }
50
+
51
+ pwms:
52
+ #
53
+ # Works without overlays in /boot/dietpiEnv.txt
54
+ # NOTE: These DO NOT match the official Orange Pi documentation.
55
+ #
56
+ 227:
57
+ pwmchip: 0
58
+ channel: 1
59
+ 226:
60
+ pwmchip: 0
61
+ channel: 2
62
+
63
+ i2cs:
64
+ #
65
+ # Add to overlays= line in /boot/dietpiEnv.txt
66
+ # i2c1-pi
67
+ #
68
+ # To get 400kHz, save this alternate overlay to the /boot/dtb/allwinner/overlay directory:
69
+ # https://github.com/vickash/linux-sbc-overlays/blob/master/orangepi/sun50i-h616-i2c1-pi_400k.dtbo
70
+ #
71
+ # Then, in /boot/dietpiEnv.txt, use instead:
72
+ # i2c1-pi_400k
73
+ 3:
74
+ scl: 263
75
+ sda: 264
76
+
77
+ spis:
78
+ #
79
+ # Add to overlays= line in /boot/dietpiEnv.txt
80
+ # spidev1_0
81
+ 1:
82
+ clk: 230
83
+ mosi: 231
84
+ miso: 232
85
+ cs0: 229