ESP32 as Slave with Lib 2.0.2 as DS1307 Emulator

Troelli
Posts: 1
Joined: Tue Apr 26, 2022 9:43 am

ESP32 as Slave with Lib 2.0.2 as DS1307 Emulator

Postby Troelli » Tue May 03, 2022 1:16 pm

Hello
I have been playing a bit with the new I2C slave library.
I would like to simulate a DS1307 (clock) on an ESP32. https://www.sparkfun.com/datasheets/Com ... DS1307.pdf
For this I have an array which should simulate the registers of the DS1307.
To be able to react on the I2c bus, I use the slave code used in the example.
There is an "onReceive" and an "onRequest" method.

When the onRequest method is called, how do I know what the master is requesting?

When do I know whether the master wants to write or read?

The onReceive method at least tells how many bytes are being transferred.

If the master wants to set a register, I can tell because the length of the transmitted bytes is greater than one.
The first byte is the register and the following bytes are the values to be entered into the registers.
But how does it work the other way round? I mean when the master requests a value from a certain register?
Has anyone already got a slave running with the new library? https://docs.espressif.com/projects/ard ... i/i2c.html

And does anyone know what the CONFIG_IDF_TARGET_ESP32 is good for? What does it mean ?

Code: Select all

#if CONFIG_IDF_TARGET_ESP32
  char message[64];
  snprintf(message, 64, "%u Packets.", i++);
  Wire.slaveWrite((uint8_t *)message, strlen(message));
#endif

I have made an attempt here to talk to the DS1307. But I'm a complete beginner and of course I failed because I'm only very weak in the C programming language. I'm not deleting my pitiful attempts so you can see what I've already tried.

Code: Select all

#include "Wire.h"


int I2C_SDA_Clock = 23;
int I2C_SCL_Clock = 19;
uint8_t I2C_DEV_ADDR = 0x68;
int I2C_SDA_CPU = 21;
int I2C_SCL_CPU = 22;
uint32_t Clock_frequency = 100000;
uint32_t i = 0;
byte counter = 0;

//57;40;10;3;3;5;22;93;   10:40:58 3/5/22 Day of week: Tuesday
//57,40,10,3,3,5,22,93

//57 = 0011 1001 = 3 <> 9   Seconds
//40 = 0010 1000 = 2 <> 8   Minutes
//10 = 0000 1010 = 0 <> 10  Hours
// 3 = 0000 0011 = 3        Day
// 3 = 0000 0011 = 3        Date
// 5 = 0000 0101 = 5        Month
//22 = 0001 0110 = 1 <> 6

//Seconds = 0101 1000 = Dez 88 = Hex 58
//Minutes = 0100 0000 = Dez 64 = Hex 40
//Hours   = 0001 0000 = Dez 16 = Hex 10
//DAY     = 0000 0011 = Dez 3 = Hex 3
//Date    = 0011 0011 = Dez 51 = Hex 33
//Month   = 0000 0101 = Dez 5 = Hex 5
//Year    = 0010 0010 = Dez 34 = Hex 22
//Controll= 0000 1010 = Dez 16 = Hex 10


//byte DS1307_Data[64] = {57,40,10,3,3,5,22,93,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,};
byte DS1307_Data[64] = {88,64,16,3,51,5,34,16,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,};
//bool TwoWire::begin(uint8_t addr, int sdaPin, int sclPin, uint32_t frequency)


TwoWire I2C_Clock = TwoWire(0);
TwoWire I2C_CPU = TwoWire(1);


//...................................................................................

void onRequest(){

//
  
  //I2C_CPU.print(counter);
  //I2C_CPU.print(DS1307_Data[counter]);
//  byte Temp = DS1307_Data[counter];
//  I2C_CPU.print(Temp);

//  Serial.print("onRequest = Send ");
//  Serial.print(counter);
//  Serial.print(" ");
//  Serial.println(DS1307_Data[counter]);
  //Serial.println("onRequest");
}

//...................................................................................

void onReceive(int len){
 //Serial.printf("onReceive[%d]: ", len);
 bool verbose_test = true;
// function that executes whenever data is received from master
//{10,19,15,6,29,4,22,93}
// A,13,F,6,1D,4,16,5D HEX
byte Register = 0;
      
      //while(I2C_CPU.available()){
        if (len > 1){
          Register = I2C_CPU.read();
          //Serial.println((Register), HEX);
          //Register = I2C_CPU.read();
          //Serial.println((Register), HEX);          
          
            for (int i=0; i < len - 1; i++){
              DS1307_Data[Register] = I2C_CPU.read();
                if (verbose_test == true){
                Serial.print(Register);
                Serial.print("<>");
                Serial.println(DS1307_Data[Register], HEX);
                //Serial.println("  Write");
                }            
            }                              
          }




        if (len == 1){
          Register = I2C_CPU.read();
          counter = Register;
          
          Serial.print("Len1= ");
          //Serial.println((Register), HEX);
          if (verbose_test == true){
          Serial.print(Register, HEX);
          Serial.print(" ");
          Serial.println(DS1307_Data[Register], HEX);
          
        }  
//        if (Register == 0) {
//          //I2C_CPU.print(DS1307_Data[0]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[0], HEX);
//          }
//        }
//        else if (Register == 1) {
//          //I2C_CPU.print(DS1307_Data[1]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[1], HEX);
//          }
//        }
//        else if (Register == 2) {
//          //I2C_CPU.print(DS1307_Data[2]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[2], HEX);
//          }
//        }
//        else if (Register == 3) {
//          //I2C_CPU.print(DS1307_Data[3]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[3], HEX);
//          }
//        }
//        else if (Register == 4) {
//          //I2C_CPU.print(DS1307_Data[4]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[4], HEX);
//          }
//        }
//        else if (Register == 5) {
//          //I2C_CPU.print(DS1307_Data[5]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[5], HEX);
//          }
//        }
//        else if (Register == 6) {
//          //I2C_CPU.print(DS1307_Data[6]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[6], HEX);
//          }
//        }
//        else if (Register == 7) {
//          //I2C_CPU.print(DS1307_Data[7]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[7], HEX);
//          }
//        }
//        else if (Register == 8) {
//          //I2C_CPU.print(DS1307_Data[8]);
//          counter = Register;
//          if (verbose_test == true){
//          Serial.print(Register, HEX);
//          Serial.print(" ");
//          Serial.println(DS1307_Data[8], HEX);
//          }
//        }                        
        } 

}




void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);  
  I2C_Clock.setPins(I2C_SDA_Clock, I2C_SCL_Clock);
  I2C_Clock.setClock (Clock_frequency);

  I2C_CPU.setPins(I2C_SDA_CPU, I2C_SCL_CPU);
  //Wire1.setClock (Clock_frequency);
  
  I2C_CPU.begin(I2C_DEV_ADDR);
  I2C_Clock.begin();
    

  I2C_CPU.onReceive(onReceive);
  I2C_CPU.onRequest(onRequest);

//{67,3,20,5,33,4,34}

//#if CONFIG_IDF_TARGET_ESP32
  //char message[64] = {88,64,16,3,51,5,34,16,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,};
  char message[64];
  snprintf(message, 64, "%u Packets.", i++);
  I2C_CPU.slaveWrite((uint8_t *)message, strlen(message));
//#endif

}

void loop() {
  // put your main code here, to run repeatedly:
  delay(100);

}



  

Who is online

Users browsing this forum: No registered users and 48 guests