Wednesday, September 10, 2014

Modify kernel configuration and device tree files to create spidev for SAMA5D3 Xplained

I previously posted how to create an SD card with debian jessie here.  In this post I want to share how to modify the device tree file and the configuration file to setup spidev, and then how to rebuild the kernel.

First challenge I notice with the default build is that the SPI doesn't show up in /dev.  To try and get this to work I make the following changes:

First I want to change the device tree binding (.dts) file so that both SPI are active.  To do this modify armv7_devel/KERNEL/arch/arm/boot/dts/at91-sama5d3_xplained.dts.  In this file you will find spi1, modify it to match the following:


spi1: spi@f8008000 {
    cs-gpios = <&pioC 25 0>;
    status = "okay";
    spidev@0 {
        compatible = "spidev";
        spi-max-frequency = <50000000>;
        reg = <0>;
    };
};


Then make sure SPI is set in the kernel configuration file.  To do this run armv7_devel/tools/rebuild.sh instead of .armv7_devel/build_kernel.sh since build_kernel.sh will revert all .dts files to the default settings.

As rebuild.sh runs it will pull up the configuration screen, in this screen make sure the following are set:

Device Drivers --->
    [*]SPI support --->
        <*> Atmel SPI Controller
        <*> User mode SPI device driver support


* means you want it built in to the kernel, m means you want it set up as a module.  When this finishes you can copy over to the SD card as documented in the previous post and shown below.  You may with to delete the dtb folder on the boot partition before copying if you make substantial changes - but in that case you might just want to start with a clean SD card.

export kernel_version=3.16.1-sama5-armv7-r6
sudo cp -v ~/armv7_devel/deploy/${kernel_version}.zImage /media/<username>/boot/zImage
sudo mkdir -p /media/boot/dtbs/
sudo tar xvo -f ~/armv7_devel/deploy/${kernel_version}-dtbs.tar.gz -C /media/<username>/boot/dtbs/
sudo tar xv -f ~/armv7_devel/deploy/${kernel_version}-modules.tar.gz -C /media/<username>/rootfs/

Once everything is copied and you boot up you should see in /dev something like the following:
# ls /dev/spidev*
/dev/spidev32765.0

If you don't see something similar then check /var/log/syslog for atmel_spi entries and they should give you an idea of any errors.

You should find SPI1 on the following pins:

J15 Header
Pin 10 PC25 Select Pin
Pin 11 PC23 MOSI
Pin 12 PC22 MISO
Pin 13 PC24 SCK

In order to program the SPI, I used code written taken from this repository (https://github.com/tanzilli/playground/tree/master/python/spi) and modified it for Python3.  You can find the modified code at this repository (https://github.com/photonthunder/thunderLightning/tree/master/spidev).  Just put the three python files in the same folder and make sure they have proper permission (chmod 755 *.py).  To run the test just connect pin 11 and 12 together and run the python code and you should get the following:

# ./spisend.py  0x55 0xaa

Enjoy the hacking.


1 comment: