2016-10-09 09:06:01 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2016-10-09 09:06:01 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-10-09 09:06:01 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2016-09-28 armink first version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2021-08-23 17:30:05 +08:00
|
|
|
#include <string.h>
|
2016-10-09 09:06:01 +08:00
|
|
|
#include <rtdevice.h>
|
|
|
|
#include "spi_flash.h"
|
|
|
|
#include "spi_flash_sfud.h"
|
|
|
|
|
|
|
|
#ifdef RT_USING_SFUD
|
|
|
|
|
|
|
|
#ifndef RT_SFUD_DEFAULT_SPI_CFG
|
2019-12-03 15:52:39 +08:00
|
|
|
|
|
|
|
#ifndef RT_SFUD_SPI_MAX_HZ
|
|
|
|
#define RT_SFUD_SPI_MAX_HZ 50000000
|
|
|
|
#endif
|
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
/* read the JEDEC SFDP command must run at 50 MHz or less */
|
2018-11-24 15:11:24 +08:00
|
|
|
#define RT_SFUD_DEFAULT_SPI_CFG \
|
|
|
|
{ \
|
|
|
|
.mode = RT_SPI_MODE_0 | RT_SPI_MSB, \
|
|
|
|
.data_width = 8, \
|
2019-12-03 15:52:39 +08:00
|
|
|
.max_hz = RT_SFUD_SPI_MAX_HZ, \
|
2018-11-24 15:11:24 +08:00
|
|
|
}
|
2020-06-13 11:50:30 +08:00
|
|
|
#endif /* RT_SFUD_DEFAULT_SPI_CFG */
|
2018-11-24 15:11:24 +08:00
|
|
|
|
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
#define RT_SFUD_DEFAULT_QSPI_CFG \
|
|
|
|
{ \
|
|
|
|
RT_SFUD_DEFAULT_SPI_CFG, \
|
|
|
|
.medium_size = 0x800000, \
|
|
|
|
.ddr_mode = 0, \
|
|
|
|
.qspi_dl_width = 4, \
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
2020-06-13 11:50:30 +08:00
|
|
|
#endif /* SFUD_USING_QSPI */
|
2016-10-09 09:06:01 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
static rt_err_t rt_sfud_control(rt_device_t dev, int cmd, void *args) {
|
2019-02-20 16:27:27 +08:00
|
|
|
RT_ASSERT(dev);
|
2016-10-09 09:06:01 +08:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case RT_DEVICE_CTRL_BLK_GETGEOME: {
|
|
|
|
struct rt_device_blk_geometry *geometry = (struct rt_device_blk_geometry *) args;
|
|
|
|
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (dev->user_data);
|
|
|
|
|
|
|
|
if (rtt_dev == RT_NULL || geometry == RT_NULL) {
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
geometry->bytes_per_sector = rtt_dev->geometry.bytes_per_sector;
|
|
|
|
geometry->sector_count = rtt_dev->geometry.sector_count;
|
|
|
|
geometry->block_size = rtt_dev->geometry.block_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RT_DEVICE_CTRL_BLK_ERASE: {
|
|
|
|
rt_uint32_t *addrs = (rt_uint32_t *) args, start_addr = addrs[0], end_addr = addrs[1], phy_start_addr;
|
|
|
|
struct spi_flash_device *rtt_dev = (struct spi_flash_device *) (dev->user_data);
|
|
|
|
sfud_flash *sfud_dev = (sfud_flash *) (rtt_dev->user_data);
|
|
|
|
rt_size_t phy_size;
|
|
|
|
|
|
|
|
if (addrs == RT_NULL || start_addr > end_addr || rtt_dev == RT_NULL || sfud_dev == RT_NULL) {
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-07-14 21:01:38 +08:00
|
|
|
if (end_addr == start_addr) {
|
|
|
|
end_addr ++;
|
|
|
|
}
|
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
phy_start_addr = start_addr * rtt_dev->geometry.bytes_per_sector;
|
|
|
|
phy_size = (end_addr - start_addr) * rtt_dev->geometry.bytes_per_sector;
|
|
|
|
|
|
|
|
if (sfud_erase(sfud_dev, phy_start_addr, phy_size) != SFUD_SUCCESS) {
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2019-02-20 16:27:27 +08:00
|
|
|
|
|
|
|
RT_ASSERT(dev);
|
|
|
|
RT_ASSERT(rtt_dev);
|
|
|
|
RT_ASSERT(sfud_dev);
|
2018-08-16 14:30:19 +08:00
|
|
|
/* change the block device's logic address to physical address */
|
2016-10-09 09:06:01 +08:00
|
|
|
rt_off_t phy_pos = pos * rtt_dev->geometry.bytes_per_sector;
|
|
|
|
rt_size_t phy_size = size * rtt_dev->geometry.bytes_per_sector;
|
|
|
|
|
|
|
|
if (sfud_read(sfud_dev, phy_pos, phy_size, buffer) != SFUD_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-02-20 16:27:27 +08:00
|
|
|
|
|
|
|
RT_ASSERT(dev);
|
|
|
|
RT_ASSERT(rtt_dev);
|
|
|
|
RT_ASSERT(sfud_dev);
|
2018-08-16 14:30:19 +08:00
|
|
|
/* change the block device's logic address to physical address */
|
2016-10-09 09:06:01 +08:00
|
|
|
rt_off_t phy_pos = pos * rtt_dev->geometry.bytes_per_sector;
|
|
|
|
rt_size_t phy_size = size * rtt_dev->geometry.bytes_per_sector;
|
|
|
|
|
|
|
|
if (sfud_erase_write(sfud_dev, phy_pos, phy_size, buffer) != SFUD_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SPI write data then read data
|
|
|
|
*/
|
|
|
|
static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
|
|
|
|
size_t read_size) {
|
|
|
|
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);
|
2019-02-20 16:27:27 +08:00
|
|
|
|
|
|
|
RT_ASSERT(spi);
|
|
|
|
RT_ASSERT(sfud_dev);
|
|
|
|
RT_ASSERT(rtt_dev);
|
2018-11-24 15:11:24 +08:00
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
struct rt_qspi_device *qspi_dev = RT_NULL;
|
|
|
|
#endif
|
2016-10-09 09:06:01 +08:00
|
|
|
if (write_size) {
|
|
|
|
RT_ASSERT(write_buf);
|
|
|
|
}
|
|
|
|
if (read_size) {
|
|
|
|
RT_ASSERT(read_buf);
|
|
|
|
}
|
2018-11-24 15:11:24 +08:00
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
if(rtt_dev->rt_spi_device->bus->mode & RT_SPI_BUS_MODE_QSPI) {
|
|
|
|
qspi_dev = (struct rt_qspi_device *) (rtt_dev->rt_spi_device);
|
|
|
|
if (write_size && read_size) {
|
2019-12-03 19:26:10 +08:00
|
|
|
if (rt_qspi_send_then_recv(qspi_dev, write_buf, write_size, read_buf, read_size) <= 0) {
|
2018-11-24 15:11:24 +08:00
|
|
|
result = SFUD_ERR_TIMEOUT;
|
|
|
|
}
|
|
|
|
} else if (write_size) {
|
2019-12-03 19:26:10 +08:00
|
|
|
if (rt_qspi_send(qspi_dev, write_buf, write_size) <= 0) {
|
2018-11-24 15:11:24 +08:00
|
|
|
result = SFUD_ERR_TIMEOUT;
|
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
2018-11-24 15:11:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (write_size && read_size) {
|
|
|
|
if (rt_spi_send_then_recv(rtt_dev->rt_spi_device, write_buf, write_size, read_buf, read_size) != RT_EOK) {
|
|
|
|
result = SFUD_ERR_TIMEOUT;
|
|
|
|
}
|
|
|
|
} else if (write_size) {
|
2019-12-03 19:26:10 +08:00
|
|
|
if (rt_spi_send(rtt_dev->rt_spi_device, write_buf, write_size) <= 0) {
|
2018-11-24 15:11:24 +08:00
|
|
|
result = SFUD_ERR_TIMEOUT;
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-03 19:26:10 +08:00
|
|
|
if (rt_spi_recv(rtt_dev->rt_spi_device, read_buf, read_size) <= 0) {
|
2018-11-24 15:11:24 +08:00
|
|
|
result = SFUD_ERR_TIMEOUT;
|
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-24 15:11:24 +08:00
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
/**
|
|
|
|
* QSPI fast read data
|
|
|
|
*/
|
|
|
|
static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format, uint8_t *read_buf, size_t read_size) {
|
|
|
|
struct rt_qspi_message message;
|
|
|
|
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);
|
|
|
|
struct rt_qspi_device *qspi_dev = (struct rt_qspi_device *) (rtt_dev->rt_spi_device);
|
2019-02-20 16:27:27 +08:00
|
|
|
|
|
|
|
RT_ASSERT(spi);
|
|
|
|
RT_ASSERT(sfud_dev);
|
|
|
|
RT_ASSERT(rtt_dev);
|
|
|
|
RT_ASSERT(qspi_dev);
|
|
|
|
|
2018-11-24 15:11:24 +08:00
|
|
|
/* set message struct */
|
|
|
|
message.instruction.content = qspi_read_cmd_format->instruction;
|
|
|
|
message.instruction.qspi_lines = qspi_read_cmd_format->instruction_lines;
|
|
|
|
|
|
|
|
message.address.content = addr;
|
|
|
|
message.address.size = qspi_read_cmd_format->address_size;
|
|
|
|
message.address.qspi_lines = qspi_read_cmd_format->address_lines;
|
|
|
|
|
|
|
|
message.alternate_bytes.content = 0;
|
|
|
|
message.alternate_bytes.size = 0;
|
|
|
|
message.alternate_bytes.qspi_lines = 0;
|
|
|
|
|
|
|
|
message.dummy_cycles = qspi_read_cmd_format->dummy_cycles;
|
|
|
|
|
|
|
|
message.parent.send_buf = RT_NULL;
|
|
|
|
message.parent.recv_buf = read_buf;
|
|
|
|
message.parent.length = read_size;
|
|
|
|
message.parent.cs_release = 1;
|
|
|
|
message.parent.cs_take = 1;
|
|
|
|
message.qspi_data_lines = qspi_read_cmd_format->data_lines;
|
2019-02-20 16:27:27 +08:00
|
|
|
|
2018-11-24 15:11:24 +08:00
|
|
|
if (rt_qspi_transfer_message(qspi_dev, &message) != read_size) {
|
|
|
|
result = SFUD_ERR_TIMEOUT;
|
|
|
|
}
|
2019-02-20 16:27:27 +08:00
|
|
|
|
2018-11-24 15:11:24 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
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);
|
|
|
|
|
2019-02-20 16:27:27 +08:00
|
|
|
RT_ASSERT(spi);
|
|
|
|
RT_ASSERT(sfud_dev);
|
|
|
|
RT_ASSERT(rtt_dev);
|
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
rt_mutex_take(&(rtt_dev->lock), RT_WAITING_FOREVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-02-20 16:27:27 +08:00
|
|
|
RT_ASSERT(spi);
|
|
|
|
RT_ASSERT(sfud_dev);
|
|
|
|
RT_ASSERT(rtt_dev);
|
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
rt_mutex_release(&(rtt_dev->lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void retry_delay_100us(void) {
|
|
|
|
/* 100 microsecond delay */
|
|
|
|
rt_thread_delay((RT_TICK_PER_SECOND * 1 + 9999) / 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
sfud_err sfud_spi_port_init(sfud_flash *flash) {
|
|
|
|
sfud_err result = SFUD_SUCCESS;
|
|
|
|
|
2019-02-20 16:27:27 +08:00
|
|
|
RT_ASSERT(flash);
|
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
/* port SPI device interface */
|
|
|
|
flash->spi.wr = spi_write_read;
|
2018-11-24 15:11:24 +08:00
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
flash->spi.qspi_read = qspi_read;
|
|
|
|
#endif
|
2016-10-09 09:06:01 +08:00
|
|
|
flash->spi.lock = spi_lock;
|
|
|
|
flash->spi.unlock = spi_unlock;
|
|
|
|
flash->spi.user_data = flash;
|
2018-06-08 12:01:30 +08:00
|
|
|
if (RT_TICK_PER_SECOND < 1000) {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_W("[SFUD] Warning: The OS tick(%d) is less than 1000. So the flash write will take more time.", RT_TICK_PER_SECOND);
|
2018-06-08 12:01:30 +08:00
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
/* 100 microsecond delay */
|
|
|
|
flash->retry.delay = retry_delay_100us;
|
|
|
|
/* 60 seconds timeout */
|
|
|
|
flash->retry.times = 60 * 10000;
|
2019-02-20 16:27:27 +08:00
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
2019-12-03 19:26:10 +08:00
|
|
|
const static struct rt_device_ops flash_device_ops =
|
2018-06-10 17:59:17 +08:00
|
|
|
{
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
rt_sfud_read,
|
|
|
|
rt_sfud_write,
|
|
|
|
rt_sfud_control
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
/**
|
2020-06-13 11:50:30 +08:00
|
|
|
* Probe SPI flash by SFUD (Serial Flash Universal Driver) driver library and though SPI device by specified configuration.
|
2016-10-09 09:06:01 +08:00
|
|
|
*
|
2016-10-10 10:24:30 +08:00
|
|
|
* @param spi_flash_dev_name the name which will create SPI flash device
|
|
|
|
* @param spi_dev_name using SPI device name
|
2020-06-13 11:50:30 +08:00
|
|
|
* @param spi_cfg SPI device configuration
|
|
|
|
* @param qspi_cfg QSPI device configuration
|
2016-10-09 09:06:01 +08:00
|
|
|
*
|
2016-10-10 10:24:30 +08:00
|
|
|
* @return probed SPI flash device, probe failed will return RT_NULL
|
2016-10-09 09:06:01 +08:00
|
|
|
*/
|
2020-06-13 12:07:20 +08:00
|
|
|
rt_spi_flash_device_t rt_sfud_flash_probe_ex(const char *spi_flash_dev_name, const char *spi_dev_name,
|
2020-06-13 11:50:30 +08:00
|
|
|
struct rt_spi_configuration *spi_cfg, struct rt_qspi_configuration *qspi_cfg)
|
|
|
|
{
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_spi_flash_device_t rtt_dev = RT_NULL;
|
|
|
|
sfud_flash *sfud_dev = RT_NULL;
|
|
|
|
char *spi_flash_dev_name_bak = RT_NULL, *spi_dev_name_bak = RT_NULL;
|
2016-10-09 09:06:01 +08:00
|
|
|
extern sfud_err sfud_device_init(sfud_flash *flash);
|
2018-11-24 15:11:24 +08:00
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
struct rt_qspi_device *qspi_dev = RT_NULL;
|
|
|
|
#endif
|
2016-10-09 09:06:01 +08:00
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
RT_ASSERT(spi_flash_dev_name);
|
|
|
|
RT_ASSERT(spi_dev_name);
|
|
|
|
|
|
|
|
rtt_dev = (rt_spi_flash_device_t) rt_malloc(sizeof(struct spi_flash_device));
|
|
|
|
sfud_dev = (sfud_flash_t) rt_malloc(sizeof(sfud_flash));
|
|
|
|
spi_flash_dev_name_bak = (char *) rt_malloc(rt_strlen(spi_flash_dev_name) + 1);
|
|
|
|
spi_dev_name_bak = (char *) rt_malloc(rt_strlen(spi_dev_name) + 1);
|
|
|
|
|
2018-01-28 18:29:33 +08:00
|
|
|
if (rtt_dev) {
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_memset(rtt_dev, 0, sizeof(struct spi_flash_device));
|
2018-01-28 18:29:33 +08:00
|
|
|
/* initialize lock */
|
2021-11-18 04:57:15 +08:00
|
|
|
rt_mutex_init(&(rtt_dev->lock), spi_flash_dev_name, RT_IPC_FLAG_PRIO);
|
2018-01-28 18:29:33 +08:00
|
|
|
}
|
2018-06-10 17:59:17 +08:00
|
|
|
|
2018-01-28 18:29:33 +08:00
|
|
|
if (rtt_dev && sfud_dev && spi_flash_dev_name_bak && spi_dev_name_bak) {
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_memset(sfud_dev, 0, sizeof(sfud_flash));
|
|
|
|
rt_strncpy(spi_flash_dev_name_bak, spi_flash_dev_name, rt_strlen(spi_flash_dev_name));
|
|
|
|
rt_strncpy(spi_dev_name_bak, spi_dev_name, rt_strlen(spi_dev_name));
|
|
|
|
/* make string end sign */
|
|
|
|
spi_flash_dev_name_bak[rt_strlen(spi_flash_dev_name)] = '\0';
|
|
|
|
spi_dev_name_bak[rt_strlen(spi_dev_name)] = '\0';
|
|
|
|
/* SPI configure */
|
|
|
|
{
|
|
|
|
/* RT-Thread SPI device initialize */
|
|
|
|
rtt_dev->rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
|
2016-10-14 22:51:18 +08:00
|
|
|
if (rtt_dev->rt_spi_device == RT_NULL || rtt_dev->rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_E("ERROR: SPI device %s not found!", spi_dev_name);
|
2016-10-10 10:24:30 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
sfud_dev->spi.name = spi_dev_name_bak;
|
2018-11-24 15:11:24 +08:00
|
|
|
|
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
/* set the qspi line number and configure the QSPI bus */
|
|
|
|
if(rtt_dev->rt_spi_device->bus->mode &RT_SPI_BUS_MODE_QSPI) {
|
|
|
|
qspi_dev = (struct rt_qspi_device *)rtt_dev->rt_spi_device;
|
2020-06-13 11:50:30 +08:00
|
|
|
qspi_cfg->qspi_dl_width = qspi_dev->config.qspi_dl_width;
|
|
|
|
rt_qspi_configure(qspi_dev, qspi_cfg);
|
2018-11-24 15:11:24 +08:00
|
|
|
}
|
|
|
|
else
|
2019-02-20 16:27:27 +08:00
|
|
|
#endif
|
2020-06-13 11:50:30 +08:00
|
|
|
rt_spi_configure(rtt_dev->rt_spi_device, spi_cfg);
|
2016-10-10 10:24:30 +08:00
|
|
|
}
|
|
|
|
/* SFUD flash device initialize */
|
|
|
|
{
|
|
|
|
sfud_dev->name = spi_flash_dev_name_bak;
|
|
|
|
/* accessed each other */
|
|
|
|
rtt_dev->user_data = sfud_dev;
|
2018-08-16 14:30:19 +08:00
|
|
|
rtt_dev->rt_spi_device->user_data = rtt_dev;
|
2016-10-10 10:24:30 +08:00
|
|
|
rtt_dev->flash_device.user_data = rtt_dev;
|
|
|
|
sfud_dev->user_data = rtt_dev;
|
|
|
|
/* initialize SFUD device */
|
|
|
|
if (sfud_device_init(sfud_dev) != SFUD_SUCCESS) {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_E("ERROR: SPI flash probe failed by SPI device %s.", spi_dev_name);
|
2016-10-10 10:24:30 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* when initialize success, then copy SFUD flash device's geometry to RT-Thread SPI flash device */
|
|
|
|
rtt_dev->geometry.sector_count = sfud_dev->chip.capacity / sfud_dev->chip.erase_gran;
|
|
|
|
rtt_dev->geometry.bytes_per_sector = sfud_dev->chip.erase_gran;
|
|
|
|
rtt_dev->geometry.block_size = sfud_dev->chip.erase_gran;
|
2018-11-24 15:11:24 +08:00
|
|
|
#ifdef SFUD_USING_QSPI
|
|
|
|
/* reconfigure the QSPI bus for medium size */
|
|
|
|
if(rtt_dev->rt_spi_device->bus->mode &RT_SPI_BUS_MODE_QSPI) {
|
2020-06-13 11:50:30 +08:00
|
|
|
qspi_cfg->medium_size = sfud_dev->chip.capacity;
|
|
|
|
rt_qspi_configure(qspi_dev, qspi_cfg);
|
2018-11-24 15:11:24 +08:00
|
|
|
if(qspi_dev->enter_qspi_mode != RT_NULL)
|
|
|
|
qspi_dev->enter_qspi_mode(qspi_dev);
|
2019-02-13 17:33:27 +08:00
|
|
|
|
|
|
|
/* set data lines width */
|
|
|
|
sfud_qspi_fast_read_enable(sfud_dev, qspi_dev->config.qspi_dl_width);
|
2018-11-24 15:11:24 +08:00
|
|
|
}
|
|
|
|
#endif /* SFUD_USING_QSPI */
|
2016-10-10 10:24:30 +08:00
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
/* register device */
|
|
|
|
rtt_dev->flash_device.type = RT_Device_Class_Block;
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
rtt_dev->flash_device.ops = &flash_device_ops;
|
|
|
|
#else
|
2016-10-10 10:24:30 +08:00
|
|
|
rtt_dev->flash_device.init = RT_NULL;
|
|
|
|
rtt_dev->flash_device.open = RT_NULL;
|
|
|
|
rtt_dev->flash_device.close = RT_NULL;
|
|
|
|
rtt_dev->flash_device.read = rt_sfud_read;
|
|
|
|
rtt_dev->flash_device.write = rt_sfud_write;
|
|
|
|
rtt_dev->flash_device.control = rt_sfud_control;
|
2018-06-10 17:59:17 +08:00
|
|
|
#endif
|
2016-10-09 09:06:01 +08:00
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_device_register(&(rtt_dev->flash_device), spi_flash_dev_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
|
|
|
|
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_I("Probe SPI flash %s by SPI device %s success.",spi_flash_dev_name, spi_dev_name);
|
2016-10-10 10:24:30 +08:00
|
|
|
return rtt_dev;
|
|
|
|
} else {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_E("ERROR: Low memory.");
|
2016-10-10 10:24:30 +08:00
|
|
|
goto error;
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
error:
|
2018-01-28 18:29:33 +08:00
|
|
|
|
|
|
|
if (rtt_dev) {
|
|
|
|
rt_mutex_detach(&(rtt_dev->lock));
|
|
|
|
}
|
2016-10-10 10:24:30 +08:00
|
|
|
/* may be one of objects memory was malloc success, so need free all */
|
|
|
|
rt_free(rtt_dev);
|
|
|
|
rt_free(sfud_dev);
|
|
|
|
rt_free(spi_flash_dev_name_bak);
|
|
|
|
rt_free(spi_dev_name_bak);
|
2016-10-09 09:06:01 +08:00
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
return RT_NULL;
|
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
|
2020-06-13 11:50:30 +08:00
|
|
|
/**
|
|
|
|
* Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device.
|
|
|
|
*
|
|
|
|
* @param spi_flash_dev_name the name which will create SPI flash device
|
|
|
|
* @param spi_dev_name using SPI device name
|
|
|
|
*
|
|
|
|
* @return probed SPI flash device, probe failed will return RT_NULL
|
|
|
|
*/
|
|
|
|
rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const char *spi_dev_name)
|
|
|
|
{
|
|
|
|
struct rt_spi_configuration cfg = RT_SFUD_DEFAULT_SPI_CFG;
|
|
|
|
#ifndef SFUD_USING_QSPI
|
2020-06-13 12:07:20 +08:00
|
|
|
return rt_sfud_flash_probe_ex(spi_flash_dev_name, spi_dev_name, &cfg, RT_NULL);
|
2020-06-13 11:50:30 +08:00
|
|
|
#else
|
|
|
|
struct rt_qspi_configuration qspi_cfg = RT_SFUD_DEFAULT_QSPI_CFG;
|
|
|
|
|
2020-06-13 12:07:20 +08:00
|
|
|
return rt_sfud_flash_probe_ex(spi_flash_dev_name, spi_dev_name, &cfg, &qspi_cfg);
|
2020-06-13 11:50:30 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
/**
|
|
|
|
* Delete SPI flash device
|
|
|
|
*
|
|
|
|
* @param spi_flash_dev SPI flash device
|
|
|
|
*
|
|
|
|
* @return the operation status, RT_EOK on successful
|
|
|
|
*/
|
|
|
|
rt_err_t rt_sfud_flash_delete(rt_spi_flash_device_t spi_flash_dev) {
|
|
|
|
sfud_flash *sfud_flash_dev = (sfud_flash *) (spi_flash_dev->user_data);
|
2016-10-09 09:06:01 +08:00
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
RT_ASSERT(spi_flash_dev);
|
|
|
|
RT_ASSERT(sfud_flash_dev);
|
|
|
|
|
|
|
|
rt_device_unregister(&(spi_flash_dev->flash_device));
|
|
|
|
|
|
|
|
rt_mutex_detach(&(spi_flash_dev->lock));
|
|
|
|
|
|
|
|
rt_free(sfud_flash_dev->spi.name);
|
|
|
|
rt_free(sfud_flash_dev->name);
|
|
|
|
rt_free(sfud_flash_dev);
|
|
|
|
rt_free(spi_flash_dev);
|
2016-10-09 09:06:01 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2019-01-15 20:13:49 +08:00
|
|
|
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;
|
2019-02-20 16:27:27 +08:00
|
|
|
|
2019-01-15 20:13:49 +08:00
|
|
|
rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
|
2019-02-20 16:27:27 +08:00
|
|
|
if (rt_spi_device == RT_NULL || rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_E("ERROR: SPI device %s not found!", spi_dev_name);
|
2019-02-20 16:27:27 +08:00
|
|
|
goto __error;
|
2019-01-15 20:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-02-20 16:27:27 +08:00
|
|
|
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);
|
2019-01-15 20:13:49 +08:00
|
|
|
return sfud_dev;
|
2019-02-20 16:27:27 +08:00
|
|
|
} else {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_E("ERROR: SFUD flash device not found!");
|
2019-02-20 16:27:27 +08:00
|
|
|
goto __error;
|
2019-01-15 20:13:49 +08:00
|
|
|
}
|
2019-02-20 16:27:27 +08:00
|
|
|
|
|
|
|
__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) {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_E("ERROR: Flash device %s not found!", flash_dev_name);
|
2019-02-20 16:27:27 +08:00
|
|
|
goto __error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rtt_dev->user_data) {
|
|
|
|
sfud_dev = (sfud_flash_t) (rtt_dev->user_data);
|
|
|
|
return sfud_dev;
|
|
|
|
} else {
|
2020-06-13 11:50:30 +08:00
|
|
|
LOG_E("ERROR: SFUD flash device not found!");
|
2019-02-20 16:27:27 +08:00
|
|
|
goto __error;
|
2019-01-15 20:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-02-20 16:27:27 +08:00
|
|
|
__error:
|
2019-01-15 20:13:49 +08:00
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-23 17:30:05 +08:00
|
|
|
#if defined(RT_USING_FINSH)
|
2016-10-09 09:06:01 +08:00
|
|
|
|
|
|
|
#include <finsh.h>
|
|
|
|
|
|
|
|
static void sf(uint8_t argc, char **argv) {
|
|
|
|
|
2019-02-20 16:27:27 +08:00
|
|
|
#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
|
|
|
|
#define HEXDUMP_WIDTH 16
|
2018-06-08 12:01:30 +08:00
|
|
|
#define CMD_PROBE_INDEX 0
|
2016-10-09 09:06:01 +08:00
|
|
|
#define CMD_READ_INDEX 1
|
|
|
|
#define CMD_WRITE_INDEX 2
|
|
|
|
#define CMD_ERASE_INDEX 3
|
|
|
|
#define CMD_RW_STATUS_INDEX 4
|
|
|
|
#define CMD_BENCH_INDEX 5
|
|
|
|
|
|
|
|
sfud_err result = SFUD_SUCCESS;
|
2016-10-10 10:24:30 +08:00
|
|
|
static const sfud_flash *sfud_dev = NULL;
|
|
|
|
static rt_spi_flash_device_t rtt_dev = NULL, rtt_dev_bak = NULL;
|
2019-02-20 16:27:27 +08:00
|
|
|
size_t i = 0, j = 0;
|
2016-10-09 09:06:01 +08:00
|
|
|
|
|
|
|
const char* sf_help_info[] = {
|
2018-06-08 12:01:30 +08:00
|
|
|
[CMD_PROBE_INDEX] = "sf probe [spi_device] - probe and init SPI flash by given 'spi_device'",
|
2016-10-09 09:06:01 +08:00
|
|
|
[CMD_READ_INDEX] = "sf read addr size - read 'size' bytes starting at 'addr'",
|
|
|
|
[CMD_WRITE_INDEX] = "sf write addr data1 ... dataN - write some bytes 'data' to flash starting at 'addr'",
|
|
|
|
[CMD_ERASE_INDEX] = "sf erase addr size - erase 'size' bytes starting at 'addr'",
|
|
|
|
[CMD_RW_STATUS_INDEX] = "sf status [<volatile> <status>] - read or write '1:volatile|0:non-volatile' 'status'",
|
2016-10-10 10:24:30 +08:00
|
|
|
[CMD_BENCH_INDEX] = "sf bench - full chip benchmark. DANGER: It will erase full chip!",
|
2016-10-09 09:06:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
rt_kprintf("Usage:\n");
|
|
|
|
for (i = 0; i < sizeof(sf_help_info) / sizeof(char*); i++) {
|
|
|
|
rt_kprintf("%s\n", sf_help_info[i]);
|
|
|
|
}
|
|
|
|
rt_kprintf("\n");
|
|
|
|
} else {
|
|
|
|
const char *operator = argv[1];
|
|
|
|
uint32_t addr, size;
|
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
if (!strcmp(operator, "probe")) {
|
2016-10-09 09:06:01 +08:00
|
|
|
if (argc < 3) {
|
2018-06-08 12:01:30 +08:00
|
|
|
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_PROBE_INDEX]);
|
2016-10-09 09:06:01 +08:00
|
|
|
} else {
|
2016-10-10 10:24:30 +08:00
|
|
|
char *spi_dev_name = argv[2];
|
|
|
|
rtt_dev_bak = rtt_dev;
|
2019-02-20 16:27:27 +08:00
|
|
|
|
2018-11-14 17:56:09 +08:00
|
|
|
/* delete the old SPI flash device */
|
|
|
|
if(rtt_dev_bak) {
|
|
|
|
rt_sfud_flash_delete(rtt_dev_bak);
|
|
|
|
}
|
2019-02-20 16:27:27 +08:00
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
rtt_dev = rt_sfud_flash_probe("sf_cmd", spi_dev_name);
|
|
|
|
if (!rtt_dev) {
|
2016-10-09 09:06:01 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-11-14 17:56:09 +08:00
|
|
|
|
2016-10-10 10:24:30 +08:00
|
|
|
sfud_dev = (sfud_flash_t)rtt_dev->user_data;
|
|
|
|
if (sfud_dev->chip.capacity < 1024 * 1024) {
|
|
|
|
rt_kprintf("%d KB %s is current selected device.\n", sfud_dev->chip.capacity / 1024, sfud_dev->name);
|
2016-10-09 09:06:01 +08:00
|
|
|
} else {
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_kprintf("%d MB %s is current selected device.\n", sfud_dev->chip.capacity / 1024 / 1024,
|
|
|
|
sfud_dev->name);
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-10 10:24:30 +08:00
|
|
|
if (!sfud_dev) {
|
|
|
|
rt_kprintf("No flash device selected. Please run 'sf probe'.\n");
|
2016-10-09 09:06:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!rt_strcmp(operator, "read")) {
|
|
|
|
if (argc < 4) {
|
|
|
|
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_READ_INDEX]);
|
|
|
|
return;
|
|
|
|
} else {
|
2019-02-20 16:27:27 +08:00
|
|
|
addr = strtol(argv[2], NULL, 0);
|
|
|
|
size = strtol(argv[3], NULL, 0);
|
2016-10-09 09:06:01 +08:00
|
|
|
uint8_t *data = rt_malloc(size);
|
|
|
|
if (data) {
|
2016-10-10 10:24:30 +08:00
|
|
|
result = sfud_read(sfud_dev, addr, size, data);
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result == SFUD_SUCCESS) {
|
|
|
|
rt_kprintf("Read the %s flash data success. Start from 0x%08X, size is %ld. The data is:\n",
|
2016-10-10 10:24:30 +08:00
|
|
|
sfud_dev->name, addr, size);
|
2016-10-09 09:06:01 +08:00
|
|
|
rt_kprintf("Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
|
2019-02-20 16:27:27 +08:00
|
|
|
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(" ");
|
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
2019-02-20 16:27:27 +08:00
|
|
|
/* 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] : '.');
|
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
2019-02-20 16:27:27 +08:00
|
|
|
rt_kprintf("\n");
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
|
|
|
rt_kprintf("\n");
|
|
|
|
}
|
|
|
|
rt_free(data);
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Low memory!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!rt_strcmp(operator, "write")) {
|
|
|
|
if (argc < 4) {
|
|
|
|
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_WRITE_INDEX]);
|
|
|
|
return;
|
|
|
|
} else {
|
2019-02-20 16:27:27 +08:00
|
|
|
addr = strtol(argv[2], NULL, 0);
|
2016-10-09 09:06:01 +08:00
|
|
|
size = argc - 3;
|
|
|
|
uint8_t *data = rt_malloc(size);
|
|
|
|
if (data) {
|
|
|
|
for (i = 0; i < size; i++) {
|
2019-02-20 16:27:27 +08:00
|
|
|
data[i] = strtol(argv[3 + i], NULL, 0);
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
2016-10-10 10:24:30 +08:00
|
|
|
result = sfud_write(sfud_dev, addr, size, data);
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result == SFUD_SUCCESS) {
|
|
|
|
rt_kprintf("Write the %s flash data success. Start from 0x%08X, size is %ld.\n",
|
2016-10-10 10:24:30 +08:00
|
|
|
sfud_dev->name, addr, size);
|
2016-10-09 09:06:01 +08:00
|
|
|
rt_kprintf("Write data: ");
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
rt_kprintf("%d ", data[i]);
|
|
|
|
}
|
|
|
|
rt_kprintf(".\n");
|
|
|
|
}
|
|
|
|
rt_free(data);
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Low memory!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!rt_strcmp(operator, "erase")) {
|
|
|
|
if (argc < 4) {
|
|
|
|
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_ERASE_INDEX]);
|
|
|
|
return;
|
|
|
|
} else {
|
2019-02-20 16:27:27 +08:00
|
|
|
addr = strtol(argv[2], NULL, 0);
|
|
|
|
size = strtol(argv[3], NULL, 0);
|
2016-10-10 10:24:30 +08:00
|
|
|
result = sfud_erase(sfud_dev, addr, size);
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result == SFUD_SUCCESS) {
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_kprintf("Erase the %s flash data success. Start from 0x%08X, size is %ld.\n", sfud_dev->name,
|
2016-10-09 09:06:01 +08:00
|
|
|
addr, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!rt_strcmp(operator, "status")) {
|
|
|
|
if (argc < 3) {
|
|
|
|
uint8_t status;
|
2016-10-10 10:24:30 +08:00
|
|
|
result = sfud_read_status(sfud_dev, &status);
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result == SFUD_SUCCESS) {
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_kprintf("The %s flash status register current value is 0x%02X.\n", sfud_dev->name, status);
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
|
|
|
} else if (argc == 4) {
|
2019-02-20 16:27:27 +08:00
|
|
|
bool is_volatile = strtol(argv[2], NULL, 0);
|
|
|
|
uint8_t status = strtol(argv[3], NULL, 0);
|
2016-10-10 10:24:30 +08:00
|
|
|
result = sfud_write_status(sfud_dev, is_volatile, status);
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result == SFUD_SUCCESS) {
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_kprintf("Write the %s flash status register to 0x%02X success.\n", sfud_dev->name, status);
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Usage: %s.\n", sf_help_info[CMD_RW_STATUS_INDEX]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!rt_strcmp(operator, "bench")) {
|
2016-10-10 10:24:30 +08:00
|
|
|
if ((argc > 2 && rt_strcmp(argv[2], "yes")) || argc < 3) {
|
2016-10-10 20:10:41 +08:00
|
|
|
rt_kprintf("DANGER: It will erase full chip! Please run 'sf bench yes'.\n");
|
2016-10-10 10:24:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
/* full chip benchmark test */
|
|
|
|
addr = 0;
|
2016-10-10 10:24:30 +08:00
|
|
|
size = sfud_dev->chip.capacity;
|
2016-10-09 09:06:01 +08:00
|
|
|
uint32_t start_time, time_cast;
|
2020-07-06 20:25:18 +08:00
|
|
|
size_t write_size = SFUD_WRITE_MAX_PAGE_SIZE, read_size = SFUD_WRITE_MAX_PAGE_SIZE, cur_op_size;
|
2016-10-09 09:06:01 +08:00
|
|
|
uint8_t *write_data = rt_malloc(write_size), *read_data = rt_malloc(read_size);
|
|
|
|
|
|
|
|
if (write_data && read_data) {
|
2020-07-06 20:25:18 +08:00
|
|
|
for (i = 0; i < write_size; i ++) {
|
|
|
|
write_data[i] = i & 0xFF;
|
|
|
|
}
|
2016-10-09 09:06:01 +08:00
|
|
|
/* benchmark testing */
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_kprintf("Erasing the %s %ld bytes data, waiting...\n", sfud_dev->name, size);
|
2016-10-09 09:06:01 +08:00
|
|
|
start_time = rt_tick_get();
|
2016-10-10 10:24:30 +08:00
|
|
|
result = sfud_erase(sfud_dev, addr, size);
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result == SFUD_SUCCESS) {
|
|
|
|
time_cast = rt_tick_get() - start_time;
|
|
|
|
rt_kprintf("Erase benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND,
|
|
|
|
time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000));
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Erase benchmark has an error. Error code: %d.\n", result);
|
|
|
|
}
|
|
|
|
/* write test */
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_kprintf("Writing the %s %ld bytes data, waiting...\n", sfud_dev->name, size);
|
2016-10-09 09:06:01 +08:00
|
|
|
start_time = rt_tick_get();
|
|
|
|
for (i = 0; i < size; i += write_size) {
|
2020-07-06 20:25:18 +08:00
|
|
|
if (i + write_size <= size) {
|
|
|
|
cur_op_size = write_size;
|
|
|
|
} else {
|
|
|
|
cur_op_size = size - i;
|
|
|
|
}
|
|
|
|
result = sfud_write(sfud_dev, addr + i, cur_op_size, write_data);
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result != SFUD_SUCCESS) {
|
2019-12-03 19:26:10 +08:00
|
|
|
rt_kprintf("Writing %s failed, already wr for %lu bytes, write %d each time\n", sfud_dev->name, i, write_size);
|
2016-10-09 09:06:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == SFUD_SUCCESS) {
|
|
|
|
time_cast = rt_tick_get() - start_time;
|
|
|
|
rt_kprintf("Write benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND,
|
|
|
|
time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000));
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Write benchmark has an error. Error code: %d.\n", result);
|
|
|
|
}
|
|
|
|
/* read test */
|
2016-10-10 10:24:30 +08:00
|
|
|
rt_kprintf("Reading the %s %ld bytes data, waiting...\n", sfud_dev->name, size);
|
2016-10-09 09:06:01 +08:00
|
|
|
start_time = rt_tick_get();
|
|
|
|
for (i = 0; i < size; i += read_size) {
|
|
|
|
if (i + read_size <= size) {
|
2020-07-06 20:25:18 +08:00
|
|
|
cur_op_size = read_size;
|
2016-10-09 09:06:01 +08:00
|
|
|
} else {
|
2020-07-06 20:25:18 +08:00
|
|
|
cur_op_size = size - i;
|
2016-10-09 09:06:01 +08:00
|
|
|
}
|
2020-07-06 20:25:18 +08:00
|
|
|
result = sfud_read(sfud_dev, addr + i, cur_op_size, read_data);
|
2018-12-05 19:59:06 +08:00
|
|
|
/* data check */
|
2020-07-06 20:25:18 +08:00
|
|
|
if (memcmp(write_data, read_data, cur_op_size))
|
2018-12-05 19:59:06 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("Data check ERROR! Please check you flash by other command.\n");
|
|
|
|
result = SFUD_ERR_READ;
|
|
|
|
}
|
2019-02-20 16:27:27 +08:00
|
|
|
|
2016-10-09 09:06:01 +08:00
|
|
|
if (result != SFUD_SUCCESS) {
|
2019-12-03 19:26:10 +08:00
|
|
|
rt_kprintf("Read %s failed, already rd for %lu bytes, read %d each time\n", sfud_dev->name, i, read_size);
|
2016-10-09 09:06:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == SFUD_SUCCESS) {
|
|
|
|
time_cast = rt_tick_get() - start_time;
|
|
|
|
rt_kprintf("Read benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND,
|
|
|
|
time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000));
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Read benchmark has an error. Error code: %d.\n", result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Low memory!\n");
|
|
|
|
}
|
|
|
|
rt_free(write_data);
|
|
|
|
rt_free(read_data);
|
|
|
|
} else {
|
|
|
|
rt_kprintf("Usage:\n");
|
|
|
|
for (i = 0; i < sizeof(sf_help_info) / sizeof(char*); i++) {
|
|
|
|
rt_kprintf("%s\n", sf_help_info[i]);
|
|
|
|
}
|
|
|
|
rt_kprintf("\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result != SFUD_SUCCESS) {
|
|
|
|
rt_kprintf("This flash operate has an error. Error code: %d.\n", result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MSH_CMD_EXPORT(sf, SPI Flash operate.);
|
2021-08-23 17:30:05 +08:00
|
|
|
#endif /* defined(RT_USING_FINSH) */
|
2016-10-09 09:06:01 +08:00
|
|
|
|
|
|
|
#endif /* RT_USING_SFUD */
|