Merge pull request #2358 from armink/fix_sfud

[DeviceDriver] Add rt_sfud_flash_find_by_dev_name function to SFUD
This commit is contained in:
Bernard Xiong 2019-02-20 18:37:16 +08:00 committed by GitHub
commit 89ff902ab5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 38 deletions

View File

@ -46,7 +46,7 @@ static char log_buf[RT_CONSOLEBUF_SIZE];
void sfud_log_debug(const char *file, const long line, const char *format, ...);
static rt_err_t rt_sfud_control(rt_device_t dev, int cmd, void *args) {
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(dev);
switch (cmd) {
case RT_DEVICE_CTRL_BLK_GETGEOME: {
@ -93,6 +93,10 @@ static rt_err_t rt_sfud_control(rt_device_t dev, int cmd, void *args) {
static rt_size_t rt_sfud_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) {
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (dev->user_data);
sfud_flash *sfud_dev = (sfud_flash *) (rtt_dev->user_data);
RT_ASSERT(dev);
RT_ASSERT(rtt_dev);
RT_ASSERT(sfud_dev);
/* change the block device's logic address to physical address */
rt_off_t phy_pos = pos * rtt_dev->geometry.bytes_per_sector;
rt_size_t phy_size = size * rtt_dev->geometry.bytes_per_sector;
@ -107,6 +111,10 @@ static rt_size_t rt_sfud_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_si
static rt_size_t rt_sfud_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) {
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (dev->user_data);
sfud_flash *sfud_dev = (sfud_flash *) (rtt_dev->user_data);
RT_ASSERT(dev);
RT_ASSERT(rtt_dev);
RT_ASSERT(sfud_dev);
/* change the block device's logic address to physical address */
rt_off_t phy_pos = pos * rtt_dev->geometry.bytes_per_sector;
rt_size_t phy_size = size * rtt_dev->geometry.bytes_per_sector;
@ -126,6 +134,10 @@ static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, si
sfud_err result = SFUD_SUCCESS;
sfud_flash *sfud_dev = (sfud_flash *) (spi->user_data);
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (sfud_dev->user_data);
RT_ASSERT(spi);
RT_ASSERT(sfud_dev);
RT_ASSERT(rtt_dev);
#ifdef SFUD_USING_QSPI
struct rt_qspi_device *qspi_dev = RT_NULL;
#endif
@ -180,7 +192,12 @@ static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi
sfud_flash *sfud_dev = (sfud_flash *) (spi->user_data);
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (sfud_dev->user_data);
struct rt_qspi_device *qspi_dev = (struct rt_qspi_device *) (rtt_dev->rt_spi_device);
RT_ASSERT(spi);
RT_ASSERT(sfud_dev);
RT_ASSERT(rtt_dev);
RT_ASSERT(qspi_dev);
/* set message struct */
message.instruction.content = qspi_read_cmd_format->instruction;
message.instruction.qspi_lines = qspi_read_cmd_format->instruction_lines;
@ -201,11 +218,11 @@ static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi
message.parent.cs_release = 1;
message.parent.cs_take = 1;
message.qspi_data_lines = qspi_read_cmd_format->data_lines;
if (rt_qspi_transfer_message(qspi_dev, &message) != read_size) {
result = SFUD_ERR_TIMEOUT;
}
return result;
}
#endif
@ -214,6 +231,10 @@ static void spi_lock(const sfud_spi *spi) {
sfud_flash *sfud_dev = (sfud_flash *) (spi->user_data);
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (sfud_dev->user_data);
RT_ASSERT(spi);
RT_ASSERT(sfud_dev);
RT_ASSERT(rtt_dev);
rt_mutex_take(&(rtt_dev->lock), RT_WAITING_FOREVER);
}
@ -221,6 +242,10 @@ static void spi_unlock(const sfud_spi *spi) {
sfud_flash *sfud_dev = (sfud_flash *) (spi->user_data);
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (sfud_dev->user_data);
RT_ASSERT(spi);
RT_ASSERT(sfud_dev);
RT_ASSERT(rtt_dev);
rt_mutex_release(&(rtt_dev->lock));
}
@ -270,6 +295,8 @@ void sfud_log_info(const char *format, ...) {
sfud_err sfud_spi_port_init(sfud_flash *flash) {
sfud_err result = SFUD_SUCCESS;
RT_ASSERT(flash);
/* port SPI device interface */
flash->spi.wr = spi_write_read;
#ifdef SFUD_USING_QSPI
@ -285,7 +312,7 @@ sfud_err sfud_spi_port_init(sfud_flash *flash) {
flash->retry.delay = retry_delay_100us;
/* 60 seconds timeout */
flash->retry.times = 60 * 10000;
return result;
}
@ -361,7 +388,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
rt_qspi_configure(qspi_dev, &qspi_cfg);
}
else
#endif
#endif
rt_spi_configure(rtt_dev->rt_spi_device, &cfg);
}
/* SFUD flash device initialize */
@ -461,27 +488,46 @@ sfud_flash_t rt_sfud_flash_find(const char *spi_dev_name)
rt_spi_flash_device_t rtt_dev = RT_NULL;
struct rt_spi_device *rt_spi_device = RT_NULL;
sfud_flash_t sfud_dev = RT_NULL;
rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
if (rt_spi_device == RT_NULL || rt_spi_device->parent.type != RT_Device_Class_SPIDevice)
{
if (rt_spi_device == RT_NULL || rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
rt_kprintf("ERROR: SPI device %s not found!\n", spi_dev_name);
goto error;
goto __error;
}
rtt_dev = (rt_spi_flash_device_t)(rt_spi_device->user_data);
if (rtt_dev && rtt_dev->user_data)
{
sfud_dev = (sfud_flash_t)(rtt_dev->user_data);
rtt_dev = (rt_spi_flash_device_t) (rt_spi_device->user_data);
if (rtt_dev && rtt_dev->user_data) {
sfud_dev = (sfud_flash_t) (rtt_dev->user_data);
return sfud_dev;
}
else
{
} else {
rt_kprintf("ERROR: SFUD flash device not found!\n");
goto error;
goto __error;
}
error:
__error:
return RT_NULL;
}
sfud_flash_t rt_sfud_flash_find_by_dev_name(const char *flash_dev_name)
{
rt_spi_flash_device_t rtt_dev = RT_NULL;
sfud_flash_t sfud_dev = RT_NULL;
rtt_dev = (rt_spi_flash_device_t) rt_device_find(flash_dev_name);
if (rtt_dev == RT_NULL || rtt_dev->flash_device.type != RT_Device_Class_Block) {
rt_kprintf("ERROR: Flash device %s not found!\n", flash_dev_name);
goto __error;
}
if (rtt_dev->user_data) {
sfud_dev = (sfud_flash_t) (rtt_dev->user_data);
return sfud_dev;
} else {
rt_kprintf("ERROR: SFUD flash device not found!\n");
goto __error;
}
__error:
return RT_NULL;
}
@ -491,6 +537,8 @@ error:
static void sf(uint8_t argc, char **argv) {
#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
#define HEXDUMP_WIDTH 16
#define CMD_PROBE_INDEX 0
#define CMD_READ_INDEX 1
#define CMD_WRITE_INDEX 2
@ -501,7 +549,7 @@ static void sf(uint8_t argc, char **argv) {
sfud_err result = SFUD_SUCCESS;
static const sfud_flash *sfud_dev = NULL;
static rt_spi_flash_device_t rtt_dev = NULL, rtt_dev_bak = NULL;
size_t i = 0;
size_t i = 0, j = 0;
const char* sf_help_info[] = {
[CMD_PROBE_INDEX] = "sf probe [spi_device] - probe and init SPI flash by given 'spi_device'",
@ -528,12 +576,12 @@ static void sf(uint8_t argc, char **argv) {
} else {
char *spi_dev_name = argv[2];
rtt_dev_bak = rtt_dev;
/* delete the old SPI flash device */
if(rtt_dev_bak) {
rt_sfud_flash_delete(rtt_dev_bak);
}
rtt_dev = rt_sfud_flash_probe("sf_cmd", spi_dev_name);
if (!rtt_dev) {
return;
@ -557,8 +605,8 @@ static void sf(uint8_t argc, char **argv) {
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_READ_INDEX]);
return;
} else {
addr = atol(argv[2]);
size = atol(argv[3]);
addr = strtol(argv[2], NULL, 0);
size = strtol(argv[3], NULL, 0);
uint8_t *data = rt_malloc(size);
if (data) {
result = sfud_read(sfud_dev, addr, size, data);
@ -566,14 +614,24 @@ static void sf(uint8_t argc, char **argv) {
rt_kprintf("Read the %s flash data success. Start from 0x%08X, size is %ld. The data is:\n",
sfud_dev->name, addr, size);
rt_kprintf("Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
for (i = 0; i < size; i++) {
if (i % 16 == 0) {
rt_kprintf("[%08X] ", addr + i);
for (i = 0; i < size; i += HEXDUMP_WIDTH)
{
rt_kprintf("[%08X] ", addr + i);
/* dump hex */
for (j = 0; j < HEXDUMP_WIDTH; j++) {
if (i + j < size) {
rt_kprintf("%02X ", data[i + j]);
} else {
rt_kprintf(" ");
}
}
rt_kprintf("%02X ", data[i]);
if (((i + 1) % 16 == 0) || i == size - 1) {
rt_kprintf("\n");
/* dump char for hex */
for (j = 0; j < HEXDUMP_WIDTH; j++) {
if (i + j < size) {
rt_kprintf("%c", __is_print(data[i + j]) ? data[i + j] : '.');
}
}
rt_kprintf("\n");
}
rt_kprintf("\n");
}
@ -587,12 +645,12 @@ static void sf(uint8_t argc, char **argv) {
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_WRITE_INDEX]);
return;
} else {
addr = atol(argv[2]);
addr = strtol(argv[2], NULL, 0);
size = argc - 3;
uint8_t *data = rt_malloc(size);
if (data) {
for (i = 0; i < size; i++) {
data[i] = atoi(argv[3 + i]);
data[i] = strtol(argv[3 + i], NULL, 0);
}
result = sfud_write(sfud_dev, addr, size, data);
if (result == SFUD_SUCCESS) {
@ -614,8 +672,8 @@ static void sf(uint8_t argc, char **argv) {
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_ERASE_INDEX]);
return;
} else {
addr = atol(argv[2]);
size = atol(argv[3]);
addr = strtol(argv[2], NULL, 0);
size = strtol(argv[3], NULL, 0);
result = sfud_erase(sfud_dev, addr, size);
if (result == SFUD_SUCCESS) {
rt_kprintf("Erase the %s flash data success. Start from 0x%08X, size is %ld.\n", sfud_dev->name,
@ -630,8 +688,8 @@ static void sf(uint8_t argc, char **argv) {
rt_kprintf("The %s flash status register current value is 0x%02X.\n", sfud_dev->name, status);
}
} else if (argc == 4) {
bool is_volatile = atoi(argv[2]);
uint8_t status = atoi(argv[3]);
bool is_volatile = strtol(argv[2], NULL, 0);
uint8_t status = strtol(argv[3], NULL, 0);
result = sfud_write_status(sfud_dev, is_volatile, status);
if (result == SFUD_SUCCESS) {
rt_kprintf("Write the %s flash status register to 0x%02X success.\n", sfud_dev->name, status);
@ -696,7 +754,7 @@ static void sf(uint8_t argc, char **argv) {
rt_kprintf("Data check ERROR! Please check you flash by other command.\n");
result = SFUD_ERR_READ;
}
if (result != SFUD_SUCCESS) {
break;
}

View File

@ -36,7 +36,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
rt_err_t rt_sfud_flash_delete(rt_spi_flash_device_t spi_flash_dev);
/**
* Find sfud flash device
* Find sfud flash device by SPI device name
*
* @param spi_dev_name using SPI device name
*
@ -44,4 +44,13 @@ rt_err_t rt_sfud_flash_delete(rt_spi_flash_device_t spi_flash_dev);
*/
sfud_flash_t rt_sfud_flash_find(const char *spi_dev_name);
/**
* Find sfud flash device by flash device name
*
* @param flash_dev_name using flash device name
*
* @return sfud flash device if success, otherwise return RT_NULL
*/
sfud_flash_t rt_sfud_flash_find_by_dev_name(const char *flash_dev_name);
#endif /* _SPI_FLASH_SFUD_H_ */