Explorar o código

[components/drivers/spi] 增加 sfud flash 设备查找接口

Signed-off-by: MurphyZhao <d2014zjt@163.com>
MurphyZhao %!s(int64=6) %!d(string=hai) anos
pai
achega
651149fcbb

+ 3 - 2
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))
 

+ 32 - 2
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 */

+ 10 - 0
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_ */