summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorMurphyZhao <[email protected]>2018-08-16 14:30:19 +0800
committerMurphyZhao <[email protected]>2018-08-16 14:48:46 +0800
commit651149fcbb4347b8daf7e738f2d85bb7143808c7 (patch)
tree9c5329900d3140743951512a04160c49e4b1419d /components
parentd06f9ac9df5002836d6b239529951023895ad840 (diff)
[components/drivers/spi] 增加 sfud flash 设备查找接口
Signed-off-by: MurphyZhao <[email protected]>
Diffstat (limited to 'components')
-rw-r--r--components/drivers/include/drivers/spi.h5
-rw-r--r--components/drivers/spi/spi_flash_sfud.c34
-rw-r--r--components/drivers/spi/spi_flash_sfud.h10
3 files changed, 45 insertions, 4 deletions
diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h
index f76b329cab..0fbaabe5dd 100644
--- a/components/drivers/include/drivers/spi.h
+++ b/components/drivers/include/drivers/spi.h
@@ -36,8 +36,8 @@ extern "C"{
#define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
/**
* At CPOL=0 the base value of the clock is zero
- * - For CPHA=0, data are captured on the clock's rising edge (low��high transition)
- * and data are propagated on a falling edge (high��low clock transition).
+ * - For CPHA=0, data are captured on the clock's rising edge (low->high transition)
+ * and data are propagated on a falling edge (high->low clock transition).
* - For CPHA=1, data are captured on the clock's falling edge and data are
* propagated on a rising edge.
* At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
@@ -118,6 +118,7 @@ struct rt_spi_device
struct rt_spi_bus *bus;
struct rt_spi_configuration config;
+ void *user_data;
};
#define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
diff --git a/components/drivers/spi/spi_flash_sfud.c b/components/drivers/spi/spi_flash_sfud.c
index 5d5da858f3..72d462a871 100644
--- a/components/drivers/spi/spi_flash_sfud.c
+++ b/components/drivers/spi/spi_flash_sfud.c
@@ -97,7 +97,7 @@ 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);
- /* change the block device��s logic address to physical address */
+ /* 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;
@@ -111,7 +111,7 @@ 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);
- /* change the block device��s logic address to physical address */
+ /* 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;
@@ -298,6 +298,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
sfud_dev->name = spi_flash_dev_name_bak;
/* accessed each other */
rtt_dev->user_data = sfud_dev;
+ rtt_dev->rt_spi_device->user_data = rtt_dev;
rtt_dev->flash_device.user_data = rtt_dev;
sfud_dev->user_data = rtt_dev;
/* initialize SFUD device */
@@ -606,6 +607,35 @@ static void sf(uint8_t argc, char **argv) {
}
MSH_CMD_EXPORT(sf, SPI Flash operate.);
+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)
+ {
+ rt_kprintf("ERROR: SPI device %s not found!\n", spi_dev_name);
+ 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);
+ return sfud_dev;
+ }
+ else
+ {
+ rt_kprintf("ERROR: SFUD flash device not found!\n");
+ goto error;
+ }
+
+error:
+ return RT_NULL;
+}
+
#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
#endif /* RT_USING_SFUD */
diff --git a/components/drivers/spi/spi_flash_sfud.h b/components/drivers/spi/spi_flash_sfud.h
index a09d133fd1..2c7f8e7f54 100644
--- a/components/drivers/spi/spi_flash_sfud.h
+++ b/components/drivers/spi/spi_flash_sfud.h
@@ -27,6 +27,7 @@
#include <rtthread.h>
#include "./sfud/inc/sfud.h"
+#include "spi_flash.h"
/**
* Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device.
@@ -47,4 +48,13 @@ 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
+ *
+ * @param spi_dev_name using SPI device name
+ *
+ * @return sfud flash device if success, otherwise return RT_NULL
+ */
+sfud_flash_t rt_sfud_flash_find(const char *spi_dev_name);
+
#endif /* _SPI_FLASH_SFUD_H_ */