Browse Source

refactor out a utils module

tmp1
heck 2 years ago
parent
commit
65d8cec87c
  1. 108
      src/cppmain.cc
  2. 16
      src/cppmain.h

108
src/cppmain.cc

@ -1,15 +1,14 @@
#include "cppmain.h" #include "cppmain.h"
#include "init.h" #include "init.h"
#include "usbd_cdc_if.h" #include "utils.h"
//#include "usb_device.h"
//#include "cmsis_os.h"
#include <cmath> #include <cmath>
#include <bitset> #include <bitset>
#include "limits" #include "limits"
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#define SAMPLE_FREQ 42000 #define SAMPLE_FREQ 42000
#define SAMPLE_MAX 4096 #define SAMPLE_MAX 4096
#define BUFFER_SIZE 256 #define BUFFER_SIZE 256
#define BLOCK_SIZE 128 #define BLOCK_SIZE 128
@ -25,27 +24,10 @@ static volatile bool process_block2{ true };
// time // time
static u64 inf_phasor{ 0 }; 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 // LED
// --- // ---
void led_green_toggle() void led_green_toggle()
@ -66,7 +48,7 @@ namespace Heck {
// CALLBACKS // CALLBACKS
void irq1_cb() void irq1_cb()
{ {
debug_suspend_continue(); ut::debug_suspend_continue();
} }
void timer3_cb() 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() void buffer_init_noise()
{ {
log("buffer_init_noise()"); ut::log("buffer_init_noise()");
for (int i = 0; i < BUFFER_SIZE; i++) { for (int i = 0; i < BUFFER_SIZE; i++) {
u32 val = random(SAMPLE_MAX); u32 val = ut::random(SAMPLE_MAX);
audio_buffer[i] = val; audio_buffer[i] = val;
} }
} }
@ -115,7 +89,7 @@ namespace Heck {
void buffer_init_sin() void buffer_init_sin()
{ {
log("buffer_init_sin()"); ut::log("buffer_init_sin()");
for (int i = 0; i < BUFFER_SIZE; i++) { for (int i = 0; i < BUFFER_SIZE; i++) {
float p = float(i) / (float)BUFFER_SIZE - 0.5; float p = float(i) / (float)BUFFER_SIZE - 0.5;
u32 val = sin(p) * SAMPLE_MAX; u32 val = sin(p) * SAMPLE_MAX;
@ -125,7 +99,7 @@ namespace Heck {
void buffer_div(int div) void buffer_div(int div)
{ {
log("init_scale()"); ut::log("init_scale()");
for (int i = 0; i < BUFFER_SIZE; i++) { for (int i = 0; i < BUFFER_SIZE; i++) {
u32 val = audio_buffer[i] / div; u32 val = audio_buffer[i] / div;
audio_buffer[i] = val; audio_buffer[i] = val;
@ -134,21 +108,12 @@ namespace Heck {
void buffer_randomize(int max) void buffer_randomize(int max)
{ {
log("buffer_randomize()"); ut::log("buffer_randomize()");
u32 buf_index = random(BUFFER_SIZE); u32 buf_index = ut::random(BUFFER_SIZE);
u32 buf_val = random(max); u32 buf_val = ut::random(max);
audio_buffer[buf_index] = buf_val; 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) u32 hz_to_samps(float hz)
{ {
@ -194,13 +159,13 @@ namespace Heck {
void main() void main()
{ {
heck_debug_suspend(); heck_debug_suspend();
log("Starting..."); ut::log("Starting...");
HAL_TIM_Base_Start_IT(&htim_blinky_led); 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_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, audio_buffer, BUFFER_SIZE, DAC_ALIGN_12B_R);
HAL_TIM_Base_Start_IT(&htim_dac1); HAL_TIM_Base_Start_IT(&htim_dac1);
log("Entering MainLoop..."); ut::log("Entering MainLoop...");
while (true) { while (true) {
if (process_block) { if (process_block) {
process_block = false; process_block = false;
@ -216,15 +181,6 @@ namespace Heck {
// C Linkage (Bridging) // 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) extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{ {
if (GPIO_Pin == GPIO_PIN_0) { 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<u32>(htim->Instance)) + // "TIM2 timer: instance: " + std::to_string(reinterpret_cast<u32>(htim->Instance)) +
// " channel: " + std::to_string(htim->Channel)); // " channel: " + std::to_string(htim->Channel));
} else if (htim->Instance == TIM3) { } else if (htim->Instance == TIM3) {
Heck::log( ut::log(
"TIM3 timer: instance: " + std::to_string(reinterpret_cast<u32>(htim->Instance)) + "TIM3 timer: instance: " + std::to_string(reinterpret_cast<u32>(htim->Instance)) +
" channel: " + std::to_string(htim->Channel)); " channel: " + std::to_string(htim->Channel));
Heck::timer3_cb(); Heck::timer3_cb();
} else { } else {
Heck::log( ut::log(
"UNKNOWN timer: instance: " + std::to_string(reinterpret_cast<u32>(htim->Instance)) + "UNKNOWN timer: instance: " + std::to_string(reinterpret_cast<u32>(htim->Instance)) +
" channel: " + std::to_string(htim->Channel)); " channel: " + std::to_string(htim->Channel));
} }
} }
extern "C" void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac) extern "C" void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
{ {
// Heck::log("HAL_DAC_ConvHalfCpltCallbackCh1"); // Heck::log("HAL_DAC_ConvHalfCpltCallbackCh1");
@ -265,37 +220,8 @@ extern "C" void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
process_block = true; process_block = true;
} }
extern "C" void heck_log(char *msg)
{
Heck::log(std::string(msg));
}
extern "C" void heck_cppmain(void) extern "C" void heck_cppmain(void)
{ {
Heck::main(); 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) {}
}

16
src/cppmain.h

@ -9,13 +9,7 @@
extern "C" { extern "C" {
#endif #endif
#include "stdint.h" #include "stdint.h"
void heck_cppmain(void); 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 #ifdef __cplusplus
}; };
#endif #endif
@ -26,20 +20,13 @@ extern "C" {
#ifdef __cplusplus #ifdef __cplusplus
#include "types.hh"
#include <iostream> #include <iostream>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
namespace Heck { namespace Heck {
void debug_suspend_continue();
// Serial Logging
void log(std::string msg);
// DAC1 // DAC1
void dac1_set(uint32_t val); void dac1_set(uint32_t val);
@ -54,7 +41,6 @@ namespace Heck {
// misc // misc
void bytebeat(); void bytebeat();
u32 random(u32 max);
// MAIN // MAIN
void main(); void main();

Loading…
Cancel
Save