ESP-IDF i2c Adafruit 0.54" Alphanumeric Backpack

Checky
Posts: 4
Joined: Sun Oct 29, 2023 4:13 pm

ESP-IDF i2c Adafruit 0.54" Alphanumeric Backpack

Postby Checky » Thu Nov 23, 2023 11:16 am

Hello, I would like to get this display to work, but somehow there are only poor documentation examples and a confusing array of libraries. This wasn't a problem at all under Arduni with the Adafruit LED Backpack Library, but somehow you don't get such pretty things that make your life easy for esp-idf orre, at least not that I know of.
So I went here and tried, on the one hand, to get the interface for the IC2 to work and to make the Adafruit library C suitable, at least the part that I need.

My main problem is that I can't seem to get the oscillator to work and the displays just stay off.

SO I WOULD BE VERY HAPPY IF SOMEONE COULD HELP ME FURTHER.
  1. main.c    VZ_Seg_setDisplay(&VZ_DISP_1, "ABCD");
  2. VZ_Seg_setDisplay(&VZ_DISP_2, "1234");
  3. VZ_Seg_setDisplay(&VZ_DISP_3, "BLUB");
  1. I2Ccon.c //###############################################################################################################################
  2. // Unsere I2Ccon.c für die Initialisierung I2C Geräte und dem Master auf dem I2C-Bus  unser Wrover-Kit
  3. //###############################################################################################################################
  4.  
  5. #include "../include/I2Ccon.h"
  6.  
  7. //###############################################################################################################################
  8.  
  9. #define CHECK_ARG(ARG) do { if (!ARG) return ESP_ERR_INVALID_ARG; } while (0)
  10.  
  11. //###############################################################################################################################
  12.      
  13.       // Master-Config
  14. // #define CONFIG_SCL_GPIO              0                        // Definieren wiir über ide menuconfig
  15. // #define CONFIG_SDA_GPIO              2
  16. #define I2C_MASTER_PORT                 I2C_NUM_0                // Standartport
  17. #define I2C_MASTER_TX_BUF_ENABLE        0
  18. #define I2C_MASTER_RX_BUF_ENABLE        0
  19. #define I2C_MASTER_FREQ_HZ              100000                   // Taktfrequenz
  20.  
  21.  
  22.     // Geräte im Bus und ihre Adressen
  23. #define BTTF_RTC_ADDR                       0x68                //*!< RTC device address */
  24. #define VZ_DISP_1_ADDR                      0x70                //*!< HT16K33 device address 1 */
  25. #define VZ_DISP_2_ADDR                      0x71                //*!< HT16K33 device address 2 */
  26. #define VZ_DISP_3_ADDR                      0x72                //*!< HT16K33 device address 3 */
  27.  
  28.  
  29. // Flag, um den I2C-Master-Status zu überprüfen
  30. static bool i2c_master_initialized = false;
  31.  
  32. i2c_dev_t BTTF_RTC, VZ_DISP_1, VZ_DISP_2, VZ_DISP_3;
  33.  
  34. // I2C //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. //###############################################################################################################################
  36.  
  37.     // Zuerst initialisiern wir den Master für den I2C-Bus    (in unserem Fall das Wrover-Kit)
  38. esp_err_t BTTF_i2c_master_init(i2c_port_t port, int sda, int scl)
  39. {
  40.     // Nur initialisieren, wenn noch nicht geschehen
  41.     if (!i2c_master_initialized) {
  42.         uint8_t i2c_master_port = I2C_MASTER_PORT;
  43.    
  44.         i2c_config_t conf = {
  45.             .mode = I2C_MODE_MASTER,
  46.             .sda_io_num = CONFIG_SDA_GPIO,
  47.             .scl_io_num = CONFIG_SCL_GPIO,
  48.             .sda_pullup_en = GPIO_PULLUP_ENABLE,
  49.             .scl_pullup_en = GPIO_PULLUP_ENABLE,
  50.             .master.clk_speed = I2C_MASTER_FREQ_HZ,
  51.             };
  52.  
  53.         esp_err_t i2c_init_result = i2c_param_config(i2c_master_port, &conf);
  54.         ESP_ERROR_CHECK(i2c_init_result);
  55.  
  56.         i2c_init_result = i2c_driver_install(
  57.             i2c_master_port,
  58.             conf.mode,
  59.             I2C_MASTER_RX_BUF_ENABLE,
  60.             I2C_MASTER_TX_BUF_ENABLE,
  61.             0
  62.             );
  63.         if (i2c_init_result != ESP_OK) {
  64.             ESP_LOGE(TAG, "Fehler bei der Installation des I2C-Treibers: %d", i2c_init_result);
  65.             const char *driver_err_msg = esp_err_to_name(i2c_init_result);
  66.             ESP_LOGE(TAG, "I2C-Treiberfehlermeldung: %s", driver_err_msg);
  67.         } else {
  68.             ESP_LOGI(TAG, "I2C-Master initialisiert");
  69.             i2c_master_initialized = true;  // Setze das Flag, dass der I2C-Master initialisiert wurde
  70.             I2C_Scan();                 // DEBUG-Funktion Scannt den I2C Bus auf vorhandene Geräte
  71.         }
  72.  
  73.         return i2c_init_result;  // Hier wird der Rückgabewert übergeben
  74.     }
  75.     // Falls bereits initialisiert, gebe ESP_OK zurück
  76.     return ESP_OK;
  77. }
  78.  
  79.  
  80. // DEBUG: Hilfsfunktion um den IC2 Bus auf Geräte zu scannen
  81. static esp_err_t check_adress_for_device(uint8_t device_address)
  82. {
  83.     uint8_t write_buf[2] = {0, 0};
  84.  
  85.     return i2c_master_write_to_device(I2C_NUM_0, device_address, write_buf, sizeof(write_buf), 10 / portTICK_PERIOD_MS);
  86. }
  87.  
  88. // DEBUG: I2C Bus auf Geräte scannen // Gibt und Anzahl und Adressen von angeschlossenen I2C-Geräten
  89. void I2C_Scan()  {
  90.     size_t devicesDiscovered = 0;
  91.     for (size_t address = 1; address < 127; address++) {
  92.         if (ESP_OK == check_adress_for_device(address)) {
  93.             ESP_LOGI(TAG, "Found device at address 0x%X", address);
  94.             devicesDiscovered++;
  95.         }
  96.     }
  97.     if (devicesDiscovered == 1){
  98.         ESP_LOGI(TAG, "Found %d device\n", (int)devicesDiscovered);
  99.     } else if (devicesDiscovered > 1) {
  100.         ESP_LOGI(TAG, "Found %d devices\n", (int)devicesDiscovered);  
  101.     } else {
  102.         ESP_LOGI(TAG, "No devices found\n");
  103.     }
  104.     vTaskDelay(pdMS_TO_TICKS(50));
  105. }
  106.  
  107.  
  108. void Init_I2C_Devices()     {
  109.  
  110.     // Initialisiere die RTC
  111.     if (ds3231_init_desc(&BTTF_RTC, I2C_NUM_0, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO) != ESP_OK) {
  112.         ESP_LOGE(TAG, "RTC: Konnte Gerätebeschreibung nicht initialisieren.");
  113.         while (1) { vTaskDelay(100); }
  114.     } else {
  115.         ESP_LOGI(TAG, "RTC erfolgreich initialisiert");
  116.     }
  117.  
  118.         // Initialisiere das VZ1-Display
  119.     if (VZ1_init_desc(&VZ_DISP_1, I2C_NUM_0, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO) != ESP_OK) {
  120.         ESP_LOGE(TAG, "VZ_DISP_1: Konnte Gerätebeschreibung nicht initialisieren.");
  121.         ESP_LOGE(TAG, "I2C-Treiberfehlermeldung: %s", esp_err_to_name(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0)));
  122.         while (1) { vTaskDelay(100); }
  123.     } else {
  124.         ESP_LOGI(TAG, "VZ_DISP_1 initialisiert");
  125.     }
  126.  
  127.         // Initialisiere das VZ2-Display
  128.     if (VZ2_init_desc(&VZ_DISP_2, I2C_NUM_0, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO) != ESP_OK) {
  129.         ESP_LOGE(TAG, "VZ_DISP_2: Konnte Gerätebeschreibung nicht initialisieren.");
  130.         ESP_LOGE(TAG, "I2C-Treiberfehlermeldung: %s", esp_err_to_name(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0)));
  131.         while (1) { vTaskDelay(100); }
  132.     } else {
  133.         ESP_LOGI(TAG, "VZ_DISP_2 initialisiert");
  134.     }
  135.  
  136.         // Initialisiere das VZ3-Display
  137.     if (VZ3_init_desc(&VZ_DISP_3, I2C_NUM_0, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO) != ESP_OK) {
  138.         ESP_LOGE(TAG, "VZ_DISP_3: Konnte Gerätebeschreibung nicht initialisieren.");
  139.         ESP_LOGE(TAG, "I2C-Treiberfehlermeldung: %s", esp_err_to_name(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0)));
  140.         while (1) { vTaskDelay(100); }
  141.     } else {
  142.         ESP_LOGI(TAG, "VZ_DISP_3 initialisiert");
  143.     }
  144.     if (VZ_Seg_begin(&VZ_DISP_1) &&
  145.         VZ_Seg_begin(&VZ_DISP_2) &&
  146.         VZ_Seg_begin(&VZ_DISP_3)) {
  147.         ESP_LOGI(TAG, "Alle Displays erfolgreich initialisiert");
  148.     } else {
  149.         ESP_LOGE(TAG, "Fehler bei der Initialisierung der Displays");
  150.     }
  151. }
  152.  
  153.  
  154.  
  155. // Funktion zur Initialisierung der RTC
  156. esp_err_t ds3231_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
  157. {
  158.     CHECK_ARG(dev);
  159.  
  160.     dev->port = port;
  161.     dev->addr = BTTF_RTC_ADDR;  // Verwende die Adresse für die RTC
  162.     dev->sda_io_num = sda_gpio;
  163.     dev->scl_io_num = scl_gpio;
  164.     dev->clk_speed = I2C_MASTER_FREQ_HZ;
  165.     return BTTF_i2c_master_init(port, sda_gpio, scl_gpio);
  166. }
  167.  
  168. // Funktion zur Initialisierung des VZ1-Displays
  169. esp_err_t VZ1_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
  170. {
  171.     CHECK_ARG(dev);
  172.  
  173.     dev->port = port;
  174.     dev->addr = VZ_DISP_1_ADDR;  
  175.     dev->sda_io_num = sda_gpio;
  176.     dev->scl_io_num = scl_gpio;
  177.     dev->clk_speed = I2C_MASTER_FREQ_HZ;
  178.     return BTTF_i2c_master_init(port, sda_gpio, scl_gpio);
  179. }
  180.  
  181. // Funktion zur Initialisierung des VZ2-Displays
  182.  
  183. esp_err_t VZ2_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
  184. {
  185.     CHECK_ARG(dev);
  186.  
  187.     dev->port = port;
  188.     dev->addr = VZ_DISP_2_ADDR;  
  189.     dev->sda_io_num = sda_gpio;
  190.     dev->scl_io_num = scl_gpio;
  191.     dev->clk_speed = I2C_MASTER_FREQ_HZ;
  192.     return BTTF_i2c_master_init(port, sda_gpio, scl_gpio);
  193. }
  194.  
  195. // Funktion zur Initialisierung des VZ1-Displays
  196.  
  197. esp_err_t VZ3_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
  198. {
  199.     CHECK_ARG(dev);
  200.  
  201.     dev->port = port;
  202.     dev->addr = VZ_DISP_3_ADDR;  
  203.     dev->sda_io_num = sda_gpio;
  204.     dev->scl_io_num = scl_gpio;
  205.     dev->clk_speed = I2C_MASTER_FREQ_HZ;
  206.     return BTTF_i2c_master_init(port, sda_gpio, scl_gpio);
  207. }
  208.  
  209. //###############################################################################################################################
  1.  I2Ccon.h //###############################################################################################################################
  2. // Unsere Headerdatei für die Init.c
  3. //###############################################################################################################################
  4.  
  5. #ifndef I2CCON_H
  6. #define I2CCON_H
  7.  
  8. //###############################################################################################################################
  9.  
  10. #include "Include.h"                              // Header-Datei für alle Includes
  11.  
  12. extern i2c_dev_t BTTF_RTC, VZ_DISP_1, VZ_DISP_2, VZ_DISP_3; // Geräte-Handels (Objekte) RTC und die 3x ht16k33 14-Seg
  13.  
  14. //FUNKTIONSDEKLARATIONEN/////////////////////////////////////////////////////////////////////////////////////////////////////////
  15.  
  16.  
  17. esp_err_t ds3231_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio);
  18. esp_err_t VZ1_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio);
  19. esp_err_t VZ2_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio);
  20. esp_err_t VZ3_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio);
  21.  
  22.  
  23. esp_err_t BTTF_i2c_master_init(i2c_port_t port, int sda, int scl);
  24.  
  25.  
  26. void            I2C_Scan();
  27. extern void     Init_I2C_Devices();
  28.  
  29. //###############################################################################################################################
  30. #endif
  1.  VZ_seg.c //###############################################################################################################################
  2. // Unsere VZ_seg.c für die Vierzehn-Segmentanzeigen die durch HT16K33 gesteuert werden
  3. //###############################################################################################################################
  4.  
  5. #include "../include/VZ_seg.h"
  6.  
  7. //###############################################################################################################################
  8.  
  9.  
  10. // DEKLARATIONEN
  11.  
  12.  
  13. const uint16_t alphafonttable[] = {
  14.  
  15.     0b0000000000000001, 0b0000000000000010, 0b0000000000000100,
  16.     0b0000000000001000, 0b0000000000010000, 0b0000000000100000,
  17.     0b0000000001000000, 0b0000000010000000, 0b0000000100000000,
  18.     0b0000001000000000, 0b0000010000000000, 0b0000100000000000,
  19.     0b0001000000000000, 0b0010000000000000, 0b0100000000000000,
  20.     0b1000000000000000, 0b0000000000000000, 0b0000000000000000,
  21.     0b0000000000000000, 0b0000000000000000, 0b0000000000000000,
  22.     0b0000000000000000, 0b0000000000000000, 0b0000000000000000,
  23.     0b0001001011001001, 0b0001010111000000, 0b0001001011111001,
  24.     0b0000000011100011, 0b0000010100110000, 0b0001001011001000,
  25.     0b0011101000000000, 0b0001011100000000,
  26.     0b0000000000000000, //
  27.     0b0000000000000110, // !
  28.     0b0000001000100000, // "
  29.     0b0001001011001110, // #
  30.     0b0001001011101101, // $
  31.     0b0000110000100100, // %
  32.     0b0010001101011101, // &
  33.     0b0000010000000000, // '
  34.     0b0010010000000000, // (
  35.     0b0000100100000000, // )
  36.     0b0011111111000000, // *
  37.     0b0001001011000000, // +
  38.     0b0000100000000000, // ,
  39.     0b0000000011000000, // -
  40.     0b0100000000000000, // .
  41.     0b0000110000000000, // /
  42.     0b0000110000111111, // 0
  43.     0b0000000000000110, // 1
  44.     0b0000000011011011, // 2
  45.     0b0000000010001111, // 3
  46.     0b0000000011100110, // 4
  47.     0b0010000001101001, // 5
  48.     0b0000000011111101, // 6
  49.     0b0000000000000111, // 7
  50.     0b0000000011111111, // 8
  51.     0b0000000011101111, // 9
  52.     0b0001001000000000, // :
  53.     0b0000101000000000, // ;
  54.     0b0010010000000000, // <
  55.     0b0000000011001000, // =
  56.     0b0000100100000000, // >
  57.     0b0001000010000011, // ?
  58.     0b0000001010111011, // @
  59.     0b0000000011110111, // A
  60.     0b0001001010001111, // B
  61.     0b0000000000111001, // C
  62.     0b0001001000001111, // D
  63.     0b0000000011111001, // E
  64.     0b0000000001110001, // F
  65.     0b0000000010111101, // G
  66.     0b0000000011110110, // H
  67.     0b0001001000001001, // I
  68.     0b0000000000011110, // J
  69.     0b0010010001110000, // K
  70.     0b0000000000111000, // L
  71.     0b0000010100110110, // M
  72.     0b0010000100110110, // N
  73.     0b0000000000111111, // O
  74.     0b0000000011110011, // P
  75.     0b0010000000111111, // Q
  76.     0b0010000011110011, // R
  77.     0b0000000011101101, // S
  78.     0b0001001000000001, // T
  79.     0b0000000000111110, // U
  80.     0b0000110000110000, // V
  81.     0b0010100000110110, // W
  82.     0b0010110100000000, // X
  83.     0b0001010100000000, // Y
  84.     0b0000110000001001, // Z
  85.     0b0000000000111001, // [
  86.     0b0010000100000000, //
  87.     0b0000000000001111, // ]
  88.     0b0000110000000011, // ^
  89.     0b0000000000001000, // _
  90.     0b0000000100000000, // `
  91.     0b0001000001011000, // a
  92.     0b0010000001111000, // b
  93.     0b0000000011011000, // c
  94.     0b0000100010001110, // d
  95.     0b0000100001011000, // e
  96.     0b0000000001110001, // f
  97.     0b0000010010001110, // g
  98.     0b0001000001110000, // h
  99.     0b0001000000000000, // i
  100.     0b0000000000001110, // j
  101.     0b0011011000000000, // k
  102.     0b0000000000110000, // l
  103.     0b0001000011010100, // m
  104.     0b0001000001010000, // n
  105.     0b0000000011011100, // o
  106.     0b0000000101110000, // p
  107.     0b0000010010000110, // q
  108.     0b0000000001010000, // r
  109.     0b0010000010001000, // s
  110.     0b0000000001111000, // t
  111.     0b0000000000011100, // u
  112.     0b0010000000000100, // v
  113.     0b0010100000010100, // w
  114.     0b0010100011000000, // x
  115.     0b0010000000001100, // y
  116.     0b0000100001001000, // z
  117.     0b0000100101001001, // {
  118.     0b0001001000000000, // |
  119.     0b0010010010001001, // }
  120.     0b0000010100100000, // ~
  121.     0b0011111111111111,
  122.  
  123. };
  124.  
  125.  
  126. uint16_t displaybuffer[8];
  127. uint8_t buffer[17]= {0}; // Initialisiert alle Elemente mit 0;
  128.  
  129. // VZ-SEG ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  130. //###############################################################################################################################
  131.  
  132. void VZ_Seg_writeDigitRaw( uint8_t n, uint16_t bitmask) {
  133.  displaybuffer[n] = bitmask;
  134. }
  135.  
  136.  
  137.  
  138. void VZ_Seg_writeDigitAscii(i2c_dev_t *display, uint8_t n, uint8_t ascii, bool d) {
  139.   uint16_t font = alphafonttable[ascii];
  140.  
  141.  displaybuffer[n] = font;
  142.  
  143.   if (d)
  144.   displaybuffer[n] |= (1 << 14);
  145. }
  146.  
  147.  
  148. void VZ_Seg_setBrightness(i2c_dev_t *display, uint8_t b) {
  149.     if (b > 15)
  150.         b = 15; // limit to max brightness
  151.  
  152.     uint8_t buffer = HT16K33_CMD_BRIGHTNESS | b;
  153.     i2c_dev_write(display, NULL, 0, &buffer, sizeof(uint8_t));
  154. }
  155.  
  156.  
  157. void VZ_Seg_blinkRate(i2c_dev_t *display, uint8_t b) {
  158.   if (b > 3)
  159.     b = 0; // turn off if not sure
  160.  
  161.   uint8_t buffer = HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (b << 1);
  162.  
  163.   // Verwende das i2c_dev_t-Objekt des Displays, um Daten zu schreiben
  164.   i2c_dev_write(display, NULL, 0, &buffer, sizeof(uint8_t));
  165. }
  166.  
  167.  
  168.  
  169.  
  170. bool VZ_Seg_begin(i2c_dev_t *display) {
  171.   ESP_LOGI(TAG, "beginn");
  172. VZ_Seg_clear(display);
  173.     // turn on oscillator
  174.     uint8_t buffer[1] = {0x21};
  175.     i2c_dev_write(display, NULL, 0, &buffer, sizeof(uint8_t));
  176.  
  177.     VZ_Seg_setBrightness(display, 15);
  178. VZ_Seg_writeDisplay(display);
  179.     return true;  // oder false je nach Erfolg oder Misserfolg der Initialisierung
  180. }
  181.  
  182. void VZ_Seg_writeDisplay(i2c_dev_t *display) {
  183.  
  184.   buffer[0] = 0x00; // start at address $00
  185.  
  186.   for (uint8_t i = 0; i < 8; i++) {
  187.     buffer[1 + 2 * i] = displaybuffer[i] & 0xFF;
  188.     buffer[2 + 2 * i] = displaybuffer[i] >> 8;
  189.   }
  190.  
  191. ESP_LOGI(TAG, "writeDisplay");
  192. ESP_LOGI(TAG, "Buffer values: %02x %02x %02x %02x %02x %02x %02x %02x",
  193.             buffer[1], buffer[2], buffer[3], buffer[4],
  194.             buffer[5], buffer[6], buffer[7], buffer[8]);
  195.  
  196.   i2c_dev_write(display, NULL, 0, &buffer, sizeof(uint8_t));
  197. }
  198.  
  199. void VZ_Seg_clear(i2c_dev_t *display) {
  200.   for (uint8_t i = 0; i < 8; i++) {
  201.     displaybuffer[i] = 0;
  202.   }
  203. }
  204.  
  205.  
  206. void VZ_Seg_setDisplay(i2c_dev_t *display, const char *text) {
  207.     VZ_Seg_clear(display);
  208.  
  209.  
  210.     for (uint8_t i = 0; i < strlen(text); i++) {
  211.         VZ_Seg_writeDigitAscii(display, i, text[i], false);
  212.     }
  213.  
  214.     VZ_Seg_writeDisplay(display);
  215. }
  1. VZ_seg.h
  2.     -------A-------
  3.     |\     |     /|
  4.     | \    J    / |
  5.     |   H  |  K   |
  6.     F    \ | /    B
  7.     |     \|/     |
  8.     |--G1--|--G2--|
  9.     |     /|\     |
  10.     E    / | \    C
  11.     |   L  |   N  |
  12.     | /    M    \ |
  13.     |/     |     \|
  14.     -------D-------  DP
  15. */
  16.  
  17. // DEFINITIONEN /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18.  
  19.  
  20. extern uint16_t displaybuffer[8];
  21.  
  22. #define LED_ON 1  ///< GFX color of lit LED segments (single-color displays)
  23. #define LED_OFF 0 ///< GFX color of unlit LED segments (single-color displays)
  24.  
  25. #define HT16K33_BLINK_CMD               0x80        ///< I2C register for BLINK setting
  26. #define HT16K33_BLINK_DISPLAYON         0x01        ///< I2C value for steady on
  27. #define HT16K33_BLINK_OFF               0           ///< I2C value for steady off
  28. #define HT16K33_BLINK_2HZ               1           ///< I2C value for 2 Hz blink
  29. #define HT16K33_BLINK_1HZ               2           ///< I2C value for 1 Hz blink
  30. #define HT16K33_BLINK_HALFHZ            3           ///< I2C value for 0.5 Hz blink
  31.  
  32.  
  33. #define HT16K33_CMD_BRIGHTNESS 0xE0 ///< I2C register for BRIGHTNESS setting
  34.  
  35. #define ALPHANUM_SEG_A  0b0000000000000001      ///< Alphanumeric segment A
  36. #define ALPHANUM_SEG_B  0b0000000000000010      ///< Alphanumeric segment B
  37. #define ALPHANUM_SEG_C  0b0000000000000100      ///< Alphanumeric segment C
  38. #define ALPHANUM_SEG_D  0b0000000000001000      ///< Alphanumeric segment D
  39. #define ALPHANUM_SEG_E  0b0000000000010000      ///< Alphanumeric segment E
  40. #define ALPHANUM_SEG_F  0b0000000000100000      ///< Alphanumeric segment F
  41. #define ALPHANUM_SEG_G1 0b0000000001000000      ///< Alphanumeric segment G1
  42. #define ALPHANUM_SEG_G2 0b0000000010000000      ///< Alphanumeric segment G2
  43. #define ALPHANUM_SEG_H  0b0000000100000000      ///< Alphanumeric segment H
  44. #define ALPHANUM_SEG_J  0b0000001000000000      ///< Alphanumeric segment J
  45. #define ALPHANUM_SEG_K  0b0000010000000000      ///< Alphanumeric segment K
  46. #define ALPHANUM_SEG_L  0b0000100000000000      ///< Alphanumeric segment L
  47. #define ALPHANUM_SEG_M  0b0001000000000000      ///< Alphanumeric segment M
  48. #define ALPHANUM_SEG_N  0b0010000000000000      ///< Alphanumeric segment N
  49. #define ALPHANUM_SEG_DP 0b0100000000000000      ///< Alphanumeric segment DP
  50.  
  51. //###############################################################################################################################
  52.  
  53.  
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57.  
  58.  
  59. // Funktionen für die C++-Implementierung in der .cpp-Datei
  60.  
  61. extern bool VZ_Seg_begin(i2c_dev_t *display);
  62. extern void VZ_Seg_setBrightness(i2c_dev_t *display, uint8_t b);
  63. void VZ_Seg_blinkRate(i2c_dev_t *display, uint8_t b);
  64. void VZ_Seg_writeDisplay(i2c_dev_t *display);
  65. void VZ_Seg_clear(i2c_dev_t *display);
  66. void VZ_Seg_writeDigitRaw(uint8_t n, uint16_t bitmask);
  67. void VZ_Seg_writeDigitAscii(i2c_dev_t *display, uint8_t n, uint8_t ascii, bool d);
  68.  
  69. extern void VZ_Seg_setDisplay(i2c_dev_t *display, const char *text);
  70.  
  71.  
  72.  
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76.  
  77.  
  78.  
  79.  
  80. //###############################################################################################################################
  81. #endif
  1. i2cdev.h #ifndef MAIN_I2CDEV_H_
  2. #define MAIN_I2CDEV_H_
  3.  
  4. #include "driver/i2c.h"
  5.  
  6. #define I2C_FREQ_HZ 400000
  7. #define I2CDEV_TIMEOUT 1000
  8.  
  9. typedef struct {
  10.     i2c_port_t port;            // I2C port number
  11.     uint8_t addr;               // I2C address
  12.     gpio_num_t sda_io_num;      // GPIO number for I2C sda signal
  13.     gpio_num_t scl_io_num;      // GPIO number for I2C scl signal
  14.         uint32_t clk_speed;             // I2C clock frequency for master mode
  15. } i2c_dev_t;
  16.  
  17. esp_err_t i2c_master_init(i2c_port_t port, int sda, int scl);
  18. esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data, size_t out_size, void *in_data, size_t in_size);
  19. esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg, size_t out_reg_size, const void *out_data, size_t out_size);
  20. inline esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg,
  21.         void *in_data, size_t in_size)
  22. {
  23.     return i2c_dev_read(dev, &reg, 1, in_data, in_size);
  24. }
  25.  
  26. inline esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg,
  27.         const void *out_data, size_t out_size)
  28. {
  29.     return i2c_dev_write(dev, &reg, 1, out_data, out_size);
  30. }
  31. #endif /* MAIN_I2CDEV_H_ */
  1. i2cdev.c #include <string.h>
  2. #include <time.h>
  3.  
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6.  
  7. #include "driver/i2c.h"
  8. #include "esp_log.h"
  9.  
  10. #include "i2cdev.h"
  11.  
  12. #define TAG "I2CDEV"
  13.  
  14. esp_err_t i2c_master_init(i2c_port_t port, int sda, int scl)
  15. {
  16.         i2c_config_t i2c_config = {
  17.                 .mode = I2C_MODE_MASTER,
  18.                 .sda_io_num = sda,
  19.                 .scl_io_num = scl,
  20.                 .sda_pullup_en = GPIO_PULLUP_ENABLE,
  21.                 .scl_pullup_en = GPIO_PULLUP_ENABLE,
  22.                 .master.clk_speed = 1000000
  23.         };
  24.         //i2c_param_config(I2C_NUM_0, &i2c_config);
  25.         //i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
  26.         i2c_param_config(port, &i2c_config);
  27.         return i2c_driver_install(port, I2C_MODE_MASTER, 0, 0, 0);
  28. }
  29.  
  30. esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data, size_t out_size, void *in_data, size_t in_size)
  31. {
  32.     if (!dev || !in_data || !in_size) return ESP_ERR_INVALID_ARG;
  33.  
  34.     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  35.     if (out_data && out_size)
  36.     {
  37.         i2c_master_start(cmd);
  38.         i2c_master_write_byte(cmd, (dev->addr << 1) | I2C_MASTER_WRITE, true);
  39.         i2c_master_write(cmd, (void *)out_data, out_size, true);
  40.     }
  41.     i2c_master_start(cmd);
  42.     i2c_master_write_byte(cmd, (dev->addr << 1) | 1, true);
  43.     i2c_master_read(cmd, in_data, in_size, I2C_MASTER_LAST_NACK);
  44.     i2c_master_stop(cmd);
  45.  
  46.     esp_err_t res = i2c_master_cmd_begin(dev->port, cmd, I2CDEV_TIMEOUT / portTICK_PERIOD_MS);
  47.     if (res != ESP_OK)
  48.         ESP_LOGE(TAG, "Could not read from device [0x%02x at %d]: %d", dev->addr, dev->port, res);
  49.     i2c_cmd_link_delete(cmd);
  50.  
  51.     return res;
  52. }
  53.  
  54. esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg, size_t out_reg_size, const void *out_data, size_t out_size)
  55. {
  56.     if (!dev || !out_data || !out_size) return ESP_ERR_INVALID_ARG;
  57.  
  58.     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  59.     i2c_master_start(cmd);
  60.     i2c_master_write_byte(cmd, (dev->addr << 1) | I2C_MASTER_WRITE, true);
  61.     if (out_reg && out_reg_size)
  62.         i2c_master_write(cmd, (void *)out_reg, out_reg_size, true);
  63.     i2c_master_write(cmd, (void *)out_data, out_size, true);
  64.     i2c_master_stop(cmd);
  65.     esp_err_t res = i2c_master_cmd_begin(dev->port, cmd, I2CDEV_TIMEOUT / portTICK_PERIOD_MS);
  66.     if (res != ESP_OK)
  67.         ESP_LOGE(TAG, "Could not write to device [0x%02x at %d]: %d", dev->addr, dev->port, res);
  68.     i2c_cmd_link_delete(cmd);
  69.  
  70.     return res;
  71. }
  1. OUTPUT : rst:0x1 (POWERON_RESET),boot:0x1e (SPI_FAST_FLASH_BOOT)
  2. configsip: 0, SPIWP:0xee
  3. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  4. mode:DIO, clock div:2
  5. load:0x3fff0030,len:7084
  6. ho 0 tail 12 room 4
  7. load:0x40078000,len:15584
  8. load:0x40080400,len:4
  9. 0x40080400: _init at ??:?
  10.  
  11. load:0x40080404,len:3876
  12. entry 0x4008064c
  13. I (31) boot: ESP-IDF v5.1.1-dirty 2nd stage bootloader
  14. I (31) boot: compile time Nov 17 2023 11:57:48        
  15. I (31) boot: Multicore bootloader
  16. I (36) boot: chip revision: v3.0
  17. I (40) boot.esp32: SPI Speed      : 40MHz
  18. I (44) boot.esp32: SPI Mode       : DIO
  19. I (49) boot.esp32: SPI Flash Size : 2MB
  20. I (53) boot: Enabling RNG early entropy source...
  21. I (59) boot: Partition Table:
  22. I (62) boot: ## Label            Usage          Type ST Offset   Length
  23. I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
  24. I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
  25. I (85) boot:  2 factory          factory app      00 00 00010000 00100000
  26. I (92) boot: End of partition table
  27. I (96) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=0c940h ( 51520) map
  28. I (123) esp_image: segment 1: paddr=0001c968 vaddr=3ffb0000 size=02230h (  8752) load
  29. I (127) esp_image: segment 2: paddr=0001eba0 vaddr=40080000 size=01478h (  5240) load
  30. I (131) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=1b22ch (111148) map
  31. I (178) esp_image: segment 4: paddr=0003b254 vaddr=40081478 size=0c91ch ( 51484) load
  32. I (199) esp_image: segment 5: paddr=00047b78 vaddr=50000000 size=00004h (     4) load
  33. I (207) boot: Loaded app from partition at offset 0x10000
  34. I (207) boot: Disabling RNG early entropy source...
  35. I (219) cpu_start: Multicore app
  36. I (220) cpu_start: Pro cpu up.
  37. I (220) cpu_start: Starting app cpu, entry point is 0x40081294
  38. 0x40081294: call_start_cpu1 at C:/Users/CheckMakRack/esp/esp-idf/components/esp_system/port/cpu_start.c:154
  39.  
  40. I (0) cpu_start: App cpu up.
  41. I (238) cpu_start: Pro cpu start user code
  42. I (238) cpu_start: cpu freq: 160000000 Hz
  43. I (238) cpu_start: Application information:
  44. I (243) cpu_start: Project name:     BTTF-TCD
  45. I (248) cpu_start: App version:      1
  46. I (252) cpu_start: Compile time:     Nov 18 2023 10:57:00
  47. I (258) cpu_start: ELF file SHA256:  6c274682c571f52c...
  48. I (264) cpu_start: ESP-IDF:          v5.1.1-dirty
  49. I (270) cpu_start: Min chip rev:     v0.0
  50. I (274) cpu_start: Max chip rev:     v3.99
  51. I (279) cpu_start: Chip rev:         v3.0
  52. I (284) heap_init: Initializing. RAM available for dynamic allocation:
  53. I (291) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
  54. I (297) heap_init: At 3FFB33F8 len 0002CC08 (179 KiB): DRAM
  55. I (303) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
  56. I (310) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
  57. I (316) heap_init: At 4008DD94 len 0001226C (72 KiB): IRAM
  58. I (324) spi_flash: detected chip: generic
  59. I (327) spi_flash: flash io: dio
  60. W (331) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
  61. W (344) rmt(legacy): legacy driver is deprecated, please migrate to `driver/rmt_tx.h` and/or `driver/rmt_rx.h`
  62. I (355) app_start: Starting scheduler on CPU0
  63. I (360) app_start: Starting scheduler on CPU1
  64. I (360) main_task: Started on CPU0
  65. I (370) main_task: Calling app_main()
  66. I (370) gpio: GPIO[4]| InputEn: 1| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  67. I (380) gpio: GPIO[5]| InputEn: 1| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  68. I (390) gpio: GPIO[12]| InputEn: 1| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  69. I (400) gpio: GPIO[14]| InputEn: 1| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  70. I (410) uart_rx_task: Start
  71. I (410) uart_tx_task: Start
  72.  
  73. sending:7e ff 6 c 0 0 0 fe ef ef
  74. received:7e ff 6 3f 0 0 2 fe ba ef
  75. I (1560) MAIN: DF_begin=1
  76. I (1560) MAIN: DFPlayer Mini initialisiert.
  77. I (1560) MAIN: Setzen Lautstärke auf 15
  78.  
  79. sending:7e ff 6 6 0 0 f fe e6 ef
  80. I (1560) MAIN: I2C-Master initialisiert
  81. I (1580) MAIN: Found device at address 0x57
  82. I (1580) MAIN: Found device at address 0x5F
  83. I (1580) MAIN: Found device at address 0x68
  84. I (1580) MAIN: Found device at address 0x70
  85. I (1590) MAIN: Found device at address 0x71
  86. I (1590) MAIN: Found device at address 0x72
  87. I (1600) MAIN: Found 6 devices
  88.  
  89. I (1650) MAIN: RTC erfolgreich initialisiert
  90. I (1650) MAIN: VZ_DISP_1 initialisiert
  91. I (1650) MAIN: VZ_DISP_2 initialisiert
  92. I (1650) MAIN: VZ_DISP_3 initialisiert
  93. I (1650) MAIN: beginn
  94. I (1660) MAIN: writeDisplay
  95. I (1660) MAIN: Buffer values: 00 00 00 00 00 00 00 00
  96. I (1660) MAIN: beginn
  97. I (1670) MAIN: writeDisplay
  98. I (1670) MAIN: Buffer values: 00 00 00 00 00 00 00 00
  99. I (1680) MAIN: beginn
  100. I (1680) MAIN: writeDisplay
  101. I (1680) MAIN: Buffer values: 00 00 00 00 00 00 00 00
  102. I (1690) MAIN: Alle Displays erfolgreich initialisiert
  103. I (1690) MAIN: I2C_SCL_GPIO = 0
  104. I (1700) getClock: 2023-11-23 10:32:00, 19.75 deg Cel
  105. I (1700) MAIN: I2C_SDA_GPIO = 2
  106. I (1710) MAIN: CONFIG_TIMEZONE= 1
  107. I (1710) MAIN: writeDisplay
  108. I (1720) MAIN: Buffer values: f7 00 8f 12 39 00 0f 12
  109. I (1720) MAIN: writeDisplay
  110. I (1720) MAIN: Buffer values: 06 00 db 00 8f 00 e6 00
  111. I (1730) MAIN: writeDisplay
  112. I (1730) MAIN: Buffer values: 8f 12 38 00 3e 00 8f 12
  113. I (1740) main_task: Returned from app_main()
  114. I (1740) MAIN: IR-RX vor RINGBUFFER -DEBUGPRINT
  115. I (11690) getClock: 2023-11-23 10:32:10, 19.75 deg Cel

Checky
Posts: 4
Joined: Sun Oct 29, 2023 4:13 pm

Re: ESP-IDF i2c Adafruit 0.54" Alphanumeric Backpack

Postby Checky » Mon Nov 27, 2023 12:34 pm

I took the example ht16k33.h and threw out MUTEX commands that don't exist anywhere and no documentation of them. Now I can write on it.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], ShinyGlossy and 217 guests