August 27, 2013

A simple SPI schematic, using MCP23S17 I/O expander

Filed under: Raspberry 3.14 — Tags: , , , , , , , , , , — admin @ 12:37 pm

Once SPI enabled in Raspbian, here is a simple test wiring, using microchip’s MCP23S17  I/O spi/I2C expander.
The chip is powered by the Pi’s 3.3V supply.
Explanation about the wiring is not really needed, as the schematic is pretty straightsimple.

A dip switches section is used to configure the mcp23s17’s SPI adress if needed (3bit) and another dip is used to have a hard-reset button if things are going bad.

One can test the circuit by using, or example, PORTB as output, and wiring leds+ a 220Ohm resistor to the ground, to get a visual feedback of the PORT’s output state.Thats what i did..

Now, after a carefull read of the MCP chip datasheet, one can properly program the registers of the chip to use it.
for our test purposes, i’ll state that we use PORTB as output.

Compile on the Pi that code
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/types.h>
#include <linux/spi/spidev.h>

#include <sys/ioctl.h>
const unsigned char init_mcp_mode[]={ 0×40,0×0a,0×20 }; // no auto-inc, et bank=0
const unsigned char init_mcp_trisb[]={ 0×40,0×01,0×00 }; // as output
int main(int argc, char *argv[])
int fd_spi;
unsigned char to_send[4];

int value;
unsigned char byte;

unsigned int speed = 250000;

if (argc != 3) {
fprintf(stderr, "usage: %s <spi-port> <spi-speed>n", argv[0]);
fd_spi = open(argv[1], O_RDWR);
if (fd_spi < 0) {

if (sscanf(argv[2], “%d”, & speed) != 1) {
fprintf(stderr, “Wrong value for speed: %sn”, argv[2]);
if (ioctl(fd_spi, SPI_IOC_WR_MAX_SPEED_HZ, & speed) != 0) {
// setup mcp
printf(”Mode …[%d]\n”,write(fd_spi,&init_mcp_mode,3 ));

while(1)  // forever loop, byte increments
byte ++;
to_send[1]=0×15; // reg=latB
to_send[3]=0; // unused
if (write(fd_spi, &to_send, 3) != 3) {
perror(”write DATA”);
usleep(25000); // 26ms sleep

Run the file :

$ ./mcp_roller /dev/spidev0.0 400000

If you have connected leds+resistors to the PORTB of the mcp23s17, you should see it binary couting, visually.

Briefly, as for the code, mcp is first initialized with bank=0, no auto-increment feature, by the sequence :
0×40 = 01000000 == 0100 (fixed) + 000 (chip addr) + 0 (=write). (nb : the code example does not use the SPI chip addressing feature )
0×0A = register 0Ah == IOCON register addr
0×20 = 0b00100000 == bank0, SEQOP = OFF

then , code sets up Direction of port B :
0×40,0×01,0×00 : register 01h , all outputs (00h)

then , the program loops in sending portB latch register (15h) values, from 0 to FFh.
Writing to the latch of Port B is : 0×40,0×15,value.

Large parts of this code comes from : http://www.blaess.fr/christophe/2012/11/02/spi-sur-raspberry-pi-1/


February 7, 2013

Raspberry PI : How it all began

Filed under: Raspberry 3.14 — Tags: , , , , , , , — admin @ 8:26 am

I’m a newbie in the world of Linux, but i could’nt resist to try the raspberry Pi , model B.
It came that,for a project, i needed a kind of industrial PC , but i was puzzled by the pricing of that devices.
Surfing around i finally found the RasPI , based on an ARM controller. I decided then to dive into the ARM sea, using my past experience with Microchip pic series, C / C++ programming, and my hardware knowledge.

Let’s start !

The RasPi comes with nothing but the PCB itself.
To let it run, you need :

  • PC monitor or TV with HDMI input ( and and DVI to HDMI cable or adapter)
  • USB keyboard (and USB mouse)
  • Internet (ethernet) link
  • SD card wich will behave like a HDD for the OS
    (and thats great : no mechanical parts - a rock solid-state SSD-like drive ! )
  • A 5V power supply (else you will need to plug the rasPi to your computer’s usb port to get power from it )

Notice that the internet connection is needed to update the linux kernel when installing for the first time.

I purchased my RasPi model B from farnell, and i needed to have a quick ready-to-go configuration, so i decided to purchase also a 4GB SD card , containing the RaspBIAN OS ( debian for raspberry distro )  , wich was available at this time on farnell also.

Once everything was plugged, i ended with that thing :

On this picture you can see the yellow ethernet cable, the 2 usb cables to keyboard/mouse , the dvi cable , the blue SD card ( the socket is located on the bottom side of the RasPi pcb) , and the mini-USB cable just next to the SD , used to power the RasPI.

After power on, follows a (long) start sequence. it’s the first time booting, and a lot of things get adjusted . the next reboots are much faster than the first one.

There’s allready a plenty of articles dealing with the basic configurations of the RasPI ( keyboard mapping , set locale , boot options ) so i will not spend more time writing about that things. that settings are pretty easy to understand .

Once configured, the RasPI console looks like that ( the monitor is a FLATRON W2242T ) :

And the “LXDE” desktop  looks like that :

Opened apps : LX console in front , and Nano text editor


cat{ } { post_252 } { } 2009-2015 EIhIS Powered by WordPress