SPI通讯与普通GPIO功能复用

GuaGua
Posts: 2
Joined: Mon Dec 18, 2023 1:24 am

SPI通讯与普通GPIO功能复用

Postby GuaGua » Wed Jan 03, 2024 8:07 am

我的需求是ESP32-C3作为SPI主机与从机通讯,通讯完成后SPI管脚切换为普通GPIO用于其他功能,待下次需要与从机通讯时再切换为主机SPI。
现在的问题是从SPI切换为GPIO功能后无法再切换至SPI功能。
大概的功能代码为:
void SetGpio()
{
//CLK信号管脚配置为OD
gpio_config_t IO_Config;
IO_Config.pin_bit_mask = PIN_NUM_CLK_PIN_SEL;
IO_Config.mode = GPIO_MODE_DEF_OD;
IO_Config.pull_up_en = 0; /*上拉*/
IO_Config.pull_down_en = 0;
IO_Config.intr_type = GPIO_INTR_DISABLE; /*不使能中断*/
gpio_config(&IO_Config);
//MOSI信号管脚配置为OD
IO_Config.pin_bit_mask = PIN_NUM_MOSI_PIN_SEL;
IO_Config.mode = GPIO_MODE_DEF_OD;
IO_Config.pull_up_en = 0; /*上拉*/
IO_Config.pull_down_en = 0;
IO_Config.intr_type = GPIO_INTR_DISABLE; /*不使能中断*/
gpio_config(&IO_Config);
}
//SPI外设初始化
void vspi_init(void)
{
spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
}
void ResetSPI()
{
gpio_config_t IO_Config;
IO_Config.pin_bit_mask = PIN_NUM_CLK_PIN_SEL;
IO_Config.mode = GPIO_MODE_DEF_OUTPUT;
IO_Config.pull_up_en = 0; /*上拉*/
IO_Config.pull_down_en = 0;
IO_Config.intr_type = GPIO_INTR_DISABLE; /*不使能中断*/
gpio_config(&IO_Config);
IO_Config.pin_bit_mask = PIN_NUM_MOSI_PIN_SEL;
IO_Config.mode = GPIO_MODE_DEF_OUTPUT;
IO_Config.pull_up_en = 0; /*上拉*/
IO_Config.pull_down_en = 0;
IO_Config.intr_type = GPIO_INTR_DISABLE; /*不使能中断*/
gpio_config(&IO_Config);
spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
}
void main()
{
vspi_init();//初始化SPI外设
SPI_communication();//作为主机与从机通讯
SetGpio();//设置 为普通IO
Gpio_Control();//进行IO控制
ResetSPI();//重新配置为SPI外设
SPI_communication();//进行SPI通讯
}
如上操作无法再次开启SPI功能 log将反馈SPI bus already initialized
我也参试过重新配置SPI前运行
spi_bus_remove_device(&spi);
spi_bus_free(SPI2_HOST);
卸载SPI再重装,但是ESP32将出现异常直接重启

Who is online

Users browsing this forum: No registered users and 104 guests