You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
4.7 KiB
183 lines
4.7 KiB
/*
|
|
* cppmain.cc
|
|
*
|
|
* Created on: Jul 4, 2023
|
|
* Author: heck
|
|
*/
|
|
|
|
#include "cppmain.h"
|
|
#include "main.h"
|
|
#include "usbd_cdc_if.h"
|
|
#include "dac.h"
|
|
#include "tim.h"
|
|
#include <cmath>
|
|
#include "limits"
|
|
|
|
const double pi{ std::acos(-1) };
|
|
|
|
|
|
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 dac1_toggle()
|
|
{
|
|
static bool dac_state{ 0 };
|
|
dac_state = !dac_state;
|
|
if (dac_state) {
|
|
dac1_set(0b0000111111111111);
|
|
} else {
|
|
dac1_set(0b0000000000000000);
|
|
}
|
|
}
|
|
|
|
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
|
|
// ---
|
|
|
|
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_off()
|
|
{
|
|
HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);
|
|
}
|
|
|
|
|
|
// 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<long>::max() - 1);
|
|
}
|
|
}
|
|
|
|
|
|
} // namespace Heck
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------
|
|
// C Linkage (Bridging)
|
|
// ----------------------------------------------------------------------------------------------
|
|
|
|
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
|
{
|
|
if (GPIO_Pin == BUT_BLUE_Pin) {
|
|
Heck::irq1_cb();
|
|
} else {
|
|
__NOP(); // why NOP? i dont know! was from an example
|
|
}
|
|
}
|
|
|
|
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
|
{
|
|
if (htim->Instance == TIM2) {
|
|
Heck::log(
|
|
"TIM2 timer: instance: " + std::to_string(reinterpret_cast<uint32_t>(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<uint32_t>(htim->Instance)) +
|
|
" channel: " + std::to_string(htim->Channel));
|
|
} else if (htim->Instance == TIM7) {
|
|
Heck::log(
|
|
"TIM7 timer: instance: " + std::to_string(reinterpret_cast<uint32_t>(htim->Instance)) +
|
|
" channel: " + std::to_string(htim->Channel));
|
|
Heck::timer7_cb();
|
|
} else {
|
|
Heck::log(
|
|
"UNKNOWN timer: instance: " + std::to_string(reinterpret_cast<uint32_t>(htim->Instance)) +
|
|
" channel: " + std::to_string(htim->Channel));
|
|
}
|
|
}
|
|
|
|
|
|
void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
|
|
{
|
|
Heck::dac_start_dma();
|
|
}
|
|
|
|
void heck_error_handler()
|
|
{
|
|
Heck::log("HECK ERRRRROR 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_TIM_Base_Start(&htim6);
|
|
while (true) {
|
|
Heck::log("huhu");
|
|
HAL_Delay(500);
|
|
}
|
|
}
|
|
|