初级会员
- 积分
- 80
- 金钱
- 80
- 注册时间
- 2020-8-10
- 在线时间
- 20 小时
|
楼主 |
发表于 2024-11-13 16:46:23
|
显示全部楼层
- //写入起始地址为:块地址BlockAddr(0~1023),页地址PageAddr(0~63),具体字节地址BytesAddr(0~2047)的连续Size个数据,数据个数(0~65535)
- w25n_status_t W25Nx_Buf_Write_Nx_Data(uint8_t *pData, uint16_t BlockAddr, uint8_t PageAddr, uint16_t BytesAddr, uint16_t Size)
- {
- uint8_t cmd[7];
- uint16_t i;
- uint8_t Page_data[i];
- /* Configure the command */
- cmd[0] = WRITE_DATA_CMD; //0x02 //数据装入缓冲区命令
- cmd[1] = (uint8_t)((BytesAddr >> 8) & 0x07);
- cmd[2] = (uint8_t)(BytesAddr & 0xFF);
-
- cmd[3] = WRITE_INPUT_PAGE_ADDR_CMD; //0x10 //程序执行,写入主存区命令
- cmd[4] = Dummy_Byte;
- cmd[5] = (uint8_t)((BlockAddr >> 2) & 0xFF); //主存区页地址
- cmd[6] = (uint8_t)(((BlockAddr << 6)+PageAddr) & 0xFF);
-
-
- //先赋三个命令在开头
- for(i=0;i<3;i++)
- {
- Page_data[i] = cmd[i];
- }
- md_delay_1ms(10);
- for(i=3;i<(Size + 3);i++) //存储页数据
- {
- Page_data[i] = *pData;
- pData++;
- }
-
-
- W25Q32_CS_CLR();
- if(ald_spi_send_byte_fast(&h_spi, WRITE_ENABLE_CMD) != OK)
- {
- W25Q32_CS_SET();
- return W25N_ERROR;
- }
- W25Q32_CS_SET();
- md_delay_1ms(10);
- //md_delay_us(100);
- W25Q32_CS_CLR();
- for(i=0;i<(Size + 3);i++)
- {
- if (ald_spi_send_byte_fast(&h_spi, Page_data[i]) != OK)
- {
- W25Q32_CS_SET();
- return W25N_ERROR;
- }
- }
- W25Q32_CS_SET();
- // md_delay_us(100);
- md_delay_1ms(10);
- W25Q32_CS_CLR();
- for(i=3;i<7;i++)//写剩余指令
- {
- if (ald_spi_send_byte_fast(&h_spi, cmd[i]) != OK)
- {
- W25Q32_CS_SET();
- }
- }
- W25_Wait_Busy();
- W25Q32_CS_SET();
- return W25N_OK;
-
-
- }
- /***********************************************************************************************
- ** name: W25_PageDataRead
- ** function: 读取指定页地址的数据,并移动到缓冲区
- **parameter: BlockAddr 写入的块地址(0-1023)
- PageAddr 写入的页地址(0-63)
- ** return: void
- ************************************************************************************************/
- void W25_PageDataRead(uint16_t BlockAddr,uint8_t PageAddr)
- {
- uint16_t i = 0;
- uint8_t cmd[4];
-
- cmd[0] = PAGE_READ_CMD; //主存区页数据传至缓冲区
- cmd[1] = Dummy_Byte;
- cmd[2] = (uint8_t)((BlockAddr >> 2) & 0xFF); //页地址
- cmd[3] = (uint8_t)(((BlockAddr << 6)|PageAddr) & 0xFF);
- W25Q32_CS_CLR();
- if (ald_spi_send_byte_fast(&h_spi, PAGE_READ_CMD) != OK) {
- W25Q32_CS_SET();
- }
- for (i = 0; i < 4; i++)
- {
- if (ald_spi_send_byte_fast(&h_spi, cmd[i]) != OK) {
- W25Q32_CS_SET();
- }
- }
- W25Q32_CS_SET();
- W25_Wait_Busy();
- }
- /***********************************************************************************************
- ** name: W25_Read
- ** function: 读取SPI FLASH,在指定地址开始读取指定长度的数据
- **parameter: pBuffer 数据存储区
- BytesAddr 写入的具体字节地址(0-1023)
- BlockAddr 写入的块地址(0-1023)
- PageAddr 写入的页地址(0-63)
- NumByteToWrite 要写入的数据个数(0-65535),该数不应该超过该页的剩余字节数!!!
- ** return: void
- 需要改造,和W25N.C文件里可以写多页的函数整合
- ************************************************************************************************/
- void W25_Read(uint8_t* pBuffer,uint16_t BlockAddr,uint8_t PageAddr,uint16_t BytesAddr,uint16_t NumByteToRead)
- {
-
- uint16_t i = 0;
- uint8_t cmd[4];
-
- int r_flag = 0;
- cmd[0] = READ_CMD; //读出缓冲区数据
- cmd[1] = (uint8_t)((BytesAddr >> 8) & 0x07);
- cmd[2] = (uint8_t)(BytesAddr & 0xFF);
- cmd[3] = Dummy_Byte;
-
- W25_PageDataRead(BlockAddr,PageAddr); //先读取页地址数据,送到缓冲区
- md_delay_1ms(10);
-
- W25Q32_CS_CLR(); //使能器件
- for (i = 0; i < 4; i++)
- {
- if (ald_spi_send_byte_fast(&h_spi, cmd[i]) != OK) {
- W25Q32_CS_SET();
- }
- }
- for(i = 0; i < NumByteToRead; i++){
- pBuffer[i] = ald_spi_recv_byte_fast(&h_spi, &r_flag); //循环读数
-
- if (r_flag != OK) {
- W25Q32_CS_SET();
-
- }
- }
-
- W25Q32_CS_SET();
- }
复制代码
这是读写的函数 |
|