X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=firmware%2Fapps%2Fspi%2Fspi.c;h=f4c6560477548099466f38ca0cc083b7a4499262;hp=e6fb370a6c6b3c71485f67a5afe515bc6a5a649d;hb=6ee7ff03148c7af2a50695337910e7fd6906d476;hpb=aa1ad64b8f1a87590c9a94d32a1561bca33e843c diff --git a/firmware/apps/spi/spi.c b/firmware/apps/spi/spi.c index e6fb370..f4c6560 100644 --- a/firmware/apps/spi/spi.c +++ b/firmware/apps/spi/spi.c @@ -17,6 +17,7 @@ #define MISO BIT2 #define SCK BIT3 + //This could be more accurate. //Does it ever need to be? #define SPISPEED 0 @@ -31,12 +32,17 @@ //! Set up the pins for SPI mode. -unsigned char spisetup(){ +void spisetup(){ + P5OUT|=SS; P5DIR|=MOSI+SCK+SS; P5DIR&=~MISO; + + //Begin a new transaction. + P5OUT&=~SS; P5OUT|=SS; } + //! Read and write an SPI bit. unsigned char spitrans8(unsigned char byte){ unsigned int bit; @@ -66,11 +72,81 @@ unsigned char spitrans8(unsigned char byte){ return byte; } + +//! Enable SPI writing +void spiflash_wrten(){ + P5OUT&=~SS; //Drop !SS to begin transaction. + spitrans8(0x04);//Write Disable + P5OUT|=SS; //Raise !SS to end transaction. + P5OUT&=~SS; //Drop !SS to begin transaction. + spitrans8(0x06);//Write Enable + P5OUT|=SS; //Raise !SS to end transaction. +} + + +//! Grab the SPI flash status byte. +unsigned char spiflash_status(){ + unsigned char c; + P5OUT|=SS; //Raise !SS to end transaction. + P5OUT&=~SS; //Drop !SS to begin transaction. + spitrans8(0x05);//GET STATUS + c=spitrans8(0xFF); + P5OUT|=SS; //Raise !SS to end transaction. + return c; +} + + +//! Grab the SPI flash status byte. +void spiflash_setstatus(unsigned char c){ + P5OUT&=~SS; //Drop !SS to begin transaction. + spitrans8(0x01);//SET STATUS + spitrans8(c); + P5OUT|=SS; //Raise !SS to end transaction. + //return c; +} + +//! Peek some blocks. +void spiflash_peek(unsigned char app, + unsigned char verb, + unsigned char len){ + register char blocks=(len>3?cmddata[3]:1); + unsigned char i,j; + + P5OUT&=~SS; //Drop !SS to begin transaction. + spitrans8(0x03);//Flash Read Command + len=3;//write 3 byte pointer + for(i=0;i