BBB板子在手头好多天了。之前完成了GPIO的控制,I2C的读取,接下来就想尝试SPI总线的驱动。本以为是一个简简单单的任务,没想到陷进去了几天时间。没想到会遇到那么多的困难。话不多说,最终还是驱动成功,开心!
先来看看我的环境。硬件:SPI设备,一块7线SPI驱动的主控为SSD1306的0.96寸的OLED屏幕。



通过杜邦线与狗板连接:
| OLED引脚 | BeagleBone Black引脚 | 功能 |
| GND | P9_1 (GND) | 地线 |
| VCC | P9_3 (3.3V) | 电源 |
| CS | P9_17 (SPI0_CS0) | 片选 |
| DC | P9_15 (GPIO) | 数据/命令选择 |
| RST | P9_12 (GPIO) | 复位 |
| D1(MOSI) | P9_18 (SPI0_D1) | 数据输入 |
| D0(CLK) | P9_22 (SPI0_SCLK) | 时钟 |
狗板的操作系统我是安装的“am335x-eMMC-flasher-debian-12.2-iot-armhf-2023-10-07-4gb.img.xz”,之前有安装最新的操作系统,但是debian 13在设备树里居然没有spi设备,果断回退到debian 12下边。

安装依赖和luma.oled库
luma开源驱动库,用来驱动OLED还是很方便的。
# 安装必要的依赖 sudo apt install -y python3-pip python3-dev python3-venv libfreetype6-dev libjpeg-dev build-essential # 创建虚拟环境(推荐) python3 -m venv ~/oled_env --system-site-packages source ~/oled_env/bin/activate # 安装luma.oled库 pip3 install luma.oled # 安装 BeagleBone GPIO 库 pip3 install Adafruit_BBIO
编写显示例程
#!/usr/bin/env python3
from luma.core.interface.serial import spi
from luma.core.render import canvas
from luma.oled.device import ssd1306
from PIL import ImageFont
import Adafruit_BBIO.GPIO as GPIO
import time
class BBIO_GPIO:
OUT = GPIO.OUT; IN = GPIO.IN; HIGH = GPIO.HIGH; LOW = GPIO.LOW
def setup(self, pin, direction): GPIO.setup(pin, direction)
def output(self, pin, value): GPIO.output(pin, value)
serial = spi(gpio=BBIO_GPIO(), device=0, port=0,
gpio_DC="P9_15", gpio_RST="P9_12", gpio_CS=None,
bus_speed_hz=8000000)
device = ssd1306(serial, width=128, height=64)
# 显示内容
with canvas(device) as draw:
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 16)
except:
font = ImageFont.load_default()
draw.rectangle(device.bounding_box, outline="white", fill="black")
draw.text((20, 20), "Hello World!", fill="white", font=font)
draw.text((25, 45), "BeagleBone Black", fill="white")
print("显示10秒...")
time.sleep(10) # 保持显示10秒
# 可选:清屏
# device.clear()这是段使用luma来驱动oled屏幕的可用的代码。但是运行后OLED屏幕没有反应。不停地请教AI,最终找到了解决方案。修改/boot/
code env.sh oled_env (oled_env) root@BeagleBone:~# cat /boot/uEnv.txt #Docs: http://elinux.org/Beagleboard:U-boot_partitioning_layout_2.0 uname_r=5.10.168-ti-r72 #uuid= #dtb= ###U-Boot Overlays### ###Documentation: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian#U-Boot_Overlays ###Master Enable enable_uboot_overlays=1 ### ###Overide capes with eeprom #uboot_overlay_addr0=<file0>.dtbo #uboot_overlay_addr1=<file1>.dtbo #uboot_overlay_addr2=<file2>.dtbo #uboot_overlay_addr3=<file3>.dtbo ### ###Additional custom capes uboot_overlay_addr4=/lib/firmware/ADAFRUIT-SPI0-00A0.dtbo #uboot_overlay_addr4=<file4>.dtbo #uboot_overlay_addr5=<file5>.dtbo #uboot_overlay_addr6=<file6>.dtbo #uboot_overlay_addr7=<file7>.dtbo ### ###Custom Cape #dtb_overlay=<file8>.dtbo ### ###Disable auto loading of virtual capes (emmc/video/wireless/adc) #disable_uboot_overlay_emmc=1 #disable_uboot_overlay_video=1 #disable_uboot_overlay_audio=1 #disable_uboot_overlay_wireless=1 #disable_uboot_overlay_adc=1 ### ###Cape Universal Enable #enable_uboot_cape_universal=1 ### ###Debug: disable uboot autoload of Cape #disable_uboot_overlay_addr0=1 #disable_uboot_overlay_addr1=1 #disable_uboot_overlay_addr2=1 #disable_uboot_overlay_addr3=1 ### ###U-Boot fdt tweaks... (60000 = 384KB) #uboot_fdt_buffer=0x60000 ###U-Boot Overlays### console=ttyS0,115200n8 cmdline=coherent_pool=1M net.ifnames=0 lpj=1990656 rng_core.default_quality=100 quiet #In the event of edid real failures, uncomment this next line: #cmdline=coherent_pool=1M net.ifnames=0 lpj=1990656 rng_core.default_quality=100 quiet video=HDMI-A-1:1024x768@60e #Use an overlayfs on top of a read-only root filesystem: #cmdline=coherent_pool=1M net.ifnames=0 lpj=1990656 rng_core.default_quality=100 quiet overlayroot=tmpfs ##enable Generic eMMC Flasher: #cmdline=init=/usr/sbin/init-beagle-flasher
这里边修改了两处,1 注释掉“enable_uboot_cape_universal”。2 添加SPI驱动“uboot_overlay_addr4=/lib/firmware/ADAFRUIT-SPI0-00A0.dtbo”。然后重启系统。
OLED驱动成功
为了图方便,所有操作都是在超级用户下运行的。



后记:不得不说现在的AI超级厉害,在使用SPI驱动OLED过程中,所有的问题都有请教AI,在AI的指导下,有做过每个管脚的高低电平驱动验证,有做过OLED屏幕好坏的验证,最终找到系统驱动包加载的问题,一步步解决了困难,最终驱动起来OLED屏幕。
我要赚赏金
