# How do I convert this ADC code for STM32F3 to STM32F4 for colorchord?

I am currently trying to make the ADC driver code for the STM32F3 version of the colorchord https://github.com/cnlohr/colorchord code work on an STM32F4, as my F4 does not have an on board microphone. Also, I still want to use direct audio-line-in anyway since the microcontroller won't be especially close to the speakers anyway.

Here is the ADC.c code. The rest of the project is pretty much the same, in how the data is processed, just, one has audio read over the PDM microphone, while the other by ADC, but both convert it into the same format.

    //Mostly from: http://www.pezzino.ch/stm32-adc-voltage-monitor/

#include <stm32f30x_rcc.h>
#include <stm32f30x_gpio.h>
#include <stm32f30x_tim.h>
#include <stm32f30x_misc.h>

static int calibration_value;
extern RCC_ClocksTypeDef RCC_Clocks;

{

GPIO_InitTypeDef      GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

// GPIOC Periph clock enable
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init( GPIOB, &GPIO_InitStructure );

// Calibration procedure

// Insert delay equal to 10 µs
_delay_us( 10 );

NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIM2 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init (&NVIC_InitStructure);

// TIM2 clock enable
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE);
// Time base configuration
RCC->CFGR |= 0X1400;
TIM_TimeBaseStructure.TIM_Prescaler = 1 - 1; // Operate at clock frequency
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit (TIM2, &TIM_TimeBaseStructure);
// TIM IT enable
TIM_ITConfig (TIM2, TIM_IT_Update, ENABLE);
/// TIM2 enable counter
TIM_Cmd (TIM2, ENABLE);

//
}

void TIM2_IRQHandler (void)
{
static uint8_t oversamp = 0;
static int32_t average = 0;
static int32_t oversampout = 0;

//

if (TIM_GetITStatus (TIM2, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit (TIM2, TIM_IT_Update);

oversampout += value;
oversamp++;

{
average = ((average*1023) + (value*1024))/1024;
value = value-(average/1024);
oversamp = 0;
oversampout = 0;
}
}

//

}


Problem is, the library for the STM32F3 and F4 are completely different so, it's not just a matter of copypasting, and/or changing pin names and numbers, the actual function set required is completely different.

Could someone please help me figure out how to set up/configure the ADC on the F4 the same way the F3 code above would?

I'm sort of on a time limit now, now and then I've been poking at the problem in my free time between study, but now its suddenly mid October and I wanted this done and working on the uC I have (stm32f407vet6) before my friend's birthday since, an RBG visualizer using this was sort of my present to them, or, going to be.

Anyway, could someone please help me out with the ADC initialization code? I have no idea what half these configurations/functions do so, it's proving extremely difficult to comb through the ST libraries to find equivalent ones, so far I have a few down, but, many lines of code don't seem to have an equivalent.

At least for the config/setup, this code should be identical to how it would be initialized on the arduino IDE. Or in short, I need the F4 equivalent for the contents of initadc().

• Stop using prehistoric dead SPL. Use HAL instead. – P__J__ Oct 19 at 10:02
• Use CMSIS and do it right. – Elliot Alderson Oct 19 at 12:13
• Find an example that does something close to what you need, and change it into what you need. – Chris Stratton Oct 19 at 16:30