Skip to content

Commit 2ce744d

Browse files
Merge pull request #201 from OnionIoT/content/spi-guide
Updated SPI w/ Python guide with step to choose between hw and sw SPI
2 parents 8e34609 + 17e8318 commit 2ce744d

File tree

5 files changed

+145
-15
lines changed

5 files changed

+145
-15
lines changed

docs/device-tree-overlay/software-spi.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ Users that need to use different pins can create their own customized version ba
2020

2121
:::caution **Drawback: Lower Bus Speed**
2222

23-
Since this is a software-based bus, it will not be as fast as a hardware SPI bus. This is adequate for most SPI devices, but it is not recommended for data-intensive use cases like driving a display.
23+
Since this is a software-based bus, it will not be as fast as a hardware SPI bus. The maximum bus speed is **1.4MHz** if CPU load is low - much slower compared the 40MHz maximum of the hardware SPI bus.
24+
25+
This is adequate for most SPI devices, but it is not recommended for data-intensive use cases like driving a display.
2426

2527
:::
2628

21.8 KB
Loading
25.7 KB
Loading

docs/guides/hardware-interfaces/using-spi-with-python.md

Lines changed: 139 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Using the SPI bus with Python
2+
title: Using SPI with Python
33
---
44

55
import { GiscusDocComment } from '/src/components/GiscusComment';
@@ -35,27 +35,91 @@ opkg update
3535
opkg install python3-light python3-spidev
3636
```
3737

38-
<!-- TODO: add a step where the user chooses between harware SPI or the sw spi? -->
38+
## Step 2: Choose Between Hardware or Software SPI
3939

40-
## Step 2: Physically Connect an SPI Device
40+
The Omega2 supports both **hardware SPI** (using a built-in controller) and **software SPI** (bit-banging GPIOs). Before proceeding, choose the SPI bus that best suits your needs.
41+
42+
### Comparison: Hardware SPI vs. Software SPI on the Omega2
43+
| Feature | Hardware SPI (CS1) | Software SPI |
44+
|-------------------------|--------------------------------------|--------------|
45+
| **Speed** | Up to **40MHz** (fast) | Up to **1.4MHz** depending on CPU load - Slower, best for low-data applications |
46+
| **Full-Duplex Support** | ❌ No | ✅ Yes |
47+
| **SPI Configuration** | Limited; CS0 is reserved for flash | Flexible; can be configured as needed |
48+
| **GPIO Flexibility** | Fixed SPI pins (see next step) | Choose from two predefined GPIO sets |
49+
| **System Load** | Low (uses dedicated SPI controller) | Higher (CPU handles SPI signals) |
50+
51+
<!-- TODO: see if chart from docs/hardware-interfaces/spi.md can be reused -->
52+
53+
### Option 1: Using the Hardware SPI Bus (CS1)
54+
The **hardware SPI bus** is pre-configured on the Omega2 and is accessed through `/dev/spidev0.1`. It supports **high-speed** SPI communication but is limited to **half-duplex mode** and uses **the fixed SPI pins**:
55+
- GPIO6
56+
- SPI_CLK pin
57+
- SPI_MOSI pin
58+
- SPI_MISO pin
59+
60+
If this setup meets your needs, **proceed to [Step 3](#step-3-physically-connect-an-spi-device)** to connect SPI devices. **For most users, this is the quickest option.**
61+
62+
63+
### Option 2: Enabling a Software SPI Bus
64+
If you need **full-duplex SPI**, additional SPI buses, or different GPIO assignments, you can enable a **software SPI bus** by installing one of two available **Software SPI Device Tree Overlay packages**.
65+
66+
67+
#### Enabling a Software SPI Bus
68+
To enable a software SPI bus, install one of the following overlay packages:
69+
70+
**For an SPI bus on GPIOs 14, 15, 16, and 17:**
71+
```
72+
opkg update
73+
opkg install onion-dt-overlay-sw-spi
74+
```
75+
76+
**For an SPI bus on GPIOs 0, 1, 2, and 3:**
77+
```
78+
opkg update
79+
opkg install onion-dt-overlay-sw-spi-alt
80+
```
81+
82+
After installation, the software SPI bus will be accessible at `/dev/spidev1.0`. Note this down for the next steps.
83+
84+
:::tip
85+
86+
Each package predefines the GPIOs that function as SPI signals. For more details, see the [Software SPI Bus article in the Device Tree Overlay chapter](/device-tree-overlay/software-spi).
87+
88+
:::
89+
90+
---
91+
92+
**Next Step:** Once you've chosen your SPI bus, proceed to [Step 3](#step-3-physically-connect-an-spi-device).
93+
94+
95+
## Step 3: Physically Connect an SPI Device
4196

4297
Before we can send data, we need to connect an SPI device to the Omega2's SPI bus.
4398

4499
### Pin Connections
45100

46-
Connect the SPI pins of your external device to the Omega2 according to this table:
101+
If you decided to use **hardware SPI** in Step 2, connect the SPI pins of your external device to the Omega2 according to this table:
47102

48-
| SPI Signal | Omega2 Pin |
103+
| SPI Signal | Omega2 Pin |
49104
|-------------|------------------|
50105
| Clock / SCK | SPI_CLK / GPIO7 |
51106
| MOSI | SPI_MOSI / GPIO8 |
52107
| MISO | SPI_MISO / GPIO9 |
53108
| CS | SPI_CS1 / GPIO6 |
54109

110+
If you decided to use **software SPI** in Step 2, connect the SPI pins of the external device according to the table below:
111+
112+
| SPI Signal | Omega2 Pin when using `onion-dt-overlay-sw-spi` | Omega2 Pin when using `onion-dt-overlay-sw-spi-alt` |
113+
|-------------|---------|--------|
114+
| Clock / SCK | GPIO14 | GPIO3 |
115+
| MOSI | GPIO16 | GPIO1 |
116+
| MISO | GPIO15 | GPIO0 |
117+
| CS | GPIO17 | GPIO2 |
118+
55119

56120
Additionally, make sure the SPI device is supplied with power according to its specifications.
57121

58-
## Step 3: SPI Hello World - Write Bytes to the Device
122+
## Step 4: SPI Hello World - Write Bytes to the Device
59123

60124
To confirm that the SPI bus is working, we'll run a Python program that **writes 256 bytes of incremental data** over SPI in eight cycles.
61125

@@ -69,6 +133,36 @@ cd /root
69133
wget https://raw.githubusercontent.com/OnionIoT/python-spidev/refs/heads/master/examples/writebytes.py
70134
```
71135

