diff --git a/Core/Inc/cppmain.h b/Core/Inc/cppmain.h index 2172832..0d59072 100644 --- a/Core/Inc/cppmain.h +++ b/Core/Inc/cppmain.h @@ -1,25 +1,53 @@ -/* - * cppmain.hh - * - * Created on: Jul 4, 2023 - * Author: heck - */ - #ifndef INC_CPPMAIN_H #define INC_CPPMAIN_H +// ---------------------------------------------------------------------------------------------- +// C++ Interface +// ---------------------------------------------------------------------------------------------- + +#ifdef __cplusplus + #include + #include + #include + +namespace Heck { + // Serial Logging + void log(std::string& msg); + + // DAC1 + void dac1_set(uint32_t val); + void dac1_toggle(); + void dac_start_dma(); + void dac_stop_dma(); + + // LED Green + void led_green_toggle(); + void led_green_on(); + void led_green_off(); + // Callbacks + void irq1_cb(); + void timer2_cb(); + void timer7_cb(); + + // misc + void bytebeat(); +} // namespace Heck +#endif + + +// ---------------------------------------------------------------------------------------------- +// C Interface +// ---------------------------------------------------------------------------------------------- #ifdef __cplusplus extern "C" { #endif - void heck_irq1(); - void heck_timer_cb(); void heck_cppmain(void); void heck_error_handler(); + #ifdef __cplusplus }; #endif - #endif /* INC_CPPMAIN_H */ diff --git a/Core/Inc/dac.h b/Core/Inc/dac.h index cbdeec9..2438518 100644 --- a/Core/Inc/dac.h +++ b/Core/Inc/dac.h @@ -32,7 +32,7 @@ extern "C" { /* USER CODE END Includes */ -extern DAC_HandleTypeDef hdac; +extern DAC_HandleTypeDef hdac_global; /* USER CODE BEGIN Private defines */ diff --git a/Core/Src/cppmain.cc b/Core/Src/cppmain.cc index a07cee7..f7ef6b4 100644 --- a/Core/Src/cppmain.cc +++ b/Core/Src/cppmain.cc @@ -11,111 +11,160 @@ #include "dac.h" #include "tim.h" #include -#include -#include #include "limits" const double pi{ std::acos(-1) }; -#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 -}; - - -// Serial Logging -// -------------- - -void log(const std::string &msg) -{ - std::string out{ msg }; - out.append("\r\n"); - CDC_Transmit_FS((uint8_t *)out.data(), out.size()); -} -void log(std::string &&msg) -{ - log(msg); -} +namespace Heck { + // Serial Logging + // -------------- + void log(const std::string &msg) + { + std::string out{ msg }; + out.append("\r\n"); + CDC_Transmit_FS((uint8_t *)out.data(), out.size()); + } // 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) + { + HAL_DAC_SetValue(&hdac_global, DAC_CHANNEL_1, DAC_ALIGN_12B_R, val); + } -void set_dac_1(uint16_t val) -{ - HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, val); -} + void dac1_toggle() + { + static bool dac_state{ 0 }; + dac_state = !dac_state; + if (dac_state) { + dac1_set(0b0000111111111111); + } else { + dac1_set(0b0000000000000000); + } + } -void set_dac_2(uint16_t val) -{ - HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, val); -} + void dac_start_dma() + { + HAL_DAC_Start_DMA(&hdac_global, DAC_CHANNEL_1, (uint32_t *)Wave_LUT, 128, DAC_ALIGN_12B_R); + } + void dac_stop_dma() + { + HAL_DAC_Stop_DMA(&hdac_global, DAC_CHANNEL_1); + } -// LED -// --- + // LED + // --- -void led_green_toggle() -{ - HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin); -} + void led_green_toggle() + { + HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin); + } -void led_green_on() -{ - HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); -} + void led_green_on() + { + HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); + } -void led_green_off() -{ - HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); -} + void led_green_off() + { + HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); + } -void dac1_toggle() + + // CALLBACKS + void irq1_cb() + { + led_green_toggle(); + dac_start_dma(); + + } + + void timer2_cb() + { + led_green_toggle(); + } + + void timer7_cb() {} + + // dont forget to start the DAC + // HAL_DAC_Start(&hdac, DAC_CHANNEL_1); + void bytebeat() + { + long t{ 0 }; + while (true) { + unsigned char res = t * ((t >> 12 | t >> 8) & 63 & t >> 4); + dac1_set((res % 256) * 100); + for (int g = 0; g < 60000; g++) {} + t++; + t %= (std::numeric_limits::max() - 1); + } + } + + +} // namespace Heck + + +// ---------------------------------------------------------------------------------------------- +// C Linkage (Bridging) +// ---------------------------------------------------------------------------------------------- + +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { - static bool dac_state{ 0 }; - dac_state = !dac_state; - if (dac_state) { - set_dac_1(0b0000111111111111); + if (GPIO_Pin == BUT_BLUE_Pin) { + Heck::irq1_cb(); } else { - set_dac_1(0b0000000000000000); + __NOP(); // why NOP? i dont know! was from an example } } -// MAIN -// ---- -void heck_irq1() +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { - led_green_toggle(); + if (htim->Instance == TIM2) { + Heck::log( + "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)) + + " channel: " + std::to_string(htim->Channel)); + } else if (htim->Instance == TIM7) { + Heck::log( + "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)) + + " channel: " + std::to_string(htim->Channel)); + } } -void heck_timer_cb() +void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac) { - // dac1_toggle(); - led_green_toggle(); - log("timerUP"); + Heck::dac_start_dma(); } -// dont forget to start the DAC -// HAL_DAC_Start(&hdac, DAC_CHANNEL_1); -void bytebeat() +void heck_error_handler() { - long t{ 0 }; - while (true) { - unsigned char res = t * ((t >> 12 | t >> 8) & 63 & t >> 4); - set_dac_1((res % 256) * 100); - for (int g = 0; g < 60000; g++) {} - t++; - t %= (std::numeric_limits::max() - 1); - } + Heck::log("HECK ERRRRROR HANDLER"); } void heck_cppmain(void) @@ -123,22 +172,12 @@ void heck_cppmain(void) // HAL_DAC_Start(&hdac, DAC_CHANNEL_1); // HAL_DAC_Start(&hdac, DAC_CHANNEL_2); - // HAL_TIM_Base_Start_IT(&htim2); - - // gen LUT - + HAL_TIM_Base_Start_IT(&htim2); + HAL_TIM_Base_Start_IT(&htim7); HAL_TIM_Base_Start(&htim6); - while (true) { - log("huhu"); -// HAL_Delay(5); - HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t *)Wave_LUT, 128, DAC_ALIGN_12B_R); + Heck::log("huhu"); + HAL_Delay(500); } } - - -void heck_error_handler() -{ - log("HECK ERRRRROR HANDLER"); -} \ No newline at end of file diff --git a/Core/Src/dac.c b/Core/Src/dac.c index ede3e65..6e61c26 100644 --- a/Core/Src/dac.c +++ b/Core/Src/dac.c @@ -24,7 +24,7 @@ /* USER CODE END 0 */ -DAC_HandleTypeDef hdac; +DAC_HandleTypeDef hdac_global; DMA_HandleTypeDef hdma_dac1; /* DAC init function */ @@ -43,8 +43,8 @@ void MX_DAC_Init(void) /** DAC Initialization */ - hdac.Instance = DAC; - if (HAL_DAC_Init(&hdac) != HAL_OK) + hdac_global.Instance = DAC; + if (HAL_DAC_Init(&hdac_global) != HAL_OK) { Error_Handler(); } @@ -53,7 +53,7 @@ void MX_DAC_Init(void) */ sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; - if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK) + if (HAL_DAC_ConfigChannel(&hdac_global, &sConfig, DAC_CHANNEL_1) != HAL_OK) { Error_Handler(); } @@ -61,7 +61,7 @@ void MX_DAC_Init(void) /** DAC channel OUT2 config */ sConfig.DAC_Trigger = DAC_TRIGGER_NONE; - if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2) != HAL_OK) + if (HAL_DAC_ConfigChannel(&hdac_global, &sConfig, DAC_CHANNEL_2) != HAL_OK) { Error_Handler(); } diff --git a/Core/Src/main.c b/Core/Src/main.c index e97fee1..c063e04 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -165,18 +165,6 @@ void SystemClock_Config(void) } /* USER CODE BEGIN 4 */ -void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) -{ - if(GPIO_Pin == BUT_BLUE_Pin) { - heck_irq1(); - } else { - __NOP(); - } -} - -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { -// heck_timer_cb(); -} /* USER CODE END 4 */ diff --git a/Core/Src/tim.c b/Core/Src/tim.c index d49c472..04c1b55 100644 --- a/Core/Src/tim.c +++ b/Core/Src/tim.c @@ -45,7 +45,7 @@ void MX_TIM2_Init(void) htim2.Instance = TIM2; htim2.Init.Prescaler = 0; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; - htim2.Init.Period = 42949000; + htim2.Init.Period = 429490000; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim2) != HAL_OK) @@ -84,7 +84,7 @@ void MX_TIM6_Init(void) htim6.Instance = TIM6; htim6.Init.Prescaler = 0; htim6.Init.CounterMode = TIM_COUNTERMODE_UP; - htim6.Init.Period = 600; + htim6.Init.Period = 1904; htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim6) != HAL_OK) { @@ -115,9 +115,9 @@ void MX_TIM7_Init(void) /* USER CODE END TIM7_Init 1 */ htim7.Instance = TIM7; - htim7.Init.Prescaler = 0; + htim7.Init.Prescaler = 10000; htim7.Init.CounterMode = TIM_COUNTERMODE_UP; - htim7.Init.Period = 65535; + htim7.Init.Period = 429490000; htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&htim7) != HAL_OK) { diff --git a/raw407cxx.ioc b/raw407cxx.ioc index 4fb9387..ab2e218 100644 --- a/raw407cxx.ioc +++ b/raw407cxx.ioc @@ -7,10 +7,10 @@ DAC.IPParameters=DAC_Trigger Dma.DAC1.0.Direction=DMA_MEMORY_TO_PERIPH Dma.DAC1.0.FIFOMode=DMA_FIFOMODE_DISABLE Dma.DAC1.0.Instance=DMA1_Stream5 -Dma.DAC1.0.MemDataAlignment=DMA_MDATAALIGN_HALFWORD +Dma.DAC1.0.MemDataAlignment=DMA_MDATAALIGN_WORD Dma.DAC1.0.MemInc=DMA_MINC_ENABLE Dma.DAC1.0.Mode=DMA_NORMAL -Dma.DAC1.0.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD +Dma.DAC1.0.PeriphDataAlignment=DMA_PDATAALIGN_WORD Dma.DAC1.0.PeriphInc=DMA_PINC_DISABLE Dma.DAC1.0.Priority=DMA_PRIORITY_LOW Dma.DAC1.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode