]> Devi Nivas Git - 4dulcd-nolibrary.git/commitdiff
Get working code
authorAdvaith Menon <noreply-git@bp4k.net>
Wed, 21 Jan 2026 15:26:01 +0000 (10:26 -0500)
committerAdvaith Menon <noreply-git@bp4k.net>
Wed, 21 Jan 2026 15:26:01 +0000 (10:26 -0500)
* Write simple Hello, World demo

main/CMakeLists.txt
main/main.c

index 6692d62edfaa55c209cacd9463583388ce54ac2e..6ee215729c99d664398ca61e8d08e97597503b3e 100644 (file)
@@ -1,3 +1,5 @@
 idf_component_register(SRCS "main.c"
                        PRIV_REQUIRES spi_flash
+                            esp_driver_uart
+                            esp_driver_gpio
                        INCLUDE_DIRS "")
index 7010f3ef61c35a9ddc0bb3ec210b7a0089374808..73c962d9bbffb84e16bf1684c6a09ebe0a3f8b66 100644 (file)
-/*
- * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: CC0-1.0
- */
-
 #include <stdio.h>
 #include <inttypes.h>
-#include "sdkconfig.h"
 #include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "esp_chip_info.h"
-#include "esp_flash.h"
+#include "esp_log.h"
 #include "esp_system.h"
+#include "driver/uart.h"
+
+#define PIN_UART_RXD 4
+#define PIN_UART_TXD 5
+
+#define ULCD_DEFAULT_BAUD_RATE 9600
+#define ULCD_DEFAULT_DATA_BITS UART_DATA_8_BITS
+#define ULCD_DEFAULT_PARITY    UART_PARITY_DISABLE
+#define ULCD_DEFAULT_STOP_BITS UART_STOP_BITS_1
+#define TIME_LIMIT 2000
+
+
+static const char *TAG = "main";
+static const uart_port_t uart_port = UART_NUM_1;
+
+/* UART config. This is handy as some sort of default. */
+static const uart_config_t uart_cfg = {
+    .baud_rate = ULCD_DEFAULT_BAUD_RATE,
+    .data_bits = ULCD_DEFAULT_DATA_BITS,
+    .parity    = ULCD_DEFAULT_PARITY,
+    .stop_bits = ULCD_DEFAULT_STOP_BITS,
+    .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
+    .rx_flow_ctrl_thresh = 10
+};
+
+
+/* Helper functions to make things easier for Arduino users */
+/* DO NOT CALL UNTIL UART IS INITIALIZED */
+int write_tx(const void *data, size_t bufsz);
+int read_rx(void *data, size_t bufsz, uint32_t timeout);
+int flush_rx();
+int flush_tx(uint32_t timeout);
+void update_baud(int baud);
+
+
+void ulcd_init(void);
+void ulcd_txt_demo(void);
+void ulcd_cls(void);
+
 
 void app_main(void)
 {
-    printf("Hello world!\n");
-
-    /* Print chip information */
-    esp_chip_info_t chip_info;
-    uint32_t flash_size;
-    esp_chip_info(&chip_info);
-    printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
-           CONFIG_IDF_TARGET,
-           chip_info.cores,
-           (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
-           (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
-           (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
-           (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");
-
-    unsigned major_rev = chip_info.revision / 100;
-    unsigned minor_rev = chip_info.revision % 100;
-    printf("silicon revision v%d.%d, ", major_rev, minor_rev);
-    if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
-        printf("Get flash size failed");
-        return;
-    }
-
-    printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
-           (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
-
-    printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
-
-    for (int i = 10; i >= 0; i--) {
-        printf("Restarting in %d seconds...\n", i);
-        vTaskDelay(1000 / portTICK_PERIOD_MS);
-    }
-    printf("Restarting now.\n");
-    fflush(stdout);
+    ESP_LOGI(TAG, "Starting program");
+    ESP_LOGI(TAG, "Initializing UART...");
+
+    /* Install Drivers */
+    const int uart_bufsz = UART_HW_FIFO_LEN(uart_port) + 4;
+    QueueHandle_t uart_q;
+    ESP_ERROR_CHECK(uart_driver_install(uart_port,
+                uart_bufsz,
+                uart_bufsz,
+                10,
+                &uart_q,
+                0));
+
+    /* Set comms parameters */
+    ESP_ERROR_CHECK(uart_param_config(uart_port, &uart_cfg));
+
+    /* Set pins */
+    ESP_ERROR_CHECK(uart_set_pin(uart_port, PIN_UART_TXD, PIN_UART_RXD,
+                UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
+
+    /* UART is initialized. We can now work on initializing the uLCD. */
+    ESP_LOGI(TAG, "Wait 3 Seconds for ULCD to be Initialized");
+    vTaskDelay(pdMS_TO_TICKS(3000)); /* see "Power-Up and Reset" in 4D docs */
+
+
+    ESP_LOGI(TAG, "Initializing ULCD...");
+    /* This is not mentioned in the code. */
+    ulcd_init();
+
+    ulcd_cls();
+    ulcd_txt_demo();
+
+
+
+
+    ESP_LOGI(TAG, "Work is done");
+    vTaskDelay(pdMS_TO_TICKS(10000));
     esp_restart();
 }
+
+
+void ulcd_init(void) {
+    /* TODO port ported library without helpers */
+}
+
+
+void ulcd_cls(void) {
+    unsigned char instr[2];
+    unsigned char ack;
+    int written;
+
+    /* 1: move cursor to 0, 0 */
+    ESP_LOGI(TAG, "Clearing screen (gfx_Cls)");
+
+    /* opcode (big-endian) */
+    instr[0] = 0xFF;
+    instr[1] = 0xD7;
+
+    written = write_tx(instr, 2);
+    flush_tx(TIME_LIMIT);
+
+    if (written != 2) ESP_LOGE(TAG, "Failed to write gfx_Cls!");
+
+    written = read_rx(&ack, 1, TIME_LIMIT);
+    if (written != 1 || ack != 6)
+        ESP_LOGE(TAG, "Failed to get acknowledge");
+}
+
+
+void ulcd_txt_demo(void) {
+    unsigned char instr[100];
+    unsigned char ack;
+    int written; /* or read */
+
+    /* 1: move cursor to 0, 0 */
+    ESP_LOGI(TAG, "Moving cursor to 0, 0");
+
+    /* opcode (big-endian) */
+    instr[0] = 0xFF;
+    instr[1] = 0xE4;
+
+    /* Line value is 2 bytes */
+    instr[2] = 0;
+    instr[3] = 0;
+
+    /* Column is again 2 bytes */
+    instr[4] = 0;
+    instr[5] = 0;
+
+    /* Since this is a demo, we do not use helpers. */
+    /* START the below code can be made into a helper */
+    written = write_tx(instr, 6);
+    flush_tx(TIME_LIMIT);
+
+    if (written != 6) ESP_LOGE(TAG, "Failed to write txt_MoveCursor!");
+
+    written = read_rx(&ack, 1, TIME_LIMIT);
+    if (written != 1 || ack != 6)
+        ESP_LOGE(TAG, "Failed to get acknowledge");
+    /* END the below code can be made into a helper */
+
+    /* write Hello, World (12) to the display */
+    /* see putstr on Goldelox */
+
+    instr[0] = 0x00;
+    instr[1] = 0x06;
+
+    instr[2] = 'H';
+    instr[3] = 'e';
+    instr[4] = 'l';
+    instr[5] = 'l';
+    instr[6] = 'o';
+    instr[7] = ',';
+    instr[8] = ' ';
+    instr[9] = 'W';
+    instr[10] = 'o';
+    instr[11] = 'r';
+    instr[12] = 'l';
+    instr[13] = 'd';
+
+    instr[14] = '\0';
+
+    written = write_tx(instr, 15);
+    flush_tx(TIME_LIMIT);
+
+    if (written != 15) ESP_LOGE(TAG, "Failed to write putstr!");
+
+    written = read_rx(&ack, 1, TIME_LIMIT);
+    if (written != 1 || ack != 6)
+        ESP_LOGE(TAG, "Failed to get acknowledge");
+}
+
+
+/* ================================================================= */
+
+int write_tx(const void *data, size_t sz) {
+    return uart_write_bytes(uart_port, data, sz);
+}
+
+
+int read_rx(void *data, size_t sz, uint32_t wait_ms) {
+    if (sizeof(size_t) > sizeof(uint32_t) && sz > UINT32_MAX)
+        return -1;
+    return uart_read_bytes(uart_port, data, (uint32_t) sz,
+            (TickType_t) wait_ms / portTICK_PERIOD_MS);
+}
+
+
+int flush_rx() {
+    return (int) uart_flush(uart_port);
+}
+
+
+int flush_tx(uint32_t wait_ms) {
+    return (int) uart_wait_tx_done(uart_port,
+            (TickType_t) wait_ms / portTICK_PERIOD_MS);
+}
+
+void update_baud(int baud) {
+    uart_config_t ucfg2 = uart_cfg;
+    ucfg2.baud_rate = baud;
+    ESP_ERROR_CHECK(uart_param_config(uart_port, &ucfg2));
+}
+
+/* ================================================================= */