flash初始化子程序_第1页
flash初始化子程序_第2页
flash初始化子程序_第3页
flash初始化子程序_第4页
flash初始化子程序_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、文档来源为 :从网络收集整理 .word 版本可编辑 .欢迎下载支持/*Filename: flash.c*Description :flash 数据读 .*Author: ldm*Date: 2013-11-29*/#include doData.h#include main.h#include flash.h#include gprs.h#include PubUse.h#include Structure.h/#include timeapp.h#include #include hal_uart.h#include hal_timer.h#include /= #define FLAS

2、H_SPI_PAGESIZE 0x100#define FLASH_SPI_SECTORSZIE 0x1000#define FLASH_SPI_HIGHT_ADDR 0x1000000 extern UPLOADSTATE_u _gcUpload;extern TESTSTATE_u _gctest;extern uint32 addr_flash;/#pragma udata bank3 = 0x300 uint8 SpiCheckBuf256;#pragma udataextern uint8 buf0256;extern uint8 DATABufGPRS_UART_BUF_SIZE;

3、extern void delay_1us(uint16 i);extern void delay_1ms(uint16 i); /* */struct bit_def char b0:1;char b1:1;char b2:1;char b3:1;char b4:1;char b5:1;char b6:1;char b7:1;union byte_defstruct bit_def bit1; char byte;/* Function name: void sleep_Flashcom(void);* Descriptions: invaild Flash communication.*

4、input parameters: none.* output argument: none* Note:*void sleep_PhoFlashcom(void) PhoDataPortIn;SclkToPhoPortIn;SicToPhoPortIn;PhoFlashCs_in;ReadPho_off;void sleep_RecFlashcom(void) RecDataPortIn;SclkToRecPortIn;SicToRecPortIn;RecFlashCs_in;/无效SPI通信/时钟脚输入状态/ 数字脚输入状态 / 选择脚输入/读拍照模块信号输出无效/无效SPI通信/ 时钟脚

5、输入状态/ 数字脚输入状态/ 选择脚输入10文档来源为 :从网络收集整理 .word 版本可编辑 .* * Function name: ready_PhoFlashcomRead(void);* Descriptions: ready Flash communication.* input parameters: none.* output argument: none* Note:SPI操作准备*/void ready_PhoFlashcomRead(void) /SPI 操作前的准备PhoFlashCs_out;/CS 控制脚输出SclkToPhoPortOut;SicToPhoPort

6、Out;PhoFlashCs_inactive;SclkToPho_off;PhoDataPortIn;/ 时钟脚输出状态/ 数字脚输出状态/CS 控制脚无效/ 拉低芯片时钟线 / 拍照数据口输入/CS 控制脚输出 / 时钟脚输出状态 / 数字脚输出状态 /CS 控制脚无效/ 拉低芯片时钟线 / 录音数据口输入void ready_RecFlashcomRead(void) /SPI 操作前的准备RecFlashCs_out;SclkToRecPortOut; SicToRecPortOut;RecFlashCs_inactive;SclkToRec_off; RecDataPortIn;*

7、* Function name: void ready_PhoFlashcomWrite(void);* Descriptions: ready Flash communication.* input parameters: none.* output argument: none* Note:SPI操作准备 */CS 控制脚输出 / 时钟脚输出状态 / 数字脚输出状态 /CS 控制脚无效 / 数字脚低/ 拉低芯片时钟线 / 拍照数据口输出/CS 控制脚输出 / 时钟脚输出状态 / 数字脚输出状态 /CS 控制脚无效 / 数字脚低/ 拉低芯片时钟线 / 拍照数据口输出void ready_Ph

8、oFlashcomWrite(void) /SPI 操作前的准备PhoFlashCs_out;SclkToPhoPortOut;SicToPhoPortOut;PhoFlashCs_inactive;/ SicToPho_off; SclkToPho_off; PhoDataPortOut;void ready_RecFlashcomWrite(void) /SPI 操作前的准备RecFlashCs_out;SclkToRecPortOut;SicToRecPortOut;RecFlashCs_inactive;/ SicToRec_off;SclkToRec_off;RecDataPortO

9、ut;* * Function name: uint8 read_1bytePhoFlash(void);* Descriptions: get byte from Flash output* input parameters: none* output argument: return the obtaned byte which type is uint8 * Note:*/uint8 read_1bytePhoFlash(void) static uint8 i;ClrWdt();PhoDataPortIn;SclkToPho_on; delay_1us(5); i=PhoDataPor

10、t; SclkToPho_off; delay_1us(5); return i;uint8 read_1byteRecFlash(void) static uint8 i;ClrWdt();RecDataPortIn;SclkToRec_on; delay_1us(5); i=RecDataPort; SclkToRec_off; delay_1us(5); return i;/ 从 Flash 读 1 字节数据/ 拍照数据口输入/ 拉高芯片时钟线/ 拉低芯片时钟线/ 从 Flash 读 1 字节数据/ 拍照数据口输入/ 拉高芯片时钟线/ 拉低芯片时钟线/* Function name: w

11、rite_1bytePhoFlash(uint8 PB_data) ;* Descriptions: write one byte to the Flash* input parameters: PB_data:the data ready to be write* output argument: none./* Note:PB_data: 待写数据*void write_1bytePhoFlash(uint8 PB_data) /写1字节数据到Flash ,SPI的写操作uint8 i;union byte_def LUB_data;LUB_data.byte=PB_data; / 待写数

12、据 for(i=0;i8;i+)SicToPho=LUB_; / 发送字节数据的高位delay_1us(5);SclkToPho_on;LUB_data.byte=1;delay_1us(5);SclkToPho_off;delay_1us(5);delay_1us(10);ClrWdt();void write_1byteRecFlash(uint8 PB_data)uint8 i;union byte_def LUB_data;LUB_data.byte=PB_data; for(i=0;i8;i+)/ 拉高芯片时钟线 ,单片机发送数据/ 取下一个位/ 拉低芯片时钟线, Flash 接收数

13、据 /us_delay(20);待写数据/写1字节数据到Flash , SPI的写操作SicToRec=LUB_; delay_1us(5); SclkToRec_on;LUB_data.byte= FLASH_SPI_HIGHT_ADDR)HightAddr = 1;Flash_SendBytePho(FLASH_CMD_WREN); Flash_SendBytePho(FLASH_CMD_PARALLEL); / 并口模式 ready_PhoFlashcomRead();/SPI 操作前准备delay_1us(5);/ 延时 10us;PhoFlashCs_active;/CS 控制脚低,

14、有效delay_1us(5);/ 延时 10us;write_1bytePhoFlash(FLASH_CMD_READ);/*! Send Read from Memory instruction */ if(HightAddr) /*! 24); write_1bytePhoFlash(ReadAddr & 0xFF0000) 16); /*! 8); /*! Send ReadAddr medium nibble address byte to read from */write_1bytePhoFlash(ReadAddr & 0xFF); /*! Send ReadAddr low n

15、ibble address byte to read from */for(i=0;i(NumByteToRead);i+) / SpiCheckBufi=read_1bytePhoFlash();/ 先读高字节 p=read_1bytePhoFlash();/ 先读高字节 *pBuffer =p;/ DATABufi+23 =read_1bytePhoFlash();/ 先读高字节 pBuffer+;if(p=0xff) pp+; delay_1us(5); / 延时 10us;PhoFlashCs_inactive;/CS 控制脚高 ,无效delay_1us(5);/ 延时 10us;sl

16、eep_PhoFlashcom(); / 无效 SPI通信 delay_1us(5);/ 延时 10us;Jif(pp= FLASH_SPI_HIGHT_ADDR)HightAddr = 1;Flash_SendByteRec(FLASH_CMD_WREN); Flash_SendByteRec(FLASH_CMD_PARALLEL); / 并口模式 ready_RecFlashcomRead();/SPI 操作前准备delay_1us(5);/ 延时 10us;RecFlashCs_active;/CS 控制脚低 ,有效delay_1us(5);/ 延时 10us;write_1byteRe

