diff --git a/components/mch2022-rp2040 b/components/mch2022-rp2040 index 2b4cbce..3c859b5 160000 --- a/components/mch2022-rp2040 +++ b/components/mch2022-rp2040 @@ -1 +1 @@ -Subproject commit 2b4cbce95827f6da3841584b8f6e80930f269f21 +Subproject commit 3c859b52f3f8cc3b3969d6a6d57a12f22781f291 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index a381bbb..fd2f49b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -25,6 +25,7 @@ idf_component_register( "animation.c" "button_test.c" "adc_test.c" + "webusb.c" INCLUDE_DIRS "." "include" "menus" diff --git a/main/factory_test.c b/main/factory_test.c index ad286be..4e154c4 100644 --- a/main/factory_test.c +++ b/main/factory_test.c @@ -60,6 +60,20 @@ bool test_stuck_buttons(uint32_t* rc) { return (state == 0x0000); } +bool test_adc_vbat(uint32_t* rc) { + float value = 0; + esp_err_t res = rp2040_read_vbat(get_rp2040(), &value); + *rc = value * 100; + return ((res == ESP_OK) && (value < 4.3) && (value > 3.9)); +} + +bool test_adc_vusb(uint32_t* rc) { + float value = 0; + esp_err_t res = rp2040_read_vusb(get_rp2040(), &value); + *rc = value * 100; + return ((res == ESP_OK) && (value > 4.5)); +} + bool test_sd_power(uint32_t* rc) { *rc = 0x00000000; // Init all GPIO pins for SD card and LED @@ -106,8 +120,12 @@ bool run_basic_tests(pax_buf_t* pax_buffer, ILI9341* ili9341) { RUN_TEST_MANDATORY("ICE40", test_ice40_init); RUN_TEST_MANDATORY("BNO055", test_bno055_init); RUN_TEST_MANDATORY("BME680", test_bme680_init); - RUN_TEST_MANDATORY("STUCK BUTTONS", test_stuck_buttons); - RUN_TEST_MANDATORY("SD/LED POWER", test_sd_power); + + /* Run tests */ + RUN_TEST("STUCK BUTTONS", test_stuck_buttons); + RUN_TEST("SD/LED POWER", test_sd_power); + RUN_TEST("Battery voltage", test_adc_vbat); + RUN_TEST("USB voltage", test_adc_vusb); error: diff --git a/main/include/webusb.h b/main/include/webusb.h new file mode 100644 index 0000000..2cd234c --- /dev/null +++ b/main/include/webusb.h @@ -0,0 +1,8 @@ +#pragma once + +#include "ice40.h" +#include "pax_gfx.h" +#include "ili9341.h" +#include + +void webusb_main(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341); diff --git a/main/main.c b/main/main.c index 3c01c49..1b3dab4 100644 --- a/main/main.c +++ b/main/main.c @@ -49,6 +49,8 @@ #include "menus/start.h" #include "factory_test.h" +#include "fpga_download.h" +#include "webusb.h" extern const uint8_t wallpaper_png_start[] asm("_binary_wallpaper_png_start"); extern const uint8_t wallpaper_png_end[] asm("_binary_wallpaper_png_end"); @@ -69,9 +71,29 @@ void display_fatal_error(pax_buf_t* pax_buffer, ILI9341* ili9341, const char* li ili9341_write(ili9341, pax_buffer->buf); } +void stop() { + ESP_LOGW(TAG, "*** HALTED ***"); + gpio_set_direction(GPIO_SD_PWR, GPIO_MODE_OUTPUT); + gpio_set_level(GPIO_SD_PWR, 1); + ws2812_init(GPIO_LED_DATA); + uint8_t led_off[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + uint8_t led_red[15] = {0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0}; + uint8_t led_red2[15] = {0, 0xFF, 0, 0, 0xFF, 0, 0, 0xFF, 0, 0, 0xFF, 0, 0, 0xFF, 0}; + while (true) { + ws2812_send_data(led_red2, sizeof(led_red2)); + vTaskDelay(pdMS_TO_TICKS(200)); + ws2812_send_data(led_red, sizeof(led_red)); + vTaskDelay(pdMS_TO_TICKS(200)); + ws2812_send_data(led_off, sizeof(led_off)); + vTaskDelay(pdMS_TO_TICKS(200)); + } +} + void app_main(void) { esp_err_t res; + audio_init(); + const esp_app_desc_t *app_description = esp_ota_get_app_description(); ESP_LOGI(TAG, "App version: %s", app_description->version); //ESP_LOGI(TAG, "Project name: %s", app_description->project_name); @@ -113,23 +135,21 @@ void app_main(void) { if (res != ESP_OK) { ESP_LOGE(TAG, "NVS init failed: %d", res); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "NVS failed to initialize", "Flash may be corrupted", NULL); - esp_restart(); + stop(); } - - audio_init(); display_boot_screen(pax_buffer, ili9341, "Starting..."); if (bsp_rp2040_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the RP2040 co-processor"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "RP2040 co-processor error", NULL, NULL); - esp_restart(); + stop(); } RP2040* rp2040 = get_rp2040(); if (rp2040 == NULL) { ESP_LOGE(TAG, "rp2040 is NULL"); - esp_restart(); + stop(); } rp2040_updater(rp2040, pax_buffer, ili9341); // Handle RP2040 firmware update & bootloader mode @@ -140,7 +160,7 @@ void app_main(void) { if (rp2040_get_uid(rp2040, rp2040_uid) != ESP_OK) { ESP_LOGE(TAG, "Failed to get RP2040 UID"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "Failed to read UID", NULL, NULL); - esp_restart(); + stop(); } printf("RP2040 UID: %02X%02X%02X%02X%02X%02X%02X%02X\n", rp2040_uid[0], rp2040_uid[1], rp2040_uid[2], rp2040_uid[3], rp2040_uid[4], rp2040_uid[5], rp2040_uid[6], rp2040_uid[7]);*/ @@ -148,13 +168,13 @@ void app_main(void) { if (bsp_ice40_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the ICE40 FPGA"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "ICE40 FPGA error", NULL, NULL); - esp_restart(); + stop(); } ICE40* ice40 = get_ice40(); if (ice40 == NULL) { ESP_LOGE(TAG, "ice40 is NULL"); - esp_restart(); + stop(); } /*display_boot_screen(pax_buffer, ili9341, "Initializing BNO055..."); @@ -162,13 +182,13 @@ void app_main(void) { if (bsp_bno055_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the BNO055 position sensor"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BNO055 sensor error", "Check I2C bus", "Remove SAO and try again"); - esp_restart(); + stop(); } BNO055* bno055 = get_bno055(); if (bno055 == NULL) { ESP_LOGE(TAG, "bno055 is NULL"); - esp_restart(); + stop(); }*/ /*display_boot_screen(pax_buffer, ili9341, "Initializing BME680..."); @@ -176,13 +196,13 @@ void app_main(void) { if (bsp_bme680_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize the BME680 position sensor"); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BME680 sensor error", "Check I2C bus", "Remove SAO and try again"); - esp_restart(); + stop(); } BME680* bme680 = get_bme680(); if (bme680 == NULL) { ESP_LOGE(TAG, "bme680 is NULL"); - esp_restart(); + stop(); }*/ //display_boot_screen(pax_buffer, ili9341, "Initializing AppFS..."); @@ -192,7 +212,7 @@ void app_main(void) { if (res != ESP_OK) { ESP_LOGE(TAG, "AppFS init failed: %d", res); display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "AppFS failed to initialize", "Flash may be corrupted", NULL); - esp_restart(); + stop(); } //display_boot_screen(pax_buffer, ili9341, "Initializing filesystem..."); @@ -235,12 +255,40 @@ void app_main(void) { /* Start WiFi */ wifi_init(); - /* Rick that roll */ - play_bootsound(); + /* Check WebUSB mode */ + + uint8_t webusb_mode; + res = rp2040_get_webusb_mode(rp2040, &webusb_mode); + if (res != ESP_OK) { + ESP_LOGE(TAG, "Failed to read WebUSB mode: %d", res); + display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "Failed to read WebUSB mode", NULL, NULL); + stop(); + } + + ESP_LOGI(TAG, "WebUSB mode 0x%02X", webusb_mode); + + if (webusb_mode == 0x00) { // Normal boot + /* Rick that roll */ + play_bootsound(); - /* Launcher menu */ - while (true) { - menu_start(rp2040->queue, pax_buffer, ili9341, app_description->version); + /* Launcher menu */ + while (true) { + menu_start(rp2040->queue, pax_buffer, ili9341, app_description->version); + } + } else if (webusb_mode == 0x01) { + display_boot_screen(pax_buffer, ili9341, "WebUSB mode"); + while (true) { + webusb_main(rp2040->queue, pax_buffer, ili9341); + } + } else if (webusb_mode == 0x02) { + display_boot_screen(pax_buffer, ili9341, "FPGA download mode"); + while (true) { + fpga_download(rp2040->queue, get_ice40(), pax_buffer, ili9341); + } + } else { + char buffer[64]; + snprintf(buffer, sizeof(buffer), "Invalid mode 0x%02X", webusb_mode); + display_boot_screen(pax_buffer, ili9341, buffer); } free(framebuffer); diff --git a/main/menus/start.c b/main/menus/start.c index fa6e54a..ce2b703 100644 --- a/main/menus/start.c +++ b/main/menus/start.c @@ -140,8 +140,12 @@ void menu_start(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili934 usb_voltage = 0; } - battery_percent = ((battery_voltage - 3.7) * 100) / (4.1 - 3.7); - if (battery_percent > 100) battery_percent = 100; + if (battery_voltage >= 3.6) { + battery_percent = ((battery_voltage - 3.6) * 100) / (4.2 - 3.6); + if (battery_percent > 100) battery_percent = 100; + } else { + battery_percent = 0; + } battery_charging = (usb_voltage > 4.0) && (battery_percent < 100); diff --git a/main/rp2040_updater.c b/main/rp2040_updater.c index e05663b..41b8896 100644 --- a/main/rp2040_updater.c +++ b/main/rp2040_updater.c @@ -47,7 +47,7 @@ void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341) { restart(); } - if (fw_version < 0x02) { // Update required + if (fw_version < 0x03) { // Update required display_rp2040_update_state(pax_buffer, ili9341, "Starting bootloader..."); rp2040_reboot_to_bootloader(rp2040); esp_restart(); diff --git a/main/webusb.c b/main/webusb.c new file mode 100644 index 0000000..995fe11 --- /dev/null +++ b/main/webusb.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "driver/uart.h" +#include "hardware.h" +#include "managed_i2c.h" +#include "pax_gfx.h" +#include "ice40.h" +#include "system_wrapper.h" +#include "graphics_wrapper.h" +#include "esp32/rom/crc.h" + +void webusb_install_uart() { + fflush(stdout); + ESP_ERROR_CHECK(uart_driver_install(0, 2048, 0, 0, NULL, 0)); + uart_config_t uart_config = { + .baud_rate = 921600, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .source_clk = UART_SCLK_APB, + }; + ESP_ERROR_CHECK(uart_param_config(0, &uart_config)); +} + +void webusb_uninstall_uart() { + uart_driver_delete(0); +} + +bool webusb_read_stdin(uint8_t* buffer, uint32_t len, uint32_t timeout) { + int read = uart_read_bytes(0, buffer, len, timeout / portTICK_PERIOD_MS); + return (read == len); +} + +bool webusb_uart_sync(uint32_t* length, uint32_t* crc) { + uint8_t rx_buffer[4*3]; + webusb_read_stdin(rx_buffer, sizeof(rx_buffer), 100); + if (memcmp(rx_buffer, "WUSB", 4) != 0) return false; + memcpy((uint8_t*) length, &rx_buffer[4 * 1], 4); + memcpy((uint8_t*) crc, &rx_buffer[4 * 2], 4); + return true; +} + +bool webusb_uart_load(uint8_t* buffer, uint32_t length) { + return webusb_read_stdin(buffer, length, 3000); +} + +void webusb_uart_mess(const char *mess) { + uart_write_bytes(0, mess, strlen(mess)); +} + +void webusb_print_status(pax_buf_t* pax_buffer, ILI9341* ili9341, char* message) { + pax_noclip(pax_buffer); + pax_background(pax_buffer, 0x325aa8); + pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, "WebUSB mode"); + pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*1, message); + ili9341_write(ili9341, pax_buffer->buf); +} + +void webusb_main(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341* ili9341) { + webusb_install_uart(); + + while (true) { + webusb_print_status(pax_buffer, ili9341, "Waiting..."); + + // 1) Wait for WUSB followed by data length as uint32 and CRC32 of the data as uint32 + uint32_t length, crc; + while (!webusb_uart_sync(&length, &crc)) { + webusb_uart_mess("WUSB"); + } + + webusb_print_status(pax_buffer, ili9341, "Receiving..."); + + // 2) Allocate RAM for the data to be received + uint8_t* buffer = malloc(length); + if (buffer == NULL) { + webusb_uart_mess("EMEM"); + webusb_print_status(pax_buffer, ili9341, "Error: malloc failed"); + vTaskDelay(100 / portTICK_PERIOD_MS); + continue; + } + + // 3) Receive data into the buffer + if (!webusb_uart_load(buffer, length)) { + free(buffer); + webusb_uart_mess("ERCV"); + webusb_print_status(pax_buffer, ili9341, "Error: receive failed"); + vTaskDelay(100 / portTICK_PERIOD_MS); + continue; + } + + // 4) Check CRC + uint32_t checkCrc = crc32_le(0, buffer, length); + + if (checkCrc != crc) { + free(buffer); + webusb_uart_mess("ECRC"); + webusb_print_status(pax_buffer, ili9341, "Error: CRC invalid"); + vTaskDelay(100 / portTICK_PERIOD_MS); + continue; + } + + webusb_uart_mess("OKOK"); + webusb_print_status(pax_buffer, ili9341, "Packet received"); + + // To-do: parse packet + } + + webusb_uninstall_uart(); +} diff --git a/resources/rp2040_firmware.bin b/resources/rp2040_firmware.bin index 917aafd..c445a28 100755 Binary files a/resources/rp2040_firmware.bin and b/resources/rp2040_firmware.bin differ