From 05932229a1fbae82e6fcb17970e699dfff87f1f2 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 7 Sep 2023 00:35:33 +0200 Subject: [PATCH] adc pot1 working (polling) --- src/cppmain.cc | 32 +++++++++------ src/init.c | 105 ++++++++++++++++++++++++------------------------- src/init.h | 2 +- 3 files changed, 71 insertions(+), 68 deletions(-) diff --git a/src/cppmain.cc b/src/cppmain.cc index 39975fa..7be5184 100644 --- a/src/cppmain.cc +++ b/src/cppmain.cc @@ -21,6 +21,7 @@ static u32 *block = &audio_buffer[0]; static volatile bool process_block{ true }; static volatile bool process_block2{ true }; +static volatile u32 pot1{ 0 }; // time static u64 inf_phasor{ 0 }; @@ -133,27 +134,27 @@ namespace Heck { return ret; } - void calculate_audio() + std::string block_to_string() { - // log("calculate_audio time: "); u32 *b = block; - u64 t = inf_phasor; - + std::stringstream ss{}; for (int i = 0; i < BLOCK_SIZE; i++) { - b[i] = saw(t, 80); -// b[i] *= saw(t, 20); - t++; + ss << std::setw(4) << b[i] << " "; } + return ss.str(); } - std::string block_to_string() + void calculate_audio() { + // log("calculate_audio time: "); u32 *b = block; - std::stringstream ss{}; + u64 t = inf_phasor; + 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() @@ -164,7 +165,6 @@ namespace Heck { 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_TIM_Base_Start_IT(&htim_dac1); - ut::log("Entering MainLoop..."); while (true) { if (process_block) { @@ -172,6 +172,13 @@ namespace Heck { calculate_audio(); 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 @@ -224,4 +231,3 @@ extern "C" void heck_cppmain(void) { Heck::main(); } - diff --git a/src/init.c b/src/init.c index ed71b61..3245675 100644 --- a/src/init.c +++ b/src/init.c @@ -1,5 +1,6 @@ #include "cppmain.h" #include "usb_device.h" +#include "utils.h" DAC_HandleTypeDef hdac1; TIM_HandleTypeDef htim_dac1; @@ -171,7 +172,7 @@ void dac_init(void) htim_dac1.Instance = TIM2; htim_dac1.Init.Prescaler = 0; 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; if (HAL_TIM_Base_Init(&htim_dac1) != HAL_OK) { Error_Handler(); @@ -232,42 +233,38 @@ void dac_init(void) void adc_init(void) { - // GPIO - GPIO_InitTypeDef conf = { - .Pin = GPIO_PIN_4, - .Mode = GPIO_MODE_ANALOG, - .Pull = GPIO_NOPULL, - .Speed = GPIO_SPEED_FREQ_MEDIUM, - }; - - HAL_GPIO_Init(GPIOC, &conf); + { + // GPIO + GPIO_InitTypeDef conf = { + .Pin = GPIO_PIN_1, + .Mode = GPIO_MODE_ANALOG, + .Pull = GPIO_NOPULL, + .Speed = GPIO_SPEED_FREQ_LOW, + }; + 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; - 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.Instance = ADC2; hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; hadc1.Init.Resolution = ADC_RESOLUTION_12B; hadc1.Init.ScanConvMode = DISABLE; @@ -284,29 +281,29 @@ void adc_init(void) } ADC_ChannelConfTypeDef sConfig = {}; - sConfig.Channel = ADC_CHANNEL_14; + sConfig.Channel = ADC_CHANNEL_1; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } - // adc1->CR2 - while (1) { - // SET_BIT(adc1->CR2, ADC_CR2_ADON); - // SET_BIT(adc1->CR2, ADC_CR2_SWSTART); - HAL_ADC_Start(&hadc1); - -// heck_log("ADC123_COMMON->CCR:" + reg_to_string(adc_common->CCR)); -// heck_log("ADC1->CR1:" + reg_to_string(adc1->CR1)); -// heck_log("ADC1->CR2:" + reg_to_string(adc1->CR2)); -// heck_log("ADC1->SQR1:" + reg_to_string(adc1->SQR1)); -// heck_log("ADC1->SQR2:" + reg_to_string(adc1->SQR2)); -// heck_log("ADC1->SQR3:" + reg_to_string(adc1->SQR3)); -// heck_log("ADC1->DR:" + reg_to_string(adc1->DR)); - heck_log("\n"); - HAL_Delay(100); - } + // // adc1->CR2 + // while (1) { + // // SET_BIT(adc1->CR2, ADC_CR2_ADON); + // // SET_BIT(adc1->CR2, ADC_CR2_SWSTART); + // HAL_ADC_Start(&hadc1); + // + // // heck_log("ADC123_COMMON->CCR:" + reg_to_string(adc_common->CCR)); + // // heck_log("ADC1->CR1:" + reg_to_string(adc1->CR1)); + // // heck_log("ADC1->CR2:" + reg_to_string(adc1->CR2)); + // // heck_log("ADC1->SQR1:" + reg_to_string(adc1->SQR1)); + // // heck_log("ADC1->SQR2:" + reg_to_string(adc1->SQR2)); + // // heck_log("ADC1->SQR3:" + reg_to_string(adc1->SQR3)); + // // heck_log("ADC1->DR:" + reg_to_string(adc1->DR)); + // heck_log("\n"); + // HAL_Delay(100); + // } } void rng_init(void) @@ -325,6 +322,6 @@ void heck_init() // heck_debug_suspend(); blinky_led_init(); dac_init(); -// adc_init(); + adc_init(); rng_init(); } diff --git a/src/init.h b/src/init.h index fad66e3..b0e51a1 100644 --- a/src/init.h +++ b/src/init.h @@ -1,8 +1,8 @@ #ifndef __INIT_H #define __INIT_H -#include "stm32f4xx_hal.h" #include "stm32f4xx.h" +#include "stm32f4xx_hal.h" #ifdef __cplusplus extern "C" {