Skip to content

Commit

Permalink
wip: esp32 firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
heiso committed Dec 18, 2024
1 parent 6d3c96b commit b27b441
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 450 deletions.
13 changes: 12 additions & 1 deletion .vscode/macrolev.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@
"gpio.h": "c",
"drv2605l.h": "c",
"main.h": "c",
"adc_oneshot.h": "c"
"adc_oneshot.h": "c",
"__bit_reference": "c",
"__node_handle": "c",
"bitset": "c",
"deque": "c",
"limits": "c",
"optional": "c",
"ratio": "c",
"regex": "c",
"scoped_allocator": "c",
"tuple": "c",
"vector": "c"
}
}
}
1 change: 1 addition & 0 deletions firmware/esp32-s3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
managed_components
1 change: 1 addition & 0 deletions firmware/esp32-s3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

set(COMPONENTS main)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(macrolev)
39 changes: 39 additions & 0 deletions firmware/esp32-s3/dependencies.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
dependencies:
espressif/esp_tinyusb:
component_hash: 4878831be091116ec8d0e5eaedcae54a5e9866ebf15a44ef101886fd42c0b91f
dependencies:
- name: idf
require: private
version: '>=5.0'
- name: espressif/tinyusb
registry_url: https://components.espressif.com
require: public
version: '>=0.14.2'
source:
registry_url: https://components.espressif.com/
type: service
version: 1.5.0
espressif/tinyusb:
component_hash: 8a9e23b8cdc733b51fed357979139f5ae63a2fed3ce4e7c41d505f685f7d741a
dependencies:
- name: idf
require: private
version: '>=5.0'
source:
registry_url: https://components.espressif.com
type: service
targets:
- esp32s2
- esp32s3
- esp32p4
version: 0.17.0~1
idf:
source:
type: idf
version: 5.3.2
direct_dependencies:
- espressif/esp_tinyusb
- idf
manifest_hash: a9d41f37d6b17d81425b0e9bf1b860eb5b14f03e2028a2443db9211783edd1ab
target: esp32s3
version: 2.0.0
5 changes: 3 additions & 2 deletions firmware/esp32-s3/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
idf_component_register(
SRCS "main.c" "keyboard.c" "config.c" "hid.c"
INCLUDE_DIRS "." "../../common/tinyusb/src"
SRCS "main.c" "keyboard.c" "config.c" "hid.c" "usb_descriptors.c"
INCLUDE_DIRS "."
PRIV_REQUIRES esp_adc esp_timer esp_driver_gpio
)
10 changes: 5 additions & 5 deletions firmware/esp32-s3/main/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const struct user_config keyboard_default_user_config = {

// {adc_channel, amux_channel}
const uint8_t channels_by_row_col[MATRIX_ROWS][MATRIX_COLS][2] = {
{{0, 10}, {0, 8}, {0, 7}, {0, 5}, {1, 9}, {1, 8}, {1, 6}, {2, 10}, {2, 9}, {2, 7}, {2, 6}, {3, 9}, {3, 8}, {3, 6}, {4, 2}},
{{0, 11}, {0, 9}, {0, 6}, {0, 4}, {1, 10}, {1, 7}, {1, 5}, {2, 11}, {2, 8}, {2, 5}, {2, 4}, {3, 10}, {3, 7}, {3, 5}, {4, 1}},
{{0, 12}, {0, 14}, {0, 2}, {1, 11}, {1, 14}, {1, 1}, {1, 4}, {2, 12}, {2, 15}, {2, 3}, {3, 11}, {3, 14}, {3, 1}, {3, 4}, {XXXX, XXXX}},
{{XXXX, XXXX}, {0, 13}, {0, 0}, {0, 3}, {1, 13}, {1, 15}, {1, 2}, {1, 3}, {2, 14}, {2, 0}, {2, 2}, {3, 12}, {3, 15}, {3, 3}, {XXXX, XXXX}},
{{XXXX, XXXX}, {0, 15}, {0, 1}, {1, 12}, {XXXX, XXXX}, {1, 0}, {XXXX, XXXX}, {2, 13}, {XXXX, XXXX}, {2, 1}, {3, 13}, {XXXX, XXXX}, {3, 0}, {3, 2}, {4, 0}},
{{0, 1}, {0, 0}, {0, 13}, {0, 12}, {1, 0}, {1, 12}, {2, 1}, {2, 0}, {2, 12}, {3, 1}, {3, 0}, {3, 12}, {4, 1}, {4, 0}, {4, 13}},
{{0, 2}, {0, 7}, {0, 10}, {1, 2}, {1, 7}, {1, 10}, {2, 3}, {2, 7}, {2, 10}, {3, 3}, {3, 7}, {3, 11}, {4, 4}, {4, 8}, {4, 12}},
{{0, 3}, {0, 6}, {0, 11}, {1, 3}, {1, 6}, {1, 11}, {2, 4}, {2, 6}, {2, 11}, {3, 4}, {3, 8}, {4, 2}, {4, 5}, {4, 7}, {XXXX, XXXX}},
{{XXXX, XXXX}, {0, 5}, {0, 8}, {1, 1}, {1, 5}, {1, 8}, {2, 2}, {2, 5}, {2, 8}, {3, 2}, {3, 6}, {3, 9}, {4, 3}, {4, 10}, {XXXX, XXXX}},
{{XXXX, XXXX}, {0, 4}, {0, 9}, {1, 4}, {XXXX, XXXX}, {1, 9}, {XXXX, XXXX}, {2, 9}, {XXXX, XXXX}, {3, 5}, {3, 10}, {XXXX, XXXX}, {4, 6}, {4, 9}, {4, 11}},
};
11 changes: 10 additions & 1 deletion firmware/esp32-s3/main/hid.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include "tusb.h"
#include "class/hid/hid_device.h"
#include "esp_log.h"
#include "tinyusb.h"
#include "usb_descriptors.h"
#include <hid.h>
#include <keyboard.h>
#include <stdlib.h>


// const static char *TAG = "MACROLEV";

extern uint8_t const desc_ms_os_20[];
extern struct key keyboard_keys[ADC_CHANNEL_COUNT][AMUX_CHANNEL_COUNT];
extern struct user_config keyboard_user_config;
Expand Down Expand Up @@ -74,6 +79,8 @@ void hid_press_key(struct key *key, uint8_t layer) {
default:
break;
}

// ESP_LOGI(TAG, "status: %x - Pressed", key->actuation.status);
}

void hid_release_key(struct key *key, uint8_t layer) {
Expand Down Expand Up @@ -105,6 +112,8 @@ void hid_release_key(struct key *key, uint8_t layer) {
default:
break;
}

// ESP_LOGI(TAG, "status: %x - Released", key->actuation.status);
}

// Invoked when received SET_PROTOCOL request
Expand Down
4 changes: 4 additions & 0 deletions firmware/esp32-s3/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_tinyusb: "^1.1"
idf: "^5.0"
12 changes: 2 additions & 10 deletions firmware/esp32-s3/main/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
#include <class/hid/hid.h>
#include <stdlib.h>

const static char *TAG = "MACROLEV";
// const static char *TAG = "MACROLEV";

struct key keyboard_keys[ADC_CHANNEL_COUNT][AMUX_CHANNEL_COUNT] = {0};

struct user_config keyboard_user_config = {0};

uint32_t keyboard_last_cycle_duration = 0;

static uint8_t key_triggered = 0;

uint8_t get_bitmask_for_modifier(uint8_t keycode) {
Expand Down Expand Up @@ -89,9 +87,6 @@ uint8_t update_key_state(struct key *key) {

// Get a reading
state.value = keyboard_read_adc();
if (key->row == 0 && key->column == 0) {
ESP_LOGI(TAG, "Row: %d, Col: %d, Value: %x", key->row, key->column, state.value);
}

if (key->calibration.cycles_count < CALIBRATION_CYCLES) {
// Calibrate idle value
Expand Down Expand Up @@ -264,7 +259,7 @@ void update_key(struct key *key) {
}

void keyboard_init_keys() {
// keyboard_read_config();
keyboard_read_config();

for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
Expand All @@ -276,7 +271,6 @@ void keyboard_init_keys() {
}

void keyboard_task() {
uint32_t started_at = keyboard_get_time();
key_triggered = 0;

for (uint8_t amux_channel = 0; amux_channel < AMUX_CHANNEL_COUNT; amux_channel++) {
Expand Down Expand Up @@ -315,6 +309,4 @@ void keyboard_task() {
}
}
}

keyboard_last_cycle_duration = keyboard_get_time() - started_at;
}
52 changes: 36 additions & 16 deletions firmware/esp32-s3/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,44 +130,64 @@ void gpio_init() {
}

void app_main(void) {
gpio_init();
adc_init();
keyboard_init_keys();
hid_init();
// vTaskDelay(pdMS_TO_TICKS(1000));
// gpio_reset_pin(GPIO_SELECT_0);
// gpio_set_direction(GPIO_SELECT_0, GPIO_MODE_OUTPUT);
// while (1) {
// gpio_set_level(GPIO_SELECT_0, 1);
// vTaskDelay(pdMS_TO_TICKS(100));
// }

while (1) {
keyboard_task();
// hid_task();
hid_task();

// for (uint8_t amux_channel = 0; amux_channel < AMUX_CHANNEL_COUNT; amux_channel++) {
// for (uint8_t i = 0; i < AMUX_SELECT_PINS_COUNT; i++) {
// gpio_set_level(amux_select_pins[i], (amux_channel >> i) & 1);
// }

// for (uint8_t adc_channel = 0; adc_channel < ADC_CHANNEL_COUNT; adc_channel++) {
// int raw_value = 0;
// adc_oneshot_read(adc_handle, adc_channels[adc_channel], &raw_value);
// }
// }

vTaskDelay(pdMS_TO_TICKS(10));
}
}

void keyboard_read_config() {
memcpy(&keyboard_user_config, &keyboard_default_user_config, sizeof(keyboard_user_config));
}

uint8_t keyboard_write_config(uint8_t *buffer, uint16_t offset, uint16_t size) {
return 1;
void keyboard_select_adc(uint8_t _adc_channel) {
adc_channel = _adc_channel;
}

void keyboard_select_amux(uint8_t amux_channel) {
// TODO: set GPIOs at the same time using bitmap on register
for (uint8_t i = 0; i < AMUX_SELECT_PINS_COUNT; i++) {
gpio_set_level(amux_select_pins[i], (amux_channel >> i) & 1);
}
}

void keyboard_select_adc(uint8_t _adc_channel) {
adc_channel = _adc_channel;
}

uint16_t keyboard_read_adc() {
static int out_raw;
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, adc_channel, &out_raw));
return out_raw;
int raw_value = 0;
adc_oneshot_read(adc_handle, adc_channels[adc_channel], &raw_value);
return raw_value;
}

void keyboard_close_adc() {
}

uint32_t keyboard_get_time() {
return esp_timer_get_time();
}
}

void keyboard_read_config() {
memcpy(&keyboard_user_config, &keyboard_default_user_config, sizeof(keyboard_user_config));
}

uint8_t keyboard_write_config(uint8_t *buffer, uint16_t offset, uint16_t size) {
return 1;
}
Loading

0 comments on commit b27b441

Please sign in to comment.