Browse Source

adc pot1 working (polling)

tmp1
heck 2 years ago
parent
commit
05932229a1
  1. 32
      src/cppmain.cc
  2. 105
      src/init.c
  3. 2
      src/init.h

32
src/cppmain.cc

@ -21,6 +21,7 @@ static u32 *block = &audio_buffer[0];
static volatile bool process_block{ true }; static volatile bool process_block{ true };
static volatile bool process_block2{ true }; static volatile bool process_block2{ true };
static volatile u32 pot1{ 0 };
// time // time
static u64 inf_phasor{ 0 }; static u64 inf_phasor{ 0 };
@ -133,27 +134,27 @@ namespace Heck {
return ret; return ret;
} }
void calculate_audio() std::string block_to_string()
{ {
// log("calculate_audio time: ");
u32 *b = block; u32 *b = block;
u64 t = inf_phasor; std::stringstream ss{};
for (int i = 0; i < BLOCK_SIZE; i++) { for (int i = 0; i < BLOCK_SIZE; i++) {
b[i] = saw(t, 80); ss << std::setw(4) << b[i] << " ";
// b[i] *= saw(t, 20);
t++;
} }
return ss.str();
} }
std::string block_to_string() void calculate_audio()
{ {
// log("calculate_audio time: ");
u32 *b = block; u32 *b = block;
std::stringstream ss{}; u64 t = inf_phasor;
for (int i = 0; i < BLOCK_SIZE; i++) { for (int i = 0; i < BLOCK_SIZE; i++) {
ss << std::setw(4) << b[i] << " "; b[i] = saw(t, pot1);
// b[i] *= saw(t, 20);
t++;
} }
return ss.str();
} }
void main() void main()
@ -164,7 +165,6 @@ namespace Heck {
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);
ut::log("Entering MainLoop..."); ut::log("Entering MainLoop...");
while (true) { while (true) {
if (process_block) { if (process_block) {
@ -172,6 +172,13 @@ namespace Heck {
calculate_audio(); calculate_audio();
inf_phasor += BLOCK_SIZE; inf_phasor += BLOCK_SIZE;
} }
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1);
u32 pot1_tmp = HAL_ADC_GetValue(&hadc1);
if (std::abs(int(pot1 - pot1_tmp)) > 20) {
pot1 = pot1_tmp;
ut::log("pot1:" + std::to_string(pot1));
}
} }
} }
} // namespace Heck } // namespace Heck
@ -224,4 +231,3 @@ extern "C" void heck_cppmain(void)
{ {
Heck::main(); Heck::main();
} }

105
src/init.c

@ -1,5 +1,6 @@
#include "cppmain.h" #include "cppmain.h"
#include "usb_device.h" #include "usb_device.h"
#include "utils.h"
DAC_HandleTypeDef hdac1; DAC_HandleTypeDef hdac1;
TIM_HandleTypeDef htim_dac1; TIM_HandleTypeDef htim_dac1;
@ -171,7 +172,7 @@ void dac_init(void)
htim_dac1.Instance = TIM2; htim_dac1.Instance = TIM2;
htim_dac1.Init.Prescaler = 0; htim_dac1.Init.Prescaler = 0;
htim_dac1.Init.CounterMode = TIM_COUNTERMODE_UP; htim_dac1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim_dac1.Init.Period = 2000;// *100; htim_dac1.Init.Period = 2000; // *100;
htim_dac1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; htim_dac1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim_dac1) != HAL_OK) { if (HAL_TIM_Base_Init(&htim_dac1) != HAL_OK) {
Error_Handler(); Error_Handler();
@ -232,42 +233,38 @@ void dac_init(void)
void adc_init(void) void adc_init(void)
{ {
// GPIO {
GPIO_InitTypeDef conf = { // GPIO
.Pin = GPIO_PIN_4, GPIO_InitTypeDef conf = {
.Mode = GPIO_MODE_ANALOG, .Pin = GPIO_PIN_1,
.Pull = GPIO_NOPULL, .Mode = GPIO_MODE_ANALOG,
.Speed = GPIO_SPEED_FREQ_MEDIUM, .Pull = GPIO_NOPULL,
}; .Speed = GPIO_SPEED_FREQ_LOW,
};
HAL_GPIO_Init(GPIOC, &conf); HAL_GPIO_Init(GPIOA, &conf);
}
{
// GPIO
GPIO_InitTypeDef conf = {
.Pin = GPIO_PIN_2,
.Mode = GPIO_MODE_ANALOG,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_LOW,
};
HAL_GPIO_Init(GPIOA, &conf);
}
{
// GPIO
GPIO_InitTypeDef conf = {
.Pin = GPIO_PIN_3,
.Mode = GPIO_MODE_ANALOG,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_LOW,
};
HAL_GPIO_Init(GPIOA, &conf);
}
ADC_Common_TypeDef *adc_common = ADC123_COMMON; hadc1.Instance = ADC2;
ADC_TypeDef *adc1 = ADC1;
// SET_BIT(adc_common->CCR, ADC_CCR_ADCPRE_0);
// CLEAR_BIT(adc_common->CCR, ADC_CCR_ADCPRE_1);
//
// CLEAR_BIT(adc1->CR1, ~ADC_CR1_RES_0);
// CLEAR_BIT(adc1->CR1, ADC_CR1_RES_1);
// SET_BIT(adc1->CR2, ADC_CR2_CONT);
// CLEAR_BIT(adc1->CR2, ADC_CR2_ALIGN);
// SET_BIT(adc1->CR2, ADC_CR2_ADON);
// SET_BIT(adc1->CR2, ADC_CR2_SWSTART);
//
//
// SET_BIT(adc1->SQR1,ADC_SQR1_L_0);
//// CLEAR_BIT(adc1->SQR1,ADC_SQR1_L_1);
//// CLEAR_BIT(adc1->SQR1,ADC_SQR1_L_2);
//// CLEAR_BIT(adc1->SQR1,ADC_SQR1_L_3);
////
//// ADC_SQR3_RK(1,1);
// SET_BIT(adc1->SQR3,ADC_SQR3_SQ1_0);
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_1);
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_2);
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_3);
//// CLEAR_BIT(adc1->SQR3,ADC_SQR3_SQ1_4);
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B; hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE; hadc1.Init.ScanConvMode = DISABLE;
@ -284,29 +281,29 @@ void adc_init(void)
} }
ADC_ChannelConfTypeDef sConfig = {}; ADC_ChannelConfTypeDef sConfig = {};
sConfig.Channel = ADC_CHANNEL_14; sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1; sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Error_Handler(); Error_Handler();
} }
// adc1->CR2 // // adc1->CR2
while (1) { // while (1) {
// SET_BIT(adc1->CR2, ADC_CR2_ADON); // // SET_BIT(adc1->CR2, ADC_CR2_ADON);
// SET_BIT(adc1->CR2, ADC_CR2_SWSTART); // // SET_BIT(adc1->CR2, ADC_CR2_SWSTART);
HAL_ADC_Start(&hadc1); // HAL_ADC_Start(&hadc1);
//
// heck_log("ADC123_COMMON->CCR:" + reg_to_string(adc_common->CCR)); // // heck_log("ADC123_COMMON->CCR:" + reg_to_string(adc_common->CCR));
// heck_log("ADC1->CR1:" + reg_to_string(adc1->CR1)); // // heck_log("ADC1->CR1:" + reg_to_string(adc1->CR1));
// heck_log("ADC1->CR2:" + reg_to_string(adc1->CR2)); // // heck_log("ADC1->CR2:" + reg_to_string(adc1->CR2));
// heck_log("ADC1->SQR1:" + reg_to_string(adc1->SQR1)); // // heck_log("ADC1->SQR1:" + reg_to_string(adc1->SQR1));
// heck_log("ADC1->SQR2:" + reg_to_string(adc1->SQR2)); // // heck_log("ADC1->SQR2:" + reg_to_string(adc1->SQR2));
// heck_log("ADC1->SQR3:" + reg_to_string(adc1->SQR3)); // // heck_log("ADC1->SQR3:" + reg_to_string(adc1->SQR3));
// heck_log("ADC1->DR:" + reg_to_string(adc1->DR)); // // heck_log("ADC1->DR:" + reg_to_string(adc1->DR));
heck_log("\n"); // heck_log("\n");
HAL_Delay(100); // HAL_Delay(100);
} // }
} }
void rng_init(void) void rng_init(void)
@ -325,6 +322,6 @@ void heck_init()
// heck_debug_suspend(); // heck_debug_suspend();
blinky_led_init(); blinky_led_init();
dac_init(); dac_init();
// adc_init(); adc_init();
rng_init(); rng_init();
} }

2
src/init.h

@ -1,8 +1,8 @@
#ifndef __INIT_H #ifndef __INIT_H
#define __INIT_H #define __INIT_H
#include "stm32f4xx_hal.h"
#include "stm32f4xx.h" #include "stm32f4xx.h"
#include "stm32f4xx_hal.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

Loading…
Cancel
Save