Friday, September 19, 2014

PWM0 on Debian Jessie with SAMA5D3 XPLAIN board

A previous post setups a fresh debian jessie install but the PWM wasn't working.  In order to get it to work I had to swap the kernel configuration file as follows:


--- Pulse-Width Modulation (PWM) Support
    <*> Atmel PWM support
    < > Atmel HLCDC PWM support 
    < > Atmel TC Block PWM support 
    < > Freescale FlexTimer Module (FTM) PWM support 
    < > NXP PCA9685 PWM driver


* means you want it built in to the kernel, m means you want it set up as a module.

After that I was able to find the directory pwmchip0 in /sys/class/pwm

To use the PWM you do the following:

For any PWM line you want to use you have to export it:

~# echo 0 > /sys/class/pwm/pwmchip0/export

To use that line as generic GPIO you can unexport it.  For the exported channel a directory called pwm0 is created. In that directory you will find different files that setup different features of the PWM.

For example to setup the PWM with a 1 mS period and 0.5 mS duty cycle do the following:

~# echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period
~# echo 500000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
~# echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable 


I found that the PWM0H pin was PA20, and PWM0L was PA21 on the XPLAIN board.

Good luck.

Thursday, September 18, 2014

Setting up Verizon Novatel USB760 USB Cell Modem with Debian Jessie on SAMA5D3

In previous posts I have setup my Novatel USB760 cell modem (Verizon) on a beaglebone black.  I wanted to get it working on the SAMA5D3.  Previously there was a Novatel Debian package that I used but I wanted to get it working without this package.  From what I read it should be plug and play but that wasn't the case for me.

So I start with the Debian Jessie setup in this previous post.  The first modification is that when you build the kernel (or rebuild it) make sure that the following are selected in the configuration menu under Device Driver:

Code: [Select]
Device Drivers  --->
  [*] USB support  --->
    <*>   USB Modem (CDC ACM) support
    <*>   USB Serial Converter support  --->
      [*]   USB Generic Serial Driver
      <*>   USB driver for GSM and CDMA modems
  [*] Network device support  --->
    <*>   PPP (point-to-point protocol) support
    <*>     PPP support for async serial ports


make sure that the following packages are installed in Debian Jessie:

Code: [Select]
apt-get install ppp wvdial eject usb-modeswitch

unzip the existing usb_modeswitch scripts (probably worth noting that the package is usb-modeswitch, but the executable program is usb_modeswitch - this can be confusing):

Code: [Select]
tar -xvf /usr/share/usb_modeswitch/configPack.tar.gz

modify the /etc/wvdial.conf file with the following:

Code: [Select]
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Modem Type = Analog Modem
Baud = 9600
New PPPD = yes
Modem = /dev/ttyUSB0
ISDN = 0
Stupid Mode = 1
Phone = #777
Username = anything@vzw3g.com
Password = vzw


You are suppose to be able to run wvdial and your modem should connect however I observed that my modem was still in storage mode.  To check this type lsusb and you will see something like the following in the list:

Code: [Select]
Bus 002 Device 003: ID 1410:5030 Novatel Wireless

To switch modes we use usb_modeswitch:

Code: [Select]
usb_modeswitch -W -i0 -v1410 -p5030 -c /usr/share/usb_modeswitch/1410\:5030

after the command lsusb should now give the following:


Code: [Select]
Bus 002 Device 003: ID 1410:6000 Novatel Wireless

0x6000 means you should be good to go.  You can double check this by making sure /dev has some /dev/USB* options like the following:

Code: [Select]
/dev/ttyUSB0 /dev/ttyUSB1 /dev/ttyUSB2 /dev/ttyUSB3

If you have other USB files these four will move in number, if that happens just change the wvdial.conf Modem line to match the first of the four.

After this just run wvdial and you should connect.

Good luck


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.


Installing Debian Jessie and Kernel 3.16 on SD card for SAMA5D3 XPLAINED board

I wanted to rewrite a previous post using Jessie instead of Wheezy.  I am running a Ubuntu 14 virtually on my apple laptop using parallels.

First make sure you have the necessary dependencies installed (there may be some I missed):

sudo apt-get update
sudo apt-get install vim git build-essential device-tree-compiler lzma lzop u-boot-tools libncurses5-dev:amd64 libncurses5:i386 libstdc++6:i386

Linux Kernel


