From 65d8cec87c978668ed2dba97fc999fa7c150064f Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 6 Sep 2023 22:08:43 +0200 Subject: [PATCH] refactor out a utils module --- src/cppmain.cc | 108 ++++++++----------------------------------------- src/cppmain.h | 16 +------- 2 files changed, 18 insertions(+), 106 deletions(-) diff --git a/src/cppmain.cc b/src/cppmain.cc index 2510bf5..39975fa 100644 --- a/src/cppmain.cc +++ b/src/cppmain.cc @@ -1,15 +1,14 @@ #include "cppmain.h" #include "init.h" -#include "usbd_cdc_if.h" -//#include "usb_device.h" -//#include "cmsis_os.h" +#include "utils.h" + #include #include #include "limits" #include #include - #define SAMPLE_FREQ 42000 +#define SAMPLE_FREQ 42000 #define SAMPLE_MAX 4096 #define BUFFER_SIZE 256 #define BLOCK_SIZE 128 @@ -25,27 +24,10 @@ static volatile bool process_block2{ true }; // time static u64 inf_phasor{ 0 }; +namespace ut = Heck::Utils; -namespace Heck { - bool debug_suspend_active = false; - - void debug_suspend_continue() - { - debug_suspend_active = false; - } - - // Serial Logging - // -------------- - void log(std::string msg) - { - std::string out{ msg }; - out.append("\r\n"); - u8 status = CDC_Transmit_FS((uint8_t *)out.data(), out.size()); - if (status == USBD_BUSY) { - // usb buffer overflow - } - } +namespace Heck { // LED // --- void led_green_toggle() @@ -66,7 +48,7 @@ namespace Heck { // CALLBACKS void irq1_cb() { - debug_suspend_continue(); + ut::debug_suspend_continue(); } void timer3_cb() @@ -86,19 +68,11 @@ namespace Heck { } } - u32 random(u32 max) - { - u32 ret{ 0 }; - HAL_RNG_GenerateRandomNumber(&hrng, &ret); - ret %= max; - return ret; - } - void buffer_init_noise() { - log("buffer_init_noise()"); + ut::log("buffer_init_noise()"); for (int i = 0; i < BUFFER_SIZE; i++) { - u32 val = random(SAMPLE_MAX); + u32 val = ut::random(SAMPLE_MAX); audio_buffer[i] = val; } } @@ -115,7 +89,7 @@ namespace Heck { void buffer_init_sin() { - log("buffer_init_sin()"); + ut::log("buffer_init_sin()"); for (int i = 0; i < BUFFER_SIZE; i++) { float p = float(i) / (float)BUFFER_SIZE - 0.5; u32 val = sin(p) * SAMPLE_MAX; @@ -125,7 +99,7 @@ namespace Heck { void buffer_div(int div) { - log("init_scale()"); + ut::log("init_scale()"); for (int i = 0; i < BUFFER_SIZE; i++) { u32 val = audio_buffer[i] / div; audio_buffer[i] = val; @@ -134,21 +108,12 @@ namespace Heck { void buffer_randomize(int max) { - log("buffer_randomize()"); - u32 buf_index = random(BUFFER_SIZE); - u32 buf_val = random(max); + ut::log("buffer_randomize()"); + u32 buf_index = ut::random(BUFFER_SIZE); + u32 buf_val = ut::random(max); audio_buffer[buf_index] = buf_val; } - std::string reg_to_string(uint32_t val) - { - std::stringstream ss{}; - const std::bitset<32> x{ val }; - ss << "0b" << x.to_string(); - ss << " - "; - ss << std::to_string(val); - return ss.str(); - } u32 hz_to_samps(float hz) { @@ -194,13 +159,13 @@ namespace Heck { void main() { heck_debug_suspend(); - log("Starting..."); + ut::log("Starting..."); HAL_TIM_Base_Start_IT(&htim_blinky_led); HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, audio_buffer, BUFFER_SIZE, DAC_ALIGN_12B_R); HAL_TIM_Base_Start_IT(&htim_dac1); - log("Entering MainLoop..."); + ut::log("Entering MainLoop..."); while (true) { if (process_block) { process_block = false; @@ -216,15 +181,6 @@ namespace Heck { // C Linkage (Bridging) // ---------------------------------------------------------------------------------------------- -extern "C" void heck_debug_suspend(void) -{ - Heck::debug_suspend_active = true; - while (Heck::debug_suspend_active == true) { - Heck::log("debug_suspend..."); - HAL_Delay(10); - } -} - extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == GPIO_PIN_0) { @@ -239,18 +195,17 @@ extern "C" void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) // "TIM2 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + // " channel: " + std::to_string(htim->Channel)); } else if (htim->Instance == TIM3) { - Heck::log( + ut::log( "TIM3 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + " channel: " + std::to_string(htim->Channel)); Heck::timer3_cb(); } else { - Heck::log( + ut::log( "UNKNOWN timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + " channel: " + std::to_string(htim->Channel)); } } - extern "C" void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac) { // Heck::log("HAL_DAC_ConvHalfCpltCallbackCh1"); @@ -265,37 +220,8 @@ extern "C" void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac) process_block = true; } - -extern "C" void heck_log(char *msg) -{ - Heck::log(std::string(msg)); -} - extern "C" void heck_cppmain(void) { Heck::main(); } -/** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -extern "C" void assert_failed(uint8_t *file, uint32_t line) -{ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ - int str_size = 1024; - char str[str_size]; - snprintf(str, str_size, "assert failed: %s:%d", file, line); - heck_log(str); -} - -extern "C" void Error_Handler(void) -{ - heck_log("Error_Handler StR1keZ!"); - __disable_irq(); - while (1) {} -} diff --git a/src/cppmain.h b/src/cppmain.h index 89a5e85..c51ce7f 100644 --- a/src/cppmain.h +++ b/src/cppmain.h @@ -9,13 +9,7 @@ extern "C" { #endif #include "stdint.h" - void heck_cppmain(void); - void heck_log(char* msg); - void heck_debug_suspend(void); - - void assert_failed(uint8_t* file, uint32_t line); - void Error_Handler(void); #ifdef __cplusplus }; #endif @@ -26,20 +20,13 @@ extern "C" { #ifdef __cplusplus + #include "types.hh" #include #include #include -using u8 = uint8_t; -using u16 = uint16_t; -using u32 = uint32_t; -using u64 = uint64_t; namespace Heck { - void debug_suspend_continue(); - // Serial Logging - void log(std::string msg); - // DAC1 void dac1_set(uint32_t val); @@ -54,7 +41,6 @@ namespace Heck { // misc void bytebeat(); - u32 random(u32 max); // MAIN void main();