Selaa lähdekoodia

[stm32][spi] spi attach函数问题解决方案 (#6864)

attach #6819
liYangYang 2 vuotta sitten
vanhempi
commit
7ff64c1cfd
29 muutettua tiedostoa jossa 45 lisäystä ja 42 poistoa
  1. 14 12
      bsp/stm32/libraries/HAL_Drivers/drv_spi.c
  2. 1 1
      bsp/stm32/libraries/HAL_Drivers/drv_spi.h
  3. 1 1
      bsp/stm32/stm32f103-100ask-pro/board/ports/spi_flash_init.c
  4. 1 1
      bsp/stm32/stm32f103-atk-nano/board/ports/spi_flash_init.c
  5. 1 1
      bsp/stm32/stm32f103-dofly-M3S/applications/nrf24l01_init.c
  6. 1 1
      bsp/stm32/stm32f103-dofly-M3S/board/ports/drv_sdcard.c
  7. 1 1
      bsp/stm32/stm32f103-dofly-M3S/board/ports/spi_flash_init.c
  8. 1 1
      bsp/stm32/stm32f103-fire-arbitrary/board/ports/spi_flash_init.c
  9. 1 1
      bsp/stm32/stm32f103-fire-arbitrary/board/ports/w5500_device.c
  10. 1 1
      bsp/stm32/stm32f103-hw100k-ibox/board/ports/w5500_device.c
  11. 1 1
      bsp/stm32/stm32f407-armfly-v5/board/ports/spi_flash_init.c
  12. 1 1
      bsp/stm32/stm32f407-atk-explorer/board/ports/spi_flash_init.c
  13. 1 1
      bsp/stm32/stm32f411-atk-nano/board/ports/spi_flash_init.c
  14. 1 1
      bsp/stm32/stm32f429-armfly-v6/board/ports/spi_flash_init.c
  15. 1 1
      bsp/stm32/stm32f429-atk-apollo/board/ports/spi_flash_init.c
  16. 1 1
      bsp/stm32/stm32f429-fire-challenger/board/ports/spi_flash_init.c
  17. 1 1
      bsp/stm32/stm32h750-artpi/board/port/drv_spi_ili9488.c
  18. 1 1
      bsp/stm32/stm32h750-artpi/board/port/spi_flash_init.c
  19. 1 1
      bsp/stm32/stm32h750-weact-ministm32h7xx/board/port/drv_spi_flash.c
  20. 2 2
      bsp/stm32/stm32l431-BearPi/board/ports/lcd/drv_lcd.c
  21. 1 1
      bsp/stm32/stm32l475-atk-pandora/applications/nrf24l01_init.c
  22. 1 1
      bsp/stm32/stm32l475-atk-pandora/board/ports/drv_filesystem.c
  23. 1 1
      bsp/stm32/stm32l475-atk-pandora/board/ports/lcd/drv_lcd.c
  24. 1 1
      bsp/stm32/stm32l496-ali-developer/board/ports/drv_spi_lcd.c
  25. 1 1
      bsp/stm32/stm32mp157a-st-discovery/board/ports/spi_sample.c
  26. 1 1
      bsp/stm32/stm32mp157a-st-ev1/board/ports/spi_sample.c
  27. 3 1
      components/drivers/include/drivers/pin.h
  28. 2 2
      components/drivers/include/drivers/spi.h
  29. 0 1
      components/drivers/spi/spi_core.c

+ 14 - 12
bsp/stm32/libraries/HAL_Drivers/drv_spi.c

@@ -85,9 +85,6 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur
     RT_ASSERT(spi_drv != RT_NULL);
     RT_ASSERT(cfg != RT_NULL);
 
-    rt_pin_mode(cfg->cs_pin, PIN_MODE_OUTPUT);
-    rt_pin_write(cfg->cs_pin, PIN_HIGH);
-
     SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
 
     if (cfg->mode & RT_SPI_SLAVE)
@@ -299,12 +296,12 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
     struct stm32_spi *spi_drv =  rt_container_of(device->bus, struct stm32_spi, spi_bus);
     SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
 
-    if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
+    if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
     {
         if (device->config.mode & RT_SPI_CS_HIGH)
-            rt_pin_write(device->config.cs_pin, PIN_HIGH);
+            rt_pin_write(device->cs_pin, PIN_HIGH);
         else
-            rt_pin_write(device->config.cs_pin, PIN_LOW);
+            rt_pin_write(device->cs_pin, PIN_LOW);
     }
 
     LOG_D("%s transfer prepare and start", spi_drv->config->bus_name);
@@ -433,12 +430,12 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
 #endif /* SOC_SERIES_STM32H7 || SOC_SERIES_STM32F7 */
     }
 
-    if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
+    if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
     {
         if (device->config.mode & RT_SPI_CS_HIGH)
-            rt_pin_write(device->config.cs_pin, PIN_LOW);
+            rt_pin_write(device->cs_pin, PIN_LOW);
         else
-            rt_pin_write(device->config.cs_pin, PIN_HIGH);
+            rt_pin_write(device->cs_pin, PIN_HIGH);
     }
 
     return message->length;
@@ -571,7 +568,7 @@ static int rt_hw_spi_bus_init(void)
 /**
   * Attach the spi device to SPI bus, this function must be used after initialization.
   */
-rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, struct rt_spi_configuration *cfg)
+rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
 {
     RT_ASSERT(bus_name != RT_NULL);
     RT_ASSERT(device_name != RT_NULL);
@@ -583,9 +580,14 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name,
     spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
     RT_ASSERT(spi_device != RT_NULL);
 
-    result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL);
+    spi_device->cs_pin = cs_pin;
+    if(cs_pin != PIN_NONE)
+    {
+        rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
+        rt_pin_write(cs_pin, PIN_HIGH);
+    }
 
-    result = rt_spi_configure(spi_device, cfg);
+    result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL);
 
     if (result != RT_EOK)
     {

+ 1 - 1
bsp/stm32/libraries/HAL_Drivers/drv_spi.h

@@ -22,7 +22,7 @@
 extern "C" {
 #endif
 
-rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, struct rt_spi_configuration *cfg);
+rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin);
 
 #ifdef __cplusplus
 }

+ 1 - 1
bsp/stm32/stm32f103-100ask-pro/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOA_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
+    rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(A, 4));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi10"))
     {

+ 1 - 1
bsp/stm32/stm32f103-atk-nano/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOB_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi2", "spi20", GPIOB, GPIO_PIN_12);
+    rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(B, 12));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q16", "spi20"))
     {

+ 1 - 1
bsp/stm32/stm32f103-dofly-M3S/applications/nrf24l01_init.c

@@ -15,7 +15,7 @@
 #include "drv_spi.h"
 static int rt_hw_nrf24l01_init(void)
 {
-    rt_hw_spi_device_attach("spi2", "spi20", GPIOG, GPIO_PIN_7);
+    rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(G, 7));
     return RT_EOK;
 }
 INIT_COMPONENT_EXPORT(rt_hw_nrf24l01_init);

+ 1 - 1
bsp/stm32/stm32f103-dofly-M3S/board/ports/drv_sdcard.c

@@ -70,7 +70,7 @@ INIT_APP_EXPORT(stm32_sdcard_mount);
 static int rt_hw_spi2_tfcard(void)
 {
     __HAL_RCC_GPIOC_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi2", "spi20", GPIOD, GPIO_PIN_2);
+    rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(D, 2));
     return msd_init("sd0", "spi20");
 }
 INIT_DEVICE_EXPORT(rt_hw_spi2_tfcard);

+ 1 - 1
bsp/stm32/stm32f103-dofly-M3S/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOA_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi2", "spi20", GPIOB, GPIO_PIN_12);
+    rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(B, 12));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi20"))
     {

+ 1 - 1
bsp/stm32/stm32f103-fire-arbitrary/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOA_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
+    rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(A, 4));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi10"))
     {

+ 1 - 1
bsp/stm32/stm32f103-fire-arbitrary/board/ports/w5500_device.c

@@ -13,6 +13,6 @@
 int w5500_spi_device_init()
 {
     __HAL_RCC_GPIOG_CLK_ENABLE();
-    return rt_hw_spi_device_attach("spi2","spi20",GPIOG,GPIO_PIN_9);
+    return rt_hw_spi_device_attach("spi2","spi20",GET_PIN(G, 9));
 }
 INIT_DEVICE_EXPORT(w5500_spi_device_init);

+ 1 - 1
bsp/stm32/stm32f103-hw100k-ibox/board/ports/w5500_device.c

@@ -14,6 +14,6 @@
 int w5500_spi_device_init()
 {
     __HAL_RCC_GPIOB_CLK_ENABLE();
-    return rt_hw_spi_device_attach("spi2","spi20",GPIOB,GPIO_PIN_12);
+    return rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(B, 12));
 }
 INIT_DEVICE_EXPORT(w5500_spi_device_init);

