adding example to drive a Nokia 5110 display with spi

This commit is contained in:
dwelch67
2014-12-17 00:20:28 -05:00
parent 257a54fa26
commit 71b4233a6f
6 changed files with 3461 additions and 0 deletions

32
spi03/Makefile Normal file
View File

@@ -0,0 +1,32 @@
ARMGNU ?= arm-none-eabi
COPS = -mthumb -Wall -O2 -nostdlib -nostartfiles -ffreestanding
all : spi03.hex spi03.bin
clean :
rm -f *.o
rm -f *.bin
rm -f *.hex
rm -f *.elf
rm -f *.list
rm -f *.img
novectors.o : novectors.s
$(ARMGNU)-as novectors.s -o novectors.o
spi03.o : spi03.c fontdata.h
$(ARMGNU)-gcc $(COPS) -c spi03.c -o spi03.o
spi03.elf : memmap novectors.o spi03.o
$(ARMGNU)-ld novectors.o spi03.o -T memmap -o spi03.elf
$(ARMGNU)-objdump -D spi03.elf > spi03.list
spi03.bin : spi03.elf
$(ARMGNU)-objcopy spi03.elf -O binary spi03.bin
spi03.hex : spi03.elf
$(ARMGNU)-objcopy spi03.elf -O ihex spi03.hex

71
spi03/README Normal file
View File

@@ -0,0 +1,71 @@
See the top level README for information on where to find the
schematic and programmers reference manual for the ARM processor
on the raspberry pi. Also find information on how to load and run
these programs.
See the top level README for information on how to connect the raspi
uart to your host computer.
This example uses the spi interface with a Nokia 5110 display, these
are probably all recycled from old phones, who knows. There seems
to be a large quantity of the out there.
https://www.sparkfun.com/products/10168
http://www.adafruit.com/products/338
I got 5 of them on ebay for $13 with pins.
Perhaps some soldering is required, in some way you need to hook up
the signals.
I use these, but bought the 100 pack
https://www.sparkfun.com/products/10898
F/F means Female/Female both ends are female which is what you want
if you have a row of pins on yours.
Raspberry Pi signals of interest, all on the P1 connector
alt function 0 for these
GPIO8 SPI0_CE0_N P1-24
GPIO10 SPI0_MOSI P1-19
GPIO11 SPI0_SCLK P1-23
these are set gpio outputs
GPIO7 SPI0_CE1_N P1-26
GPIO25 GPIO_GEN6 P1-22
power and ground
+3V3 P1-1
GND P1-25
Now the sparkfun and adafruit and ebay special dont have the same
pinout, the same signals are there but not necessarily in the same
order. So this chart is not necessarily in the same pin order as your
display. The names may not exactly match either...
Nokia 5110 to raspi
GND P1-25 ground
VCC P1-1 power
DIN (MOSI) P1-19 data in, spi mosi
SCK/CLK P1-23 spi clock
D/C P1-22 data/command
RST P1-26 reset
CS/CE P1-24 spi chip select
Your board may have a row of pins above and below the display, they
are the same you only need one row.
The various examples out there use the same init routine.

2905
spi03/fontdata.h Normal file

File diff suppressed because it is too large Load Diff

12
spi03/memmap Normal file
View File

@@ -0,0 +1,12 @@
MEMORY
{
ram : ORIGIN = 0x8000, LENGTH = 0x1000
}
SECTIONS
{
.text : { *(.text*) } > ram
.bss : { *(.bss*) } > ram
}

49
spi03/novectors.s Normal file
View File

