Hi,
It has been a long since I posted this thread but I was also working on this.
So now during porting the API I have had an issue. The code compiles as needed but seems that it is not working from ESP32 I2C to notecard I2C. Following is the code for that.
bool noteI2CReset(uint16_t dev_addr);
const char * noteI2CTransmit(uint16_t dev_addr, uint8_t *pBuffer, uint16_t size);
const char * noteI2CReceive(uint16_t dev_addr, uint8_t *pBuffer, uint16_t size, uint32_t *available);
void delay(uint32_t ms);
uint32_t ticks(void);
static esp_err_t i2c_master_init(void){
int i2c_port = CONFIG_I2C_MASTER_PORT;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = CONFIG_I2C_MASTER_SDA_IO,
.scl_io_num = CONFIG_I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_param_config(i2c_port, &conf);
return i2c_driver_install(i2c_port, conf.mode, 0, 0, 0);
}
const char * noteI2CTransmit(uint16_t dev_addr, uint8_t *pBuffer, uint16_t size){
esp_err_t err;
const char * errstr = NULL;
uint8_t send_buffer[256];
send_buffer[0] = (size & NOTE_I2C_BUFFER_SIZE);
memcpy(&send_buffer[1], pBuffer, send_buffer[0]);
err = i2c_master_write_to_device(CONFIG_I2C_MASTER_PORT, dev_addr, send_buffer, sizeof(send_buffer[0] + send_buffer[1]), 1000 / portTICK_PERIOD_MS);
switch(err) {
case ESP_OK:
errstr = NULL;
break;
case ESP_FAIL:
errstr = "Error in i2c Transmission";
break;
}
return errstr;
}
const char * noteI2CReceive(uint16_t dev_addr, uint8_t *pBuffer, uint16_t size, uint32_t *available){
esp_err_t error;
const char * errstr;
uint8_t query_request[2];
query_request[0] = 0x00;
query_request[1] = (size & NOTE_I2C_BUFFER_SIZE);
uint8_t goodbyte = 0;
uint8_t availbyte = 0;
error = i2c_master_write_to_device(CONFIG_I2C_MASTER_PORT, dev_addr, query_request, sizeof(query_request), 1000 / portTICK_PERIOD_MS);
switch(error){
case ESP_OK:
errstr = NULL;
break;
case ESP_FAIL:
errstr = "Failed to Transmit";
break;
}
if (!errstr){
uint8_t buffer[NOTE_I2C_BUFFER_SIZE];
int readlen = (size + 2);
error = i2c_master_read_from_device(CONFIG_I2C_MASTER_PORT, dev_addr, buffer, readlen, 1000 / portTICK_PERIOD_MS);
if(error == ESP_OK){
availbyte = buffer[0];
goodbyte = buffer[1];
if(goodbyte != size) {
ESP_LOGE(TAG, "i2c: incorrect amount of data");
}
else{
memcpy(pBuffer, &buffer[2], buffer[1]);
}
}
}
if (errstr != NULL){
return errstr;
}
*available = availbyte;
return NULL;
}
bool noteI2CReset(uint16_t dev_addr)
{
const char * err;
//uint8_t buffer[NOTE_I2C_BUFFER_SIZE];
uint32_t available = 0;
// Empty the Notecard send buffer
err = noteI2CReceive(dev_addr, NULL, 0, &available);
return err;
}
void delay(uint32_t ms)
{
vTaskDelay(pdMS_TO_TICKS(ms));
}
uint32_t ticks(void)
{
return (esp_timer_get_time() / 1000);
}
Then inside the main function I do something like this
ESP_ERROR_CHECK(i2c_master_init());
NoteSetFn(malloc, free, delay, ticks);
// Set Notecard I2C Interface
NoteSetFnI2C(NOTE_I2C_ADDR_DEFAULT, NOTE_I2C_BUFFER_SIZE, noteI2CReset, noteI2CTransmit, noteI2CReceive);
J *req = NoteNewRequest("hub.set");
JAddStringToObject(req, "product", BLUES_PRODUCT_UID);
JAddStringToObject(req, "mode", "continuous");
NoteRequest(req);
vTaskDelay(10 * 1000 / portTICK_PERIOD_MS);