flash: add flash_get_size()
This commit is contained in:
parent
86afdc1707
commit
f61ecefdc6
4 changed files with 49 additions and 55 deletions
|
@ -3,6 +3,14 @@
|
|||
|
||||
#include <flash.h>
|
||||
|
||||
size_t flash_get_size(struct flash_dev *dev)
|
||||
{
|
||||
if (!dev)
|
||||
return 0;
|
||||
|
||||
return dev->ops->get_size(dev);
|
||||
}
|
||||
|
||||
int flash_read(struct flash_dev *dev, uint32_t addr, void *data, size_t len)
|
||||
{
|
||||
if (!dev)
|
||||
|
|
|
@ -17,6 +17,7 @@ struct stdio_flash_priv {
|
|||
size_t size;
|
||||
};
|
||||
|
||||
static size_t stdio_flash_get_size(struct flash_dev *dev);
|
||||
static int stdio_flash_read(struct flash_dev *dev, uint32_t addr, void *data,
|
||||
size_t len);
|
||||
static int stdio_flash_write(struct flash_dev *dev, uint32_t addr,
|
||||
|
@ -24,11 +25,19 @@ static int stdio_flash_write(struct flash_dev *dev, uint32_t addr,
|
|||
static int stdio_flash_erase(struct flash_dev *dev, uint32_t addr, size_t len);
|
||||
|
||||
static struct flash_ops stdio_flash_ops = {
|
||||
.get_size = stdio_flash_get_size,
|
||||
.read = stdio_flash_read,
|
||||
.write = stdio_flash_write,
|
||||
.erase = stdio_flash_erase,
|
||||
};
|
||||
|
||||
static size_t stdio_flash_get_size(struct flash_dev *dev)
|
||||
{
|
||||
struct stdio_flash_priv *priv = dev->priv;
|
||||
|
||||
return priv->size;
|
||||
}
|
||||
|
||||
static int stdio_flash_read(struct flash_dev *dev, uint32_t addr, void *data,
|
||||
size_t len)
|
||||
{
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <flash.h>
|
||||
#include <macros.h>
|
||||
#include <spi.h>
|
||||
#include <spi_flash.h>
|
||||
|
||||
static size_t spi_flash_get_size(struct flash_dev *dev);
|
||||
static int spi_flash_read(struct flash_dev *dev, uint32_t addr, void *data,
|
||||
size_t len);
|
||||
static int spi_flash_write(struct flash_dev *dev, uint32_t addr,
|
||||
|
@ -14,6 +16,7 @@ static int spi_flash_write(struct flash_dev *dev, uint32_t addr,
|
|||
static int spi_flash_erase(struct flash_dev *dev, uint32_t addr, size_t len);
|
||||
|
||||
static struct flash_ops spi_flash_ops ={
|
||||
.get_size = spi_flash_get_size,
|
||||
.read = spi_flash_read,
|
||||
.write = spi_flash_write,
|
||||
.erase = spi_flash_erase,
|
||||
|
@ -46,6 +49,33 @@ static void spi_flash_write_disable(struct flash_dev *dev)
|
|||
spi_set_cs_level(spi_dev, 1);
|
||||
}
|
||||
|
||||
static int spi_flash_get_jedec_id(struct flash_dev *dev, uint8_t *jedec_id,
|
||||
size_t len)
|
||||
{
|
||||
uint8_t cmd[5] = { SPI_FLASH_JEDEC_ID, 0, 0, 0, 0 };
|
||||
uint8_t buf[5];
|
||||
struct spi_dev *spi_dev = dev->priv;
|
||||
|
||||
spi_set_cs_level(spi_dev, 0);
|
||||
spi_tx_rx(spi_dev, buf, cmd, sizeof cmd);
|
||||
spi_set_cs_level(spi_dev, 1);
|
||||
|
||||
memcpy(jedec_id, buf + 1, min(4, len));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t spi_flash_get_size(struct flash_dev *dev)
|
||||
{
|
||||
uint8_t jedec_id[4];
|
||||
|
||||
spi_flash_get_jedec_id(dev, jedec_id, sizeof jedec_id);
|
||||
|
||||
printf("%02x%02x%02x%02x\n", jedec_id[0], jedec_id[1], jedec_id[2], jedec_id[3]);
|
||||
|
||||
return SPI_FLASH_SIZE(jedec_id[2]);
|
||||
}
|
||||
|
||||
static int spi_flash_read(struct flash_dev *dev, uint32_t addr, void *data,
|
||||
size_t len)
|
||||
{
|
||||
|
@ -120,59 +150,6 @@ err_disable_write:
|
|||
return 0;
|
||||
}
|
||||
|
||||
static char *size_to_str(size_t size)
|
||||
{
|
||||
char *s;
|
||||
int ret;
|
||||
|
||||
if (size == 0) {
|
||||
ret = asprintf(&s, "0");
|
||||
#if defined(TIB)
|
||||
} else if (size % TIB == 0) {
|
||||
ret = asprintf(&s, "%u TiB", size / TIB);
|
||||
#endif
|
||||
} else if (size % GIB == 0) {
|
||||
ret = asprintf(&s, "%u GiB", size / GIB);
|
||||
} else if (size % MIB == 0) {
|
||||
ret = asprintf(&s, "%u MiB", size / MIB);
|
||||
} else if (size % KIB == 0) {
|
||||
ret = asprintf(&s, "%u KiB", size / KIB);
|
||||
} else {
|
||||
ret = asprintf(&s, "%u B", size);
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static int flash_detect(struct flash_dev *dev)
|
||||
{
|
||||
char cmd[1] = { SPI_FLASH_JEDEC_ID };
|
||||
uint8_t data[4];
|
||||
struct spi_dev *spi_dev = dev->priv;
|
||||
char *s;
|
||||
size_t size;
|
||||
|
||||
spi_set_cs_level(spi_dev, 0);
|
||||
spi_tx_rx(spi_dev, data, cmd, sizeof cmd);
|
||||
spi_tx_rx(spi_dev, data, NULL, sizeof data);
|
||||
spi_set_cs_level(spi_dev, 1);
|
||||
|
||||
printf("JEDEC ID: %02x%02x%02x%02x\n",
|
||||
data[0], data[1], data[2], data[3]);
|
||||
|
||||
size = SPI_FLASH_SIZE(data[2]);
|
||||
s = size_to_str(size);
|
||||
|
||||
printf("Capacity: %u blocks (%s)\n",
|
||||
size / (4 * KIB), s);
|
||||
free(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct flash_dev *flash_probe(void)
|
||||
{
|
||||
struct flash_dev *dev;
|
||||
|
@ -185,8 +162,6 @@ struct flash_dev *flash_probe(void)
|
|||
|
||||
dev->ops = &spi_flash_ops;
|
||||
|
||||
flash_detect(dev);
|
||||
|
||||
return dev;
|
||||
|
||||
err_free_dev:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue