Move RP2040 driver into separate repository

This commit is contained in:
Renze Nicolai 2022-05-11 00:17:07 +02:00
parent ecd1389301
commit 6814193934
5 changed files with 6 additions and 343 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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"
) )

View file

@ -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, &reg_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);
}

View file

@ -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);

@ -0,0 +1 @@
Subproject commit 8ade81bcb3619cb57168a6c980726942e751873a