diff --git a/firmware/CMakeLists.txt b/firmware/CMakeLists.txt index ce0b6a3..af83803 100644 --- a/firmware/CMakeLists.txt +++ b/firmware/CMakeLists.txt @@ -30,4 +30,4 @@ add_custom_target( WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) -add_executable(Z_DUMMY_TARGET ${SRC_LIST} src/macros.h src/config.h) +add_executable(Z_DUMMY_TARGET ${SRC_LIST}) diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 1a3949b..582756b 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -13,16 +13,28 @@ ;board = wemos_d1_uno32 ;framework = arduino -[env:s2] -platform = espressif32 -board = lolin_s2_mini -framework = arduino +; ESP32-S2 and C3 both has a problem where the ADC jumps around a LOT +;[env:s2] +;platform = espressif32 +;board = lolin_s2_mini +;framework = arduino ;[env:c3] ;platform = espressif32 ;board = dfrobot_beetle_esp32c3 ;framework = arduino +; YD STM32F411CEU6 +[env:stm32f411ce] +platform = ststm32 +board = blackpill_f411ce +framework = arduino +;upload_protocol = stlink +; Flash using yd-link +;debug_tool = cmsis-dap +upload_protocol = cmsis-dap + +; Arduino nano is good but too few pins ;[env:n328p] ;platform = atmelavr ;board = nanoatmega328 diff --git a/firmware/src/config.h b/firmware/src/config.h index 424a98b..4b6365e 100644 --- a/firmware/src/config.h +++ b/firmware/src/config.h @@ -1,19 +1,24 @@ #ifndef FIRMWARE_CONFIG_H #define FIRMWARE_CONFIG_H +#include "stm32f4xx_hal.h" + // ======================================== -// Configuration +// Pin Configuration // ======================================== +// LED indicator for pulling update +const int LED_REFRESH = PA13; + // 4 1:16 Multiplexers: GPIO pins for each analog multiplexer that handles 12 sensors of an octave // Notes are connected in order: Mux #1 (0-11), Mux #2 (12-23), Mux #3 (24-35), Mux #4 (36-47), Mux #5 (48-59) const int NUM_MUX = 5; const int PINS_PER_MUX = 12; -const int MUX_IN[NUM_MUX] = {34, 34, 35, 34, 34}; // Analog signal input +const int MUX_IN[NUM_MUX] = {PA0, PA1, PA2, PA3, PA4}; // Analog signal input // Select pins for every multiplexer, each multiplexer has 4 select pins, all sel0 are connected to 14, etc. const int NUM_MUX_SEL = 4; -const int MUX_SEL_OUT[NUM_MUX_SEL] = {14, 27, 16, 17}; +const int MUX_SEL_OUT[NUM_MUX_SEL] = {PB4, PB5, PB6, PB7}; // 3 1:8 Multiplexers for the midi panel const int P_NUM_MUX = 3; diff --git a/firmware/src/macros.h b/firmware/src/macros.h index d6c728e..254bfe1 100644 --- a/firmware/src/macros.h +++ b/firmware/src/macros.h @@ -5,7 +5,8 @@ #define u16 uint16_t #define u32 uint32_t #define u64 uint64_t -#define timeMillis() std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count() +//#define timeMillis() std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count() +#define timeMillis() HAL_GetTick() #define min(a, b) ((a) < (b) ? (a) : (b)) #define let auto #define val const auto diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index c2fbbde..79ca92c 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -4,6 +4,8 @@ #include "macros.h" #include "config.h" +#include "stm32f4xx_hal.h" + // ======================================== // Code // ======================================== @@ -26,15 +28,19 @@ const Note notes[] = { {"C7", 96} }; -int lasts[NUM_NOTES]; // variable to store the value coming from the sensor +u32 lasts[NUM_NOTES]; // variable to store the value coming from the sensor u64 last_hit_times[NUM_NOTES]; val max_sensor = 4096; val max_threshold = 2000; val active_threshold = 100; // Minimum value to be considered as a hit +let led_refresh_on = false; + void setup() { + HAL_Init(); + // Initialize pin and serial for (int pin: MUX_IN) pinMode(pin, INPUT); for (int pin: MUX_SEL_OUT) pinMode(pin, OUTPUT); @@ -49,7 +55,7 @@ void setup() * * @param id Sensor index */ -void on_sensor_update(int id, u64 time, int last, int current) +void on_sensor_update(int id, u64 time, u32 last, u32 current) { // If the last value is larger than the current value, check timeout if (last > current && last > active_threshold) @@ -77,6 +83,8 @@ void loop() u64 time = timeMillis(); u64 elapsed = time - start_time; + // Toggle LED refresh indicator + digitalWrite(LED_REFRESH, led_refresh_on = !led_refresh_on); // Serial.printf("%" PRIu64 "=============\r\n", elapsed); // Loop through each multiplexer state @@ -96,11 +104,11 @@ void loop() if (note_id >= NUM_NOTES) break; // Read the analog input - int v = analogRead(MUX_IN[j]); + u32 v = analogRead(MUX_IN[j]); if (v != lasts[note_id]) { // Serial prints are really slow, so don't use them in debug mode - Serial.printf("%s %d\r\n", notes[note_id].name, v); + Serial.printf("%s %d\r\n", notes[note_id].name, v); on_sensor_update(note_id, time, lasts[note_id], v); } lasts[note_id] = v;