spi: flash: wait for operations to complete

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent 5bafd2ac8f
commit f20e7128eb
  1. 3
      include/spi_flash.h
  2. 20
      source/drivers/spi_flash.c

@ -4,6 +4,9 @@
#define SPI_FLASH_MFR_DEV_ID 0x90 #define SPI_FLASH_MFR_DEV_ID 0x90
#define SPI_FLASH_JEDEC_ID 0x9f #define SPI_FLASH_JEDEC_ID 0x9f
#define SPI_FLASH_READ_SR1 0x05
#define SPI_FLASH_SR1_BUSY BIT(0)
#define SPI_FLASH_READ 0x03 #define SPI_FLASH_READ 0x03
#define SPI_FLASH_PAGE_PROGRAM 0x02 #define SPI_FLASH_PAGE_PROGRAM 0x02

@ -33,6 +33,24 @@ static void spi_flash_addr(char *cmd, uint32_t addr)
cmd[3] = addr; cmd[3] = addr;
} }
static uint8_t spi_flash_get_status1(struct flash_dev *dev)
{
struct spi_dev *spi_dev = dev->priv;
uint8_t cmd = SPI_FLASH_READ_SR1;
uint8_t ret;
spi_tx_rx(spi_dev, &ret, sizeof ret, &cmd, sizeof cmd);
return ret;
}
static int spi_flash_wait(struct flash_dev *dev)
{
while (spi_flash_get_status1(dev) & SPI_FLASH_SR1_BUSY);
return 0;
}
static void spi_flash_write_enable(struct flash_dev *dev) static void spi_flash_write_enable(struct flash_dev *dev)
{ {
char cmd[1] = { SPI_FLASH_WRITE_ENABLE }; char cmd[1] = { SPI_FLASH_WRITE_ENABLE };
@ -98,6 +116,7 @@ static size_t spi_flash_write(struct flash_dev *dev, uint32_t addr,
memcpy(cmd + 4, data, len); memcpy(cmd + 4, data, len);
spi_tx_rx(spi_dev, NULL, 0, cmd, sizeof cmd); spi_tx_rx(spi_dev, NULL, 0, cmd, sizeof cmd);
spi_flash_wait(dev);
spi_flash_write_disable(dev); spi_flash_write_disable(dev);
@ -115,6 +134,7 @@ static int spi_flash_erase(struct flash_dev *dev, uint32_t addr)
spi_flash_addr(cmd, addr << dev->log2_block_size); spi_flash_addr(cmd, addr << dev->log2_block_size);
spi_tx_rx(spi_dev, NULL, 0, cmd, sizeof cmd); spi_tx_rx(spi_dev, NULL, 0, cmd, sizeof cmd);
spi_flash_wait(dev);
spi_flash_write_disable(dev); spi_flash_write_disable(dev);

Loading…
Cancel
Save