This blog has permanently moved to

Contact Form | Email Address

© 2018 All rights reserved by Boseji

Thank you for all your support.

Saturday, April 4, 2015

Raspberry Pi GPIO+SPI to use ST7565 LCD as an Additional Display

We all want to have add-on displays for status outputs on PC. Yes that's the small display on your PC tower displaying how Awesome is the hardware inside is doing. Well we need one for our python scripts running in background on a Raspberry Pi. We had some spare ST7565 driver based LCDs but could not find any generic utility to connect these. So we wrote one ourselves.
These LCDs use SPI communication to configure the driver. Both data and commands are multiplexed using the A0 pin of the LCD.

We found that there are two variants of the LCD:
1. Adafruit Graphics LCD based on ST7565:
 This one had the issue of stange frame locations. These needed some mangling to get it right. Also there was additional column than the mentioned 128x64 resolution of the display. We managed to get this working and added a specific compatibility to our driver. You can buy one of these from Adafruit at http://www.adafruit.com/products/250 and this page contains all the datasheet. However we took the liberty of adding the datasheet to our github repository.

2. Ebay clones Graphics LCDs with ST7565: http://goo.gl/wvmmsl These are the clone of the original Adafruit display but do not have mangling in terms of the pages or the columns. However they have an additional problem, they do not have a roll over function so that when you keep writing data to the LCD row it rolls over to the next. They did not have this function available, so we did an adaptation for that too to help us get the driver running. These are slightly cheaper than the Adafruit ones.

Now for the Raspberry Pi interfacing part we had two choices for the driver:
1. Wiring Pi: This was good but a bit slow for SPI.
2. Pigpio: This was similar but fared better having 20Mhz SPI speed.

We tried out multiple ways of having a Python module do the interfacing work but it was terribly slow in communicating with the LCD. Finally we though of going the C way with Pigpio. And this turned out to be the best choice.

We wanted to have a simple command line interface to work with the LCD. As simple as the 'echo' command in Linux. We needed capability to be able to write, position the cursor and clear the display to begin with.

We created the LCD program a command line tool to communicate with the LCD.
The python scripts can now direct their output to this LCD via commands even though they run in background.

First step was to connect the LCD to the Raspberry Pi, we used a RPi-B+ model as shown in the picture. Here is the pin configuration of J1:

Rpi Connector      ST7565 LCD
3.3v (Pin01)     - LCD Back A
GND  (Pin09)     - LCD Back K
GND  (Pin06)     - GND
3.3V (Pin17)     - VCC

GPIO24           - A0
GPIO25           - nRST

We use the SPI0 bus of the Raspberry Pi to connect the LCD, of course there is MISO as the LCD is only one way traffic. Also we need 2 GPIOs to control the A0 and nRST pins. The nCS chipselect pin used one of the SPI chip select available on the Raspberry Pi (CS0N pin).

Next, since we use the Pigpio program we had to download and install this program.
Here are the instructions:
wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
make install

For the benefit of the users we have created a scrip called 'pigpio.sh' in the github repository to help install this driver.

Finally one needs to download the Repository from Github to build this program. Here are the commands:
git clone https://github.com/boseji/Rpi-ST7565-SPI-LCD.git
This would download and build the code into an executable called 'lcd'

Now for the commands to access the LCD. All commands need root user access we are accessing the hardware. Hence 'sudo' is added to most of the commands. Listed below are the important commands:
  • sudo ./lcd init - Used to initialize the LCD
  • sudo ./lcd c - Used to clear the LCD screen
  • sudo ./lcd w "String" - Used to write the string to the LCD at the current location
  • sudo ./lcd g {COLUMN} {ROW} - This is used to position the current cursor at a give location of the LCD screen
  • sudo ./lcd sleep - Used to put the LCD into Low power mode
  • sudo ./lcd wakeup - Used to restore the LCD from low power mode

The source code of this driver is available on Github: https://github.com/boseji/Rpi-ST7565-SPI-LCD

Our next goal is to build this into a frame buffer driver then the display can be used as an output display for the Raspberry Pi. Let us know your feedback and comments.

Monday, January 12, 2015

Avrdude on Windows : Long time after WinAVR

All tinkers and hacker like Arduino, we too like it. However some project demand drivers to be developed in the Atmel Studio 6.x for the need of performance and custom IP needs. We have been handling several projects like that for our partners. However whenever production programming comes in question we always tend to use Linux machines. This time around we needed to use a Windows machine instead. We were troubled by the non-availability of avrdude like programming front-end and USB driver issues for programmers. We found one solution by using Atmel's programming back-end. Still every production PC needs to run the full blown version of Atmel Studio 6.x which in itself is a big software. We made it to work but were not satisfied. We liked our good old trusted avrdude from our WinAVR days. So we kept scraping the forums and interacted with our fellow tinkers. Our efforts were paid in full. That's why we would like to share this lesson we learned with you so that if you ever encounter this problem this might be your solution.

In this tutorial we would be using the standard production capable programmer 'AVRISP mkII'. Of course the same goes for its clones and all other programmers supported by Avrdue. The main problem being support of USB AVR programmers for Avrdude on Windows.
AVRISP mkII programmer
We have taken this example since we could not get the clones or the other programmers to work using the Atmel Studio back-end. So we had not choice but to use the original 'AVRISP mkII'.
But later in Part2 we would explain how to get Avrdude working. After that we were able to use Olimex ISP and USBtiny also on Windows PC as production programmers.

PART1 - AVR Studio Programming

This is mostly a standard programming interface API from Atmel. This programming method can be good for using multiple AVRISP mkII progammers to program several chips using a single PC. However in most cases we end up with one programmer with one board per setup.

[1] Download & Install
In order to use the AVR Studio back-end one first needs to install the Avr Studio itself. We have used AVR Studio v6.2 with Service Pack (1).
Its a big download and a long install.
Also as a caution in case of Windows 8 and beyond the driver install protection feature needs to be disabled. This can be found in another tutorial http://goo.gl/cj4V1J

[2] Running the AVR Studio Back end

First thing that's needed is to add the AVR Studio Back end to the System Path.

The AVR Studio back end is located at
'C:\Program Files (x86)\Atmel\Atmel Studio 6.2\atbackend'
in case of 64bit architectures.

In case of 32bit architectures AVR Studio is Located at
'C:\Program Files\Atmel\Atmel Studio 6.2\atbackend'

The program used is 'atprogram.exe' which renders the programming API.

Lets look at various commands to check and program the microcontroller. In this case we would use a Arduino UNO with ATmega328p connected to the PC via USB and AVRISP mkII connected to the ISP connector of the board.

a. Check for Programmers connected
'atprogram list'
Here is a template output:
Output for list command
In this case the output shows a AVRISP mkII programmer connected with Serial Number '000200208327' . This serial number is important to distinguish the Programmer

b. Listing chip parameters:
'atprogram -t avrispmk2 -s 000200208327 -i isp -d atmega328p info'
  • '-t' specifies the Programmer Name.
  • '-s' specifies the Serial Number
  • '-i' specifies the Programming interface in this case 'ISP'
  • '-d' specifies the device name ATmega328p

Here is a sample output:

c. Program Fuse:
'atprogram -t avrispmk2 -s 000200208327 -i isp -d atmega328p write -fs -v --values FFDEFD'
Here one needs to be careful in the order of the Fuse. [Extended][High][Low].
Its substituted at the end of the command as HEX values.
  •  '-fs' indicates we have fuse write operation.
  •   '-v' for verification after write.
  •   '--values' gives the 16-bit fuse to be programmed

d. Program Flash:
 'atprogram -t avrispmk2 -s 000200208327 -i isp -d atmega328p program -fl --verify -c --format hex -f program.hex'
This runs with the Program command. Different type of file formats are supported while programming as well as options to perform mass erase.

  • '-fl' specifies that the Flash needs to be programmed
  • '--verify' specifies that after the Flash has been programmed it needs to be verified.
  • '-c' specified before flashing a MASS ERASE operation needs to be performed before programming Flash.
  • '--format' specifies the format of the input program file - it can be 'bin', 'hex' or if not specified 'elf'
  • '-f' provides the file name of the program that needs to be Flashed into the chip

e. Program EEPROM:
'atprogram -t avrispmk2 -s 000200208327 -i isp -d atmega328p program -ee --verify --format hex -f eeprom.eep'
This programs the EEPROM again using the program command.
  • '-ee' specifies the EEPROM memory needs to be programmed.
These command help to perform the normal programming operations. The Serial number helps to send commands to multiple interfaces for programming in a concurrent manner.

PART2 - Avrdude Based Programming

We would recommend this for any one trying to implement a multi platform production setup where the programmer used are not necessarily the AVRISP mkII. The best part is it does not damage or alter the existing Atmel Studio driver compatibility on Windows.

[1] In order to begin with this we have packaged AVRdude for windows with the required driver DLLs and converted into portable setup.

Downloads Folder : http://goo.gl/O7TVrN

For 64bit use: avrdude-win-64bit.zip
For 32bit use: avrdude-win-32bit.zip

Download the required files as per the machine architecture type - 32bit or 64bit.
We have tested this working on a Windows 7 x64 installation.

[2] Next step is to install the required Driver.
Once the archive mentioned above is extracted we would have a avrdude directory containing the driver directory.
In our case we had the 'driver-amd64' directory since we had x64 bit system.

a. Run the File 'install-filter-win.exe'
b. It would ask for 'User Access Control' Allow it by pressing 'Yes'
c. Select 'Install a device filter' and press 'Next'
d. Next it sows a list of devices
Select the 'AVRISP mkII' since we are using that. Possibly the same can be done for any other programmer connected with USB.
e. Click on 'Install' after selection of the correct Hardware ID.
f. If the driver installation was successful it would show a confirmation screen.
g. Once done with driver instillation the program would again prompt you to select another Hardware ID. If not needed press 'Cancel' to Exit the installer.

