diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index f76b329cabd296a4c662d3d78166756b7cff7156..0fbaabe5dd8f2acff35a840114423e2b61fc462e 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 5d5da858f3ee362e33da22c1e39b7a796babad35..72d462a87192cd160b16652b2e3f35be1a99ea0a 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 a09d133fd1e87943c45b0fd76cceb731e70848f2..2c7f8e7f5487c30d51744be13452daebd04689ae 100644 --- a/components/drivers/spi/spi_flash_sfud.h +++ b/components/drivers/spi/spi_flash_sfud.h @@ -27,6 +27,7 @@ #include #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_ */