📄 spidrv.lst
字号:
197 2 Rxtemp = SBUF; //接收
198 2 //SBUF = Rxtemp; //发送
199 2 rx_ok = 1;
200 2 return;
201 2 }
202 1 if(TI)
203 1 {
204 2 TI = 0;
205 2 MYTI = 1;
206 2 }
207 1 }
208
209 void IO_Send_Byte(uchar out)
210 {
211 1 uchar i = 0;
212 1 _cs = 0;
213 1 for (i = 0; i < 8; i++)
214 1 {
215 2 if ((out & 0x80) == 0x80) /* check if MSB is high */
216 2 _di = 1;
217 2 else
218 2 _di = 0; /* if not, set to low */
219 2 _clk = 1; /* toggle clock high */
220 2 out = (out << 1); /* shift 1 place for next bit */
221 2 nop();nop();nop();nop();
222 2 _clk = 0; /* toggle clock low */
223 2 }
224 1 }
225
226 uchar IO_Get_Byte()
227 {
228 1 uchar i = 0, in = 0, temp = 0;
229 1 _cs = 0;
230 1 for (i = 0; i < 8; i++)
231 1 {
232 2 in = (in << 1); /* shift 1 place to the left or shift in 0 */
233 2 temp = _do; /* save input */
234 2 _clk = 1; /* toggle clock high */
235 2 if (temp == 1) /* check to see if bit is high */
236 2 in |= 0x01; /* if high, make bit high */
237 2 _clk = 0; /* toggle clock low */
238 2 }
239 1 return in;
240 1 }
241
C51 COMPILER V7.50 SPIDRV 09/25/2008 17:18:42 PAGE 5
242 void delay(uchar tt)
243 {
244 1 while(tt--);
245 1 }
246
247 void IO_Wait_Busy()
248 {
249 1 /* waste time until not busy WEL & Busy bit all be 1 (0x03). */
250 1 while (IO_Read_StatusReg() == 0x03)
251 1 IO_Read_StatusReg();
252 1 }
253
254 void IO_init()
255 {
256 1 _clk = 0; /* set clock to low initial state for SPI operation mode 0 */
257 1 // _clk = 1; /* set clock to low initial state for SPI operation mode 3 */
258 1 // _hold = 1;
259 1 _wp = 1;
260 1 _cs = 1;
261 1
262 1 IO_Write_Disable();
263 1
264 1 }
265
266 uchar IO_Read_StatusReg()
267 {
268 1 uchar byte = 0;
269 1 _cs = 0; /* enable device */
270 1 IO_Send_Byte(W25P_ReadStatusReg); /* send Read Status Register command */
271 1 byte = IO_Get_Byte(); /* receive byte */
272 1 _cs = 1; /* disable device */
273 1
274 1 return byte;
275 1 }
276
277 void IO_Write_StatusReg(byte)
278 {
279 1 _cs = 0; /* enable device */
280 1 IO_Send_Byte(W25P_WriteStatusReg); /* select write to status register */
281 1 IO_Send_Byte(byte); /* data that will change the status(only bits 2,3,7 can be written) */
282 1 _cs = 1; /* disable the device */
283 1 }
284
285 void IO_Write_Enable()
286 {
287 1 _cs = 0; /* enable device */
288 1 IO_Send_Byte(W25P_WriteEnable); /* send W25P_Write_Enable command */
289 1 _cs = 1; /* disable device */
290 1 }
291
292 void IO_PowerDown()
293 {
294 1 _cs = 0;; /* enable device */
295 1 IO_Send_Byte(W25P_PowerDown); /* send W25P_PowerDown command 0xB9 */
296 1 _cs = 1;; /* disable device */
297 1 delay(6); /* remain CS high for tPD = 3uS */
298 1 }
299
300 void IO_ReleasePowerDown()
301 {
302 1 _cs = 0; /* enable device */
303 1 IO_Send_Byte(W25P_ReleasePowerDown); /* send W25P_PowerDown command 0xAB */
C51 COMPILER V7.50 SPIDRV 09/25/2008 17:18:42 PAGE 6
304 1 _cs = 1; /* disable device */
305 1 delay(6); /* remain CS high for tRES1 = 3uS */
306 1 }
307
308 uchar IO_Read_ID1()
309 {
310 1 uchar byte;
311 1 _cs = 0; /* enable device */
312 1 IO_Send_Byte(W25P_DeviceID); /* send read device ID command (ABh) */
313 1 IO_Send_Byte(0); /* send address */
314 1 IO_Send_Byte(0); /* send address */
315 1 IO_Send_Byte(0); /* send 3_Dummy address */
316 1 byte = IO_Get_Byte(); /* receive Device ID byte */
317 1
318 1 _cs = 1; /* disable device */
319 1 delay(4); /* remain CS high for tRES2 = 1.8uS */
320 1
321 1 return byte;
322 1 }
323
324 uint IO_Read_ID2(uchar ID_Addr)
325 {
326 1 uint IData16;
327 1 _cs = 0; /* enable device */
328 1 IO_Send_Byte(W25P_ManufactDeviceID); /* send read ID command (90h) */
329 1 IO_Send_Byte(0x00); /* send address */
330 1 IO_Send_Byte(0x00); /* send address */
331 1 IO_Send_Byte(ID_Addr); /* send W25Pxx selectable ID address 00H or 01H */
332 1 IData16 = IO_Get_Byte()<<8; /* receive Manufature or Device ID byte */
333 1 IData16 |= IO_Get_Byte(); /* receive Device or Manufacture ID byte */
334 1 _cs = 1; /* disable device */
335 1
336 1 return IData16;
337 1 }
338
339 uint IO_Read_ID3()
340 {
341 1 uint IData16;
342 1 _cs = 0; /* enable device */
343 1 IO_Send_Byte(0x9f); /* send read ID command (90h) */
344 1
345 1 IData16 = IO_Get_Byte()<<8; /* receive Manufature or Device ID byte */
346 1 IData16 |= IO_Get_Byte(); /* receive Device or Manufacture ID byte */
347 1 tx_buff[2] = IO_Get_Byte();
348 1 _cs = 1; /* disable device */
349 1
350 1 return IData16;
351 1 }
352
353 uchar IO_Read_Byte(uint32 Dst_Addr)
354 {
355 1 uchar byte = 0;
356 1
357 1 _cs = 0; /* enable device */
358 1 IO_Send_Byte(W25P_ReadData); /* read command */
359 1 IO_Send_Byte((uchar)((Dst_Addr & 0xFFFFFF) >> 16)); /* send 3 address bytes */
360 1 IO_Send_Byte((uchar)((Dst_Addr & 0xFFFF) >> 8));
361 1 IO_Send_Byte((uchar)(Dst_Addr & 0xFF));
362 1 byte = IO_Get_Byte();
363 1 _cs = 1; /* disable device */
364 1
365 1 return byte; /* return one byte read */
C51 COMPILER V7.50 SPIDRV 09/25/2008 17:18:42 PAGE 7
366 1 }
367
368 void IO_Read_nBytes(uint32 Dst_Addr, uchar nBytes_128)
369 {
370 1 uint32 i = 0;
371 1
372 1 _cs = 0; /* enable device */
373 1 IO_Send_Byte(W25P_ReadData); /* read command */
374 1 IO_Send_Byte(((Dst_Addr & 0xFFFFFF) >> 16)); /* send 3 address bytes */
375 1 IO_Send_Byte(((Dst_Addr & 0xFFFF) >> 8));
376 1 IO_Send_Byte(Dst_Addr & 0xFF);
377 1 for (i = 0; i < nBytes_128; i++) /* read until no_bytes is reached */
378 1 {
379 2 upper_128[i] = IO_Get_Byte(); /* receive byte and store at address 80H - FFH */
380 2 }
381 1
382 1 _cs = 1; /* disable device */
383 1
384 1 }
385
386 uchar IO_FastRead_Byte(uint32 Dst_Addr)
387 {
388 1 uchar byte = 0;
389 1
390 1 _cs = 0; /* enable device */
391 1 IO_Send_Byte(W25P_FastReadData); /* fast read command */
392 1 IO_Send_Byte(((Dst_Addr & 0xFFFFFF) >> 16)); /* send 3 address bytes */
393 1 IO_Send_Byte(((Dst_Addr & 0xFFFF) >> 8));
394 1 IO_Send_Byte(Dst_Addr & 0xFF);
395 1 IO_Send_Byte(0xFF); /*dummy byte*/
396 1 byte = IO_Get_Byte();
397 1 _cs = 1; /* disable device */
398 1
399 1 return byte; /* return one byte read */
400 1 }
401
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -