Page 1 of 1
SPI 添加设备返回 ESP_ERR_NO_MEM
Posted: Fri Apr 23, 2021 11:48 am
by fantasy
int luat_spi_setup(luat_spi_t *spi)
{
// SPI2_HOST = 1
// SPI3_HOST = 2
if (spi->id == 1)
{
spi_bus_config_t spi_config = {
.miso_io_num = 12,
.mosi_io_num = 13,
.sclk_io_num = 14,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
// .flags = SPICOMMON_BUSFLAG_MASTER
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &spi_config, 0));
}
else if (spi->id == 2)
{
spi_bus_config_t spi_config = {
.miso_io_num = 19,
.mosi_io_num = 23,
.sclk_io_num = 18,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
// .flags = SPICOMMON_BUSFLAG_MASTER
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &spi_config, 0));
}
else
{
return -1;
}
spi_device_interface_config_t devcfg;
if (spi->CPHA == 0)
{
if (spi->CPOL == 0)
{
devcfg.mode = 0;
}
else if (spi->CPOL == 1)
{
devcfg.mode = 1;
}
}
if (spi->CPHA)
{
if (spi->CPOL == 0)
{
devcfg.mode = 2;
}
else if (spi->CPOL == 1)
{
devcfg.mode = 3;
}
}
if (spi->bit_dict == 2)
{
devcfg.flags = SPI_DEVICE_BIT_LSBFIRST;
}
devcfg.cs_ena_pretrans = 1;
devcfg.clock_speed_hz = spi->bandrate;
devcfg.spics_io_num = spi->id==1?15:5;
//获取芯片内部堆大小
printf(" esp_get_free_heap_size : %d \n", esp_get_free_heap_size());
//获取芯片内部堆大小
printf(" esp_get_free_heap_size : %d \n", esp_get_free_heap_size());
//获取最小可用堆
printf(" esp_get_free_internal_heap_size : %d \n", esp_get_free_internal_heap_size());
ESP_ERROR_CHECK(spi_bus_add_device(spi->id, &devcfg, &spi_h));
return 0;
}
- 监视器报错输出
- %[7[MFRO7ZKRH62XY[}6RQU.png (30.49 KiB) Viewed 5986 times
堆内存还有很多,尝试将栈从8k加到64k依然不能解决该问题
IDF4.2 ESP32 NodeMcu开发板
Re: SPI 添加设备返回 ESP_ERR_NO_MEM
Posted: Fri Apr 23, 2021 12:19 pm
by ESP_Gargamel
这个你追下 spi_bus_add_device 代码,基本能发现问题。在 spi_bus_add_device 里找可能会返回 ESP_ERR_NO_MEM 的地方,其中 dev_config->queue_size 过大会导致这一错误。而 spi_device_interface_config_t devcfg; 是位于栈上,未初始话的话,里面的值是不确定的,这就导致 dev_config->queue_size 可能是一个巨大的值,导致内存分配失败。你可以在进入到 spi_bus_add_device 之前打印出来看下,是否是这样。最后,你定义这个变量的时候,需要对其进行初始化,可以是这样 spi_device_interface_config_t devcfg = {};。希望是这个问题导致。
Re: SPI 添加设备返回 ESP_ERR_NO_MEM
Posted: Fri Apr 23, 2021 12:46 pm
by fantasy
ESP_Gargamel wrote: ↑Fri Apr 23, 2021 12:19 pm
这个你追下 spi_bus_add_device 代码,基本能发现问题。在 spi_bus_add_device 里找可能会返回 ESP_ERR_NO_MEM 的地方,其中 dev_config->queue_size 过大会导致这一错误。而 spi_device_interface_config_t devcfg; 是位于栈上,未初始话的话,里面的值是不确定的,这就导致 dev_config->queue_size 可能是一个巨大的值,导致内存分配失败。你可以在进入到 spi_bus_add_device 之前打印出来看下,是否是这样。最后,你定义这个变量的时候,需要对其进行初始化,可以是这样 spi_device_interface_config_t devcfg = {};。希望是这个问题导致。
感谢大佬,问题解决了,不会在爆内存了
Re: SPI 添加设备返回 ESP_ERR_NO_MEM
Posted: Fri Apr 23, 2021 2:14 pm
by fantasy
SPI初始化成功了,但是在收发数据的时候遇到了未知异常,之前是spi_master.c下esp_err_t check_trans_valid中的//check working mode下,返回的错误参数,“incompatible iface params”,请问下这个该怎么解决,是需要配置特定模式才能使用吗
//初始化配置SPI各项参数,并打开SPI,向SPI总线添加设备
int luat_spi_setup(luat_spi_t *spi)
{
// SPI2_HOST = 1
// SPI3_HOST = 2
if (spi->id == 1)
{
spi_bus_config_t spi_config = {
.miso_io_num = 12,
.mosi_io_num = 13,
.sclk_io_num = 14,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
.flags = SPICOMMON_BUSFLAG_MASTER
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &spi_config, 0));
}
else if (spi->id == 2)
{
spi_bus_config_t spi_config = {
.miso_io_num = 19,
.mosi_io_num = 23,
.sclk_io_num = 18,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
.flags = SPICOMMON_BUSFLAG_MASTER
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &spi_config, 0));
}
else
{
return -1;
}
spi_device_interface_config_t devcfg;
if (spi->CPHA == 0)
{
if (spi->CPOL == 0)
{
devcfg.mode = 0;
}
else if (spi->CPOL == 1)
{
devcfg.mode = 1;
}
}
if (spi->CPHA == 1)
{
if (spi->CPOL == 0)
{
devcfg.mode = 2;
}
else if (spi->CPOL == 1)
{
devcfg.mode = 3;
}
}
if (spi->bit_dict == 2)
{
devcfg.flags = SPI_DEVICE_BIT_LSBFIRST;
}
devcfg.cs_ena_pretrans = 1;
devcfg.clock_speed_hz = spi->bandrate;
devcfg.spics_io_num = spi->id==1?15:5;
devcfg.queue_size=7;
ESP_ERROR_CHECK(spi_bus_add_device(spi->id, &devcfg, &spi_h));
return 0;
}
//关闭SPI,成功返回0
int luat_spi_close(int spi_id)
{
if (spi_id == 1 || spi_id == 2)
{
ESP_ERROR_CHECK(spi_bus_remove_device(spi_h));
ESP_ERROR_CHECK(spi_bus_free(spi_id));
return 0;
}
return -1;
}
//收发SPI数据,返回接收字节数
int luat_spi_transfer(int spi_id, const char *send_buf, char *recv_buf, size_t length)
{
if (spi_id == 1 || spi_id == 2)
{
spi_transaction_t send_spi = {
.length = length*2,
.tx_buffer = send_buf,
.rx_buffer = recv_buf,
.rxlength = length,
};
ESP_ERROR_CHECK(spi_device_transmit(spi_h, &send_spi));
return 0;
}
return -1;
}
- 日志输出
- D5IB~2[13`0)Q}G)@7}@UX7.png (49.3 KiB) Viewed 5961 times
Re: SPI 添加设备返回 ESP_ERR_NO_MEM
Posted: Sat Apr 24, 2021 1:57 pm
by ESP_Gargamel
再说一遍 spi_device_interface_config_t devcfg = {};,否则这个结构体下变量值位置,出什么问题都未知。
Re: SPI 添加设备返回 ESP_ERR_NO_MEM
Posted: Tue Apr 27, 2021 4:36 am
by fantasy
谢谢,这样写之后问题解决了