I started to look at the SPI interface of the raspberry pi for only one purpose, to be able to to use it as a control panel for some hacking project.
I had a LCD Screen laying around that I bought at watterrott so I decided to connect it to my raspberry pi.
MI0283QT-2
The MI0283QT-2 module is a nice 2.8” Color LCD with a resolution of 320×240 controlled by a hx8347 controller and a touchscreen based on the ADS7846 controller.
Since the spi interface is 3.3 – 5 V tolerant, it is directly compatible with the raspberry pi, cool.
Connection to the raspberry-pi
C++ Library
The guy from watteroot is providing an ardurino library for controlling the LCD. I decided to start from it to quicly have some working code.
In fact, it was not so easy to implemented since I have barly no experience in C/C++ and the spidev was a huge mystery at that time.
Hopefully, I managed to make it working, and here is the result of my work. The code is available from github.
At this time only the LCD library is implemented, I will work on the touch screen library as soon as this come as a priority to me
As usual feel free to check the code, and use as you wish. You will quickly see it is really a quick port.
Tips :
You need a kernel with a SPI library. I only tested on Chris boot kernel
For performance reason, some functions are sending quite big SPI command. Make sure to load the spidev with a buffer big enough.
modprobe spidev bufsiz=256000
Finding that I need that parameter was not easy, the only symptom was that all screen was not clearing. After couple of hours I noticed that only function which was sending less that 4096 bytes were working.
The library does not directly support to set the led light level. However if you connect the led pin to the GPIO18 you can control it using the WiringPI gpio tools.
For the moment I am trying to implements a framebufffer driver for this device. This will allows any king of gui application to work on this lcd, including the console at the boot time.
The current status is that I am able to clear the sceen. I have to admit that it is not easy task, mainly because I am lacking of C experiences, linux kernel development experiences and I do not know the kernel SPI interfaces at well. But that really challanging job.
Edit 15 March 2013
The information in this blog entry is rather oudated.
Here some informations about using a framebuffer based on my code, with support for the touch screen. MI0283QT2-pi Github project of Dronus
The model of LCD sold by Watteroot changed, and it is not compatible anymore with this code.
On the lallafa blog, you can find the appropirate frame buffer for this new LCD Watterott MI0283QT-9A Display for the Rasbperry Pi.
You can also find a true framebuffer for the old version of LCD on the same blog Watterott Display on Raspberry Pi
If you followed my previous blog entry, you have a nice raspberry pi system with support for i2c.
Now it is time to test if the i2c bus and the driver is working.
Let’s try to connect a i2c eeprom to the raspberry, since it is the only i2c device that I have in my drawer.
This code has been tested with a 24C16. It will probably work with smaller one, and bigger with code changes ( page size, and page number )
Connect the device
Easy, just connect the 3,3v, the gnd, the SCL and the SDA pins.
Load the i2c driver
modprob i2c-dev
You can also add the line i2c-dev to the /dev/modules file
For testing the reading I am using a test program from i2ctools
root# wget http://www.lm-sensors.org/browser/i2c-tools/trunk/eepromer/eeprom.c?format=txt
root# gcc –o eeprom eeprom.c
root# ./eeprom –r –f data
base-address of eeproms : 0x50
number of pages to read : 8 (0x50 .. 0x57)
file opened for writing : d
on filedescriptor : 3
i2c-devicenode is : /dev/i2c-0
on filedescriptor : 4
Read 16 bytes from eeprom at 0x50, offset 00000000
...
Read 16 bytes from eeprom at 0x57, offset 000000f0
The program should work out of the box, and the content of the eeprom must be save into data.
If you are using a 24C16 the file should be 2048 bytes length.
Writing the eeprom.
The program provided by lmsensor seems to be bugged, basically when it write to the eeprom, we must way until the content has been written before continuing.
This is well documented in the datasheet (http://www.bookly.com/images/24C64-BM.pdf ) as Acknowledge (ACK) Polling
Is it a bug in the driver, or in the application level ?
Anyway, I find a solution, I simply reposition the eeprom read-pointer until it succeded.
If you have a more elegant solution, I will be glad to hear about it.
root# wget https://raw.github.com/dgallot/i2c-tools/master/eepromer/eeprom.c
root# gcc –o eeprom eeprom.c
root# ./eeprom -f data -w
base-address of eeproms : 0x50
number of pages to read : 8 (0x50 .. 0x57)
file opened for reading : data
on filedescriptor : 3
i2c-devicenode is : /dev/i2c-0
on filedescriptor : 4
**WARNING**
- You have chosen to WRITE to this eeprom.
Make sure that this tiny chip is *NOT* vital to the
operation of your computer as you can easily corrupt
the configuration memory of your SDRAM-memory-module,
your IBM ThinkPad or whatnot...! Fixing these errors can be
a time-consuming and very costly process!
Things to consider:
- You can have more than one i2c-bus, check in /proc/bus/i2c
and specify the correct one with -d
right now you have chosen to use '/dev/i2c-0'
- A eeprom can occupy several i2c-addresses (one per page)
so please make sure that there is no vital eeprom in your computer
sitting at addresses between 0x50 and 0x57
Enter 'yes' to continue:yes
Wrote 16 bytes to eeprom at 0x50, offset 00000000
... acked
Wrote 16 bytes to eeprom at 0x50, offset 00000010
..... acked
Wrote 16 bytes to eeprom at 0x50, offset 00000020
...... acked
Wrote 16 bytes to eeprom at 0x50, offset 00000030
...... acked
Wrote 16 bytes to eeprom at 0x57, offset 000000f0
....... acked
This guide will show you the entire step that I had to do in order to produce my custom kernel. I wanted to have the latest kernel of Chris boot, which include support for spi and i2c, but I also need to include the latest realtek driver for my wifi dongle.
The Chris Boot’s website contains a lot of information, but it does not contains this information required to build his kernel from scratch by real beginner. I will try to fill the gap.
Prerequisits
First install Ubuntu 12.04 LTS
Since I have a mac, I installed it on Parallels and it is working like a charm.
There are several way of doing that, you can start simply download the Chris Boot images, and install his latest kernel.
The config is located in /boot/config-3.2.20-rpi1+, or you can simply download it from my site config-3.2.20-rpi1+
Copy this to your ubuntu system as .config
cd ~/rpi-3.2.20/linux
wget http://www.gallot.be/resources/config-3.2.20-rpi1+
cp config-3.2.20-rpi1+ .config
Change the kernel name, so it will not conflict with any other one.
Edit the Makefile and change the first line from :
EXTRAVERSION =
To :
EXTRAVERSION=-spi-i2c-rpi1
Import the config and disable the old rlt8192 module
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- oldconfig
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
Open the following menu :
Device Drivers
Network device support
Wireless LAN
And unselect the driver by typing [space]
Realtek RTL8192CU/RTL8188CU USB Wireless Network Adapter
Hit [Tab] to select exit multiple time, and save the configuration.
Build the kernel.
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j5
Prepare the file to be copied to the system.
mkdir -p ~/rpi-3.2.20/install/boot
make ARCH=arm INSTALL_MOD_PATH=~/rpi-3.2.20/install CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- modules_install
make INSTALL_PATH=~/rpi-3.2.20/install/boot ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- INSTALLKERNEL=none install
Build the 8192cu kernel driver.
Copy and unpack the driver.
cd ~/rpi-3.2.20
wget http://www.gallot.be/resources/RTL819xCU_USB_linux_v3.4.2_3727.20120404.zip
unzip RTL819xCU_USB_linux_v3.4.2_3727.20120404.zip
cd RTL8188C_8192C_8192D_USB_linux_v3.4.2_3727.20120404/driver
tar -xvzf rtl8188C_8192C_8192D_usb_linux_v3.4.2_3727.20120404.tar.gz
Build the 8192cu kernel driver.
cd ~/rpi-3.2.20/RTL8188C_8192C_8192D_USB_linux_v3.4.2_3727.20120404/driver/rtl8188C_8192C_8192D_usb_linux_v3.4.2_3727.20120404
make KSRC=~/rpi-3.2.20/linux ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k -j5
Now you simply need to install the new files to you sd card.
Plug you sd card, and if you are working from a virtual machine connect the sd card reader to the ubuntu vm.
The boot partition will be the fat partition, on my system it is mount into /media/00D7-4C15
The system partition is the ext2 partition, on my system it is mount into /media/1bee6d88-1731-433f-8f74-49a7c1ce1035
I really do not know if it is required to install the kernel in the boot partition and in the system partition ( in the /boot ).
But since it does not harm, I copy it on both location.
The latest firmware is not able to directly start the linux image, you do not need to create a disk image anymore.
So to activate it, simply override the kernel.img file with the new kernel.
The only remaining step required to start you raspberry pi, and connect a keybord and a screen to it !
And issue a simple depmod command and reboot.
depmod -a
Reboot
I delibered choosed to not merge the RTL819x driver to the kernel source tree.
Doing so slighly complicate the process, but if you want to do it check the website of lumux which explain how to do it.
If you do so, this step is not required
The next article will explain how to modify this image make it usable without a keyboard by doing
Run the depmod -a on the first boot
Do an offline configuration of the wifi
Unattented installation of the required package like ssh