[3] Test it out
Now we get to check if every thing went fine or not. For this experiment again we would use a Arduino board connected to the PC via USB and the AVRISP mkII connected to the ISP connector on it.

a. Open a command prompt from Start Menu by typing 'cmd'
b. Go to the 'avrdude' directory where it was extracted.
d. Give the following command to check the presence of the MCU in this case ATmega328p
avrdude -c avrispmkii -P usb -p m328p
Should show the following output:
That's all ! now you have a working setup of Avrdude.

This works for all types of programmers that would need USB drivers in Windows.
Further it allows you to use the Eclipse environment to develop the AVR code using the AVRgcc windows package.

Python Registry : Making its presence known on Windows

We have been having difficulty in installing some of the Python packages on windows. For example PIL, GTK+ all in one .etc.

We took help from http://effbot.org/ famous for the Tkinter book that we have used several times for our other projects. There was a specific technique mentioned by which one can add the Python installation to the system Registry. This would allow us to install the other Python packages.

However it still did not work on our Windows 7 x64 installation. We searched further to find one more solution. Since the machine / architecture we were using is x64 there is a chance that it might require some more workaround. Thanks to our favorite http://stackoverflow.com/ we were saved.
Solution is here : http://stackoverflow.com/a/4579917
The above link mentions about a different key location in the Windows registry that helps to fix the install signature of Python.

In order to execute this fix one would need prior installation of PyWin32. We are using build 219 for this on a Windows 7 x64 installation. And the Python Executable should be kept in System Path.
Now we have prepared an update for the script published at effbot.org:

To run this script one needs to run this in Administrator Prompt. In case the Registry keys already exists then this program would ignore the installation.

Saturday, January 10, 2015

Python App development : Windows based Apps made easy

We were investigating frameworks that can help us create stand alone apps using Python back end. Our exploration led us to some unique solutions, that might as well be helpful for people looking to create small apps out of python. What's best that they work even if python is not installed in the target.
We have seen how the Arduino packages the Java interpreter and VM into its UI. We did not want to make the same thing using python. As python programmers have already built us frameworks that can help stand-alone deployment.

We started with two themes - Command line apps and Windowed Apps.
For the command line app the user interface is the command line itself. Possibly the app would need parameters apart from that most of the interaction is prompt driven. This was one of the category of apps that we already had.
Another category was the TCL/TK based apps that have a basic gui designed. This was another category of apps that we wanted to make standalone.
Apparently in Linux the things are a bit easier as you can make the file executable directly but such is not the case in windows.

We have designed 2 examples to help understand the various packages that can help us with this.

Here is a list of the packages that we tested:
  1. Py2Exe
  2. cx-Freeze
  3. PyInstaller
We created two sample programs for this.

Command line Program:
GUI Program:

In order to use these packages here are the installations needed:
  1. Open Command prompt using cmd command in Start Menu
  2. Enter Command SET PATH=C:\Python27;C:\Python27\Scripts;%PATH% to make sure that the Scripts directory is in PATH environment variable This ensures that the Path is configured properly and have access to python package manager.
  3. Now for pip installations use the following Commands:
    pip install cx_freeze PyInstaller virtualenv
    pip install wget - In case one needs a download manager
    pip install --upgrade cx_freeze PyInstaller virtualenv - For upgradation
  4. Download the Py2exe Installations:
  5. Download the PyWin32 Installation:
With these we are ready to for creation of the executable Apps.

We had already prepared python based execution programs tagged as run-*.

Now each package needs specific command to run.

cx-Freeze Procedure

This package system automatically assembles the Distribution package such that all the dependencies are take care off. The space taken by this package is the least. And it generate directly only one directory in the fastest process.
The command used is:
python c:\Python27\Scripts\cxfreeze program.py -O -c --target-dir dist-cx
In order to make this process faster we have a python file run-cxFreeze.py. The command would create the dist-cx directory where the program executable would be created.

PyInstaller Procedure

This package system builds the Dependencies and then build the Distribution with python + support packages. One of the best advantage is that cross dependencies are handled very well.
The command used is:
pyinstaller program.py
We have a runner program in python run-pyinstaller.py. There are also .spec file that would be generated on the basis of default configuration. Further advanced options can be configured by writing the .spec file with custom options. Here is the Documentation Link: http://pythonhosted.org/PyInstaller/

Py2exe Procedure

This package is the oldest of the implemntations. It works by building and then preparing the distribution. In order to make this package work we would need a specific setup.py as the py2exe needes the Python distribution engine to run.
Example setup.py:
from distutils.core import setup
import py2exe
The command used is:
python setup.py py2exe
We have both the setup.py and runner program run-py2exe.py.


We have tested all the three, we found the cx-Freeze to be best unless we would need to many external modules then PyInstaller is better to use.

We have included all this info and created 2 Github Repositories: