nopnop2002 / esp-idf-ssd1306

SSD1306/SH1106 Driver for esp-idf
MIT License
266 stars 74 forks source link

i2c.master: I2C transaction unexpected nack detected with esp-idf 5.3 #27

Closed blfuentes closed 2 months ago

blfuentes commented 2 months ago

I'm trying to follow the sample just to show some text in a ssd1306 128x32 but I'm getting this error after flashing: Board: ESP32-S3 framework: espressif 5.3

The code is just the textdemo:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"

#include "ssd1306.h"
#include "font8x8_basic.h"

/*
 You have to set this config value with menuconfig
 CONFIG_INTERFACE

 for i2c
 CONFIG_MODEL
 CONFIG_SDA_GPIO
 CONFIG_SCL_GPIO
 CONFIG_RESET_GPIO

 for SPI
 CONFIG_CS_GPIO
 CONFIG_DC_GPIO
 CONFIG_RESET_GPIO
*/

#define tag "SSD1306"

void app_main(void)
{
    SSD1306_t dev;
    int center, top, bottom;
    char lineChar[20];

#if CONFIG_I2C_INTERFACE
    ESP_LOGI(tag, "INTERFACE is i2c");
    ESP_LOGI(tag, "CONFIG_SDA_GPIO=%d",CONFIG_SDA_GPIO);
    ESP_LOGI(tag, "CONFIG_SCL_GPIO=%d",CONFIG_SCL_GPIO);
    ESP_LOGI(tag, "CONFIG_RESET_GPIO=%d",CONFIG_RESET_GPIO);
    i2c_master_init(&dev, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_I2C_INTERFACE

#if CONFIG_SPI_INTERFACE
    ESP_LOGI(tag, "INTERFACE is SPI");
    ESP_LOGI(tag, "CONFIG_MOSI_GPIO=%d",CONFIG_MOSI_GPIO);
    ESP_LOGI(tag, "CONFIG_SCLK_GPIO=%d",CONFIG_SCLK_GPIO);
    ESP_LOGI(tag, "CONFIG_CS_GPIO=%d",CONFIG_CS_GPIO);
    ESP_LOGI(tag, "CONFIG_DC_GPIO=%d",CONFIG_DC_GPIO);
    ESP_LOGI(tag, "CONFIG_RESET_GPIO=%d",CONFIG_RESET_GPIO);
    spi_master_init(&dev, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_SPI_INTERFACE

#if CONFIG_FLIP
    dev._flip = true;
    ESP_LOGW(tag, "Flip upside down");
#endif

#if CONFIG_SSD1306_128x64
    ESP_LOGI(tag, "Panel is 128x64");
    ssd1306_init(&dev, 128, 64);
#endif // CONFIG_SSD1306_128x64
#if CONFIG_SSD1306_128x32
    ESP_LOGI(tag, "Panel is 128x32");
    ssd1306_init(&dev, 128, 32);
#endif // CONFIG_SSD1306_128x32

    ssd1306_clear_screen(&dev, false);
    ssd1306_contrast(&dev, 0xff);
    ssd1306_display_text_x3(&dev, 0, "Hello", 5, false);
    vTaskDelay(3000 / portTICK_PERIOD_MS);

#if CONFIG_SSD1306_128x64
    top = 2;
    center = 3;
    bottom = 8;
    ssd1306_display_text(&dev, 0, "SSD1306 128x64", 14, false);
    ssd1306_display_text(&dev, 1, "ABCDEFGHIJKLMNOP", 16, false);
    ssd1306_display_text(&dev, 2, "abcdefghijklmnop",16, false);
    ssd1306_display_text(&dev, 3, "Hello World!!", 13, false);
    //ssd1306_clear_line(&dev, 4, true);
    //ssd1306_clear_line(&dev, 5, true);
    //ssd1306_clear_line(&dev, 6, true);
    //ssd1306_clear_line(&dev, 7, true);
    ssd1306_display_text(&dev, 4, "SSD1306 128x64", 14, true);
    ssd1306_display_text(&dev, 5, "ABCDEFGHIJKLMNOP", 16, true);
    ssd1306_display_text(&dev, 6, "abcdefghijklmnop",16, true);
    ssd1306_display_text(&dev, 7, "Hello World!!", 13, true);
#endif // CONFIG_SSD1306_128x64

#if CONFIG_SSD1306_128x32
    top = 1;
    center = 1;
    bottom = 4;
    ssd1306_display_text(&dev, 0, "SSD1306 128x32", 14, false);
    ssd1306_display_text(&dev, 1, "Hello World!!", 13, false);
    //ssd1306_clear_line(&dev, 2, true);
    //ssd1306_clear_line(&dev, 3, true);
    ssd1306_display_text(&dev, 2, "SSD1306 128x32", 14, true);
    ssd1306_display_text(&dev, 3, "Hello World!!", 13, true);
#endif // CONFIG_SSD1306_128x32
    vTaskDelay(3000 / portTICK_PERIOD_MS);

    // Display Count Down
    uint8_t image[24];
    memset(image, 0, sizeof(image));
    ssd1306_display_image(&dev, top, (6*8-1), image, sizeof(image));
    ssd1306_display_image(&dev, top+1, (6*8-1), image, sizeof(image));
    ssd1306_display_image(&dev, top+2, (6*8-1), image, sizeof(image));
    for(int font=0x39;font>0x30;font--) {
        memset(image, 0, sizeof(image));
        ssd1306_display_image(&dev, top+1, (7*8-1), image, 8);
        memcpy(image, font8x8_basic_tr[font], 8);
        if (dev._flip) ssd1306_flip(image, 8);
        ssd1306_display_image(&dev, top+1, (7*8-1), image, 8);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

    // Scroll Up
    ssd1306_clear_screen(&dev, false);
    ssd1306_contrast(&dev, 0xff);
    ssd1306_display_text(&dev, 0, "---Scroll  UP---", 16, true);
    //ssd1306_software_scroll(&dev, 7, 1);
    ssd1306_software_scroll(&dev, (dev._pages - 1), 1);
    for (int line=0;line<bottom+10;line++) {
        lineChar[0] = 0x01;
        sprintf(&lineChar[1], " Line %02d", line);
        ssd1306_scroll_text(&dev, lineChar, strlen(lineChar), false);
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
    vTaskDelay(3000 / portTICK_PERIOD_MS);

    // Scroll Down
    ssd1306_clear_screen(&dev, false);
    ssd1306_contrast(&dev, 0xff);
    ssd1306_display_text(&dev, 0, "--Scroll  DOWN--", 16, true);
    //ssd1306_software_scroll(&dev, 1, 7);
    ssd1306_software_scroll(&dev, 1, (dev._pages - 1) );
    for (int line=0;line<bottom+10;line++) {
        lineChar[0] = 0x02;
        sprintf(&lineChar[1], " Line %02d", line);
        ssd1306_scroll_text(&dev, lineChar, strlen(lineChar), false);
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
    vTaskDelay(3000 / portTICK_PERIOD_MS);

    // Page Down
    ssd1306_clear_screen(&dev, false);
    ssd1306_contrast(&dev, 0xff);
    ssd1306_display_text(&dev, 0, "---Page  DOWN---", 16, true);
    ssd1306_software_scroll(&dev, 1, (dev._pages-1) );
    for (int line=0;line<bottom+10;line++) {
        //if ( (line % 7) == 0) ssd1306_scroll_clear(&dev);
        if ( (line % (dev._pages-1)) == 0) ssd1306_scroll_clear(&dev);
        lineChar[0] = 0x02;
        sprintf(&lineChar[1], " Line %02d", line);
        ssd1306_scroll_text(&dev, lineChar, strlen(lineChar), false);
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
    vTaskDelay(3000 / portTICK_PERIOD_MS);

    // Horizontal Scroll
    ssd1306_clear_screen(&dev, false);
    ssd1306_contrast(&dev, 0xff);
    ssd1306_display_text(&dev, center, "Horizontal", 10, false);
    ssd1306_hardware_scroll(&dev, SCROLL_RIGHT);
    vTaskDelay(5000 / portTICK_PERIOD_MS);
    ssd1306_hardware_scroll(&dev, SCROLL_LEFT);
    vTaskDelay(5000 / portTICK_PERIOD_MS);
    ssd1306_hardware_scroll(&dev, SCROLL_STOP);

    // Vertical Scroll
    ssd1306_clear_screen(&dev, false);
    ssd1306_contrast(&dev, 0xff);
    ssd1306_display_text(&dev, center, "Vertical", 8, false);
    ssd1306_hardware_scroll(&dev, SCROLL_DOWN);
    vTaskDelay(5000 / portTICK_PERIOD_MS);
    ssd1306_hardware_scroll(&dev, SCROLL_UP);
    vTaskDelay(5000 / portTICK_PERIOD_MS);
    ssd1306_hardware_scroll(&dev, SCROLL_STOP);

    // Invert
    ssd1306_clear_screen(&dev, true);
    ssd1306_contrast(&dev, 0xff);
    ssd1306_display_text(&dev, center, "  Good Bye!!", 12, true);
    vTaskDelay(5000 / portTICK_PERIOD_MS);

    // Fade Out
    ssd1306_fadeout(&dev);

#if 0
    // Fade Out
    for(int contrast=0xff;contrast>0;contrast=contrast-0x20) {
        ssd1306_contrast(&dev, contrast);
        vTaskDelay(40);
    }
#endif
    // Restart module
    esp_restart();
}

image image

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce2810,len:0x178c
load:0x403c8700,len:0x4
load:0x403c8704,len:0xcb8
load:0x403cb700,len:0x2db0
entry 0x403c890c
(27) boot: ESP-IDF 5.3.0 2nd stage bootloader
(27) boot: compile time Aug 11 2024 10:12:48
(27) boot: Multicore bootloader
(30) boot: chip revision: v0.2
(34) boot.esp32s3: Boot SPI Speed : 80MHz
(38) boot.esp32s3: SPI Mode       : DIO
(43) boot.esp32s3: SPI Flash Size : 16MB
(48) boot: Enabling RNG early entropy source...
(53) boot: Partition Table:
(57) boot: ## Label            Usage          Type ST Offset   Length
(64) boot:  0 nvs              WiFi data        01 02 00009000 00006000
(72) boot:  1 phy_init         RF data          01 01 0000f000 00001000
(79) boot:  2 factory          factory app      00 00 00010000 00100000
(87) boot: End of partition table
(91) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=0c894h ( 51348) map
(108) esp_image: segment 1: paddr=0001c8bc vaddr=3fc92f00 size=03244h ( 12868) load
(111) esp_image: segment 2: paddr=0001fb08 vaddr=40374000 size=00510h (  1296) load
(116) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=1fba8h (129960) map
(148) esp_image: segment 4: paddr=0003fbd0 vaddr=40374510 size=0e9c4h ( 59844) load
(167) boot: Loaded app from partition at offset 0x10000
(168) boot: Disabling RNG early entropy source...
(179) cpu_start: Multicore app
(189) cpu_start: Pro cpu start user code
(189) cpu_start: cpu freq: 160000000 Hz
(189) app_init: Application information:
(192) app_init: Project name:     ro-empujo-bot
(197) app_init: App version:      f6ffbf7-dirty
(203) app_init: Compile time:     Aug 11 2024 10:21:37
(209) app_init: ELF file SHA256:  636dfdb6a...
(214) app_init: ESP-IDF:          5.3.0
(219) efuse_init: Min chip rev:     v0.0
(223) efuse_init: Max chip rev:     v0.99 
(228) efuse_init: Chip rev:         v0.2
(233) heap_init: Initializing. RAM available for dynamic allocation:
(240) heap_init: At 3FC96A20 len 00052CF0 (331 KiB): RAM
(246) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
(252) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
(259) heap_init: At 600FE100 len 00001EE8 (7 KiB): RTCRAM
(266) spi_flash: detected chip: generic
(270) spi_flash: flash io: dio
(274) sleep: Configure to isolate all GPIO pins in sleep state
(280) sleep: Enable automatic switching of GPIO sleep configuration
(288) main_task: Started on CPU0
(318) main_task: Calling app_main()
(318) SSD1306: INTERFACE is i2c
(318) SSD1306: CONFIG_SDA_GPIO=42
(318) SSD1306: CONFIG_SCL_GPIO=41
(328) SSD1306: CONFIG_RESET_GPIO=-1
(328) SSD1306: New i2c driver is used
(338) gpio: GPIO[42]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 1| Pulldown: 0| Intr:0 
(338) gpio: GPIO[41]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 1| Pulldown: 0| Intr:0 
(348) SSD1306: Panel is 128x32
(358) i2c.master: I2C transaction unexpected nack detected
(358) i2c.master: s_i2c_synchronous_transaction(870): I2C transaction failed
(368) i2c.master: i2c_master_transmit(1072): I2C transaction failed
(378) SSD1306: Could not write to device [0x3c at 0]: 259 (ESP_ERR_INVALID_STATE)
(388) i2c.master: I2C transaction unexpected nack detected
(388) i2c.master: s_i2c_synchronous_transaction(870): I2C transaction failed

Any idea what could be wrong?

nopnop2002 commented 2 months ago

There are no problems. Please check the wiring again.

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce2810,len:0x178c
load:0x403c8700,len:0x4
load:0x403c8704,len:0xcb8
load:0x403cb700,len:0x2db0
entry 0x403c8914
I (27) boot: ESP-IDF v5.3 2nd stage bootloader
I (27) boot: compile time Aug 12 2024 06:54:12
I (27) boot: Multicore bootloader
I (30) boot: chip revision: v0.1
I (34) boot.esp32s3: Boot SPI Speed : 80MHz
I (38) boot.esp32s3: SPI Mode       : DIO
I (43) boot.esp32s3: SPI Flash Size : 2MB
I (48) boot: Enabling RNG early entropy source...
I (53) boot: Partition Table:
I (57) boot: ## Label            Usage          Type ST Offset   Length
I (64) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (71) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (79) boot:  2 factory          factory app      00 00 00010000 00100000
I (86) boot: End of partition table
I (91) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=0c8b8h ( 51384) map
I (108) esp_image: segment 1: paddr=0001c8e0 vaddr=3fc92f00 size=03244h ( 12868) load
I (111) esp_image: segment 2: paddr=0001fb2c vaddr=40374000 size=004ech (  1260) load
I (116) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=1fbe8h (130024) map
I (148) esp_image: segment 4: paddr=0003fc10 vaddr=403744ec size=0e9e8h ( 59880) load
I (167) boot: Loaded app from partition at offset 0x10000
I (167) boot: Disabling RNG early entropy source...
I (179) cpu_start: Multicore app
I (189) cpu_start: Pro cpu start user code
I (189) cpu_start: cpu freq: 240000000 Hz
I (189) app_init: Application information:
I (192) app_init: Project name:     ssd1306
I (196) app_init: App version:      b375737-dirty
I (202) app_init: Compile time:     Aug 12 2024 07:02:54
I (208) app_init: ELF file SHA256:  65024e1fa...
I (213) app_init: ESP-IDF:          v5.3
I (218) efuse_init: Min chip rev:     v0.0
I (222) efuse_init: Max chip rev:     v0.99
I (227) efuse_init: Chip rev:         v0.1
I (232) heap_init: Initializing. RAM available for dynamic allocation:
I (239) heap_init: At 3FC96A20 len 00052CF0 (331 KiB): RAM
I (245) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
I (252) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (258) heap_init: At 600FE100 len 00001EE8 (7 KiB): RTCRAM
I (265) spi_flash: detected chip: gd
I (268) spi_flash: flash io: dio
W (272) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (285) sleep: Configure to isolate all GPIO pins in sleep state
I (292) sleep: Enable automatic switching of GPIO sleep configuration
I (299) main_task: Started on CPU0
I (319) main_task: Calling app_main()
I (319) SSD1306: INTERFACE is i2c
I (319) SSD1306: CONFIG_SDA_GPIO=42
I (319) SSD1306: CONFIG_SCL_GPIO=41
I (329) SSD1306: CONFIG_RESET_GPIO=-1
I (329) SSD1306: New i2c driver is used
I (339) gpio: GPIO[42]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 1| Pulldown: 0| Intr:0
I (339) gpio: GPIO[41]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 1| Pulldown: 0| Intr:0
I (349) SSD1306: Panel is 128x64
I (359) SSD1306: OLED configured successfully

You can use this to check the wiring. https://github.com/espressif/esp-idf/tree/master/examples/peripherals/i2c/i2c_tools

 ==============================================================
 |             Steps to Use i2c-tools                         |
 |                                                            |
 |  1. Try 'help', check all supported commands               |
 |  2. Try 'i2cconfig' to configure your I2C bus              |
 |  3. Try 'i2cdetect' to scan devices on the bus             |
 |  4. Try 'i2cget' to get the content of specific register   |
 |  5. Try 'i2cset' to set the value of specific register     |
 |  6. Try 'i2cdump' to dump all the register (Experiment)    |
 |                                                            |
 ==============================================================

Type 'help' to get the list of commands.
Use UP/DOWN arrows to navigate through command history.
Press TAB when typing command name to auto-complete.
I (840) main_task: Returned from app_main()
i2c-tools> i2cdetect
     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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
blfuentes commented 2 months ago

Thanks! it was indeed a wiring problem... image