STMEMS Standard C Driver

theBASTI0N
Posts: 4
Joined: Fri Jul 03, 2020 8:40 am

STMEMS Standard C Driver

Postby theBASTI0N » Fri Jul 03, 2020 10:01 am

Hey,

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.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_system.h"
  7. #include "driver/spi_master.h"
  8. #include "driver/gpio.h"
  9. #include "iis3dwb_reg.h"
  10.  
  11.  
  12. // SPI interface definitions for ESP32
  13. #define SPI_BUS       HSPI_HOST
  14. #define SPI_SCK_GPIO  19
  15. #define SPI_MOSI_GPIO 33
  16. #define SPI_MISO_GPIO 25
  17. #define SPI_CS_GPIO   32
  18.  
  19. #define INT1_PIN      35
  20. #define INT2_PIN      34
  21.  
  22. typedef union{
  23.   int16_t i16bit[3];
  24.   uint8_t u8bit[6];
  25. } axis3bit16_t;
  26.  
  27. typedef union{
  28.   int16_t i16bit;
  29.   uint8_t u8bit[2];
  30. } axis1bit16_t;
  31.  
  32. /* Private variables ---------------------------------------------------------*/
  33. static axis3bit16_t data_raw_acceleration;
  34. static axis1bit16_t data_raw_temperature;
  35. static float acceleration_mg[3];
  36. static float temperature_degC;
  37. static uint8_t whoamI, rst;
  38. static uint8_t tx_buffer[1000];
  39. spi_device_handle_t spi_handle;
  40.  
  41. static void delay(uint32_t ms);
  42. static esp_err_t spi_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
  43. static esp_err_t spi_write(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);
  44.  
  45. void iis3dwb_read_data_polling(void)
  46. {
  47.     printf("Start IIS3DWB\n");
  48.     stmdev_ctx_t dev_ctx;
  49.  
  50.     /* Initialize mems driver interfce */
  51.     dev_ctx.write_reg = spi_write;
  52.     dev_ctx.read_reg = spi_read;
  53.  
  54.     delay(1000);
  55.  
  56.     /* Check device ID */
  57.     printf("Check for IIS3DWB\n");
  58.     iis3dwb_device_id_get(&dev_ctx, &whoamI);
  59.     if (whoamI != IIS3DWB_ID)
  60.         while(1);
  61.    
  62.     printf("IIS3DWB Found");
  63.  
  64.     /* Restore default configuration */
  65.     iis3dwb_reset_set(&dev_ctx, PROPERTY_ENABLE);
  66.     do {
  67.         iis3dwb_reset_get(&dev_ctx, &rst);
  68.     } while (rst);
  69.  
  70.     /* Enable Block Data Update */
  71.     iis3dwb_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  72.  
  73.     /* Set Output Data Rate */
  74.     iis3dwb_xl_data_rate_set(&dev_ctx, IIS3DWB_XL_ODR_26k7Hz);
  75.  
  76.     /* Set full scale */
  77.     iis3dwb_xl_full_scale_set(&dev_ctx, IIS3DWB_2g);
  78.  
  79.     /* Configure filtering chain(No aux interface)
  80.     * Accelerometer - LPF1 + LPF2 path
  81.     */
  82.     iis3dwb_xl_hp_path_on_out_set(&dev_ctx, IIS3DWB_LP_ODR_DIV_100);
  83.     iis3dwb_xl_filter_lp2_set(&dev_ctx, PROPERTY_ENABLE);
  84.  
  85.     /* Read samples in polling mode (no int) */
  86.     while(1)
  87.     {
  88.         uint8_t reg;
  89.  
  90.         /* Read output only if new xl value is available */
  91.         iis3dwb_xl_flag_data_ready_get(&dev_ctx, &reg);
  92.         if (reg)
  93.         {
  94.         /* Read acceleration field data */
  95.         memset(data_raw_acceleration.u8bit, 0x00, 3 * sizeof(int16_t));
  96.         iis3dwb_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
  97.         acceleration_mg[0] =
  98.             iis3dwb_from_fs2g_to_mg(data_raw_acceleration.i16bit[0]);
  99.         acceleration_mg[1] =
  100.             iis3dwb_from_fs2g_to_mg(data_raw_acceleration.i16bit[1]);
  101.         acceleration_mg[2] =
  102.             iis3dwb_from_fs2g_to_mg(data_raw_acceleration.i16bit[2]);
  103.  
  104.         sprintf((char*)tx_buffer, "Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
  105.                 acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
  106.         printf("%s", tx_buffer);
  107.         }
  108.  
  109.         iis3dwb_temp_flag_data_ready_get(&dev_ctx, &reg);
  110.         if (reg)
  111.         {
  112.         /* Read temperature data */
  113.         memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
  114.         iis3dwb_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
  115.         temperature_degC = iis3dwb_from_lsb_to_celsius(data_raw_temperature.i16bit);
  116.  
  117.         sprintf((char*)tx_buffer,
  118.                 "Temperature [degC]:%6.2f\r\n", temperature_degC);
  119.         printf("%s", tx_buffer);
  120.         }
  121.     }
  122. }
  123. void app_main()
  124. {
  125.     delay(1000);
  126.     printf("Start Main\n");
  127.    
  128.     esp_err_t ret;
  129.    
  130.  
  131.     //Configuration for the SPI bus
  132.     spi_bus_config_t buscfg={
  133.         .mosi_io_num=SPI_MOSI_GPIO,
  134.         .miso_io_num=SPI_MISO_GPIO,
  135.         .sclk_io_num=SPI_SCK_GPIO,
  136.         .quadwp_io_num=-1,
  137.         .quadhd_io_num=-1,
  138.         .max_transfer_sz=32* 8 * 2
  139.     };
  140.  
  141.     //Configuration for the SPI device on the other side of the bus
  142.     spi_device_interface_config_t devcfg={
  143.         .clock_speed_hz=1*1000*1000,
  144.         .mode=1,
  145.         .spics_io_num=SPI_CS_GPIO,
  146.         .queue_size=12
  147.     };
  148.  
  149.     //Initialize the SPI bus
  150.     ret=spi_bus_initialize(SPI_BUS, &buscfg, 1);
  151.     ESP_ERROR_CHECK(ret);
  152.     //Attach the LCD to the SPI bus
  153.     ret=spi_bus_add_device(SPI_BUS, &devcfg, &spi_handle);
  154.     ESP_ERROR_CHECK(ret);
  155.  
  156.     iis3dwb_read_data_polling();
  157. }
  158.  
  159. static esp_err_t spi_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  160. {
  161.     spi_transaction_t t;
  162.     //uint8_t *buf = pvPortMallocCaps(64*8, MALLOC_CAP_DMA);
  163.    
  164.     /* Read command */
  165.     reg |= 0x80;
  166.     t.flags= SPI_TRANS_USE_RXDATA;
  167.     t.length= len*8;
  168.     t.addr = reg;
  169.     t.tx_buffer= NULL;
  170.     t.rx_buffer= bufp;
  171.     esp_err_t err = spi_device_transmit(spi_handle, &t);
  172.     //bufp = buf;
  173.     return err;
  174. }
  175.  
  176. static esp_err_t spi_write(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
  177. {
  178.    
  179.     spi_transaction_t t;
  180.     //uint8_t *buf = pvPortMallocCaps(64*8, MALLOC_CAP_DMA);
  181.     //buf = bufp;
  182.     t.flags=SPI_TRANS_USE_TXDATA;
  183.     t.length= len*8;
  184.     t.addr = reg;
  185.     t.tx_buffer= bufp;
  186.     t.rx_buffer= NULL;
  187.     esp_err_t err = spi_device_transmit(spi_handle, &t);
  188.     return err;
  189. }
  190.  
  191. static void delay(uint32_t ms)
  192. {
  193.     // Return control or wait, for a period amount of milliseconds
  194.     vTaskDelay(ms / portTICK_PERIOD_MS);
  195. }

ESP_Sprite
Posts: 9020
Joined: Thu Nov 26, 2015 4:08 am

Re: STMEMS Standard C Driver

Postby ESP_Sprite » Fri Jul 03, 2020 12:05 pm

I think you want to use heap_caps_malloc instead nowadays.

theBASTI0N
Posts: 4
Joined: Fri Jul 03, 2020 8:40 am

Re: STMEMS Standard C Driver

Postby theBASTI0N » Sat Jul 11, 2020 2:25 am

Thanks for the feedback ESP_Sprite. I have adjusted the code to:

Code: Select all

static esp_err_t spi_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
    spi_transaction_t t;
    uint8_t *buf = heap_caps_malloc(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= &buf;
    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 = heap_caps_malloc(64*8, MALLOC_CAP_DMA);
    buf = bufp;
    t.flags=SPI_TRANS_USE_TXDATA;
    t.length= len*8;
    t.addr = reg;
    t.tx_buffer= buf;
    t.rx_buffer= NULL;
    esp_err_t err = spi_device_transmit(spi_handle, &t);
   
But am still getitng this error:
E (4309) spi_master: check_trans_valid(667): rxdata transfer > 32 bits without configured DMA

ESP_Sprite
Posts: 9020
Joined: Thu Nov 26, 2015 4:08 am

Re: STMEMS Standard C Driver

Postby ESP_Sprite » Sat Jul 11, 2020 1:06 pm

Are you sure the last parameter for spi_bus_initialize is not zero?

theBASTI0N
Posts: 4
Joined: Fri Jul 03, 2020 8:40 am

Re: STMEMS Standard C Driver

Postby theBASTI0N » Sun Jul 12, 2020 3:12 am

If I set it to 0, I still get the same error.

ESP_Sprite
Posts: 9020
Joined: Thu Nov 26, 2015 4:08 am

Re: STMEMS Standard C Driver

Postby ESP_Sprite » Sun Jul 12, 2020 8:06 am

Ah, gotcha, You have 't.flags=SPI_TRANS_USE_TXDATA;'. In that case, you should put (up to 32 bits of) data in the tx_data member of the transaction. You're not doing that, you give the address of a buffer, which is perfectly valid, but you should not set that flag then.

theBASTI0N
Posts: 4
Joined: Fri Jul 03, 2020 8:40 am

Re: STMEMS Standard C Driver

Postby theBASTI0N » Mon Jul 13, 2020 9:10 am

I tired the below but still get:

spi_master: check_trans_valid(667): rxdata transfer > 32 bits without configured DMA

Code: Select all

static esp_err_t spi_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
{
    spi_transaction_t t;
    uint8_t *buf = heap_caps_malloc(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= &buf;
    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;
    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;
}

ESP_Sprite
Posts: 9020
Joined: Thu Nov 26, 2015 4:08 am

Re: STMEMS Standard C Driver

Postby ESP_Sprite » Mon Jul 13, 2020 9:55 am

Same exact story goes for SPI_TRANS_USE_RXDATA, unsurprisingly.

Who is online

Users browsing this forum: No registered users and 184 guests