00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00055 #define USE_FIFO 1
00056
00057 #include "sceptre.h"
00058
00059
00060
00061
00062
00063
00064 #define SSPCR0_DSS 0
00065 #define SSPCR0_CPOL 6
00066 #define SSPCR0_CPHA 7
00067 #define SSPCR0_SCR 8
00068 #define SSPCR1_SSE 1
00069 #define SSPSR_TNF 1
00070 #define SSPSR_RNE 2
00071 #define SSPSR_BSY 4
00072
00073 #define PCONP_PCSPI1 10
00074
00075 #define SD_CS_BIT 20
00076 #define SD_CS_DIR IODIR0
00077 #define SD_CS_SET IOSET0
00078 #define SD_CS_CLR IOCLR0
00079 #define SD_CS_SEL PINSEL1
00080 #define SD_CS_SEL_BIT 8
00081
00082
00083 #define SPI_SPEED_20MHz 4
00084 #define SPI_SPEED_25MHz 4
00085 #define SPI_SPEED_400kHz 150
00086
00087
00088
00089 static inline void SELECT(void)
00090 {
00091 SD_CS_CLR = ( 1UL << SD_CS_BIT );
00092 }
00093
00094
00095 static inline void DESELECT( void )
00096 {
00097 SD_CS_SET = ( 1UL << SD_CS_BIT );
00098 }
00099
00100
00101 static void spiSetSpeed(BYTE speed)
00102 {
00103 speed &= 0xFE;
00104 if (speed<2) speed = 2;
00105 SSPCPSR = speed;
00106 }
00107
00108
00109 static inline BYTE spi_write_read(BYTE outgoing)
00110 {
00111 BYTE incoming;
00112
00114 SSPDR = outgoing;
00115 while ((SSPSR&(1<<SSPSR_RNE))==0)
00116 {
00117 }
00118 incoming = SSPDR;
00119
00120 return incoming;
00121 }
00122
00123
00124 #if USE_FIFO
00125
00126 #define FIFO_ELEM 8
00127
00128
00129
00130
00131 static inline void spi_rcvr_block(BYTE *buff, UINT btr)
00132 {
00133 UINT hwtr, startcnt, i, rec;
00134
00135 hwtr = btr/2;
00136 if (btr<FIFO_ELEM)
00137 {
00138 startcnt = hwtr;
00139 }
00140 else
00141 {
00142 startcnt = FIFO_ELEM;
00143 }
00144
00145 SSPCR0 |= 0x0000000f;
00146
00147 for (i=startcnt; i; i--)
00148 {
00149 SSPDR = 0xffff;
00150 }
00151
00152 do
00153 {
00154 while ((SSPSR&(1<<SSPSR_RNE))==0)
00155 {
00156
00157 }
00158 rec = SSPDR;
00159 if (i<(hwtr-startcnt))
00160 {
00161 SSPDR = 0xffff;
00162 }
00163 *buff++ = (BYTE)(rec>>8);
00164 *buff++ = (BYTE)(rec);
00165 i++;
00166 }
00167 while (i<hwtr);
00168
00169 SSPCR0 &= ~0x00000008;
00170 }
00171
00172
00173
00174
00175 static inline void spi_xmit_block(const BYTE *buff)
00176 {
00177 UINT cnt;
00178 WORD data;
00179
00180 SSPCR0 |= 0x0000000f;
00181
00182 for (cnt=0; cnt<(512/2); cnt++)
00183 {
00184 while ((SSPSR&(1<<SSPSR_TNF))==0)
00185 {
00186
00187 }
00188 data = (*buff++) << 8;
00189 data |= *buff++;
00190 SSPDR = data;
00191 }
00192
00193 while ((SSPSR&(1<<SSPSR_BSY))!=0)
00194 {
00195
00196 }
00197 while ((SSPSR&(1<<SSPSR_RNE))!=0)
00198 {
00199 data = SSPDR;
00200 }
00201
00202 SSPCR0 &= ~0x00000008;
00203 }
00204 #endif
00205
00206
00207 static void init_spi( void )
00208 {
00209 UINT dummy;
00210
00211
00212 SD_CS_SEL = (SD_CS_SEL & ~(3UL<<SD_CS_SEL_BIT));
00213 SD_CS_DIR |= (1UL << SD_CS_BIT);
00214 DESELECT();
00215
00216
00217 PCONP |= (1UL << PCONP_PCSPI1);
00218
00219
00220 PINSEL1 &= ~(
00221 (3UL << 2) |
00222 (3UL << 4) |
00223 (3UL << 6) |
00224 (3UL << 8)
00225 );
00226
00227 PINSEL1 |= (
00228 (2UL << 2) |
00229 (2UL << 4) |
00230 (2UL << 6)
00231
00232 );
00233
00234
00235 SSPCR0 = ((8-1)<<SSPCR0_DSS) | (0<<SSPCR0_CPOL) | (0<<SSPCR0_CPHA) | (0<<SSPCR0_SCR);
00236 SSPCR1 = (1<<SSPCR1_SSE);
00237
00238
00239 spiSetSpeed(SPI_SPEED_400kHz);
00240
00241
00242 while ((SSPSR&(1<<SSPSR_BSY))!=0)
00243 {
00244
00245 }
00246
00247 while ((SSPSR&(1<<SSPSR_RNE))!=0)
00248 {
00249 dummy = SSPDR;
00250 }
00251 }
00252
00253
00254 static void close_spi( void )
00255 {
00256
00257 SSPCR1 = 0;
00258
00259
00260 PINSEL1 &= ~(
00261 (3UL << 2) |
00262 (3UL << 4) |
00263 (3UL << 6) |
00264 (3UL << 8)
00265 );
00266
00267
00268 PCONP &= ~(1UL << PCONP_PCSPI1);
00269
00270 }
00271