//芯片写 static int w25qxx_write_sr(uint8_t value) { w25qxx_write_enable(); spi_xfer(W25X_WriteStatusReg, W25QXX_CS_LOW); spi_xfer(value, W25QXX_CS_HIGH); return 0; }
//芯片写 static int w25qxx_write_sr(uint8_t value) { uint8_t buf[2]; buf[0] = W25X_WriteStatusReg; buf[1] = value; w25qxx_write_enable(); spi_write(&device, buf, 2, true); return SPI_EOK; }
//擦除整个芯片 int w25qxx_erase_chip(void) { uint8_t buf_send[1]; w25qxx_write_enable(); while((w25qxx_read_sr() & 0x01) == 0x01); buf_send[0] = W25X_ChipErase; spi_write(&device, buf_send, sizeof(buf_send), true); while((w25qxx_read_sr() & 0x01) == 0x01); return SPI_EOK; }
//擦除整个芯片 int w25qxx_erase_chip(void) { w25qxx_write_enable(); while((w25qxx_read_sr() & 0x01) == 0x01); spi_xfer(W25X_ChipErase, W25QXX_CS_HIGH); while((w25qxx_read_sr() & 0x01) == 0x01) { w25_dev.ops.delayms(10); } return 0; }
//擦除扇区 static int w25qxx_erase_sector(uint32_t addr) { uint8_t buf_send[4]; addr /= W25X_SECTOR_SIZE; addr *= W25X_SECTOR_SIZE; //round addr to N x W25X_SECTOR_SIZE buf_send[0] = W25X_SectorErase; buf_send[1] = (uint8_t)((addr)>>16); buf_send[2] = (uint8_t)((addr)>>8); buf_send[3] = (uint8_t)addr; w25qxx_write_enable(); while((w25qxx_read_sr() & 0x01) == 0x01); spi_write(&device, buf_send, sizeof(buf_send), true); while((w25qxx_read_sr() & 0x01) == 0x01); return SPI_EOK; }
//写一页数据 static int w25qxx_write_page(uint32_t addr, uint8_t *buf, uint32_t len) { uint8_t buf_send[4]; w25qxx_write_enable(); buf_send[0] = W25X_PageProgram; buf_send[1] = (uint8_t)((addr)>>16); buf_send[2] = (uint8_t)((addr)>>8); buf_send[3] = (uint8_t)addr; if(spi_write(&device, buf_send, sizeof(buf_send), false)) { return SPI_ERROR; } spi_write(&device, buf, len, true); /* wait busy */ while((w25qxx_read_sr() & 0x01) == 0x01); return SPI_EOK; }
//擦除扇区 int w25qxx_erase_sector(uint32_t addr) { addr /= w25_dev.attr.sector_size; addr *= w25_dev.attr.sector_size; //round addr to N x W25X_SECTOR_SIZE w25qxx_write_enable(); while((w25qxx_read_sr() & 0x01) == 0x01); spi_xfer(W25X_SectorErase, W25QXX_CS_LOW); spi_xfer((uint8_t)((addr)>>16), W25QXX_CS_LOW); spi_xfer((uint8_t)((addr)>>8), W25QXX_CS_LOW); spi_xfer((uint8_t)addr, W25QXX_CS_HIGH); while((w25qxx_read_sr() & 0x01) == 0x01) { w25_dev.ops.delayms(10); } return 0; }
void w25qxx_enter_qspi_mode(struct rt_qspi_device *device) { char status = 0; /* 0x38 enter qspi mode */ char instruction = 0x38; char write_status2_buf[2] = {0}; /* 0x31 write status register2 */ write_status2_buf[0] = 0x31; status = w25qxx_read_status_register2(device); if (!(status & 0x02)) { status |= 1 << 1; w25qxx_write_enable(device); write_status2_buf[1] = status; rt_qspi_send(device, &write_status2_buf, 2); rt_qspi_send(device, &instruction, 1); rt_kprintf("flash already enter qspi mode\n"); rt_thread_mdelay(10); } }
//写一页数据 int w25qxx_write_page(uint32_t addr, uint8_t *buf, uint32_t len) { w25qxx_write_enable(); /* send addr */ spi_xfer(W25X_PageProgram, W25QXX_CS_LOW); spi_xfer((uint8_t)((addr)>>16), W25QXX_CS_LOW); spi_xfer((uint8_t)((addr)>>8), W25QXX_CS_LOW); spi_xfer((uint8_t)addr, W25QXX_CS_LOW); w25_dev.ops.xfer(NULL, buf, len, W25QXX_CS_HIGH); while(w25_dev.ops.get_reamin() != 0) { w25_dev.ops.delayms(10); } /* wait busy */ while((w25qxx_read_sr() & 0x01) == 0x01) { w25_dev.ops.delayms(1); } return 0; }