17、cFlash(FLASH_CMD_READ);/*! Send Read from Memory instruction */ if(HightAddr)/*! 24); write_1byteRecFlash(ReadAddr & 0xFF0000) 16); /*! 8); /*! Send ReadAddr medium nibble address byte to read from */write_1byteRecFlash(ReadAddr & 0xFF); /*! Send ReadAddr low nibble address byte to read from */for(i

18、=0;i(NumByteToRead);i+) / SpiCheckBufi=read_1byteRecFlash();/ 先读高字节 *pBuffer =read_1byteRecFlash();/ 先读高字节 pBuffer+;delay_1us(5);/ 延时10us;RecFlashCs_inactive; /CS 控制脚高,无效delay_1us(5);/ 延时10us;sleep_RecFlashcom();/ 无效 SPI 通信delay_1us(5);/ 延时10us;return 1;/* Function name: Flash_WritePage(uint8 * pBuf

19、fer, uint32 WriteAddr, uint16 NumByteToWrite)* Descriptions: 写一页* input parameters: none.* output argument: none* Note:* brief Writes more than one byte to the FLASH with a single WRITE cycle*(Page WRITE sequence).* note The number of byte cant exceed the FLASH page size.* param pBuffer: pointer to

20、the buffer containing the data to be written*to the FLASH.* param WriteAddr: FLASHs internal address to write to.* param NumByteToWrite: number of bytes to write to the FLASH, must be equal* or less than Flash_PAGESIZE value.* retval None*/uint8 Flash_WritePage(uint8 *pBuffer, uint32 WriteAddr, uint

21、16 NumByteToWrite) uint8 HightAddr = 0,rst = 0;uint16 i,WriteNum = NumByteToWrite;uint8 * pData = pBuffer;Flash_SendByteRec(FLASH_CMD_WREN); /写使能 /*! Enable the write access to the FLASH */Flash_SendByteRec(FLASH_CMD_PARALLEL); / 并口模式 ready_RecFlashcomWrite(); /SPI 操作前准备 RecFlashCs_active; /SPI 有效 d

22、elay_1us(5);/us_delay(10);write_1byteRecFlash(FLASH_CMD_PP); /*! 页编程 */ if(HightAddr) /*! 24);write_1byteRecFlash(WriteAddr & 0xFF0000) 16);/*! 8);/*! Send SectorAddr medium nibbleaddress byte */write_1byteRecFlash(WriteAddr & 0xFF); /*! Send SectorAddr low nibble address byte */for(i=0;iNumByteToWr

23、ite;i+) /*! while there is data to be written on the FLASH */ write_1byteFlash(*pBuffer); /*! Send the current byte */RecDataPort=*pBuffer; / 并口delay_1us(1);SclkToRec_on;delay_1us(1);SclkToRec_off; delay_1us(1); pBuffer+;/*! Point on the next byte to be written */文档来源为 :从网络收集整理 .word 版本可编辑 .欢迎下载支持/u

24、s_delay(10);/ 无效 SPI/us_delay(10);/无效SPI通信/us_delay(10);delay_1us(5);RecFlashCs_inactive;delay_1us(5);sleep_RecFlashcom();delay_1us(5);/* if(Flash_WaitForWriteEnd() = 0) return 0;if(Flash_ReadRecBuffer(WriteAddr,SpiCheckBuf,WriteNum) = 0) return 0;for(i=0; iWriteNum; i+) if(pDatai != SpiCheckBufi) r

25、eturn 0;*/return 1;/* brief Writes block of data to the FLASH. In this function, the number of* WRITE cycles are reduced, using Page WRITE sequence.* param pBuffer: pointer to the buffer containing the data to be written* to the FLASH.* param WriteAddr: FLASHs internal address to write to.* param Nu

26、mByteToWrite: number of bytes to write to the FLASH.* retval None/* */uint8 Flash_WriteBuffer(uint32 WriteAddr,uint8 *pBuffer,uint16 NumByteToWrite) uint16 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;Addr = WriteAddr % FLASH_SPI_PAGESIZE;count = FLASH_SPI_PAGESIZE - Addr;NumOfPage

27、= NumByteToWrite / FLASH_SPI_PAGESIZE;NumOfSingle = NumByteToWrite % FLASH_SPI_PAGESIZE;if (Addr = 0) /*! WriteAddr is Flash_PAGESIZE aligned */if (NumOfPage = 0) /*! NumByteToWrite Flash_PAGESIZE */if(Flash_WritePage(pBuffer, WriteAddr, NumByteToWrite) = 0) return 0;else /*! Flash_PAGESIZE */while

