00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "at91sam7s64.h"
00021 #include "global.h"
00022
00023
00024 #include "spi.h"
00025 #include "spiflash.h"
00026
00027
00028
00029
00030
00031
00032
00033 #define SPIFLASH_CONFIG_CS AT91C_BASE_PIOA->PIO_PER = AT91C_PIO_PA30; \
00034 AT91C_BASE_PIOA->PIO_OER = AT91C_PIO_PA30
00035 #define SPIFLASH_ASSERT_CS AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA30
00036 #define SPIFLASH_RELEASE_CS AT91C_BASE_PIOA->PIO_SODR = AT91C_PIO_PA30
00037
00038
00039 void spiflashInit(void)
00040 {
00041
00042
00043
00044 SPIFLASH_RELEASE_CS;
00045 SPIFLASH_CONFIG_CS;
00046 }
00047
00048 unsigned short spiflashGetID(void)
00049 {
00050 unsigned short id;
00051
00052 SPIFLASH_ASSERT_CS;
00053 spiTransferByte(SPIFLASH_CMD_RDID);
00054 id = spiTransferByte(0x00)<<8;
00055 id |= spiTransferByte(0x00);
00056 SPIFLASH_RELEASE_CS;
00057
00058 return id;
00059 }
00060
00061 void spiflashChipErase(void)
00062 {
00063
00064 SPIFLASH_ASSERT_CS;
00065 spiTransferByte(SPIFLASH_CMD_WREN);
00066 SPIFLASH_RELEASE_CS;
00067
00068
00069 spiTransferByte(0x00);
00070
00071
00072 SPIFLASH_ASSERT_CS;
00073 spiTransferByte(SPIFLASH_CMD_CHIPERASE);
00074 SPIFLASH_RELEASE_CS;
00075
00076
00077 spiTransferByte(0x00);
00078
00079
00080 SPIFLASH_ASSERT_CS;
00081 spiTransferByte(SPIFLASH_CMD_RDSR);
00082 while(spiTransferByte(0x00) & SPIFLASH_STATUS_BUSY);
00083 SPIFLASH_RELEASE_CS;
00084 }
00085
00086 void spiflashRead(unsigned long addr, unsigned long nbytes, unsigned char *data)
00087 {
00088
00089 SPIFLASH_ASSERT_CS;
00090
00091 spiTransferByte(SPIFLASH_CMD_READ);
00092
00093 spiTransferByte(addr>>16);
00094 spiTransferByte(addr>>8);
00095 spiTransferByte(addr>>0);
00096
00097 while(nbytes--)
00098 *data++ = spiTransferByte(0x00);
00099
00100 SPIFLASH_RELEASE_CS;
00101 }
00102
00103 void spiflashWrite(unsigned long addr, unsigned long nbytes, unsigned char *data)
00104 {
00105 unsigned int page;
00106 unsigned int i;
00107 unsigned int pagelen;
00108
00109
00110 for(page=0; page<((nbytes+SPIFLASH_PAGESIZE-1)>>8); page++)
00111 {
00112
00113
00114
00115 SPIFLASH_ASSERT_CS;
00116 spiTransferByte(SPIFLASH_CMD_WREN);
00117 SPIFLASH_RELEASE_CS;
00118
00119
00120 spiTransferByte(0x00);
00121
00122
00123 SPIFLASH_ASSERT_CS;
00124
00125 spiTransferByte(SPIFLASH_CMD_PAGEPROG);
00126
00127 spiTransferByte(addr>>16);
00128 spiTransferByte(addr>>8);
00129 spiTransferByte(addr>>0);
00130
00131 if( ((page<<8)+SPIFLASH_PAGESIZE) <= nbytes)
00132 pagelen = SPIFLASH_PAGESIZE;
00133 else
00134 pagelen = nbytes-(page<<8);
00135
00136 for(i=0; i<pagelen; i++)
00137 spiTransferByte(*data++);
00138
00139 SPIFLASH_RELEASE_CS;
00140
00141
00142 spiTransferByte(0x00);
00143
00144
00145 SPIFLASH_ASSERT_CS;
00146 spiTransferByte(SPIFLASH_CMD_RDSR);
00147 while(spiTransferByte(0x00) & SPIFLASH_STATUS_BUSY);
00148 SPIFLASH_RELEASE_CS;
00149
00150
00151 spiTransferByte(0x00);
00152 }
00153 }