This tutorial is "Part II" tutorial on Raspberry Pi Sense Hat Integration on Petalinux with Kria KR260.
For details of VIVADO block design and Petalinux flow for "how senseHAT based design be created on VIVADO for Kria KR260", please follow "Part I- SenseHAT sensors interfacing with Kria KR260 in Petalinux" tutorial.
Tools Version used: VIVADO/Vitis and Petalinux - 2022.2
Detail version of this "Tutorial" is also available in PDF format at footer section of this tutorial (Download it).
Design Files of this Project:
We have provided VIVADO Tcl, XDC file, Petalinux BSP file (ready to build BSP file), drivers and python application for SenseHAT sensors and LED Matrix at: https://github.com/LogicTronix/KR260-SenseHAT-Petalinux [Git Branch: KR260-SenseHAT-LedMatrix].
You can clone git repo of this tutorial by:
git clone -b KR260-SenseHAT-LedMatrix https://github.com/logictronix/kr260-sensehat-petalinux
The LED Matrix on the Sense Hat is driven by the led2472g IC which is a LED driver manufactured by STMicroelectronics. This LED driver is controlled by ATTiny88. The ATTiny88 acts as a slave on the IIC bus and can be accessed at 0x46 slave address.
The Sense HAT python API uses 8 bit (0 to 255) colors for R, G and B components of a pixel. When these values are written to the Linux frame buffer they're bit shifted into RGB 5 6 5. The driver converts them to RGB 5 5 5 before it passes them to the ATTiny88 AVR for writing to the LEDs.
VIVADO Block DesignThe VIVADO Block design can also be created from the given Tcl Source[Link],
The Sense HAT sits over the RPi header on the KR260 as shown in the picture above. The description of the 40 pins is shown for this alignment. The sensors communicate over I2C which uses pins 3 and 5.
ConstraintingOnly 2 pins (AE14 and AE15) on the KR260 require constraining for I2C to work. AE15 is used for SDA and AE14 is used for SCL.
set_property PACKAGE_PIN AE15 [get_ports {iic_sda_io}]
set_property IOSTANDARD LVCMOS33 [get_ports {iic_sda_io}]
set_property PULLUP true [get_ports {iic_sda_io}]
set_property PACKAGE_PIN AE14 [get_ports {iic_scl_io}]
set_property IOSTANDARD LVCMOS33 [get_ports {iic_scl_io}]
set_property PULLUP true [get_ports {iic_scl_io}]
However, the following image can be used as a reference to map and constrain all other pins.
- From the File menu, select Export > Export Platform.
- Select the Hardware option in the Platform Type window.
- Select the Pre-Synthesis state and also select the Include bitstream option in the Platform State window.
- Change required Platform Properties.
- Enter a name for the XSA and select a folder to export the XSA to and select Finish.
Sense HAT Sensor's Driver Availability
C/C++ Drivers for most sensors are available from the vendor themselves. Some drivers are also written by independent developers.
Humidity/Temperature
Accelerometer/Gyroscope and Magnetometer
Pressure/Temperature
- https://github.com/ameltech/sme-lps25h-library
- https://github.com/kirananto/RaZorReborn/tree/master/drivers/sensors/pressure/lps25h
LED Driver
Petalinux Project- PrerequisitesCreating a petalinux project requires a supported BSP which can be downloaded from the link below. A project also can be created without the BSP which is less convenient.
- Download the KR260-SenseHAT BSP from: https://github.com/LogicTronix/KR260-SenseHAT-Petalinux/tree/KR260-SenseHAT-LedMatrix/design_files
- This provided BSP already has configured the necessary packages and drivers, so it can be directly build. Or you can update the "Hardware on this BSP" using Get-Hardware-Config command of Petalinux.
petalinux-create --type project -s <location-to-bsp> --name <project-name>
cd <project-name>
petalinux-build
- On this provided BSP, we have included "KR260 App in the RootFS", so the Kria App automatically loads while booting on the KR260-Board.
- We also have added SenseHAT drivers by creating "recipes-modules" on this BSP file itself. Further details of the "SenseHAT" drivers can be found at: https://github.com/LogicTronix/KR260-SenseHAT-Petalinux/tree/KR260-SenseHAT-LedMatrix/driver
- So "petalinux-build" basically did all config and build the petalinux project.
Run the following command to create a bootable WIC image.
petalinux-package --wic --images-dir images/linux/ --bootfiles "ramdisk.cpio.gz.u-boot, boot.scr, Image, system.dtb, system-zynqmp-sck-kr-g-revB.dtb" --disk-name "sda"
- Make sure the dtb file is present in the <petalinux-project-dir>/images/linux/ directory.
- Burn the SD card with the WIC image, and boot up the KR260 board (with SenseHAT).
- On this WIC image, the "kr260 overlay" is automatically added on "RootFS of WIC image", the BSP have that configuration. So this step sort-out(remove) steps like "copying dtbo, bit.bin and shell.json" into Board(/lib/firmware/xilinx/) manually.
List the apps present on the device.
sudo xmutil listapps
Unload the current app.
sudo xmutil unloadapp
Load your app (kr260 in this case).
sudo xmutil loadapp kr260
Check if the XIIC is listed as an I2C bus in the system.
i2cdetect -l
- This will list all the I2C bus in the system (i2c-7 in this case).
- If the bus is unavailable, unload and load the app again.
Check for devices on the XIIC bus.
- sudo i2cdetect -y -r <i2c-bus-number>
- This will show the address of the sensors connected to the I2C bus.
- If the Sense Hat is working, it should show the following addresses.
- The following address map of I2C bus 7 shows all the sensor addresses from the Sense HAT. These addresses are also listed in table 1.
- Make the bus accessible to your user group by running the following command.
- sudo chown root:petalinux /dev/i2c-<bus_number> && sudo chmod g+rw /dev/i2c-<bus_number>
- e.g. sudo chown root:petalinux /dev/i2c-7 && sudo chmod g+rwx /dev/i2c-7
To communicate with the sensors attached to the bus with python, a python package needs to be installed.
Installing SMBusTo install SMBus, run pip3 install smbus. SMBus provides python bindings for Linux SMBus access through i2c-dev.
KR260-SenseHAT-Petalinux repoGit Repo Structure:KR260-SenseHAT-Petalinux [Git Branch: KR260-SenseHAT-LedMatrix]
[Optional] Running Git-Repo on KR260 Board(Direct Method)
To communicate with the sensors, we require an object of the SenseHat class that provides us with various functions that make communicating easier. We create a new instance of the SenseHat class by passing the I2C bus number that our sensors are attached to.
sense = SenseHat(<i2c-bus-number>)
With the sense object, we can access the following functions that help us read data from the sensors.
For a list of available functions and properties, check out the readme at https://github.com/LogicTronix/KR260-SenseHAT-Petalinux/tree/KR260-SenseHAT-LedMatrix.
Code Snippet for Reading Temperaturefrom time import sleep
from sense_hat import SenseHat
# 7 is I2C bus
sense = SenseHat(7)
while True:
# Temperature
print(f'\nTemperature\t: {sense.temp:.4f}° C')
print(f'\nTemperature\t: {sense.temperature:.4f}° C')
print(f'\nTemperature\t: {sense.get_temperature_from_humidity():.4f}° C')
print(f'\nTemperature\t: {sense.get_temperature_from_pressure():.4f}° C')
print(f'\nTemperature\t: {sense.get_temperature():.4f}° C')
# Humidity
print(f'Relative Humidity\t: {sense.humidity:.4f} %')
print(f'Relative Humidity\t: {sense.get_humidity():.4f} %')
# Pressure
print(f'Pressure\t: {sensor.pressure():.4f} hPa')
print(f'Pressure\t: {sensor.get_pressure():.4f} hPa')
sleep(1)
Output from the SensorsTemperature and Humidity reading from the HTS221 sensorDisplaying Information on the LED Matrix
from time import sleep
from sense_hat import SenseHat
# 7 is I2C bus
sense = SenseHat(7)
while True:
sense.show_message('Hello World.')
sense.show_message(f'{sense.get_temperature_from_humidity():.4f}° C')
sleep(1)
Displaying 8x8 RPi Logo on the LED Matrix
from sense_hat import SenseHat
# 7 is I2C bus
sense = SenseHat(7)
while True:
sense.load_image('rpi.png', redraw=True)
Demo - Pictorial:Again, for VIVADO, Petalinux project source and Python source code, check: Github or run following Git clone command at your terminal:
git clone -b KR260-SenseHAT-LedMatrix https://github.com/logictronix/kr260-sensehat-petalinux
Thanks to Frank Shrestha [frank_shrestha@logictronix.com] for creating this "in-depth" tutorial!
Comments