📄 spidrv.lst
字号:
187 {
188 1 if(RI)
189 1 {
190 2 RI = 0;
191 2 Rxtemp = SBUF; //接收
192 2 //SBUF = Rxtemp; //发送
193 2 rx_ok = 1;
194 2 return;
195 2 }
196 1 if(TI)
197 1 {
198 2 TI = 0;
199 2 MYTI = 1;
200 2 }
201 1 }
202
203 void SPI_Send_Byte(uchar out)
204 {
205 1 uchar i = 0;
206 1 W25X_CS = 0;
207 1 for (i = 0; i < 8; i++)
208 1 {
209 2 if ((out & 0x80) == 0x80) /* check if MSB is high */
210 2 W25X_DI = 1;
211 2 else
212 2 W25X_DI = 0; /* if not, set to low */
213 2 W25X_CLK = 1; /* toggle clock high */
214 2 out = (out << 1); /* shift 1 place for next bit */
215 2 nop();nop();nop();nop();
216 2 W25X_CLK = 0; /* toggle clock low */
217 2 }
218 1 }
219 //=================================================================================================
220 uchar SPI_Get_Byte()
221 { uchar i = 0, in = 0, temp = 0;
222 1 W25X_CS = 0;
223 1 for (i = 0; i < 8; i++)
224 1 { in = (in << 1); // shift 1 place to the left or shift in 0
225 2 temp = W25X_DO; // save input
226 2 W25X_CLK = 1; // toggle clock high
227 2 if (temp == 1) // check to see if bit is high
228 2 in |= 0x01; // if high, make bit high
229 2 W25X_CLK = 0; // toggle clock low
230 2 }
231 1 return in;
232 1 }
233 //=================================================================================================
234 void delay(uchar tt)
235 {
236 1 while(tt--);
237 1 }
238 //=================================================================================================
239 void SPI_Wait_Busy()
240 {
241 1 /* waste time until not busy WEL & Busy bit all be 1 (0x03). */
C51 COMPILER V7.50 SPIDRV 01/11/2009 22:30:34 PAGE 5
242 1 while (SPI_Read_StatusReg() == 0x03)
243 1 SPI_Read_StatusReg();
244 1 }
245 //=================================================================================================
246 void SPI_init()
247 {
248 1 W25X_CLK = 0; // set clock to low initial state for SPI operation mode 0
249 1 // W25X_CLK = 1; // set clock to High initial state for SPI operation mode 3
250 1 // _hold = 1;
251 1 W25X_WP = 1;
252 1 W25X_CS = 1;
253 1 SPI_WriteW25X_Disable();
254 1 }
255 //=================================================================================================
256 uchar SPI_Read_StatusReg()
257 { uchar byte = 0;
258 1 W25X_CS = 0; // enable device
259 1 SPI_Send_Byte(W25X_ReadStatusReg); // send Read Status Register command
260 1 byte = SPI_Get_Byte(); // receive byte
261 1 W25X_CS = 1; // disable device
262 1 return byte;
263 1 }
264 //=================================================================================================
265 void SPI_Write_StatusReg(byte)
266 { W25X_CS = 0; // enable device
267 1 SPI_Send_Byte(W25X_WriteStatusReg); // select write to status register
268 1 SPI_Send_Byte(byte); // data that will change the status(only bits 2,3,7 can be written)
269 1 W25X_CS = 1; // disable the device
270 1 }
271 //=================================================================================================
272 void SPI_Write_Enable()
273 { W25X_CS = 0; // enable device
274 1 SPI_Send_Byte(W25X_WriteEnable); // send W25X_Write_Enable command
275 1 W25X_CS = 1; // disable device
276 1 }
277 //=================================================================================================
278 void SPI_PowerDown()
279 { W25X_CS = 0; // enable device
280 1 SPI_Send_Byte(W25X_PowerDown); // send W25X_PowerDown command 0xB9
281 1 W25X_CS = 1; // disable device
282 1 delay(6); // remain CS high for tPD = 3uS
283 1 }
284 //=================================================================================================
285 void SPI_ReleasePowerDown()
286 { W25X_CS = 0; // enable device
287 1 SPI_Send_Byte(W25X_ReleasePowerDown); // send W25X_PowerDown command 0xAB
288 1 W25X_CS = 1; // disable device
289 1 delay(6); // remain CS high for tRES1 = 3uS
290 1 }
291 //=================================================================================================
292 uchar SPI_Read_ID1()
293 { uchar byte;
294 1 W25X_CS = 0; // enable device
295 1 SPI_Send_Byte(W25X_DeviceID); // send read device ID command (ABh)
296 1 SPI_Send_Byte(0); // send address
297 1 SPI_Send_Byte(0); // send address
298 1 SPI_Send_Byte(0); // send 3_Dummy address
299 1 byte = SPI_Get_Byte(); // receive Device ID byte
300 1
301 1 W25X_CS = 1; // disable device
302 1 delay(4); // remain CS high for tRES2 = 1.8uS
303 1 return byte;
C51 COMPILER V7.50 SPIDRV 01/11/2009 22:30:34 PAGE 6
304 1 }
305 //=================================================================================================
306 uint SPI_Read_ID2(uchar ID_Addr)
307 {
308 1 uint IData16;
309 1 W25X_CS = 0; // enable device
310 1 SPI_Send_Byte(W25X_ManufactDeviceID); // send read ID command (90h)
311 1 SPI_Send_Byte(0x00); // send address
312 1 SPI_Send_Byte(0x00); // send address
313 1 SPI_Send_Byte(ID_Addr); // send W25Pxx selectable ID address 00H or 01H
314 1 IData16 = SPI_Get_Byte()<<8; // receive Manufature or Device ID byte
315 1 IData16 |= SPI_Get_Byte(); // receive Device or Manufacture ID byte
316 1 W25X_CS = 1; // disable device
317 1
318 1 return IData16;
319 1 }
320 //=================================================================================================
321 uint SPI_Read_ID3()
322 { uint IData16;
323 1 W25X_CS = 0; // enable device
324 1 SPI_Send_Byte(0x9f); // send read ID command (90h)
325 1
326 1 IData16 = SPI_Get_Byte()<<8; // receive Manufature or Device ID byte
327 1 IData16 |= SPI_Get_Byte(); // receive Device or Manufacture ID byte
328 1 tx_buff[2] = SPI_Get_Byte();
329 1 W25X_CS = 1; // disable device
330 1 return IData16;
331 1 }
332 //=================================================================================================
333 uchar SPI_Read_Byte(uint32 Dst_Addr)
334 {
335 1 uchar byte = 0;
336 1
337 1 W25X_CS = 0; // enable device
338 1 SPI_Send_Byte(W25X_ReadData); // read command
339 1 SPI_Send_Byte((uchar)((Dst_Addr & 0xFFFFFF) >> 16));// send 3 address bytes
340 1 SPI_Send_Byte((uchar)((Dst_Addr & 0xFFFF) >> 8));
341 1 SPI_Send_Byte((uchar)(Dst_Addr & 0xFF));
342 1 byte = SPI_Get_Byte();
343 1 W25X_CS = 1; // disable device
344 1
345 1 return byte; // return one byte read
346 1 }
347 //=================================================================================================
348 void SPI_Read_nBytes(uint32 Dst_Addr, uchar nBytes_128)
349 {
350 1 uint32 i = 0;
351 1
352 1 W25X_CS = 0; // enable device
353 1 SPI_Send_Byte(W25X_ReadData); // read command
354 1 SPI_Send_Byte(((Dst_Addr & 0xFFFFFF) >> 16)); // send 3 address bytes
355 1 SPI_Send_Byte(((Dst_Addr & 0xFFFF) >> 8));
356 1 SPI_Send_Byte(Dst_Addr & 0xFF);
357 1 for (i = 0; i < nBytes_128; i++) // read until no_bytes is reached
358 1 upper_128[i] = SPI_Get_Byte(); // receive byte and store at address 80H - FFH
359 1 W25X_CS = 1; // disable device
360 1 }
361 //=================================================================================================
362 uchar SPI_FastRead_Byte(uint32 Dst_Addr)
363 {
364 1 uchar byte = 0;
365 1
C51 COMPILER V7.50 SPIDRV 01/11/2009 22:30:34 PAGE 7
366 1 W25X_CS = 0; /* enable device */
367 1 SPI_Send_Byte(W25X_FastReadData); /* fast read command */
368 1 SPI_Send_Byte(((Dst_Addr & 0xFFFFFF) >> 16)); /* send 3 address bytes */
369 1 SPI_Send_Byte(((Dst_Addr & 0xFFFF) >> 8));
370 1 SPI_Send_Byte(Dst_Addr & 0xFF);
371 1 SPI_Send_Byte(0xFF); /*dummy byte*/
372 1 byte = SPI_Get_Byte();
373 1 W25X_CS = 1; /* disable device */
374 1
375 1 return byte; /* return one byte read */
376 1 }
377 //=================================================================================================
378 void SPI_FastRead_nBytes(uint32 Dst_Addr, uchar nBytes_128)
379 {
380 1 uchar i = 0;
381 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -