Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions bsp/stm32/libraries/HAL_Drivers/drv_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -302,9 +299,9 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
{
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);
Expand Down Expand Up @@ -436,9 +433,9 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
{
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;
Expand Down Expand Up @@ -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);
Expand All @@ -583,10 +580,12 @@ 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);

rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
rt_pin_write(cs_pin, PIN_HIGH);
spi_device->cs_pin = cs_pin;

result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL);

result = rt_spi_configure(spi_device, cfg);

if (result != RT_EOK)
{
LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/libraries/HAL_Drivers/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f103-atk-nano/board/ports/spi_flash_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f103-dofly-M3S/applications/nrf24l01_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f103-dofly-M3S/board/ports/drv_sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f103-dofly-M3S/board/ports/spi_flash_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f103-hw100k-ibox/board/ports/w5500_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f407-armfly-v5/board/ports/spi_flash_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f411-atk-nano/board/ports/spi_flash_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32f429-armfly-v6/board/ports/spi_flash_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32h750-artpi/board/port/drv_spi_ili9488.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32h750-artpi/board/port/spi_flash_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions bsp/stm32/stm32l431-BearPi/board/ports/lcd/drv_lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", -1);

cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS;
Expand Down
2 changes: 1 addition & 1 deletion bsp/stm32/stm32mp157a-st-ev1/board/ports/spi_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, -1);

cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS;
Expand Down
4 changes: 2 additions & 2 deletions components/drivers/include/drivers/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include <stdlib.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/pin.h>

#ifdef __cplusplus
extern "C"{
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};

Expand Down
1 change: 0 additions & 1 deletion components/drivers/spi/spi_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down