Skip to content

Day27 - SPI Sensor Driver

Objective

  • Add SPI support to existing sensor driver
  • Bind SPI device via Device Tree
  • Verify communication with STM32 SPI slave

Step 1 - Build Driver

make
scp mysensor_spi.ko pi:/home/pi/

Step 2 - Install Overlay

scp myspi_sensor.dtbo pi:/boot/firmware/overlays/

Step 3 - Configure config.txt

sudo nano /boot/firmware/config.txt

Add:

dtoverlay=spi0-1cs,cs0_spidev=disabled
dtoverlay=myspi_sensor

Step 4 - Reboot

sudo reboot

Step 5 - Insert Module

sudo insmod mysensor_spi.ko

Step 6 - Verify SPI Device

ls /sys/bus/spi/devices/

Expected:

spi0.0

Step 7 - Verify Driver Binding

readlink /sys/bus/spi/devices/spi0.0/driver

Expected:

.../mysensor_spi

Step 8 - Check dmesg

dmesg | grep mysensor

Expected:

mysensor spi probe start
mysensor_core probed: ID=161

Step 9 - Enable Polling

echo 1 | sudo tee /sys/class/hwmon/hwmon*/device/polling_enable

Step 10 - Read Temperature

cat /sys/class/hwmon/hwmon*/temp1_input

Expected Result

  • CHIP_ID correctly read (0xA1)
  • Temperature readable
  • No SPI errors

Optional Debug

find /proc/device-tree -name "*mysensor*"
dtc -I fs /proc/device-tree > live.dts