ESP32 + Arduino FREERTOS + TCA9548A + 8 pcs LIS2MDDL

AlanSbor
Posts: 2
Joined: Thu Jan 07, 2021 3:11 pm

ESP32 + Arduino FREERTOS + TCA9548A + 8 pcs LIS2MDDL

Postby AlanSbor » Tue Jun 06, 2023 8:45 pm

Good evening All!

I'm trying to get data from LIS2MDL sensors through the TCA9548A multiplexer under Arduino IDE with FREERTOS in relation to esp32.

I scan one sensor at a time in a separate program - everything is readable, the signal is excellent with an oscilloscope.

As soon as I start it through task and try to get the data from the sensors sequentially switching through the channels, nothing happens. The readings are read only on the zero bus of the multiplexer, the rest do not give out data.

If in a normal environment, in the loop loop, I read the LIS2mdl sensor, then it is read, but only any one sensor out of 8, but not sequentially.

Please tell me what am I doing wrong? Sample code below.

All sensor and multiplexer buses are pulled up with 2.4 kOhm resistors.

Code: Select all


#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif

#define I2C_Freq 400000
#define SDA_1 32
#define SCL_1 33

#define SDA_0 21
#define SCL_0 22

#define TCA9548_ADDRESS 0x71
//#define TCA9548_ADDRESS 0x72
//#define TCA9548_ADDRESS 0x73

// Номер канала мультиплексора (0-7)
#define TCA9548_CHANNEL 0



//------------------ INCLUDE -----------------

#include <Adafruit_LIS2MDL.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include "esp_timer.h"

//#include <stdio.h>
 
//-----------------------------------

const uint8_t tca_addresses[] = { 0x71, 0x72, 0x73 };

static uint32_t step;// = STEP_BTON; For  WiFi  и BLE

// Defining the SensorInfo structure and the sensorInfoArray array
struct SensorInfo {
    uint8_t multiplexerAddress; //
    uint8_t channel_num; // 
    float x;
    float y;
    float z;
};

SensorInfo sensorInfoArray[20] = {
    // First column
  {0x71, 0, 0, 0, 0}, {0x71, 1, 0, 0, 0}, {0x71, 2, 0, 0, 0}, {0x71, 3, 0, 0, 0}, {0x71, 4, 0, 0, 0},
  // Second column
  {0x71, 7, 0, 0, 0}, {0x71, 6, 0, 0, 0}, {0x71, 5, 0, 0, 0}, {0x72, 8, 0, 0, 0}, {0x72, 1, 0, 0, 0},
  // Third column
  {0x72, 6, 0, 0, 0}, {0x72, 5, 0, 0, 0}, {0x72, 4, 0, 0, 0}, {0x72, 3, 0, 0, 0}, {0x72, 2, 0, 0, 0},
  // Fourth column
  {0x73, 4, 0, 0, 0}, {0x73, 3, 0, 0, 0}, {0x73, 2, 0, 0, 0}, {0x72, 1, 0, 0, 0}, {0x73, 0, 0, 0, 0}

};


Adafruit_LIS2MDL lis2mdl = Adafruit_LIS2MDL();

SemaphoreHandle_t i2cMutex; // Mutex for I2C
SemaphoreHandle_t arrayMutex; // Mutex for array with sensor data

/* our Array for data 20 sensor X,Y,Z float

1 column | 2 column  |3 column | 4 column
         |           |         |
71-0     |  71-7     |  72-6   |  73-4
71-1     |  71-6     |  72-5   |  73-3
71-2     |  71-5     |  72-4   |  73-2
71-3     |  72-0     |  72-3   |  73-1
71-4     |  72-1     |  72-2   |  73-8

*/

//-----------TASKS------------------------------ 
// define tasks
// polling 3 multiplexers and 20 lis2mdl sensors

void Task_1_READ_71(void* pvParameters);

//------------------------------------------------

//-------------Functions ---------------------- 

void tcaSelect_71(uint8_t channel);
bool tca9548Available();

//------------------------------------------------


// the setup function runs once when you press reset or power the board
void setup() {

    // initialize serial communication at 115200 bits per second:
    Serial.begin(115200);

    Wire.begin(SDA_0, SCL_0, I2C_Freq);
    Wire.setClock(400000);

    tcaSelect_71(TCA9548_CHANNEL);
   

    if (!tca9548Available()) { Serial.println("TCA9548 not found!"); while (1); }

    // Initialise the sensor 
    if (!lis2mdl.begin()) {  // I2C mode
        Serial.println("Ooops, no LIS2MDL detected ... Check your wiring!");
        while (1) delay(10);
    }

     for (uint8_t channels = 0; channels < 8; channels++) {
          tcaSelect_71(channels);
          lis2mdl.enableAutoRange(true);
      }

 
i2cMutex = xSemaphoreCreateMutex(); // Create a mutex for I2C
  if (i2cMutex == NULL) {
      Serial.println("Failed to create I2C mutex");
      while (1);
  }

 arrayMutex = xSemaphoreCreateMutex(); // Create a mutex for the array with data from the sensor
  if (arrayMutex == NULL) {
      Serial.println("Failed to create array mutex");
      while (1);
  }

  

  // Now set up two tasks to run independently.
  xTaskCreatePinnedToCore(
      Task_1_READ_71
      , "Task_1_READ_71"   // A name just for humans
      , 2048  // This stack size can be checked & adjusted by reading the Stack Highwater
      , NULL
      , 2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
      , NULL
      , ARDUINO_RUNNING_CORE);
  
}

void loop()
{
    // Empty. Things are done in Tasks.
}


/*--------------------------------------------------*/
/*------------------- FUNCTIONS --------------------*/
/*--------------------------------------------------*/


// Function for channel selection on TCA9548 multiplexer uint8_t
void tcaSelect_71(uint8_t channel) {
    Wire.beginTransmission(TCA9548_ADDRESS);
    Wire.write(1 << channel);
    Wire.endTransmission();
}


bool tca9548Available() {
     Wire.beginTransmission(TCA9548_ADDRESS);
   // Wire.beginTransmission(uint8_t(0x71));
    byte status = Wire.endTransmission();
    return status == 0;
}


/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void Task_1_READ_71(void* pvParameters) {
    (void)pvParameters;

    sensors_event_t event71;

   //      READ sensors 71-0 ->71-7      Write to array

    while (1) {
        
        int64_t start_time = esp_timer_get_time();

        for (uint8_t i = 0; i < 8; i++) {
          xSemaphoreTake(i2cMutex, portMAX_DELAY); // Grab the I2C mutex
                tcaSelect_71(sensorInfoArray[i].channel_num); // Select a channel on the TCA9548A multiplexer
                                  
                xSemaphoreTake(arrayMutex, portMAX_DELAY);// Now grab the array mutex
                               
                lis2mdl.getEvent(&event71);
               
                // Convert received data to microtesla and write to array
                
                sensorInfoArray[i].x = event71.magnetic.x;
                sensorInfoArray[i].y = event71.magnetic.y;
                sensorInfoArray[i].z = event71.magnetic.z;

               xSemaphoreGive(arrayMutex); // Release the array mutex
                
                xSemaphoreGive(i2cMutex); // Release the I2C mutex

                Serial.println("========== AFTER =================");
                Serial.print(sensorInfoArray[i].multiplexerAddress, HEX);
                Serial.print(" ");
                Serial.print(sensorInfoArray[i].channel_num);
                Serial.print("  ");
                Serial.print(sensorInfoArray[i].x);
                Serial.print("  ");
                Serial.print(sensorInfoArray[i].y);
                Serial.print("  ");
                Serial.println(sensorInfoArray[i].z);
                Serial.println("  ");
                               
            vTaskDelay(500 / portTICK_PERIOD_MS); // Delay between polling sensors

 
        }

        int64_t end_time = esp_timer_get_time();

        int64_t elapsed_time = end_time - start_time;
        Serial.print("  ");
        Serial.print("Elapsed time: ");
        Serial.print(elapsed_time);
        Serial.println(" ns");

        vTaskDelay(10 / portTICK_PERIOD_MS); // 
    }
}


    
}

Who is online

Users browsing this forum: No registered users and 43 guests