diff --git a/Core/Inc/cppmain.h b/Core/Inc/cppmain.h index 0d59072..1c57b0c 100644 --- a/Core/Inc/cppmain.h +++ b/Core/Inc/cppmain.h @@ -5,12 +5,17 @@ // C++ Interface // ---------------------------------------------------------------------------------------------- + #ifdef __cplusplus #include #include #include +using u32 = uint32_t; + namespace Heck { + bool is_running {false}; + // Serial Logging void log(std::string& msg); @@ -32,6 +37,10 @@ namespace Heck { // misc void bytebeat(); + u32 random(u32 max); + + // MAIN + void main(); } // namespace Heck #endif diff --git a/Core/Src/cppmain.cc b/Core/Src/cppmain.cc index 5f812a5..a6a3511 100644 --- a/Core/Src/cppmain.cc +++ b/Core/Src/cppmain.cc @@ -9,6 +9,7 @@ #include "main.h" #include "usbd_cdc_if.h" #include "dac.h" +#include "rng.h" #include "tim.h" #include #include "limits" @@ -16,6 +17,7 @@ const double pi{ std::acos(-1) }; + namespace Heck { // Serial Logging // -------------- @@ -29,20 +31,21 @@ namespace Heck { // DAC // --- -#define NS 128 - uint32_t Wave_LUT[NS] = { - 2048, 2149, 2250, 2350, 2450, 2549, 2646, 2742, 2837, 2929, 3020, 3108, 3193, 3275, 3355, - 3431, 3504, 3574, 3639, 3701, 3759, 3812, 3861, 3906, 3946, 3982, 4013, 4039, 4060, 4076, - 4087, 4094, 4095, 4091, 4082, 4069, 4050, 4026, 3998, 3965, 3927, 3884, 3837, 3786, 3730, - 3671, 3607, 3539, 3468, 3394, 3316, 3235, 3151, 3064, 2975, 2883, 2790, 2695, 2598, 2500, - 2400, 2300, 2199, 2098, 1997, 1896, 1795, 1695, 1595, 1497, 1400, 1305, 1212, 1120, 1031, - 944, 860, 779, 701, 627, 556, 488, 424, 365, 309, 258, 211, 168, 130, 97, - 69, 45, 26, 13, 4, 0, 1, 8, 19, 35, 56, 82, 113, 149, 189, - 234, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258, - 1353, 1449, 1546, 1645, 1745, 1845, 1946, 2047 - }; - - void dac1_set(uint32_t val) +#define BUFFER_SIZE 1 + u32 audio_buffer[BUFFER_SIZE]; +// u32 audio_buffer[BUFFER_SIZE] = { 2048, 2149, 2250, 2350, 2450, 2549, 2646, 2742, 2837, 2929, 3020, 3108, +// 3193, 3275, 3355, 3431, 3504, 3574, 3639, 3701, 3759, 3812, 3861, 3906, +// 3946, 3982, 4013, 4039, 4060, 4076, 4087, 4094, 4095, 4091, 4082, 4069, +// 4050, 4026, 3998, 3965, 3927, 3884, 3837, 3786, 3730, 3671, 3607, 3539, +// 3468, 3394, 3316, 3235, 3151, 3064, 2975, 2883, 2790, 2695, 2598, 2500, +// 2400, 2300, 2199, 2098, 1997, 1896, 1795, 1695, 1595, 1497, 1400, 1305, +// 1212, 1120, 1031, 944, 860, 779, 701, 627, 556, 488, 424, 365, +// 309, 258, 211, 168, 130, 97, 69, 45, 26, 13, 4, 0, +// 1, 8, 19, 35, 56, 82, 113, 149, 189, 234, 283, 336, +// 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258, +// 1353, 1449, 1546, 1645, 1745, 1845, 1946, 2047 }; + + void dac1_set(u32 val) { HAL_DAC_SetValue(&hdac_global, DAC_CHANNEL_1, DAC_ALIGN_12B_R, val); } @@ -60,7 +63,7 @@ namespace Heck { void dac_start_dma() { - HAL_DAC_Start_DMA(&hdac_global, DAC_CHANNEL_1, (uint32_t *)Wave_LUT, 128, DAC_ALIGN_12B_R); + HAL_DAC_Start_DMA(&hdac_global, DAC_CHANNEL_1, (u32 *)audio_buffer, BUFFER_SIZE, DAC_ALIGN_12B_R); } void dac_stop_dma() @@ -90,9 +93,13 @@ namespace Heck { // CALLBACKS void irq1_cb() { + if (!is_running) { + is_running = true; + return; + } + led_green_toggle(); dac_start_dma(); - } void timer2_cb() @@ -116,6 +123,66 @@ namespace Heck { } } + u32 random(u32 max) + { + u32 ret{ 0 }; + HAL_RNG_GenerateRandomNumber(&hrng, &ret); + ret %= max; + return ret; + } + + void init_buffer_noise() + { + log("init_buffer_noise()"); + for (int i = 0; i < BUFFER_SIZE; i++) { + u32 val = random(4096); + audio_buffer[i] = val; + log(std::to_string(val)); + } + } + + void init_buffer_sin() + { + log("init_buffer_sin()"); + for (int i = 0; i < BUFFER_SIZE; i++) { + float p = float(i) / (float)BUFFER_SIZE - 0.5; + u32 val = sin(p) * 4096; + audio_buffer[i] = val; + log(std::to_string(val)); + } + } + + void noisify_buffer() + { + u32 buf_index = random(BUFFER_SIZE); + u32 buf_val = random(4096); + audio_buffer[buf_index] = buf_val; + log("i:" + std::to_string(buf_index) + " v:" + std::to_string(buf_val)); + } + + void main() + { + while (!is_running) { + HAL_Delay(1); + } + log("Starting..."); + + // HAL_DAC_Start(&hdac, DAC_CHANNEL_1); + // HAL_DAC_Start(&hdac, DAC_CHANNEL_2); + HAL_TIM_Base_Start_IT(&htim2); + HAL_TIM_Base_Start_IT(&htim7); + HAL_TIM_Base_Start(&htim6); + + init_buffer_noise(); + // init_buffer_sin(); + log("Entering MainLoop..."); + while (true) { + + init_buffer_noise(); +// noisify_buffer(); +// HAL_Delay(100); + } + } } // namespace Heck @@ -137,21 +204,21 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim->Instance == TIM2) { Heck::log( - "TIM2 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + + "TIM2 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + " channel: " + std::to_string(htim->Channel)); Heck::timer2_cb(); } else if (htim->Instance == TIM6) { Heck::log( - "TIM6 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + + "TIM6 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + " channel: " + std::to_string(htim->Channel)); } else if (htim->Instance == TIM7) { Heck::log( - "TIM7 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + + "TIM7 timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + " channel: " + std::to_string(htim->Channel)); Heck::timer7_cb(); } else { Heck::log( - "UNKNOWN timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + + "UNKNOWN timer: instance: " + std::to_string(reinterpret_cast(htim->Instance)) + " channel: " + std::to_string(htim->Channel)); } } @@ -170,14 +237,5 @@ void heck_error_handler() void heck_cppmain(void) { - // HAL_DAC_Start(&hdac, DAC_CHANNEL_1); - // HAL_DAC_Start(&hdac, DAC_CHANNEL_2); - HAL_TIM_Base_Start_IT(&htim2); - HAL_TIM_Base_Start_IT(&htim7); - HAL_RNG_GenerateRandomNumber(hrng) - HAL_TIM_Base_Start(&htim6); - while (true) { - Heck::log("huhu"); - HAL_Delay(500); - } + Heck::main(); }