Merge pull request #17 from badgeteam/renze/rp2040-cleanup
Cleanup (first of many): move RP2400 driver code
This commit is contained in:
commit
e2f9affae5
10 changed files with 270 additions and 735 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -19,3 +19,6 @@
|
||||||
[submodule "components/pax-graphics"]
|
[submodule "components/pax-graphics"]
|
||||||
path = components/pax-graphics
|
path = components/pax-graphics
|
||||||
url = https://github.com/robotman2412/pax-graphics.git
|
url = https://github.com/robotman2412/pax-graphics.git
|
||||||
|
[submodule "components/mch2022-rp2040"]
|
||||||
|
path = components/mch2022-rp2040
|
||||||
|
url = https://github.com/badgeteam/esp32-component-mch2022-rp2040.git
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
idf_component_register(
|
idf_component_register(
|
||||||
SRCS "hardware.c" "rp2040.c"
|
SRCS "hardware.c"
|
||||||
INCLUDE_DIRS "."
|
INCLUDE_DIRS "."
|
||||||
REQUIRES
|
REQUIRES
|
||||||
"appfs"
|
"appfs"
|
||||||
|
@ -10,4 +10,5 @@ idf_component_register(
|
||||||
"sdcard"
|
"sdcard"
|
||||||
"spi-ice40"
|
"spi-ice40"
|
||||||
"spi-ili9341"
|
"spi-ili9341"
|
||||||
|
"mch2022-rp2040"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,184 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) 2022 Nicolai Electronics
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <sdkconfig.h>
|
|
||||||
#include <driver/gpio.h>
|
|
||||||
#include "rp2040.h"
|
|
||||||
#include "managed_i2c.h"
|
|
||||||
|
|
||||||
static const char *TAG = "RP2040";
|
|
||||||
|
|
||||||
inline void _send_input_change(RP2040* device, uint8_t input, bool value) {
|
|
||||||
rp2040_input_message_t message;
|
|
||||||
message.input = input;
|
|
||||||
message.state = value;
|
|
||||||
xQueueSend(device->queue, &message, portMAX_DELAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rp2040_intr_task(void *arg) {
|
|
||||||
RP2040* device = (RP2040*) arg;
|
|
||||||
uint32_t state;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
if (xSemaphoreTake(device->_intr_trigger, portMAX_DELAY)) {
|
|
||||||
esp_err_t res = i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_INPUT1, (uint8_t*) &state, 4);
|
|
||||||
if (res != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "RP2040 interrupt task failed to read from RP2040");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
//ESP_LOGW(TAG, "RP2040 input state %08x", state);
|
|
||||||
uint16_t interrupt = state >> 16;
|
|
||||||
uint16_t values = state & 0xFFFF;
|
|
||||||
for (uint8_t index = 0; index < 16; index++) {
|
|
||||||
if ((interrupt >> index) & 0x01) {
|
|
||||||
_send_input_change(device, index, (values >> index) & 0x01);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void rp2040_intr_handler(void *arg) {
|
|
||||||
/* in interrupt handler context */
|
|
||||||
RP2040* device = (RP2040*) arg;
|
|
||||||
xSemaphoreGiveFromISR(device->_intr_trigger, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_init(RP2040* device) {
|
|
||||||
esp_err_t res;
|
|
||||||
|
|
||||||
res = rp2040_get_firmware_version(device, &device->_fw_version);
|
|
||||||
if (res != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "Failed to read firmware version");
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (device->_fw_version < 1) {
|
|
||||||
ESP_LOGE(TAG, "Unsupported RP2040 firmware version (%u) found", device->_fw_version);
|
|
||||||
return ESP_ERR_INVALID_VERSION;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_GPIO_DIR, &device->_gpio_direction, 1);
|
|
||||||
if (res != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "Failed to read GPIO direction");
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_GPIO_OUT, &device->_gpio_value, 1);
|
|
||||||
if (res != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "Failed to read GPIO state");
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Create interrupt trigger
|
|
||||||
device->_intr_trigger = xSemaphoreCreateBinary();
|
|
||||||
if (device->_intr_trigger == NULL) return ESP_ERR_NO_MEM;
|
|
||||||
|
|
||||||
//Attach interrupt to interrupt pin
|
|
||||||
if (device->pin_interrupt >= 0) {
|
|
||||||
res = gpio_isr_handler_add(device->pin_interrupt, rp2040_intr_handler, (void*) device);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
|
|
||||||
gpio_config_t io_conf = {
|
|
||||||
.intr_type = GPIO_INTR_NEGEDGE,
|
|
||||||
.mode = GPIO_MODE_INPUT,
|
|
||||||
.pin_bit_mask = 1LL << device->pin_interrupt,
|
|
||||||
.pull_down_en = 0,
|
|
||||||
.pull_up_en = 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
res = gpio_config(&io_conf);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
|
|
||||||
xTaskCreate(&rp2040_intr_task, "RP2040 interrupt", 4096, (void*) device, 10, &device->_intr_task_handle);
|
|
||||||
xSemaphoreGive(device->_intr_trigger);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_firmware_version(RP2040* device, uint8_t* version) {
|
|
||||||
return i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_FW_VER, version, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_bootloader_version(RP2040* device, uint8_t* version) {
|
|
||||||
if (device->_fw_version != 0xFF) return ESP_FAIL;
|
|
||||||
return i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_BL_REG_BL_VER, version, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_bootloader_state(RP2040* device, uint8_t* state) {
|
|
||||||
if (device->_fw_version != 0xFF) return ESP_FAIL;
|
|
||||||
return i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_BL_REG_BL_STATE, state, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_set_bootloader_ctrl(RP2040* device, uint8_t action) {
|
|
||||||
if (device->_fw_version != 0xFF) return ESP_FAIL;
|
|
||||||
return i2c_write_reg_n(device->i2c_bus, device->i2c_address, RP2040_BL_REG_BL_CTRL, &action, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_reboot_to_bootloader(RP2040* device) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
uint8_t value = 0xBE;
|
|
||||||
return i2c_write_reg_n(device->i2c_bus, device->i2c_address, RP2040_REG_BL_TRIGGER, &value, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_gpio_dir(RP2040* device, uint8_t gpio, bool* direction) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
esp_err_t res = i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_GPIO_DIR, &device->_gpio_direction, 1);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
*direction = (device->_gpio_direction >> gpio) & 0x01;
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
esp_err_t rp2040_set_gpio_dir(RP2040* device, uint8_t gpio, bool direction) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
if (direction) {
|
|
||||||
device->_gpio_direction |= 1UL << gpio;
|
|
||||||
} else {
|
|
||||||
device->_gpio_direction &= ~(1UL << gpio);
|
|
||||||
}
|
|
||||||
return i2c_write_reg_n(device->i2c_bus, device->i2c_address, RP2040_REG_GPIO_DIR, &device->_gpio_direction, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_gpio_value(RP2040* device, uint8_t gpio, bool* value) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
uint8_t reg_value;
|
|
||||||
esp_err_t res = i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_GPIO_IN, ®_value, 1);
|
|
||||||
if (res != ESP_OK) return res;
|
|
||||||
*value = (reg_value >> gpio) & 0x01;
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_set_gpio_value(RP2040* device, uint8_t gpio, bool value) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
if (value) {
|
|
||||||
device->_gpio_value |= 1UL << gpio;
|
|
||||||
} else {
|
|
||||||
device->_gpio_value &= ~(1UL << gpio);
|
|
||||||
}
|
|
||||||
return i2c_write_reg_n(device->i2c_bus, device->i2c_address, RP2040_REG_GPIO_OUT, &device->_gpio_value, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_lcd_backlight(RP2040* device, uint8_t* brightness) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
return i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_LCD_BACKLIGHT, brightness, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_set_lcd_backlight(RP2040* device, uint8_t brightness) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_OK; // Ignore if unsupported
|
|
||||||
return i2c_write_reg_n(device->i2c_bus, device->i2c_address, RP2040_REG_LCD_BACKLIGHT, &brightness, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_set_fpga(RP2040* device, bool enabled) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
uint8_t value = enabled ? 0x01 : 0x00;
|
|
||||||
return i2c_write_reg_n(device->i2c_bus, device->i2c_address, RP2040_REG_FPGA, &value, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t rp2040_read_buttons(RP2040* device, uint16_t* value) {
|
|
||||||
if ((device->_fw_version < 0x01) && (device->_fw_version >= 0xFF)) return ESP_FAIL;
|
|
||||||
return i2c_read_reg(device->i2c_bus, device->i2c_address, RP2040_REG_INPUT1, (uint8_t*) value, 2);
|
|
||||||
}
|
|
|
@ -1,158 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <esp_err.h>
|
|
||||||
#include <esp_log.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <freertos/FreeRTOS.h>
|
|
||||||
#include <freertos/semphr.h>
|
|
||||||
#include <freertos/task.h>
|
|
||||||
#include <freertos/queue.h>
|
|
||||||
|
|
||||||
enum {
|
|
||||||
RP2040_REG_FW_VER = 0,
|
|
||||||
RP2040_REG_GPIO_DIR,
|
|
||||||
RP2040_REG_GPIO_IN,
|
|
||||||
RP2040_REG_GPIO_OUT,
|
|
||||||
RP2040_REG_LCD_BACKLIGHT,
|
|
||||||
RP2040_REG_FPGA,
|
|
||||||
RP2040_REG_INPUT1,
|
|
||||||
RP2040_REG_INPUT2,
|
|
||||||
RP2040_REG_INTERRUPT1,
|
|
||||||
RP2040_REG_INTERRUPT2,
|
|
||||||
RP2040_REG_ADC_TRIGGER,
|
|
||||||
RP2040_REG_ADC_VALUE_VUSB1,
|
|
||||||
RP2040_REG_ADC_VALUE_VUSB2,
|
|
||||||
RP2040_REG_ADC_VALUE_VBAT1,
|
|
||||||
RP2040_REG_ADC_VALUE_VBAT2,
|
|
||||||
RP2040_REG_USB,
|
|
||||||
RP2040_REG_BL_TRIGGER,
|
|
||||||
RP2040_REG_SCRATCH0, // Used by the ESP32 to store boot parameters, can also be read and written to from WebUSB
|
|
||||||
RP2040_REG_SCRATCH1,
|
|
||||||
RP2040_REG_SCRATCH2,
|
|
||||||
RP2040_REG_SCRATCH3,
|
|
||||||
RP2040_REG_SCRATCH4,
|
|
||||||
RP2040_REG_SCRATCH5,
|
|
||||||
RP2040_REG_SCRATCH6,
|
|
||||||
RP2040_REG_SCRATCH7,
|
|
||||||
RP2040_REG_SCRATCH8,
|
|
||||||
RP2040_REG_SCRATCH9,
|
|
||||||
RP2040_REG_SCRATCH10,
|
|
||||||
RP2040_REG_SCRATCH11,
|
|
||||||
RP2040_REG_SCRATCH12,
|
|
||||||
RP2040_REG_SCRATCH13,
|
|
||||||
RP2040_REG_SCRATCH14,
|
|
||||||
RP2040_REG_SCRATCH15,
|
|
||||||
RP2040_REG_SCRATCH16,
|
|
||||||
RP2040_REG_SCRATCH17,
|
|
||||||
RP2040_REG_SCRATCH18,
|
|
||||||
RP2040_REG_SCRATCH19,
|
|
||||||
RP2040_REG_SCRATCH20,
|
|
||||||
RP2040_REG_SCRATCH21,
|
|
||||||
RP2040_REG_SCRATCH22,
|
|
||||||
RP2040_REG_SCRATCH23,
|
|
||||||
RP2040_REG_SCRATCH24,
|
|
||||||
RP2040_REG_SCRATCH25,
|
|
||||||
RP2040_REG_SCRATCH26,
|
|
||||||
RP2040_REG_SCRATCH27,
|
|
||||||
RP2040_REG_SCRATCH28,
|
|
||||||
RP2040_REG_SCRATCH29,
|
|
||||||
RP2040_REG_SCRATCH30,
|
|
||||||
RP2040_REG_SCRATCH31,
|
|
||||||
RP2040_REG_SCRATCH32,
|
|
||||||
RP2040_REG_SCRATCH33,
|
|
||||||
RP2040_REG_SCRATCH34,
|
|
||||||
RP2040_REG_SCRATCH35,
|
|
||||||
RP2040_REG_SCRATCH36,
|
|
||||||
RP2040_REG_SCRATCH37,
|
|
||||||
RP2040_REG_SCRATCH38,
|
|
||||||
RP2040_REG_SCRATCH39,
|
|
||||||
RP2040_REG_SCRATCH40,
|
|
||||||
RP2040_REG_SCRATCH41,
|
|
||||||
RP2040_REG_SCRATCH42,
|
|
||||||
RP2040_REG_SCRATCH43,
|
|
||||||
RP2040_REG_SCRATCH44,
|
|
||||||
RP2040_REG_SCRATCH45,
|
|
||||||
RP2040_REG_SCRATCH46,
|
|
||||||
RP2040_REG_SCRATCH47,
|
|
||||||
RP2040_REG_SCRATCH48,
|
|
||||||
RP2040_REG_SCRATCH49,
|
|
||||||
RP2040_REG_SCRATCH50,
|
|
||||||
RP2040_REG_SCRATCH51,
|
|
||||||
RP2040_REG_SCRATCH52,
|
|
||||||
RP2040_REG_SCRATCH53,
|
|
||||||
RP2040_REG_SCRATCH54,
|
|
||||||
RP2040_REG_SCRATCH55,
|
|
||||||
RP2040_REG_SCRATCH56,
|
|
||||||
RP2040_REG_SCRATCH57,
|
|
||||||
RP2040_REG_SCRATCH58,
|
|
||||||
RP2040_REG_SCRATCH59,
|
|
||||||
RP2040_REG_SCRATCH60,
|
|
||||||
RP2040_REG_SCRATCH61,
|
|
||||||
RP2040_REG_SCRATCH62,
|
|
||||||
RP2040_REG_SCRATCH63
|
|
||||||
};
|
|
||||||
|
|
||||||
enum {
|
|
||||||
RP2040_BL_REG_FW_VER,
|
|
||||||
RP2040_BL_REG_BL_VER,
|
|
||||||
RP2040_BL_REG_BL_STATE,
|
|
||||||
RP2040_BL_REG_BL_CTRL
|
|
||||||
};
|
|
||||||
|
|
||||||
enum {
|
|
||||||
RP2040_INPUT_BUTTON_HOME = 0,
|
|
||||||
RP2040_INPUT_BUTTON_MENU,
|
|
||||||
RP2040_INPUT_BUTTON_START,
|
|
||||||
RP2040_INPUT_BUTTON_ACCEPT,
|
|
||||||
RP2040_INPUT_BUTTON_BACK,
|
|
||||||
RP2040_INPUT_FPGA_CDONE,
|
|
||||||
RP2040_INPUT_BATTERY_CHARGING,
|
|
||||||
RP2040_INPUT_BUTTON_SELECT,
|
|
||||||
RP2040_INPUT_JOYSTICK_LEFT,
|
|
||||||
RP2040_INPUT_JOYSTICK_PRESS,
|
|
||||||
RP2040_INPUT_JOYSTICK_DOWN,
|
|
||||||
RP2040_INPUT_JOYSTICK_UP,
|
|
||||||
RP2040_INPUT_JOYSTICK_RIGHT
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef void (*rp2040_intr_t)();
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int i2c_bus;
|
|
||||||
int i2c_address;
|
|
||||||
int pin_interrupt;
|
|
||||||
xQueueHandle queue;
|
|
||||||
rp2040_intr_t _intr_handler;
|
|
||||||
TaskHandle_t _intr_task_handle;
|
|
||||||
xSemaphoreHandle _intr_trigger;
|
|
||||||
uint8_t _gpio_direction;
|
|
||||||
uint8_t _gpio_value;
|
|
||||||
uint8_t _fw_version;
|
|
||||||
} RP2040;
|
|
||||||
|
|
||||||
typedef struct _rp2040_input_message {
|
|
||||||
uint8_t input;
|
|
||||||
bool state;
|
|
||||||
} rp2040_input_message_t;
|
|
||||||
|
|
||||||
esp_err_t rp2040_init(RP2040* device);
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_firmware_version(RP2040* device, uint8_t* version);
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_bootloader_version(RP2040* device, uint8_t* version);
|
|
||||||
esp_err_t rp2040_get_bootloader_state(RP2040* device, uint8_t* state);
|
|
||||||
esp_err_t rp2040_set_bootloader_ctrl(RP2040* device, uint8_t action);
|
|
||||||
esp_err_t rp2040_reboot_to_bootloader(RP2040* device);
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_gpio_dir(RP2040* device, uint8_t gpio, bool* direction);
|
|
||||||
esp_err_t rp2040_set_gpio_dir(RP2040* device, uint8_t gpio, bool direction);
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_gpio_value(RP2040* device, uint8_t gpio, bool* value);
|
|
||||||
esp_err_t rp2040_set_gpio_value(RP2040* device, uint8_t gpio, bool value);
|
|
||||||
|
|
||||||
esp_err_t rp2040_get_lcd_backlight(RP2040* device, uint8_t* brightness);
|
|
||||||
esp_err_t rp2040_set_lcd_backlight(RP2040* device, uint8_t brightness);
|
|
||||||
|
|
||||||
esp_err_t rp2040_set_fpga(RP2040* device, bool enabled);
|
|
||||||
|
|
||||||
esp_err_t rp2040_read_buttons(RP2040* device, uint16_t* value);
|
|
1
components/mch2022-rp2040
Submodule
1
components/mch2022-rp2040
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 8ade81bcb3619cb57168a6c980726942e751873a
|
|
@ -1,4 +1,4 @@
|
||||||
idf_component_register(
|
idf_component_register(
|
||||||
SRCS "main.c" "menu.c" "fpga_test.c" "pax_keyboard.c" "system_wrapper.c" "appfs_wrapper.c" "graphics_wrapper.c" "settings.c" "wifi_connection.c"
|
SRCS "main.c" "menu.c" "fpga_test.c" "pax_keyboard.c" "system_wrapper.c" "appfs_wrapper.c" "graphics_wrapper.c" "settings.c" "wifi_connection.c" "rp2040_updater.c"
|
||||||
INCLUDE_DIRS "." "include"
|
INCLUDE_DIRS "." "include"
|
||||||
)
|
)
|
||||||
|
|
13
main/include/rp2040_updater.h
Normal file
13
main/include/rp2040_updater.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sdkconfig.h>
|
||||||
|
#include <esp_system.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
#include <freertos/queue.h>
|
||||||
|
#include "pax_gfx.h"
|
||||||
|
#include "ili9341.h"
|
||||||
|
|
||||||
|
|
||||||
|
void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341, uint8_t* framebuffer);
|
394
main/main.c
394
main/main.c
|
@ -18,6 +18,7 @@
|
||||||
#include "driver_framebuffer.h"
|
#include "driver_framebuffer.h"
|
||||||
|
|
||||||
#include "rp2040.h"
|
#include "rp2040.h"
|
||||||
|
#include "rp2040bl.h"
|
||||||
|
|
||||||
#include "fpga_test.h"
|
#include "fpga_test.h"
|
||||||
|
|
||||||
|
@ -27,11 +28,10 @@
|
||||||
#include "appfs_wrapper.h"
|
#include "appfs_wrapper.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "wifi_connection.h"
|
#include "wifi_connection.h"
|
||||||
|
#include "rp2040_updater.h"
|
||||||
|
|
||||||
#include "ws2812.h"
|
#include "ws2812.h"
|
||||||
|
|
||||||
#include "rp2040firmware.h"
|
|
||||||
|
|
||||||
#include "esp32/rom/crc.h"
|
#include "esp32/rom/crc.h"
|
||||||
|
|
||||||
static const char *TAG = "main";
|
static const char *TAG = "main";
|
||||||
|
@ -261,150 +261,7 @@ void menu_wifi_settings(xQueueHandle buttonQueue, pax_buf_t* pax_buffer, ILI9341
|
||||||
menu_free(menu);
|
menu_free(menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
void flush_stdin() {
|
|
||||||
uint8_t* data = malloc(2048);
|
|
||||||
uart_read_bytes(0, data, 2048 - 1, 20 / portTICK_PERIOD_MS);
|
|
||||||
free(data);
|
|
||||||
/*while (true) {
|
|
||||||
char in = getc(stdin);
|
|
||||||
if (in == 0xFF) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
bool 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);
|
|
||||||
|
|
||||||
/*uint8_t index = 0;
|
|
||||||
uint32_t timeout_counter = 0;
|
|
||||||
while (index < len) {
|
|
||||||
char in = getc(stdin);
|
|
||||||
if (in != 0xFF) {
|
|
||||||
buffer[index] = in;
|
|
||||||
index++;
|
|
||||||
} else {
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
timeout_counter+=10;
|
|
||||||
if (timeout_counter > timeout) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;*/
|
|
||||||
}
|
|
||||||
|
|
||||||
bool rp2040_bl_sync() {
|
|
||||||
flush_stdin();
|
|
||||||
char command[] = "SYNC";
|
|
||||||
uart_write_bytes(0, command, 4);
|
|
||||||
uint8_t rx_buffer[4 * 6];
|
|
||||||
read_stdin(rx_buffer, sizeof(rx_buffer), 1000);
|
|
||||||
if (memcmp(rx_buffer, "PICO", 4) != 0) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool rp2040_bl_get_info(uint32_t* flash_start, uint32_t* flash_size, uint32_t* erase_size, uint32_t* write_size, uint32_t* max_data_len) {
|
|
||||||
flush_stdin();
|
|
||||||
char command[] = "INFO";
|
|
||||||
uart_write_bytes(0, command, 4);
|
|
||||||
/*printf("INFO");*/
|
|
||||||
uint8_t rx_buffer[4 * 6];
|
|
||||||
read_stdin(rx_buffer, sizeof(rx_buffer), 1000);
|
|
||||||
if (memcmp(rx_buffer, "OKOK", 4) != 0) return false;
|
|
||||||
memcpy((uint8_t*) flash_start, &rx_buffer[4 * 1], 4);
|
|
||||||
memcpy((uint8_t*) flash_size, &rx_buffer[4 * 2], 4);
|
|
||||||
memcpy((uint8_t*) erase_size, &rx_buffer[4 * 3], 4);
|
|
||||||
memcpy((uint8_t*) write_size, &rx_buffer[4 * 4], 4);
|
|
||||||
memcpy((uint8_t*) max_data_len, &rx_buffer[4 * 5], 4);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool rp2040_bl_erase(uint32_t address, uint32_t length) {
|
|
||||||
flush_stdin();
|
|
||||||
char command[12];
|
|
||||||
snprintf(command, 5, "ERAS");
|
|
||||||
memcpy(command + 4, (char*) &address, 4);
|
|
||||||
memcpy(command + 8, (char*) &length, 4);
|
|
||||||
uart_write_bytes(0, command, sizeof(command));
|
|
||||||
/*printf("ERAS");
|
|
||||||
uint8_t* addres_u8 = (uint8_t*) &address;
|
|
||||||
putc(addres_u8[0], stdout);
|
|
||||||
putc(addres_u8[1], stdout);
|
|
||||||
putc(addres_u8[2], stdout);
|
|
||||||
putc(addres_u8[3], stdout);
|
|
||||||
uint8_t* length_u8 = (uint8_t*) &length;
|
|
||||||
putc(length_u8[0], stdout);
|
|
||||||
putc(length_u8[1], stdout);
|
|
||||||
putc(length_u8[2], stdout);
|
|
||||||
putc(length_u8[3], stdout);*/
|
|
||||||
uint8_t rx_buffer[4];
|
|
||||||
read_stdin(rx_buffer, sizeof(rx_buffer), 10000);
|
|
||||||
if (memcmp(rx_buffer, "OKOK", 4) != 0) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool rp2040_bl_write(uint32_t address, uint32_t length, uint8_t* data, uint32_t* crc) {
|
|
||||||
flush_stdin();
|
|
||||||
char command[12];
|
|
||||||
snprintf(command, 5, "WRIT");
|
|
||||||
memcpy(command + 4, (char*) &address, 4);
|
|
||||||
memcpy(command + 8, (char*) &length, 4);
|
|
||||||
uart_write_bytes(0, command, sizeof(command));
|
|
||||||
/*printf("WRIT");
|
|
||||||
uint8_t* addres_u8 = (uint8_t*) &address;
|
|
||||||
putc(addres_u8[0], stdout);
|
|
||||||
putc(addres_u8[1], stdout);
|
|
||||||
putc(addres_u8[2], stdout);
|
|
||||||
putc(addres_u8[3], stdout);
|
|
||||||
uint8_t* length_u8 = (uint8_t*) &length;
|
|
||||||
putc(length_u8[0], stdout);
|
|
||||||
putc(length_u8[1], stdout);
|
|
||||||
putc(length_u8[2], stdout);
|
|
||||||
putc(length_u8[3], stdout);
|
|
||||||
|
|
||||||
for (uint32_t index = 0; index < length; index++) {
|
|
||||||
putc(data[index], stdout);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
uart_write_bytes(0, data, length);
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t rx_buffer[8];
|
|
||||||
read_stdin(rx_buffer, sizeof(rx_buffer), 10000);
|
|
||||||
if (memcmp(rx_buffer, "OKOK", 4) != 0) return false;
|
|
||||||
|
|
||||||
memcpy((uint8_t*) crc, &rx_buffer[4 * 1], 4);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool rp2040_bl_seal(uint32_t addr, uint32_t vtor, uint32_t length, uint32_t crc) {
|
|
||||||
flush_stdin();
|
|
||||||
char command[20];
|
|
||||||
snprintf(command, 5, "SEAL");
|
|
||||||
memcpy(command + 4, (char*) &addr, 4);
|
|
||||||
memcpy(command + 8, (char*) &vtor, 4);
|
|
||||||
memcpy(command + 12, (char*) &length, 4);
|
|
||||||
memcpy(command + 16, (char*) &crc, 4);
|
|
||||||
uart_write_bytes(0, command, sizeof(command));
|
|
||||||
uint8_t rx_buffer[4];
|
|
||||||
read_stdin(rx_buffer, sizeof(rx_buffer), 10000);
|
|
||||||
if (memcmp(rx_buffer, "OKOK", 4) != 0) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool rp2040_bl_go(uint32_t vtor) {
|
|
||||||
flush_stdin();
|
|
||||||
char command[8];
|
|
||||||
snprintf(command, 5, "GOGO");
|
|
||||||
memcpy(command + 4, (char*) &vtor, 4);
|
|
||||||
uart_write_bytes(0, command, sizeof(command));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void app_main(void) {
|
void app_main(void) {
|
||||||
flush_stdin();
|
|
||||||
esp_err_t res;
|
esp_err_t res;
|
||||||
|
|
||||||
/* Initialize memory */
|
/* Initialize memory */
|
||||||
|
@ -474,252 +331,7 @@ void app_main(void) {
|
||||||
uint8_t ledBuffer[15] = {50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0};
|
uint8_t ledBuffer[15] = {50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0, 50, 0, 0};
|
||||||
ws2812_send_data(ledBuffer, sizeof(ledBuffer));
|
ws2812_send_data(ledBuffer, sizeof(ledBuffer));
|
||||||
|
|
||||||
uint8_t fw_version;
|
rp2040_updater(rp2040, pax_buffer, ili9341, framebuffer);
|
||||||
if (rp2040_get_firmware_version(rp2040, &fw_version) != ESP_OK) {
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "RP2040 FW VERSION READ FAILED");
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fw_version == 0xFF) {
|
|
||||||
// RP2040 is in bootloader mode
|
|
||||||
char buffer[255] = {0};
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Updating RP2040...");
|
|
||||||
vTaskDelay(1 / portTICK_PERIOD_MS);
|
|
||||||
uint8_t bl_version;
|
|
||||||
if (rp2040_get_bootloader_version(rp2040, &bl_version) != ESP_OK) {
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Communication error (1)");
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
if (bl_version != 0x01) {
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Unknown BL version");
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(uart_driver_install(0, 2048, 0, 0, NULL, 0));
|
|
||||||
uart_config_t uart_config = {
|
|
||||||
.baud_rate = 115200,
|
|
||||||
.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));
|
|
||||||
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Waiting for bootloader...");
|
|
||||||
while (true) {
|
|
||||||
vTaskDelay(1 / portTICK_PERIOD_MS);
|
|
||||||
uint8_t bl_state;
|
|
||||||
if (rp2040_get_bootloader_state(rp2040, &bl_state) != ESP_OK) {
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Communication error (2)");
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
if (bl_state == 0xB0) {
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Bootloader ready");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (bl_state > 0xB0) {
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Unknown BL state");
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Sync to BL...");
|
|
||||||
char rx_buffer[16];
|
|
||||||
uint8_t rx_buffer_pos = 0;
|
|
||||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
||||||
while (true) {
|
|
||||||
//printf("SYNC");
|
|
||||||
|
|
||||||
//uint8_t rx_buffer[4];
|
|
||||||
//read_stdin(rx_buffer, sizeof(rx_buffer), 1000);
|
|
||||||
|
|
||||||
/*char in = 0xFF;
|
|
||||||
do {
|
|
||||||
in = getc(stdin);
|
|
||||||
if (in != 0xFF) {
|
|
||||||
rx_buffer[rx_buffer_pos] = in;
|
|
||||||
rx_buffer_pos++;
|
|
||||||
}
|
|
||||||
} while ((in != 0xFF) && (rx_buffer_pos < sizeof(rx_buffer)));*/
|
|
||||||
|
|
||||||
//if (memcmp(rx_buffer, "PICO", 4) == 0) {
|
|
||||||
// break;
|
|
||||||
//}
|
|
||||||
|
|
||||||
/*if (rx_buffer_pos >= 4) {
|
|
||||||
for (uint8_t i = 0; i < sizeof(rx_buffer) - 1; i++) {
|
|
||||||
rx_buffer[i] = rx_buffer[i+1];
|
|
||||||
if (memcmp(rx_buffer, "PICO", 4) == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rx_buffer_pos--;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
flush_stdin();
|
|
||||||
if (rp2040_bl_sync()) break;
|
|
||||||
|
|
||||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Synced to BL!");
|
|
||||||
|
|
||||||
/*char in = 0xFF;
|
|
||||||
do {
|
|
||||||
in = getc(stdin);
|
|
||||||
} while (in != 0xFF);*/
|
|
||||||
flush_stdin();
|
|
||||||
|
|
||||||
uint32_t flash_start = 0, flash_size = 0, erase_size = 0, write_size = 0, max_data_len = 0;
|
|
||||||
|
|
||||||
bool success = rp2040_bl_get_info(&flash_start, &flash_size, &erase_size, &write_size, &max_data_len);
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "BL INFO FAIL");
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
pax_noclip(pax_buffer);
|
|
||||||
pax_background(pax_buffer, 0xCCCCCC);
|
|
||||||
char message[64];
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
snprintf(message, sizeof(message) - 1, "Flash start: 0x%08X", flash_start);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "Flash size : 0x%08X", flash_size);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*1, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "Erase size : 0x%08X", erase_size);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*2, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "Write size : 0x%08X", write_size);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*3, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "Max data ln: 0x%08X", max_data_len);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*4, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
|
|
||||||
bool eraseSuccess = rp2040_bl_erase(flash_start, flash_size - erase_size);
|
|
||||||
snprintf(message, sizeof(message) - 1, "Erase : %s", eraseSuccess ? "YES" : "NO");
|
|
||||||
pax_draw_text(pax_buffer, eraseSuccess ? 0xFF000000 : 0xFFFF0000, NULL, 18, 0, 20*5, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
||||||
|
|
||||||
if (!eraseSuccess) {
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t position = 0;
|
|
||||||
uint32_t txSize = write_size;
|
|
||||||
uint8_t* txBuffer = malloc(write_size);
|
|
||||||
|
|
||||||
uint32_t blockCrc = 0;
|
|
||||||
uint32_t totalCrc = 0;
|
|
||||||
uint32_t totalLength = 0;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
if ((sizeof(mch2022_firmware_bin) - position) < txSize) {
|
|
||||||
txSize = sizeof(mch2022_firmware_bin) - position;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (txSize == 0) break;
|
|
||||||
|
|
||||||
pax_noclip(pax_buffer);
|
|
||||||
pax_background(pax_buffer, 0xCCCCCC);
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
snprintf(message, sizeof(message) - 1, "Write to : 0x%08X", 0x10010000 + position);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "Write len: 0x%08X", txSize);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*1, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
uint32_t checkCrc = 0;
|
|
||||||
memset(txBuffer, 0, write_size);
|
|
||||||
memcpy(txBuffer, &mch2022_firmware_bin[position], txSize);
|
|
||||||
blockCrc = crc32_le(0, txBuffer, write_size);
|
|
||||||
totalCrc = crc32_le(totalCrc, txBuffer, write_size);
|
|
||||||
totalLength += write_size;
|
|
||||||
bool writeSuccess = rp2040_bl_write(0x10010000 + position, write_size, txBuffer, &checkCrc);
|
|
||||||
snprintf(message, sizeof(message) - 1, "Write res: %s", writeSuccess ? "YES" : "NO");
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*2, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "CRC : %08X", blockCrc);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*3, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "CRC CHECK: %08X", checkCrc);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*4, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "CRC OK? : %s", (blockCrc == checkCrc) ? "YES" : "NO");
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*5, message);
|
|
||||||
snprintf(message, sizeof(message) - 1, "CRC TOTAL: %08X", totalCrc);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*7, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
if (writeSuccess && (blockCrc == checkCrc)) {
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
position += txSize;
|
|
||||||
} else {
|
|
||||||
while (!rp2040_bl_sync()) {
|
|
||||||
flush_stdin();
|
|
||||||
vTaskDelay(20 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(txBuffer);
|
|
||||||
|
|
||||||
pax_noclip(pax_buffer);
|
|
||||||
pax_background(pax_buffer, 0xCCCCCC);
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
snprintf(message, sizeof(message) - 1, "Sealing...");
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
|
|
||||||
bool sealRes = rp2040_bl_seal(0x10010000, 0x10010000, totalLength, totalCrc);
|
|
||||||
|
|
||||||
snprintf(message, sizeof(message) - 1, "Result: %s", sealRes ? "OK" : "FAIL");
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*1, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
|
|
||||||
if (sealRes) {
|
|
||||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
||||||
pax_noclip(pax_buffer);
|
|
||||||
pax_background(pax_buffer, 0xCCCCCC);
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
snprintf(message, sizeof(message) - 1, "Waiting for reset...");
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
rp2040_bl_go(0x10010000);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pax_noclip(pax_buffer);
|
|
||||||
pax_background(pax_buffer, 0xCCCCCC);
|
|
||||||
char message[64];
|
|
||||||
memset(message, 0, sizeof(message));
|
|
||||||
snprintf(message, sizeof(message) - 1, "RP2040 firmware: 0x%02X", fw_version);
|
|
||||||
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
|
||||||
ili9341_write(ili9341, framebuffer);
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
||||||
|
|
||||||
/*while (true) {
|
|
||||||
fpga_test(ili9341, ice40, rp2040->queue);
|
|
||||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*while (true) {
|
|
||||||
uint16_t state;
|
|
||||||
rp2040_read_buttons(rp2040, &state);
|
|
||||||
printf("Button state: %04X\n", state);
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
ledBuffer[1] = 255;
|
|
||||||
ws2812_send_data(ledBuffer, sizeof(ledBuffer));
|
|
||||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
||||||
ledBuffer[1] = 0;
|
|
||||||
ledBuffer[0] = 255;
|
|
||||||
ws2812_send_data(ledBuffer, sizeof(ledBuffer));
|
|
||||||
fpga_test(ili9341, ice40, rp2040->queue);
|
|
||||||
ledBuffer[0] = 0;
|
|
||||||
ws2812_send_data(ledBuffer, sizeof(ledBuffer));
|
|
||||||
}*/
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
menu_action_t menu_action;
|
menu_action_t menu_action;
|
||||||
|
|
247
main/rp2040_updater.c
Normal file
247
main/rp2040_updater.c
Normal file
|
@ -0,0 +1,247 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sdkconfig.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
#include <freertos/queue.h>
|
||||||
|
#include <esp_system.h>
|
||||||
|
#include <esp_err.h>
|
||||||
|
#include <esp_log.h>
|
||||||
|
#include "driver/uart.h"
|
||||||
|
#include "hardware.h"
|
||||||
|
#include "managed_i2c.h"
|
||||||
|
#include "pax_gfx.h"
|
||||||
|
#include "rp2040.h"
|
||||||
|
#include "rp2040bl.h"
|
||||||
|
#include "rp2040_firmware.h"
|
||||||
|
#include "system_wrapper.h"
|
||||||
|
#include "graphics_wrapper.h"
|
||||||
|
#include "esp32/rom/crc.h"
|
||||||
|
|
||||||
|
void rp2040_updater(RP2040* rp2040, pax_buf_t* pax_buffer, ILI9341* ili9341, uint8_t* framebuffer) {
|
||||||
|
char message[64];
|
||||||
|
|
||||||
|
uint8_t fw_version;
|
||||||
|
if (rp2040_get_firmware_version(rp2040, &fw_version) != ESP_OK) {
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xa85a32);
|
||||||
|
snprintf(message, sizeof(message) - 1, "RP2040 error");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Failed to read firmware version");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xCCCCCC);
|
||||||
|
memset(message, 0, sizeof(message));
|
||||||
|
snprintf(message, sizeof(message) - 1, "RP2040 firmware: 0x%02X", fw_version);
|
||||||
|
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
if (fw_version < 0x01) { // Update required
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0x325aa8);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Updating RP2040...");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Starting bootloader");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
rp2040_reboot_to_bootloader(rp2040);
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fw_version == 0xFF) { // RP2040 is in bootloader mode
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0x325aa8);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Updating RP2040...");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
|
||||||
|
uint8_t bl_version;
|
||||||
|
if (rp2040_get_bootloader_version(rp2040, &bl_version) != ESP_OK) {
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xa85a32);
|
||||||
|
snprintf(message, sizeof(message) - 1, "RP2040 update failed");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Communication error (1)");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
if (bl_version != 0x01) {
|
||||||
|
pax_background(pax_buffer, 0xa85a32);
|
||||||
|
snprintf(message, sizeof(message) - 1, "RP2040 update failed");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Unsupported bootloader version");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
rp2040_bl_install_uart();
|
||||||
|
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0x325aa8);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Updating RP2040...");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Waiting for bootloader");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
vTaskDelay(1 / portTICK_PERIOD_MS);
|
||||||
|
uint8_t bl_state;
|
||||||
|
if (rp2040_get_bootloader_state(rp2040, &bl_state) != ESP_OK) {
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xa85a32);
|
||||||
|
snprintf(message, sizeof(message) - 1, "RP2040 update failed");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Communication error (2)");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
if (bl_state == 0xB0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (bl_state > 0xB0) {
|
||||||
|
graphics_task(pax_buffer, ili9341, framebuffer, NULL, "Unknown BL state");
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0x325aa8);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Updating RP2040...");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Waiting for bootloader sync");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
|
||||||
|
char rx_buffer[16];
|
||||||
|
uint8_t rx_buffer_pos = 0;
|
||||||
|
memset(rx_buffer, 0, sizeof(rx_buffer));
|
||||||
|
while (true) {
|
||||||
|
if (rp2040_bl_sync()) break;
|
||||||
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t flash_start = 0, flash_size = 0, erase_size = 0, write_size = 0, max_data_len = 0;
|
||||||
|
|
||||||
|
bool success = rp2040_bl_get_info(&flash_start, &flash_size, &erase_size, &write_size, &max_data_len);
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xa85a32);
|
||||||
|
snprintf(message, sizeof(message) - 1, "RP2040 update failed");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Failed to read information");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0x325aa8);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Updating RP2040...");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Erasing flash");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
|
||||||
|
uint32_t erase_length = sizeof(mch2022_firmware_bin);
|
||||||
|
erase_length = erase_length + erase_size - (erase_length % erase_size); // Round up to erase size
|
||||||
|
|
||||||
|
if (erase_length > flash_size - erase_size) {
|
||||||
|
erase_length = flash_size - erase_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool eraseSuccess = rp2040_bl_erase(flash_start, erase_length);
|
||||||
|
|
||||||
|
if (!eraseSuccess) {
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xa85a32);
|
||||||
|
snprintf(message, sizeof(message) - 1, "RP2040 update failed");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Failed to erase flash");
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t position = 0;
|
||||||
|
uint32_t txSize = write_size;
|
||||||
|
uint8_t* txBuffer = malloc(write_size);
|
||||||
|
|
||||||
|
uint32_t blockCrc = 0;
|
||||||
|
uint32_t totalCrc = 0;
|
||||||
|
uint32_t totalLength = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if ((sizeof(mch2022_firmware_bin) - position) < txSize) {
|
||||||
|
txSize = sizeof(mch2022_firmware_bin) - position;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (txSize == 0) break;
|
||||||
|
|
||||||
|
uint8_t percentage = position * 100 / sizeof(mch2022_firmware_bin);
|
||||||
|
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0x325aa8);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Updating RP2040... %u%%", percentage);
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 18, 0, 20*0, message);
|
||||||
|
snprintf(message, sizeof(message) - 1, "Writing @ 0x%08X", 0x10010000 + position);
|
||||||
|
pax_draw_text(pax_buffer, 0xFFFFFFFF, NULL, 12, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
|
||||||
|
uint32_t checkCrc = 0;
|
||||||
|
memset(txBuffer, 0, write_size);
|
||||||
|
memcpy(txBuffer, &mch2022_firmware_bin[position], txSize);
|
||||||
|
blockCrc = crc32_le(0, txBuffer, write_size);
|
||||||
|
totalCrc = crc32_le(totalCrc, txBuffer, write_size);
|
||||||
|
totalLength += write_size;
|
||||||
|
bool writeSuccess = rp2040_bl_write(0x10010000 + position, write_size, txBuffer, &checkCrc);
|
||||||
|
if (writeSuccess && (blockCrc == checkCrc)) {
|
||||||
|
position += txSize;
|
||||||
|
} else {
|
||||||
|
while (!rp2040_bl_sync()) {
|
||||||
|
vTaskDelay(20 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(txBuffer);
|
||||||
|
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xCCCCCC);
|
||||||
|
memset(message, 0, sizeof(message));
|
||||||
|
snprintf(message, sizeof(message) - 1, "Sealing...");
|
||||||
|
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
|
||||||
|
bool sealRes = rp2040_bl_seal(0x10010000, 0x10010000, totalLength, totalCrc);
|
||||||
|
|
||||||
|
snprintf(message, sizeof(message) - 1, "Result: %s", sealRes ? "OK" : "FAIL");
|
||||||
|
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*1, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
|
||||||
|
if (sealRes) {
|
||||||
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||||
|
pax_noclip(pax_buffer);
|
||||||
|
pax_background(pax_buffer, 0xCCCCCC);
|
||||||
|
memset(message, 0, sizeof(message));
|
||||||
|
snprintf(message, sizeof(message) - 1, "Waiting for reset...");
|
||||||
|
pax_draw_text(pax_buffer, 0xFF000000, NULL, 18, 0, 20*0, message);
|
||||||
|
ili9341_write(ili9341, framebuffer);
|
||||||
|
rp2040_bl_go(0x10010000);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue