wip, learning about the spi master

This commit is contained in:
dwelch67
2014-04-01 17:24:24 -04:00
parent 8b811c38fa
commit 378efabd89
5 changed files with 326 additions and 0 deletions

32
spi01/Makefile Normal file
View File

@@ -0,0 +1,32 @@
ARMGNU ?= arm-none-eabi
COPS = -mthumb -Wall -O2 -nostdlib -nostartfiles -ffreestanding
all : spi01.hex spi01.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
spi01.o : spi01.c
$(ARMGNU)-gcc $(COPS) -c spi01.c -o spi01.o
spi01.elf : memmap novectors.o spi01.o
$(ARMGNU)-ld novectors.o spi01.o -T memmap -o spi01.elf
$(ARMGNU)-objdump -D spi01.elf > spi01.list
spi01.bin : spi01.elf
$(ARMGNU)-objcopy spi01.elf -O binary spi01.bin
spi01.hex : spi01.elf
$(ARMGNU)-objcopy spi01.elf -O ihex spi01.hex

10
spi01/README Normal file
View File

@@ -0,0 +1,10 @@
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 is a work in progress, learning how to use the spi master.

12
spi01/memmap Normal file
View File

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

49
spi01/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.
;@
;@-------------------------------------------------------------------------

223
spi01/spi01.c Normal file
View File

@@ -0,0 +1,223 @@
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
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 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
//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);
}
//spix_clk = (system_clock_freq)/(2*(speed_field+1))
//spix_clk = 250000000/(2*(sf+1))
//spix_clk = 125000000/(sf+1)
//spix_clk = 125000000/(0xFFF+1) = 30.52khz.
//GPIO7 SPI0_CE1_N P1-26
//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...
//P1 1 +3V3
//P1 6 GND
//P1 9 GND
//P1 14 GND
//P1 20 GND
//P1 25 GND
//p1 22 GPIO_GEN6
//p1 26 GPIO_CE1
//------------------------------------------------------------------------
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&=~(7<<24); //gpio8
ra|=4<<24; //alt0
ra&=~(7<<21); //gpio7
ra|=4<<21; //alt0
PUT32(GPFSEL0,ra);
ra=GET32(GPFSEL1);
ra&=~(7<<0); //gpio10/
ra|=4<<0; //alt0
ra&=~(7<<3); //gpio11/
ra|=4<<0; //alt0
PUT32(GPFSEL1,ra);
PUT32(AUX_SPI0_CS,0x0000030);
PUT32(AUX_SPI0_CLK,0xFFFF);
}
//------------------------------------------------------------------------
int notmain ( void )
{
unsigned int ra;
uart_init();
hexstring(0x12345678);
for(ra=0;ra<10;ra++)
{
hexstring(ra);
}
spi_init();
PUT32(AUX_SPI0_CS,0x00400000); //cs1 low, reset
for(ra=0;ra<0x10000;ra++) dummy(ra);
PUT32(AUX_SPI0_CS,0x004000B0); //TA=1 cs asserted
while(1)
{
if(GET32(AUX_SPI0_CS)&(1<<18)) break; //TXD
}
PUT32(AUX_SPI0_FIFO,0xAC);
PUT32(AUX_SPI0_FIFO,0x53);
PUT32(AUX_SPI0_FIFO,0x00);
PUT32(AUX_SPI0_FIFO,0x00);
while(1)
{
ra=GET32(AUX_SPI0_CS);
hexstring(ra);
if(ra&(1<<16)) break;
}
hexstring(GET32(AUX_SPI0_CS));
hexstring(GET32(AUX_SPI0_FIFO));
hexstring(GET32(AUX_SPI0_CS));
hexstring(GET32(AUX_SPI0_FIFO));
hexstring(GET32(AUX_SPI0_CS));
hexstring(GET32(AUX_SPI0_FIFO));
hexstring(GET32(AUX_SPI0_CS));
hexstring(GET32(AUX_SPI0_FIFO));
hexstring(GET32(AUX_SPI0_CS));
PUT32(AUX_SPI0_CS,0x00400000); //cs comes back up
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.
//
//-------------------------------------------------------------------------