From 6ea69df2fa2308ca773148b946fb9920079f47ff Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 14 Feb 2024 10:13:50 +0100 Subject: [PATCH 1/9] Testing multicore --- CMakeLists.txt | 1 + src/raspi_keyer.cpp | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e97dcc4..72fb7c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ target_link_libraries(raspi_keyer hardware_exception hardware_pwm pico_flash + pico_multicore ) pico_add_extra_outputs(raspi_keyer) diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index 984a45c..e51e3a5 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -1,6 +1,8 @@ #include #include "pico/stdlib.h" +#include "pico/multicore.h" +#include "pico/util/queue.h" #include "settings.h" #include "keyer.h" @@ -15,6 +17,8 @@ extern const uint LEFT_PADDLE_PIN = 14; extern const uint RIGHT_PADDLE_PIN = 15; extern const uint BUZZER_PIN = 13; +queue_t keyerQueue; + void setup() { stdio_init_all(); @@ -33,6 +37,20 @@ void setup() sleep_ms(1000); } +void core1_main() +{ + Settings settings; + queue_remove_blocking(&keyerQueue, &settings); + + Keyer keyer(settings.wpm, settings.mode); + + while (true) + { + keyer.run(); + } + +} + int main() { timer_hw->dbgpause = 0; // workaround for problem with debug and sleep_ms @@ -42,12 +60,14 @@ int main() Settings settings{read_settings()}; - Keyer keyer(settings.wpm, settings.mode); + /*Keyer keyer(settings.wpm, settings.mode);*/ + + queue_add_blocking(&keyerQueue, &settings); while (true) { - keyer.run(); - } + sleep_ms(100); + } return 0; } From 60e9da0877712278db44e640168e1c3237043fbd Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 14 Feb 2024 10:49:38 +0100 Subject: [PATCH 2/9] Keyer should now run on core1 --- src/raspi_keyer.cpp | 49 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index e51e3a5..d904166 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -17,6 +17,20 @@ extern const uint LEFT_PADDLE_PIN = 14; extern const uint RIGHT_PADDLE_PIN = 15; extern const uint BUZZER_PIN = 13; +// Stuff for communicating between cores +enum class KeyerQueueCommand +{ + Run, + Stop, + Config, +}; +struct KeyerQueueData +{ + KeyerQueueCommand cmd; + uint8_t wpm; + Mode mode; +}; + queue_t keyerQueue; void setup() @@ -37,18 +51,33 @@ void setup() sleep_ms(1000); } +/* Let's do all the keying stuff in the second core, so there are no timing problems. */ void core1_main() { - Settings settings; - queue_remove_blocking(&keyerQueue, &settings); + KeyerQueueData data; + queue_remove_blocking(&keyerQueue, &data); - Keyer keyer(settings.wpm, settings.mode); + Keyer keyer(data.wpm, data.mode); while (true) { - keyer.run(); - } + queue_try_remove(&keyerQueue, &data); + switch (data.cmd) + { + case KeyerQueueCommand::Run: + keyer.run(); + break; + case KeyerQueueCommand::Stop: + break; + case KeyerQueueCommand::Config: + // set speed and wpm + data.cmd = KeyerQueueCommand::Run; + break; + default: + break; + } + } } int main() @@ -60,14 +89,14 @@ int main() Settings settings{read_settings()}; - /*Keyer keyer(settings.wpm, settings.mode);*/ - - queue_add_blocking(&keyerQueue, &settings); + KeyerQueueData keyerQueueData{KeyerQueueCommand::Run, settings.wpm, settings.mode}; + queue_add_blocking(&keyerQueue, &keyerQueueData); while (true) { - sleep_ms(100); - } + // Currently there's nothing to do on core0 + sleep_ms(1000); + } return 0; } From be9ac6b0745fcacbff78383b4e3293c85dac3d7f Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 14 Feb 2024 11:05:33 +0100 Subject: [PATCH 3/9] set mode/speed added --- src/keyer.cpp | 6 ++++++ src/keyer.h | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/keyer.cpp b/src/keyer.cpp index 72d5207..3e81201 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -40,6 +40,12 @@ Keyer::Keyer(uint8_t wpm, Mode mode) : m_wpm(wpm), m_mode(mode) m_elementDuration = calcElementDurationUs(m_wpm); } +void Keyer::setSpeed(uint8_t wpm) +{ + m_wpm = wpm; + m_elementDuration = calcElementDurationUs(wpm); +} + void Keyer::run() { auto timestamp = get_absolute_time(); diff --git a/src/keyer.h b/src/keyer.h index 1c48384..bc32db9 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -16,12 +16,15 @@ public: Dah, InterCharSpace, }; + Keyer() = delete; Keyer(uint8_t wpm, Mode mode); + void setMode(Mode mode) { m_mode = mode; } + void setSpeed(uint8_t wpm); + void run(); private: - Keyer(){}; State state{State::Wait}; State m_previousState{State::Wait}; uint8_t m_wpm{18}; From 251c365095494a7320039253c169c6772440e5fe Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 14 Feb 2024 11:32:09 +0100 Subject: [PATCH 4/9] new feature: stopping keyer --- src/keyer.cpp | 15 ++++++++++++++- src/keyer.h | 2 ++ src/raspi_keyer.cpp | 8 +++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/keyer.cpp b/src/keyer.cpp index 3e81201..afe5f85 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -149,8 +149,21 @@ void Keyer::run() } break; - + case State::Abort: + gpio_put(LED_PIN, 0); + m_Sidetone.off(); + m_keyNextIambicB = false; + m_currentlyPausing = false; + m_currentlyKeying = false; + m_previousState = State::Abort; + state = State::Wait; + break; default: break; } +} + +void Keyer::stop() +{ + state = State::Abort; } \ No newline at end of file diff --git a/src/keyer.h b/src/keyer.h index bc32db9..95e4aa9 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -15,6 +15,7 @@ public: Dit, Dah, InterCharSpace, + Abort, }; Keyer() = delete; Keyer(uint8_t wpm, Mode mode); @@ -23,6 +24,7 @@ public: void setSpeed(uint8_t wpm); void run(); + void stop(); private: State state{State::Wait}; diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index d904166..34c283f 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -23,6 +23,7 @@ enum class KeyerQueueCommand Run, Stop, Config, + Wait, }; struct KeyerQueueData { @@ -69,11 +70,16 @@ void core1_main() keyer.run(); break; case KeyerQueueCommand::Stop: + keyer.stop(); + data.cmd = KeyerQueueCommand::Wait; break; case KeyerQueueCommand::Config: - // set speed and wpm + keyer.setSpeed(data.wpm); + keyer.setMode(data.mode); data.cmd = KeyerQueueCommand::Run; break; + case KeyerQueueCommand::Wait: + break; default: break; } From 4b7c0ba6c67c4b46279d5274a40631e0df66ca6d Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 14 Feb 2024 18:13:09 +0100 Subject: [PATCH 5/9] runs, but unable to debug --- CMakeLists.txt | 2 +- src/raspi_keyer.cpp | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 72fb7c2..430e256 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ pico_set_program_name(raspi_keyer "raspi_keyer") pico_set_program_version(raspi_keyer "0.0.1") pico_enable_stdio_uart(raspi_keyer 1) -pico_enable_stdio_usb(raspi_keyer 0) +pico_enable_stdio_usb(raspi_keyer 1) target_compile_options(raspi_keyer PRIVATE -Wall -Wextra -Werror) diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index 34c283f..d206760 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -49,12 +49,13 @@ void setup() gpio_set_dir(RIGHT_PADDLE_PIN, GPIO_IN); gpio_pull_up(RIGHT_PADDLE_PIN); - sleep_ms(1000); + //sleep_ms(1000); } /* Let's do all the keying stuff in the second core, so there are no timing problems. */ void core1_main() { + printf("Hello from core1!\n"); KeyerQueueData data; queue_remove_blocking(&keyerQueue, &data); @@ -88,20 +89,25 @@ void core1_main() int main() { - timer_hw->dbgpause = 0; // workaround for problem with debug and sleep_ms + //timer_hw->dbgpause = 0; // workaround for problem with debug and sleep_ms // https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639 setup(); + printf("Hello from core0!\n"); + Settings settings{read_settings()}; + queue_init(&keyerQueue, sizeof(KeyerQueueData), 2); + multicore_launch_core1(core1_main); + KeyerQueueData keyerQueueData{KeyerQueueCommand::Run, settings.wpm, settings.mode}; queue_add_blocking(&keyerQueue, &keyerQueueData); while (true) { // Currently there's nothing to do on core0 - sleep_ms(1000); + //sleep_ms(1000); } return 0; From aee58bf2740fc046c4d699cfe073b48fbb934bc1 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 15 Feb 2024 08:36:05 +0100 Subject: [PATCH 6/9] debugging works again. --- src/raspi_keyer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index d206760..c377cc5 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -99,6 +99,7 @@ int main() Settings settings{read_settings()}; queue_init(&keyerQueue, sizeof(KeyerQueueData), 2); + multicore_reset_core1(); multicore_launch_core1(core1_main); KeyerQueueData keyerQueueData{KeyerQueueCommand::Run, settings.wpm, settings.mode}; @@ -107,7 +108,7 @@ int main() while (true) { // Currently there's nothing to do on core0 - //sleep_ms(1000); + sleep_ms(1000); } return 0; From 026b1d04067a5c4ab22ed5d019a744a1c3c511dd Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 15 Feb 2024 10:03:29 +0100 Subject: [PATCH 7/9] Handle the case when the other paddle is pressed during the inter character space --- src/keyer.cpp | 37 ++++++++++++++++++++++++++++++++----- src/keyer.h | 1 + 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/keyer.cpp b/src/keyer.cpp index afe5f85..3efe547 100644 --- a/src/keyer.cpp +++ b/src/keyer.cpp @@ -56,7 +56,7 @@ void Keyer::run() if (left_paddle_pressed() && right_paddle_pressed()) { m_keyNextIambicB = false; - + if (m_previousState == State::Dit) { state = State::Dah; @@ -141,11 +141,37 @@ void Keyer::run() m_pausing_until = make_timeout_time_us(m_elementDuration); m_currentlyPausing = true; } - - if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0) + // Handle the case when the other paddle is pressed during the inter character space + else if (absolute_time_diff_us(timestamp, m_pausing_until) > 0) { - m_currentlyPausing = false; - state = State::Wait; + if (left_paddle_pressed() && m_previousState == State::Dah && m_nextState == State::Wait) + { + printf("--- Pause --- Next state is Dit!\n"); + m_nextState = State::Dit; + + m_keyNextIambicB = right_paddle_pressed() ? true : false; + } + else if (right_paddle_pressed() && m_previousState == State::Dit && m_nextState == State::Wait) + { + printf("--- Pause --- Next state is Dah!\n"); + m_nextState = State::Dah; + + m_keyNextIambicB = left_paddle_pressed() ? true : false; + } + else if (m_previousState == State::Wait) + { + m_nextState = State::Wait; + } + } + else + { + + if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0) + { + m_currentlyPausing = false; + state = m_nextState; + m_nextState = State::Wait; + } } break; @@ -156,6 +182,7 @@ void Keyer::run() m_currentlyPausing = false; m_currentlyKeying = false; m_previousState = State::Abort; + m_nextState = State::Wait; state = State::Wait; break; default: diff --git a/src/keyer.h b/src/keyer.h index 95e4aa9..8732030 100644 --- a/src/keyer.h +++ b/src/keyer.h @@ -29,6 +29,7 @@ public: private: State state{State::Wait}; State m_previousState{State::Wait}; + State m_nextState{State::Wait}; uint8_t m_wpm{18}; Mode m_mode{Mode::IAMBIC_B}; uint64_t m_elementDuration{0}; From 543802168270b08ffc1476361ad0b4143edf2406 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 15 Feb 2024 10:03:54 +0100 Subject: [PATCH 8/9] =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/raspi_keyer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index c377cc5..8724fdc 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -89,7 +89,7 @@ void core1_main() int main() { - //timer_hw->dbgpause = 0; // workaround for problem with debug and sleep_ms + timer_hw->dbgpause = 1; // workaround for problem with debug and sleep_ms // https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639 setup(); @@ -97,6 +97,7 @@ int main() printf("Hello from core0!\n"); Settings settings{read_settings()}; + settings.wpm = 25; // TODO: remove! queue_init(&keyerQueue, sizeof(KeyerQueueData), 2); multicore_reset_core1(); From 5d751b6247796e5e63e6797a8d4933e1171c4d42 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Thu, 15 Feb 2024 12:32:54 +0100 Subject: [PATCH 9/9] buzzer pin changed --- src/raspi_keyer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/raspi_keyer.cpp b/src/raspi_keyer.cpp index 8724fdc..20538f0 100644 --- a/src/raspi_keyer.cpp +++ b/src/raspi_keyer.cpp @@ -15,7 +15,7 @@ namespace extern const uint LED_PIN = PICO_DEFAULT_LED_PIN; extern const uint LEFT_PADDLE_PIN = 14; extern const uint RIGHT_PADDLE_PIN = 15; -extern const uint BUZZER_PIN = 13; +extern const uint BUZZER_PIN = 18; // Stuff for communicating between cores enum class KeyerQueueCommand