I am trying to use one of ST MEMS standard C driver with a ESP32.
https://github.com/STMicroelectronics/S ... _C_drivers
The MEMS model I am trying to use is the IIS3DWB.
The sample code I have attached at the bottom is what I am trying to use. I get the following error when the ESP32 tries to talk to the sensor: spi_master: check_trans_valid(799): rxdata transfer > 32 bits without configured DMA
When I try to use: pvPortMallocCaps,
I get the following error: error: implicit declaration of function 'pvPortMallocCaps'; did you mean 'pvPortMalloc'?
Any feedback would be greatly appreciated as I am new to using the ESP-IDF.
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_system.h"
- #include "driver/spi_master.h"
- #include "driver/gpio.h"
- #include "iis3dwb_reg.h"
- // SPI interface definitions for ESP32
- #define SPI_BUS HSPI_HOST
- #define SPI_SCK_GPIO 19
- #define SPI_MOSI_GPIO 33
- #define SPI_MISO_GPIO 25
- #define SPI_CS_GPIO 32
- #define INT1_PIN 35
- #define INT2_PIN 34
- typedef union{
- int16_t i16bit[3];
- uint8_t u8bit[6];
- } axis3bit16_t;
- typedef union{
- int16_t i16bit;
- uint8_t u8bit[2];
- } axis1bit16_t;
- /* Private variables ---------------------------------------------------------*/
- static axis3bit16_t data_raw_acceleration;
- static axis1bit16_t data_raw_temperature;
- static float acceleration_mg[3];
- static float temperature_degC;
- static uint8_t whoamI, rst;
- static uint8_t tx_buffer[1000];
- spi_device_handle_t spi_handle;
- static void delay(uint32_t ms);
- static esp_err_t spi_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
- static esp_err_t spi_write(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
- void iis3dwb_read_data_polling(void)
- {
- printf("Start IIS3DWB\n");
- stmdev_ctx_t dev_ctx;
- /* Initialize mems driver interfce */
- dev_ctx.write_reg = spi_write;
- dev_ctx.read_reg = spi_read;
- delay(1000);
- /* Check device ID */
- printf("Check for IIS3DWB\n");
- iis3dwb_device_id_get(&dev_ctx, &whoamI);
- if (whoamI != IIS3DWB_ID)
- while(1);
- printf("IIS3DWB Found");
- /* Restore default configuration */
- iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);
- do {
- iis3dwb_reset_get(&dev_ctx, &rst);
- } while (rst);
- /* Enable Block Data Update */
- iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
- /* Set Output Data Rate */
- iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);
- /* Set full scale */
- iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);
- /* Configure filtering chain(No aux interface)
- * Accelerometer - LPF1 + LPF2 path
- */
- iis3dwb_xl_hp_path_on_out_set(&dev_ctx, IIS3DWB_LP_ODR_DIV_100);
- iis3dwb_xl_filter_lp2_set(&dev_ctx, PROPERTY_ENABLE);
- /* Read samples in polling mode (no int) */
- while(1)
- {
- uint8_t reg;
- /* Read output only if new xl value is available */
- iis3dwb_xl_flag_data_ready_get(&dev_ctx, ®);
- if (reg)
- {
- /* Read acceleration field data */
- memset(data_raw_acceleration.u8bit, 0x00, 3 * sizeof(int16_t));
- iis3dwb_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
- acceleration_mg[0] =
- iis3dwb_from_fs2g_to_mg(data_raw_acceleration.i16bit[0]);
- acceleration_mg[1] =
- iis3dwb_from_fs2g_to_mg(data_raw_acceleration.i16bit[1]);
- acceleration_mg[2] =
- iis3dwb_from_fs2g_to_mg(data_raw_acceleration.i16bit[2]);
- sprintf((char*)tx_buffer, "Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
- acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
- printf("%s", tx_buffer);
- }
- iis3dwb_temp_flag_data_ready_get(&dev_ctx, ®);
- if (reg)
- {
- /* Read temperature data */
- memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
- iis3dwb_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
- temperature_degC = iis3dwb_from_lsb_to_celsius(data_raw_temperature.i16bit);
- sprintf((char*)tx_buffer,
- "Temperature [degC]:%6.2f\r\n", temperature_degC);
- printf("%s", tx_buffer);
- }
- }
- }
- void app_main()
- {
- delay(1000);
- printf("Start Main\n");
- esp_err_t ret;
- //Configuration for the SPI bus
- spi_bus_config_t buscfg={
- .mosi_io_num=SPI_MOSI_GPIO,
- .miso_io_num=SPI_MISO_GPIO,
- .sclk_io_num=SPI_SCK_GPIO,
- .quadwp_io_num=-1,
- .quadhd_io_num=-1,
- .max_transfer_sz=32* 8 * 2
- };
- //Configuration for the SPI device on the other side of the bus
- spi_device_interface_config_t devcfg={
- .clock_speed_hz=1*1000*1000,
- .mode=1,
- .spics_io_num=SPI_CS_GPIO,
- .queue_size=12
- };
- //Initialize the SPI bus
- ret=spi_bus_initialize(SPI_BUS, &buscfg, 1);
- ESP_ERROR_CHECK(ret);
- //Attach the LCD to the SPI bus
- ret=spi_bus_add_device(SPI_BUS, &devcfg, &spi_handle);
- ESP_ERROR_CHECK(ret);
- iis3dwb_read_data_polling();
- }
- static esp_err_t spi_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
- {
- spi_transaction_t t;
- //uint8_t *buf = pvPortMallocCaps(64*8, MALLOC_CAP_DMA);
- /* Read command */
- reg |= 0x80;
- t.flags= SPI_TRANS_USE_RXDATA;
- t.length= len*8;
- t.addr = reg;
- t.tx_buffer= NULL;
- t.rx_buffer= bufp;
- esp_err_t err = spi_device_transmit(spi_handle, &t);
- //bufp = buf;
- return err;
- }
- static esp_err_t spi_write(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
- {
- spi_transaction_t t;
- //uint8_t *buf = pvPortMallocCaps(64*8, MALLOC_CAP_DMA);
- //buf = bufp;
- t.flags=SPI_TRANS_USE_TXDATA;
- t.length= len*8;
- t.addr = reg;
- t.tx_buffer= bufp;
- t.rx_buffer= NULL;
- esp_err_t err = spi_device_transmit(spi_handle, &t);
- return err;
- }
- static void delay(uint32_t ms)
- {
- // Return control or wait, for a period amount of milliseconds
- vTaskDelay(ms / portTICK_PERIOD_MS);
- }