Home > Blockchain >  ESP32 freeRTOS u8g2 oled i2c -> i2c clock choice is invalid
ESP32 freeRTOS u8g2 oled i2c -> i2c clock choice is invalid

Time:11-26

I am using the https://github.com/nkolban/esp32-snippets/tree/master/hardware/displays/U8G2 lib for an WROOM32 ESP32 Dev module.When I am scanning i2c I got this result:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

When I am calling my OLED in the task, the ESP ist rebooting over and over again with this error in the terminal:

␛[0;32mI (0) cpu_start: Starting scheduler on APP CPU.␛[0m
␛[0;32mI (0) u8g2_hal: sda_io_num 22␛[0m
␛[0;32mI (0) u8g2_hal: scl_io_num 19␛[0m
␛[0;32mI (10) u8g2_hal: clk_speed 1000000␛[0m
␛[0;32mI (10) u8g2_hal: i2c_param_config 1␛[0m
␛[0;31mE (20) i2c: i2c_param_config(647): i2c clock choice is invalid, please check flag and frequency␛[0m
␛[0;31mE (30) err: esp_err_t = 258␛[0m
assertion "0 && "i2c_param_config(I2C_MASTER_NUM, &conf)"" failed: file "src/u8g2_esp32_hal.c", line 129, function: u8g2_esp32_i2c_byte_cb

This is my full code:

#include <driver/i2c.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include <u8g2.h>
#include <u8g2_esp32_hal.h>

#define SDA_PIN 22
#define SCL_PIN 19

static char tag[] = "i2cscanner";

void task_i2cscanner(void *ignore) {

    //The scanner works!
    ESP_LOGD(tag, ">> i2cScanner");
    i2c_config_t conf;
    conf.mode = I2C_MODE_MASTER;
    conf.sda_io_num = SDA_PIN;
    conf.scl_io_num = SCL_PIN;
    conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
    conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
    conf.master.clk_speed = 100000;
    i2c_param_config(I2C_NUM_0, &conf);

    i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);

    int i;
    esp_err_t espRc;
    printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
    printf("00:         ");
    for (i=3; i< 0x78; i  ) {
        i2c_cmd_handle_t cmd = i2c_cmd_link_create();
        i2c_master_start(cmd);
        i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
        i2c_master_stop(cmd);

        espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
        if (i%16 == 0) {
            printf("\n%.2x:", i);
        }
        if (espRc == 0) {
            printf(" %.2x", i);
        } else {
            printf(" --");
        }
        //ESP_LOGD(tag, "i=%d, rc=%d (0x%x)", i, espRc, espRc);
        i2c_cmd_link_delete(cmd);
    }
    printf("\n");
    vTaskDelete(NULL);
}

void task_test_SSD1306i2c(void *pvParameter)
{
    //The display does not work!
    u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
    u8g2_esp32_hal.sda = SDA_PIN;
    u8g2_esp32_hal.scl = SCL_PIN;
    u8g2_esp32_hal_init(u8g2_esp32_hal);

    u8g2_t u8g2; // a structure which will contain all the data for one display
    u8g2_Setup_ssd1306_i2c_128x32_univision_f(
        &u8g2,
        U8G2_R0,
        //u8x8_byte_sw_i2c,
        u8g2_esp32_i2c_byte_cb,
        u8g2_esp32_gpio_and_delay_cb); 

    u8x8_SetI2CAddress(&u8g2.u8x8, 0x3c);
    u8g2_InitDisplay(&u8g2); 
    u8g2_SetPowerSave(&u8g2, 0); 

    u8g2_ClearBuffer(&u8g2);
    u8g2_DrawBox(&u8g2, 0, 26, 80, 6);
    u8g2_DrawFrame(&u8g2, 0, 26, 100, 6);
    u8g2_SetFont(&u8g2, u8g2_font_ncenB14_tr);
    u8g2_DrawStr(&u8g2, 2, 17, "Hi nkolban!");
    u8g2_SendBuffer(&u8g2);
    printf("All done!");
    vTaskDelete(NULL);
}

void app_main()
{
    ets_timer_init();
    //xTaskCreate(&task_i2cscanner, "task_i2cscanner", 4096, NULL, 10, NULL);
    xTaskCreate(&task_test_SSD1306i2c, "task_test_SSD1306i2c", 4096, NULL, 10, NULL);
}

Question: What am I doing wrong to use the display?

CodePudding user response:

The library is out of date relative to newer releases of ESP IDF. The I2C configuration structure has received a new member clk_flags which this library doesn't set. See the details in this thread.

You can either drop back to an older release of ESP IDF (no idea which, sorry) or fix the U8G2 library (seems easy enough, check the reference documentation).

CodePudding user response:

In your u8g2_esp32_hal.c file after line 127:

        conf.master.clk_speed = I2C_MASTER_FREQ_HZ;

try adding the line:

        conf.master.clk_flags = 0;

I believe your issue is that the clk_flags are not being set

  • Related