Not going to happen. I’m an electronics engineer first and embedded coder second… my code is far from “beautiful” - but works
I’ll have a look here to see what’s going on… I’m not sure how “fully implemented” the MG24 Arduino library is.
There is something odd with the Arduino I2C on the ESP32-C6. It all gets initialised but the I2C interrupts just fail to fire
I ended up using basic functions from one of the Espressif examples and it worked fine.
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
/*
This Arduino Sketch configures the ESP32 C6 DEVKITC 1 as an I2C slave device
using the ESP-IDF framework. The I2C bus uses GPIO 22 as SDA and GPIO 23 as
SCL. The device responds to requests and receptions at the I2C address 0x74.
*/
#include "driver/i2c.h"
#include "esp_log.h"
#define I2C_ADDRESS 0x74
#define SDA_PIN 22
#define SCL_PIN 23
#define I2C_PORT I2C_NUM_0
static const char *TAG = "I2C_Slave";
void do_second_tick();
void receive_event(i2c_port_t i2c_num, uint8_t *data, size_t len)
{
ESP_LOGI(TAG, "Received data:");
for (size_t i = 0; i < len; i++)
{
ESP_LOGI(TAG, "%02x ", data[i]);
}
}
void request_event(i2c_port_t i2c_num)
{
uint8_t ack[] = "ACK";
i2c_slave_write_buffer(i2c_num, ack, sizeof(ack), 1000 / portTICK_PERIOD_MS);
}
// Check the millisecond tick and log a message every second
void do_second_tick()
{
static uint32_t last_tick = 0;
uint32_t current_tick = xTaskGetTickCount() * portTICK_PERIOD_MS;
if (current_tick - last_tick >= 1000)
{
ESP_LOGI(TAG, "One second has passed");
last_tick = current_tick;
}
}
void app_main()
{
i2c_config_t conf = {
.mode = I2C_MODE_SLAVE,
.sda_io_num = SDA_PIN,
.scl_io_num = SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.slave = {
.slave_addr = I2C_ADDRESS,
.maximum_speed = 100000}};
i2c_param_config(I2C_PORT, &conf);
i2c_driver_install(I2C_PORT, conf.mode, 128, 128, 0);
while (1)
{
uint8_t data[128];
int len = i2c_slave_read_buffer(I2C_PORT, data, sizeof(data), 1000 / portTICK_PERIOD_MS);
if (len > 0)
{
receive_event(I2C_PORT, data, len);
}
request_event(I2C_PORT);
vTaskDelay(100 / portTICK_PERIOD_MS);
do_second_tick();
}
}