28、(NumOfPage-) 11文档来源为 :从网络收集整理 .word 版本可编辑文档来源为 :从网络收集整理 .word 版本可编辑 .欢迎下载支持if(Flash_WritePage(pBuffer, WriteAddr, FLASH_SPI_PAGESIZE) = 0) return 0;WriteAddr += FLASH_SPI_PAGESIZE; pBuffer += FLASH_SPI_PAGESIZE;if (NumOfSingle != 0) if(Flash_WritePage(pBuffer, WriteAddr, NumOfSingle) = 0) return 0;e

29、lse /*! WriteAddr is not Flash_PAGESIZE aligned */if (NumOfPage = 0) /*! NumByteToWrite count) /*! Flash_PAGESIZE */ temp = NumOfSingle - count;if(Flash_WritePage(pBuffer, WriteAddr, count) = 0) return 0;WriteAddr += count; pBuffer += count; if(Flash_WritePage(pBuffer, WriteAddr, temp) = 0) return 0

30、;else if(Flash_WritePage(pBuffer, WriteAddr, NumByteToWrite) = 0) return 0;else /*! Flash_PAGESIZE */NumByteToWrite -= count;NumOfPage = NumByteToWrite / FLASH_SPI_PAGESIZE;NumOfSingle = NumByteToWrite % FLASH_SPI_PAGESIZE; if(Flash_WritePage(pBuffer, WriteAddr, count) = 0) return 0;WriteAddr += cou

31、nt;pBuffer += count;while (NumOfPage-)if(Flash_WritePage(pBuffer, WriteAddr, FLASH_SPI_PAGESIZE) = 0) return 0;WriteAddr += FLASH_SPI_PAGESIZE;pBuffer += FLASH_SPI_PAGESIZE;if (NumOfSingle != 0)if(Flash_WritePage(pBuffer, WriteAddr, NumOfSingle) = 0) return 0;return 1;/* brief Polls the status of th

32、e Write In Progress (WIP) flag in the FLASHs*status register and loop until write opertaion has completed.* param None* retval None/*/uint8 Flash_WaitForWriteEnd(void)uint8 rst = 0;/uint32 CurTick;/*! Send Read Status Register instruction */NSec = 5;/CurTick = NSec;/GetSystemSecond() + 3;/*! Loop as

33、 long as the memory is busy with a write cycle */do/*! Send a dummy byte to generate the clock needed by the FLASH and put the value of the status register in FLASH_Status variable */ /flashstatus = Flash_SendByte(FLASH_DUMMY_BYTE); ready_RecFlashcomRead(); /SPI 操作前准备 delay_1us(5);/ 延时 10us;RecFlash

34、Cs_active;/SPI 有效delay_1us(5);/ 延时 10us;write_1byteRecFlash(FLASH_CMD_RDSR);delay_1ms(10);rst = read_1byteRecFlash();ClrWdt();if( NSec = 0 )/CurTickGetSystemSecond()13文档来源为 :从网络收集整理 .word 版本可编辑文档来源为 :从网络收集整理 .word 版本可编辑 .欢迎下载支持return 0;while (rst & FLASH_WIP_FLAG) = 1); /* Write in progress */ delay

35、_1us(5);/ 延时 10us;RecFlashCs_inactive; / 无效 SPI delay_1us(5);/ 延时 10us;return 1;/* Function name: Flash_EraseSector(uint32 SectorAddr)* Descriptions: 擦除指定扇区* input parameters: none.* output argument: none* Note: SectorAddr: address of the sector to erase*/uint8 Flash_EraseSector(uint32 SectorAddr)uint8 HightAddr = 0,rst = 0;/*!= FLASH_SPI_HIGHT_ADDR)/ 地址大于 FLASH容量HightAddr = 1;/ if(Flash_Enter4Mode() = 0)return 0;/*! Send write enable instruction */ Flash_SendByteRec(FLASH_CMD_WREN); Flash_Se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论