+ 1 - 1
bsp/stm32/stm32f407-armfly-v5/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOF_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi3", "spi30", GPIOF, GPIO_PIN_8);
+    rt_hw_spi_device_attach("spi3", "spi30", GET_PIN(F, 8));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi30"))
     {

+ 1 - 1
bsp/stm32/stm32f407-atk-explorer/board/ports/spi_flash_init.c

@@ -18,7 +18,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOB_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14);
+    rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(B, 14));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
     {

+ 1 - 1
bsp/stm32/stm32f411-atk-nano/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOB_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi2", "spi20", GPIOB, GPIO_PIN_12);
+    rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(B, 12));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q16", "spi20"))
     {

+ 1 - 1
bsp/stm32/stm32f429-armfly-v6/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOF_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi3", "spi30", GPIOD, GPIO_PIN_13);
+    rt_hw_spi_device_attach("spi3", "spi30", GET_PIN(D, 13));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi30"))
     {

+ 1 - 1
bsp/stm32/stm32f429-atk-apollo/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOF_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi5", "spi50", GPIOF, GPIO_PIN_6);
+    rt_hw_spi_device_attach("spi5", "spi50", GET_PIN(F, 6));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q256", "spi50"))
     {

+ 1 - 1
bsp/stm32/stm32f429-fire-challenger/board/ports/spi_flash_init.c

@@ -17,7 +17,7 @@
 static int rt_hw_spi_flash_init(void)
 {
     __HAL_RCC_GPIOF_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi5", "spi50", GPIOF, GPIO_PIN_6);
+    rt_hw_spi_device_attach("spi5", "spi50", GET_PIN(F, 6));
 
     if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi50"))
     {

+ 1 - 1
bsp/stm32/stm32h750-artpi/board/port/drv_spi_ili9488.c

@@ -149,7 +149,7 @@ static void lcd_gpio_init(void)
 int rt_hw_spi_lcd_init(void)
 {
     __HAL_RCC_GPIOI_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi2", "spi20", GPIOI, GPIO_PIN_0);
+    rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(I, 0));
     lcd_gpio_init();
 
     rt_pin_write(LCD_RES_PIN, PIN_HIGH);

+ 1 - 1
bsp/stm32/stm32h750-artpi/board/port/spi_flash_init.c

@@ -18,7 +18,7 @@ static int rt_flash_init(void)
     extern rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const char *spi_dev_name);
     extern int fal_init(void);
 
-    rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
+    rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(A, 4));
 
     /* initialize SPI Flash device */
     rt_sfud_flash_probe("norflash0", "spi10");

+ 1 - 1
bsp/stm32/stm32h750-weact-ministm32h7xx/board/port/drv_spi_flash.c

@@ -27,7 +27,7 @@
 static int rt_hw_spi_flash_with_sfud_init(void)
 {
     rt_err_t err = RT_EOK;
-    rt_hw_spi_device_attach("spi1", "spi10", SPI_CS_GPIO, SPI_CS_PIN);
+    rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(D, 6));
 
     /* init W25Q16    , And register as a block device */
     if (RT_NULL == rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, "spi10"))

+ 2 - 2
bsp/stm32/stm32l431-BearPi/board/ports/lcd/drv_lcd.c

@@ -37,7 +37,7 @@ static int rt_hw_lcd_config(void)
         struct rt_spi_configuration cfg;
         cfg.data_width = 8;
         cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB;
-        cfg.max_hz = 42 * 1000 * 1000; /* 42M,SPI max 42MHz,lcd 4-wire spi */
+        cfg.max_hz = 42 * 1000 * 1000; /* 42M, SPI max 42MHz, lcd 4-wire spi */
 
         rt_spi_configure(spi_dev_lcd, &cfg);
     }
@@ -122,7 +122,7 @@ static void lcd_gpio_init(void)
 
 static int rt_hw_lcd_init(void)
 {
-    rt_hw_spi_device_attach("spi2", "lcd", GPIOC, GPIO_PIN_3);
+    rt_hw_spi_device_attach("spi2", "lcd", GET_PIN(C, 3));
     lcd_gpio_init();
     /* Memory Data Access Control */
     lcd_write_cmd(0x36);

+ 1 - 1
bsp/stm32/stm32l475-atk-pandora/applications/nrf24l01_init.c

@@ -14,7 +14,7 @@
 #include "drv_spi.h"
 static int rt_hw_nrf24l01_init(void)
 {
-    rt_hw_spi_device_attach("spi2", "spi20", GPIOD, GPIO_PIN_5);
+    rt_hw_spi_device_attach("spi2", "spi20", GET_PIN(D, 5));
     return RT_EOK;
 }
 INIT_COMPONENT_EXPORT(rt_hw_nrf24l01_init);

+ 1 - 1
bsp/stm32/stm32l475-atk-pandora/board/ports/drv_filesystem.c

@@ -32,7 +32,7 @@
 static int rt_hw_spi1_tfcard(void)
 {
     __HAL_RCC_GPIOC_CLK_ENABLE();
-    rt_hw_spi_device_attach("spi1", "spi10", GPIOC, GPIO_PIN_3);
+    rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(C, 3));
     return msd_init("sd0", "spi10");
 }
 INIT_DEVICE_EXPORT(rt_hw_spi1_tfcard);

+ 1 - 1
bsp/stm32/stm32l475-atk-pandora/board/ports/lcd/drv_lcd.c

@@ -130,7 +130,7 @@ static int rt_hw_lcd_init(void)
 {
     __HAL_RCC_GPIOD_CLK_ENABLE();
 
-    rt_hw_spi_device_attach("spi3", "spi30", GPIOD, GPIO_PIN_7);
+    rt_hw_spi_device_attach("spi3", "spi30", GET_PIN(D, 7));
     lcd_gpio_init();
 
     /* Memory Data Access Control */

+ 1 - 1
bsp/stm32/stm32l496-ali-developer/board/ports/drv_spi_lcd.c

@@ -18,7 +18,7 @@
 
 static int rt_hw_spi_lcd_init(void)
 {
-    rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
+    rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(A, 4));
 
     return RT_EOK;
 }

+ 1 - 1
bsp/stm32/stm32mp157a-st-discovery/board/ports/spi_sample.c

@@ -21,7 +21,7 @@ static int rt_spi_device_init(void)
 {
     struct rt_spi_configuration cfg;
 
-    rt_hw_spi_device_attach("spi5", "spi50", NULL, NULL);
+    rt_hw_spi_device_attach("spi5", "spi50", PIN_NONE);
 
     cfg.data_width = 8;
     cfg.mode   = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS;

+ 1 - 1
bsp/stm32/stm32mp157a-st-ev1/board/ports/spi_sample.c

@@ -22,7 +22,7 @@ static int rt_spi_device_init(void)
 {
     struct rt_spi_configuration cfg;
 
-    rt_hw_spi_device_attach(SPI_NAME, SPI_DEVICE_NAME, NULL, NULL);
+    rt_hw_spi_device_attach(SPI_NAME, SPI_DEVICE_NAME, PIN_NONE);
 
     cfg.data_width = 8;
     cfg.mode   = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS;

+ 3 - 1
components/drivers/include/drivers/pin.h

@@ -25,6 +25,8 @@ struct rt_device_pin
     const struct rt_pin_ops *ops;
 };
 
+#define PIN_NONE                (-1)
+
 #define PIN_LOW                 0x00
 #define PIN_HIGH                0x01
 
@@ -43,7 +45,7 @@ struct rt_device_pin
 #define PIN_IRQ_DISABLE                 0x00
 #define PIN_IRQ_ENABLE                  0x01
 
-#define PIN_IRQ_PIN_NONE                -1
+#define PIN_IRQ_PIN_NONE                PIN_NONE
 
 struct rt_device_pin_mode
 {

+ 2 - 2
components/drivers/include/drivers/spi.h

@@ -15,7 +15,7 @@
 
 #include <stdlib.h>
 #include <rtthread.h>
-#include <rtdevice.h>
+#include <drivers/pin.h>
 
 #ifdef __cplusplus
 extern "C"{
@@ -79,7 +79,6 @@ struct rt_spi_configuration
     rt_uint8_t mode;
     rt_uint8_t data_width;
     rt_uint16_t reserved;
-    rt_base_t cs_pin;
 
     rt_uint32_t max_hz;
 };
@@ -113,6 +112,7 @@ struct rt_spi_device
     struct rt_spi_bus *bus;
 
     struct rt_spi_configuration config;
+    rt_base_t cs_pin;
     void   *user_data;
 };
 

+ 0 - 1
components/drivers/spi/spi_core.c

@@ -80,7 +80,6 @@ rt_err_t rt_spi_configure(struct rt_spi_device        *device,
     device->config.data_width = cfg->data_width;
     device->config.mode       = cfg->mode & RT_SPI_MODE_MASK ;
     device->config.max_hz     = cfg->max_hz ;
-    device->config.cs_pin     = cfg->cs_pin ;
 
     if (device->bus != RT_NULL)
     {