Now we build the kernel, I am using the latest kernel build by Robert C Nelson and then modifying it to my needs, his wiki page is here.

Now make sure you are in the master directory where you will put all of your linux build.  I just called my master.  The following steps will clone Roberts' git repository, checkout a branch called tmp and build the kernel (this will take some time).

cd ~/master
git clone https://github.com/RobertCNelson/armv7_devel.git
cd armv7_devel/
git checkout origin/v3.16.x-sama5-armv7 -b tmp
./build_kernel.sh

As the linux kernel is built it will prompt for configuration options.  This first time just exit, we will come back and make changes in a later post.  When done change back to the master directory.

U-boot Compile


To compile U-boot so that it will work with the SAMA5D3 do the following (run this from your master folder).  This relies on the cross compiler built with the kernel script.  Probably a good time to mention that the numbers below will change as scripts are updating.  So if there is a problem running any part of this blog search the directory for the updated numbers.  For example, "find ${DIR}/armv7_devel/dl -name gcc-linaro-arm-linux-gnueabi*_linux -type d" will give you the path used by export CROSS_COMPILE below.


git clone git://git.denx.de/u-boot.git
cd u-boot/
git checkout v2014.07 -b tmp
wget -c https://raw.githubusercontent.com/eewiki/u-boot-patches/master/v2014.07/0001-sama5d3_xplained-uEnv.txt-bootz-n-fixes.patch
patch -p1 < 0001-sama5d3_xplained-uEnv.txt-bootz-n-fixes.patch
export CROSS_COMPILE=~/master/armv7_devel/dl/gcc-linaro-arm-linux-gnueabihf-4.9-2014.08_linux/bin/arm-linux-gnueabihf-
export ARCH=arm
make distclean
make sama5d3_xplained_mmc_config
make

You will need to create a uEnv.txt file that setups the environment and gives some initial instructions, these are specific to the build that follows
vim uEnv.txt
<insert the following>
#Enable systemd in Debian
optargs=quiet init=/lib/systemd/systemd
console=ttyS0,115200
mmcroot=/dev/mmcblk0p2 ro
mmcrootfstype=ext4 rootwait fixrtc
Finally, while in the u-boot directory you can create a MAC address for the board as referenced in this link.  This is not necessary since the board will create one at random.
tools/gen_eth_addr
XX:XX:XX:XX:XX:XX
The result will be a mac address you can use for testing.  If you want to get real ones for deployment check out this link.  Save this MAC address for use later in the install.

Now you are done compiling the Uboot files so back out of the u-boot directory (cd ..)

Root File System


First is to install all the necessary packages.

sudo apt-get install debootstrap
sudo apt-get install binfmt-support
sudo apt-get install qemu-user-static

With everything installed we can now start to create the root file system

mkdir armhf-root
cd armhf-root
sudo debootstrap --foreign --arch=armhf jessie .

This will take a while to download all basic packages.  The next steps setup the root file system so that you can run it as if you were on the SAMA5D3 XPLAINED board :

sudo cp /usr/bin/qemu-arm-static usr/bin
sudo LC_ALL=C LANGUAGE=C LANG=C chroot . /debootstrap/debootstrap --second-stage
sudo LC_ALL=C LANGUAGE=C LANG=C chroot . dpkg --configure -a

Now it is just a matter of making modifications and additions to meet your needs.  First lets add a password for root (all these commands are run from the armhf-root directory):

sudo chroot . passwd

setup the package repository (./etc/apt/sources.list) which will vary based on your needs:

deb http://ftp.us.debian.org/debian/ jessie main contrib non-free #deb-src http://ftp.us.debian.org/debian/ jessie main contrib non-free deb http://ftp.us.debian.org/debian/ jessie-updates main contrib non-free #deb-src http://ftp.us.debian.org/debian/ jessie-updates main contrib non-free deb http://security.debian.org/ jessie/updates main contrib non-free #deb-src http://security.debian.org/ jessie/updates main contrib non-free #deb http://ftp.debian.org/debian jessie-backports main contrib non-free ##deb-src http://ftp.debian.org/debian jessie-backports main contrib non-free

and install some packages (these will be different depending on your needs):

LC_ALL=C LANGUAGE=C LANG=C chroot . apt-get update
LC_ALL=C LANGUAGE=C LANG=C chroot . apt-get install <package_name>

Some packages to add include locales, dialog, python3, python3-serial, python3-bson, vim, openssh-server, ntpdate, ppp, wvdial, usb-modeswitch, eject etc.

Then you can modify different files to fit your needs as well.  For example:
modify etc/fstab using the following:
sudo sh -c "echo '/dev/mmcblk0p2 / auto errors=remount-ro 0 1' >> etc/fstab"
sudo sh -c "echo '/dev/mmcblk0p1 /boot/uboot auto defaults 0 2' >> etc/fstab"
and modify etc/inittab using the following:
sudo sh -c "echo 'T0:23:respawn:/sbin/getty -L ttyS0 115200 vt102' >> etc/inittab"

Or modify the network interfaces:

sudo vim etc/network/interfaces

and add the following (the hwaddress portion is optional, the board will create one at random), use the MAC address created previously in the u-boot section:
allow-hotplug eth0
iface eth0 inet dhcp
hwaddress ether XX:XX:XX:XX:XX:XX
After this you can make other changes and modifications as needed, just remember to always use chroot . when calling an application.

when done remove qemu-arm-static so it doesn't cause problems when you copy over to the SD card.
sudo rm usr/bin/qemu-arm-static
After this you can make other changes and modifications as needed, just remember to always use chroot . when calling an application.

Create SD Card

With these three components we can now create an SD card for use with the SAMA5D3 Xplained.  So start with an SD card preferably with class 10 for better reliability.  My card is an 8 Gbyte SD card.  I  connected the SD card to my parallels Ubuntu.  You may wish to monitor dmesg to identify the SD card.  Mine was /dev/sdb.

The following commands clear the SD card and then setup the partitions on the SD card (one for boot and one for roots).  The sfdisk command will change the terminal prompt which should be expected just type in the last three lines.
export DISK=/dev/sdb
sudo dd if=/dev/zero of=${DISK} bs=1M count=16
sudo sfdisk --in-order --Linux --unit M ${DISK} <<-__EOF__
1,48,0xE,*
,,,-
__EOF__
After that name the partitions as follows:
sudo mkfs.vfat -F 16 ${DISK}1 -n boot
sudo mkfs.ext4 ${DISK}2 -L rootfs
wait 10 seconds and then eject the SD card and reinsert it again.  On some systems it will mount automatically, ubuntu mounts both partitions at /media/username where username is the username of the current login user.  If it doesn't mount you can mount it yourself with the following:
sudo mkdir -p /media/<useranem>/boot/
sudo mkdir -p /media/<username>/rootfs/
sudo mount ${DISK}1 /media/<username>/boot/
sudo mount ${DISK}2 /media/<username>/rootfs/

Copy Files to SD Card


Now we want to copy everything over to our SD card.  Your location for these files may be different depending on where you placed them in your file system.
Check the file location before running these commands.  <username> below should be replaced with your username if using ubuntu or the whole path should be modified to match your settings.  I am running all these commands from my master folder that contains u-boot, armv7_devel, and armhf-root.

First we copy over the u-boot related files
sudo cp -v ./u-boot/spl/u-boot-spl.bin /media/<username>/boot/BOOT.BIN
sudo cp -v ./u-boot/u-boot.img /media/<username>/boot/
sudo cp -v ./u-boot/uEnv.txt /media/<username>/boot/
Now we copy over the root file system:
sudo rsync -axHAX --progress armhf-root/ /media/<username>/rootfs/
then we copy over the kernel image (zImage), the device tree files (dtbs), and any modules related files (the export kernel_version command should make it a little easier to update with a new build in the future).

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/
After that just unmount the SD card with the following:
sync
sudo umount /media/<username>/boot
sudo umount /media/<username>/rootfs
And then remove the SD card, power down the SAMA5D3 Xplained and pull jumper JP5.  Then insert the SD card into the SAMA5D3 and power up.  Use a serial cable to connect to the SAMA5D3 board and watch it boot up.  Once it is up you may wish to get the modules up and running with the following command:
depmod -a

That was a very basic build and you will quickly want to make changes.  First things is to make your changes.  You can update u-boot, the linux kernel, or the root file system and then just copy over the new files to the SD card.  For the device tree files and the root files system you may wish to delete (remove) the files first before making the changes.  Just using sudo rm -r is a quick and dirty way to remove, if you are doing something for deployment you may wish to do a full clean of the partition as previously described in this post.