- 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;
- }
IDF4.2 ESP32 NodeMcu开发板