136+
### Software SPI: Change SPI bus in script
137+
138+
If you decided to use a software SPI bus in step 2, the script needs to be updated to use the correct device.
139+
140+
Use the `vi` editor to modify the change
141+
142+
```
143+
spi = spidev.SpiDev(0,1)
144+
```
145+
146+
to
147+
148+
```
149+
spi = spidev.SpiDev(1,0)
150+
```
151+
152+
:::tip Using the vi Editor
153+
154+
If you're not familiar, the `vi` text editor is included on the Omega by default. To make the change:
155+
1. Run `vi /root/writebytes.py`
156+
1. Enter insert mode by pressing `i`
157+
1. Make the changes
158+
1. Return to command mode by pressing `esc` once
159+
1. Save and close the file by typing `:wq` and pressing enter
160+
161+
Learn more about the small but powerful `vi` editor [online](https://www.redhat.com/en/blog/introduction-vi-editor).
162+
163+
:::
164+
165+
72166
### Run the Program
73167

74168
Now, let’s run the script to send data over SPI:
@@ -77,11 +171,16 @@ Now, let’s run the script to send data over SPI:
77171
python writebytes.py
78172
```
79173

80-
This will send 256 bytes of incremental data (`0x00`, `0x01`, `0x02`, ...) over SPI, repeating 8 times. If you have a logic analyzer or oscilloscope connected, you should see activity.
174+
This will send 256 bytes of incremental data (`0x00`, `0x01`, `0x02`, ...) over SPI, repeating 8 times. If you have a logic analyzer or oscilloscope connected, you should see activity:
175+
176+
![logic analyzer showing 8 SPI transmissions](./assets/spi-writebytes-0.png)
177+
*Logic analyzer showing eight blocks of SPI writes*
81178

82-
<!-- TODO: add screenhot output of logic analyzer? -->
179+
Zooming in, we can confirm the data being sent is incremental:
180+
181+
![logic analyzer showing incremental data being transmitted](./assets/spi-writebytes-1.png)
83182

84-
## Step 4: Half-Duplex SPI Transmission
183+
## Step 5: Half-Duplex SPI Transmission
85184

86185
Next, let's run a Python script that performs a half-duplex SPI transaction—meaning it writes a byte and immediately reads a specified number of bytes in return. This is useful for reading registers on an SPI device.
87186

@@ -116,6 +215,35 @@ This kind of transaction is useful when interacting with SPI devices that store
116215

117216
To exit the vi editor without saving changes, type `:q!` and press Enter.
118217

218+
### Software SPI: Change SPI bus in script
219+
220+
If you decided to use a software SPI bus in step 2, the script needs to be updated to use the correct device.
221+
222+
Use the `vi` editor to modify the change
223+
224+
```
225+
spi = spidev.SpiDev(0,1)
226+
```
227+
228+
to
229+
230+
```
231+
spi = spidev.SpiDev(1,0)
232+
```
233+
234+
:::tip Using the vi Editor
235+
236+
If you're not familiar, the `vi` text editor is included on the Omega by default. To make the change:
237+
1. Run `vi /root/xfer3.py`
238+
1. Enter insert mode by pressing `i`
239+
1. Make the changes
240+
1. Return to command mode by pressing `esc` once
241+
1. Save and close the file by typing `:wq` and pressing enter
242+
243+
Learn more about the small but powerful `vi` editor [online](https://www.redhat.com/en/blog/introduction-vi-editor).
244+
245+
:::
246+
119247
### Run the Program
120248

121249
How, execute the script:
@@ -128,14 +256,12 @@ python xfer3.py
128256
The program will display the received bytes on the terminal:
129257

130258
```
131-
root@Omega-FB94://# python xfer3.py
259+
root@Omega-FB94:~# python xfer3.py
132260
Half-duplex transmission: writing 1 byte, reading 2 bytes
133-
Read: 0x11, 0x9a
261+
Read: [4, 2]
134262
Done
135263
```
136264

137-
<!-- TODO: confirm above output -->
138-
139265
This confirms that the SPI device is responding to commands and returning data.
140266

141267

docs/hardware-interfaces/spi.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The Omega2 has one hardware SPI interface available:
4141

4242
The Omega2 does not support full-duplex SPI transmissions. This is due to a hardware bug in the underlying MT7688 SoC used in the Omega2.
4343

44+
For use cases where full-duplex SPI is a requirement, consider using a [software-based SPI bus](#software-based-spi-bus).
45+
4446
:::
4547

4648
The SPI and CS1 pins are highlighted on the Omega2/2S diagrams below.
@@ -72,7 +74,7 @@ Unlike hardware SPI, which uses a dedicated controller to handle communication,
7274

7375
| Feature | Hardware SPI (CS1) | Software SPI |
7476
|-------------------------|--------------------------------------|--------------|
75-
| **Speed** | Up to **40MHz** (fast) | Slower, best for low-data applications |
77+
| **Speed** | Up to **40MHz** (fast) | Up to **1.4MHz** - Slower, best for low-data applications |
7678
| **Full-Duplex Support** | ❌ No on Omega2 | ✅ Yes |
7779
| **SPI Configuration** | Limited; CS0 is reserved for flash | Flexible; can be configured as needed |
7880
| **GPIO Flexibility** | Fixed to SPI pins | Any GPIOs can be used |

0 commit comments

Comments
 (0)