@@ -0,0 +1,49 @@
.globl _start
_start:
ldr sp,stack_start
ldr r0,thumb_start_add
bx r0
stack_start: .word 0x8000
thumb_start_add: .word thumb_start
.word 0
.word 0
.thumb
.thumb_func
thumb_start:
bl notmain
hang: b hang
.thumb_func
.globl PUT32
PUT32:
str r1,[r0]
bx lr
.thumb_func
.globl GET32
GET32:
ldr r0,[r0]
bx lr
.thumb_func
.globl dummy
dummy:
bx lr
;@-------------------------------------------------------------------------
;@
;@ Copyright (c) 2012 David Welch dwelch@dwelch.com
;@
;@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
;@
;@ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
;@
;@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;@
;@-------------------------------------------------------------------------

392
spi03/spi03.c Normal file
View File

@@ -0,0 +1,392 @@
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
#include "fontdata.h"
extern void PUT32 ( unsigned int, unsigned int );
extern unsigned int GET32 ( unsigned int );
extern void dummy ( unsigned int );
#define GPFSEL0 0x20200000
#define GPFSEL1 0x20200004
#define GPFSEL2 0x20200008
#define GPSET0 0x2020001C
#define GPCLR0 0x20200028
#define GPPUD 0x20200094
#define GPPUDCLK0 0x20200098
#define AUX_ENABLES 0x20215004
#define AUX_MU_IO_REG 0x20215040
#define AUX_MU_IER_REG 0x20215044
#define AUX_MU_IIR_REG 0x20215048
#define AUX_MU_LCR_REG 0x2021504C
#define AUX_MU_MCR_REG 0x20215050
#define AUX_MU_LSR_REG 0x20215054
#define AUX_MU_MSR_REG 0x20215058
#define AUX_MU_SCRATCH 0x2021505C
#define AUX_MU_CNTL_REG 0x20215060
#define AUX_MU_STAT_REG 0x20215064
#define AUX_MU_BAUD_REG 0x20215068
#define AUX_SPI0_CS 0x20204000
#define AUX_SPI0_FIFO 0x20204004
#define AUX_SPI0_CLK 0x20204008
#define AUX_SPI0_DLEN 0x2020400C
#define AUX_SPI0_LTOH 0x20204010
#define AUX_SPI0_DC 0x20204014
#define SETCONTRAST 0x81
#define DISPLAYALLONRESUME 0xA4
#define DISPLAYALLON 0xA5
#define NORMALDISPLAY 0xA6
#define INVERTDISPLAY 0xA7
#define DISPLAYOFF 0xAE
#define DISPLAYON 0xAF
#define SETDISPLAYOFFSET 0xD3
#define SETCOMPINS 0xDA
#define SETVCOMDESELECT 0xDB
#define SETDISPLAYCLOCKDIV 0xD5
#define SETPRECHARGE 0xD9
#define SETMULTIPLEX 0xA8
#define SETLOWCOLUMN 0x00
#define SETHIGHCOLUMN 0x10
#define SETSTARTLINE 0x40
#define MEMORYMODE 0x20
#define COMSCANINC 0xC0
#define COMSCANDEC 0xC8
#define SEGREMAP 0xA0
#define CHARGEPUMP 0x8D
#define EXTERNALVCC 0x01
#define SWITCHCAPVCC 0x02
unsigned int hex_screen_history[6];
//GPIO14 TXD0 and TXD1
//GPIO15 RXD0 and RXD1
//alt function 5 for uart1
//alt function 0 for uart0
//((250,000,000/115200)/8)-1 = 270
//------------------------------------------------------------------------
void uart_init ( void )
{
unsigned int ra;
ra=GET32(AUX_ENABLES);
ra|=1; //enable mini uart
PUT32(AUX_ENABLES,ra);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_CNTL_REG,0);
PUT32(AUX_MU_LCR_REG,3);
PUT32(AUX_MU_MCR_REG,0);
PUT32(AUX_MU_IER_REG,0);
PUT32(AUX_MU_IIR_REG,0xC6);
PUT32(AUX_MU_BAUD_REG,270);
//setup gpio before enabling uart
ra=GET32(GPFSEL1);
ra&=~(7<<12); //gpio14
ra|=2<<12; //alt5
ra&=~(7<<15); //gpio15
ra|=2<<15; //alt5
PUT32(GPFSEL1,ra);
PUT32(GPPUD,0);
for(ra=0;ra<150;ra++) dummy(ra);
PUT32(GPPUDCLK0,(1<<14)|(1<<15));
for(ra=0;ra<150;ra++) dummy(ra);
PUT32(GPPUDCLK0,0);
//enable uart
PUT32(AUX_MU_CNTL_REG,3);
}
//------------------------------------------------------------------------
void uart_putc ( unsigned int c )
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x20) break;
}
PUT32(AUX_MU_IO_REG,c);
}
//------------------------------------------------------------------------
void hexstrings ( unsigned int d )
{
//unsigned int ra;
unsigned int rb;
unsigned int rc;
rb=32;
while(1)
{
rb-=4;
rc=(d>>rb)&0xF;
if(rc>9) rc+=0x37; else rc+=0x30;
uart_putc(rc);
if(rb==0) break;
}
uart_putc(0x20);
}
//------------------------------------------------------------------------
void hexstring ( unsigned int d )
{
hexstrings(d);
uart_putc(0x0D);
uart_putc(0x0A);
}
//GPIO7 SPI0_CE1_N P1-26 (use for reset)
//GPIO8 SPI0_CE0_N P1-24
//GPIO9 SPI0_MISO P1-21
//GPIO10 SPI0_MOSI P1-19
//GPIO11 SPI0_SCLK P1-23
//alt function 0 for all of the above
//P1 1 +3V3
//P1 25 GND
//P1 22 GPIO25 use as D/C
//------------------------------------------------------------------------
void spi_init ( void )
{
unsigned int ra;
ra=GET32(AUX_ENABLES);
ra|=2; //enable spi0
PUT32(AUX_ENABLES,ra);
ra=GET32(GPFSEL0);
//ra&=~(7<<27); //gpio9
//ra|=4<<27; //alt0
//ra|=1<<27; //output
ra&=~(7<<24); //gpio8
ra|=4<<24; //alt0
ra&=~(7<<21); //gpio7
//ra|=4<<21; //alt0
ra|=1<<21; //output
PUT32(GPFSEL0,ra);
ra=GET32(GPFSEL1);
ra&=~(7<<0); //gpio10/
ra|=4<<0; //alt0
ra&=~(7<<3); //gpio11/
ra|=4<<3; //alt0
PUT32(GPFSEL1,ra);
ra=GET32(GPFSEL2);
ra&=~(7<<15); //gpio25/
ra|=1<<15; //output
PUT32(GPFSEL2,ra);
PUT32(AUX_SPI0_CS,0x0000030);
// PUT32(AUX_SPI0_CLK,0x0000); //slowest possible, could probably go faster here
PUT32(AUX_SPI0_CLK,26);
}
//------------------------------------------------------------------------
void spi_one_byte ( unsigned int x )
{
PUT32(AUX_SPI0_CS,0x000000B0); //TA=1 cs asserted
while(1)
{
if(GET32(AUX_SPI0_CS)&(1<<18)) break; //TXD
}
PUT32(AUX_SPI0_FIFO,x&0xFF);
while(1) if(GET32(AUX_SPI0_CS)&(1<<16)) break;
//while(1) if(GET32(AUX_SPI0_CS)&(1<<17)) break; //should I wait for this?
PUT32(AUX_SPI0_CS,0x00000000); //cs0 comes back up
}
//------------------------------------------------------------------------
void spi_command ( unsigned int cmd )
{
PUT32(GPCLR0,1<<25); //D/C = 0 for command
spi_one_byte(cmd);
}
//------------------------------------------------------------------------
void spi_data ( unsigned int data )
{
PUT32(GPSET0,1<<25); //D/C = 1 for data
spi_one_byte(data);
}
//------------------------------------------------------------------------
void show_string ( unsigned int row, const char *s )
{
unsigned int ra;
unsigned int rb;
unsigned int rc;
unsigned int rd;
row&=7;
spi_command(0x80); //column
spi_command(0x40|row); //row
rc=0;
for(ra=0;ra<11;ra++)
{
if(s[ra]==0) break;
for(rb=0;rb<8;rb++)
{
rd=s[ra];
spi_data(fontdata[rd][rb]);
rc++;
}
}
for(;rc<84;rc++) spi_data(0);
}
//------------------------------------------------------------------------
int notmain ( void )
{
unsigned int ra;
unsigned int rb;
unsigned int rc;
unsigned int rd;
unsigned int re;
unsigned int rf;
unsigned int tim[4];
uart_init();
hexstring(0x12345678);
for(ra=0;ra<10;ra++)
{
hexstring(ra);
}
spi_init();
PUT32(GPSET0,1<<7); //reset high
for(ra=0;ra<0x10000;ra++) dummy(ra);
PUT32(GPCLR0,1<<7); //reset low
for(ra=0;ra<0x10000;ra++) dummy(ra);
PUT32(GPSET0,1<<7); //reset high
spi_command(0x21); //extended commands
spi_command(0xB0); //vop
spi_command(0x04); //temp coef
spi_command(0x14); //bias mode 1:48
spi_command(0x20); //extended off
spi_command(0x0C); //display on
//experiment 1
if(1)
{
spi_command(0x80); //column
spi_command(0x40); //row
for(ra=0;ra<504;ra++) spi_data(ra);
}
//experiment 2
if(1)
{
spi_command(0x80); //column
spi_command(0x40); //row
for(ra=0;ra<10;ra++)
{
for(rb=0;rb<8;rb++) spi_data(fontdata[ra][rb]);
}
show_string(1,"Hello");
show_string(2,"World");
}
//experiment 3
//4 boxes wide a clock would have three full digits plus a 1
//2 boxes wide for the 1, 4 each for the other three 2+4+4+$ = 14
//84/14 = 6 so six pixes wide per box...(8 tall).
if(1)
{
for(ra=0;ra<10;ra++)
{
for(rb=0;rb<5;rb++)
{
spi_command(0x80); //column
spi_command(0x40|rb); //row
rc=clockdata[ra][rb];
for(rd=0x8;rd;rd>>=1)
{
if(rc&rd) re=0xFF;
else re=0x00;
for(rf=0;rf<6;rf++) spi_data(re);
}
}
for(rf=0;rf<0x100000;rf++) dummy(rf);
}
}
if(1)
{
spi_command(0x80); //column
spi_command(0x40|5); //row
for(rf=0;rf<84;rf++) spi_data(0x00);
tim[0]=0;
tim[1]=0;
tim[2]=0;
tim[3]=0;
while(1)
{
for(rb=0;rb<5;rb++)
{
spi_command(0x80); //column
spi_command(0x40|rb); //row
if(tim[0]==0) re=0x00;
else re=0xFF;
for(rf=0;rf<6;rf++) spi_data(re);
for(rf=0;rf<6;rf++) spi_data(0x00);
for(ra=1;ra<4;ra++)
{
rc=clockdata[tim[ra]][rb];
for(rd=0x8;rd;rd>>=1)
{
if(rc&rd) re=0xFF;
else re=0x00;
for(rf=0;rf<6;rf++) spi_data(re);
}
}
for(rf=0;rf<0x1000;rf++) dummy(rf);
}
if(tim[3]==9)
{
tim[3]=0;
if(tim[2]==9)
{
tim[2]=0;
if(tim[1]==9)
{
tim[1]=0;
if(tim[0]==1) break;
else tim[0]++;
}
else
{
tim[1]++;
}
}
else
{
tim[2]++;
}
}
else
{
tim[3]++;
}
}
}
hexstring(0x12345678);
return(0);
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//
// Copyright (c) 2014 David Welch dwelch@dwelch.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-------------------------------------------------------------------------