/* * initadcs.c * * Created on: March 31, 2015 * Author: David harrison */ #include "sfx.h" #define VBATEN_MASK 0x1000000 void ADC1Init(void) { // Analogue input pins have already been configured in DefinePins() // Configure the ADC clocks RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div128); // Enable ADC1/2 and ADC3/4 clocks and DMA1 clock RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12 | RCC_AHBPeriph_DMA1, ENABLE); // Calibration procedure // Set ADC voltage regulator on ADC1->CR |= ADC_CR_ADVREGEN_0; // Insert delay equal to 10 µs core_timer_delay_us(10); // Set calibration mode ADC1->CR &= (~ADC_CR_ADCALDIF); ADC1->CR |= ADC_CalibrationMode_Single; // Start calibration ADC1->CR |= ADC_CR_ADCAL; // Wait until calibration is complete while (ADC1->CR & ADC_CR_ADCAL); ADC_Cmd(ADC1, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC1, &ADC_CommonInitStructure); // ADC1_17 monitors VBAT/2 internally, but need to set the VBATEN bit in the ADC12_CCR ADC1_2->CCR |= VBATEN_MASK; ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 10; ADC_Init(ADC1, &ADC_InitStructure); // ADC1 regular channel configuration ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5); // B1 8S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_7Cycles5); // B1 7S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_SampleTime_7Cycles5); // B1 6S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_7Cycles5); // B1 5S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_SampleTime_7Cycles5); // B1 4S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 6, ADC_SampleTime_7Cycles5); // B1 3S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 7, ADC_SampleTime_7Cycles5); // B1 2S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 8, ADC_SampleTime_7Cycles5); // B1 1S Input ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 9, ADC_SampleTime_7Cycles5); // Rx Batt Input ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 10, ADC_SampleTime_7Cycles5); // Bkup Batt (VBAT/2) // DMA1 channel1 configuration DMA_Cmd(DMA1_Channel1, DISABLE); DMA_DeInit(DMA1_Channel1); DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (U32)&(ADC1->DR); // Source address DMA_InitStructure.DMA_MemoryBaseAddr = (U32)&sysData.Dataset1.BattVoltages.ADCValues; // Destination address DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 10; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(DMA1_Channel1, &DMA_InitStructure); // Enable DMA1 Channel1 transfer DMA_Cmd(DMA1_Channel1, ENABLE); // Clear DMA1_Ch1 global flag DMA_ClearFlag(DMA1_FLAG_GL1); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); // Enable DMA for ADC ADC_DMACmd(ADC1, ENABLE); // wait for ADRDY while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); // Start first ADC1 Software Conversion ADC_StartConversion(ADC1); } void ADC2Init(void) { // Analogue input pins have already been configured in DefinePins() // Configure the ADC/DMA clocks - already done in ADC1Init() // Enable ADC1/2 clocks and DMA2 clock RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); // Calibration procedure // Set ADC voltage regulator on ADC2->CR |= ADC_CR_ADVREGEN_0; // Insert delay equal to 10 µs core_timer_delay_us(10); // Set calibration mode ADC2->CR &= (~ADC_CR_ADCALDIF); ADC2->CR |= ADC_CalibrationMode_Single; // Start calibration ADC2->CR |= ADC_CR_ADCAL; // Wait until calibration is complete while (ADC2->CR & ADC_CR_ADCAL); ADC_Cmd(ADC2, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC2, &ADC_CommonInitStructure); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 8; ADC_Init(ADC2, &ADC_InitStructure); // ADC2 regular channel configuration ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5); // B2 8S Input ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 2, ADC_SampleTime_7Cycles5); // B2 7S Input ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 3, ADC_SampleTime_7Cycles5); // B2 6S Input ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 4, ADC_SampleTime_7Cycles5); // B2 5S Input ADC_RegularChannelConfig(ADC2, ADC_Channel_5, 5, ADC_SampleTime_7Cycles5); // B2 4S Input ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 6, ADC_SampleTime_7Cycles5); // B2 3S Input ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 7, ADC_SampleTime_7Cycles5); // B2 2S Input ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 8, ADC_SampleTime_7Cycles5); // B2 1S Input // DMA2 channel1 configuration DMA_Cmd(DMA2_Channel1, DISABLE); DMA_DeInit(DMA2_Channel1); DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (U32)&(ADC2->DR); // Source address DMA_InitStructure.DMA_MemoryBaseAddr = (U32)&sysData.Dataset2.BattVoltages.ADCValues; // Destination address DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 8; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(DMA2_Channel1, &DMA_InitStructure); // Enable DMA2_Ch1 transfer DMA_Cmd(DMA2_Channel1, ENABLE); // Clear DMA2_Ch1 global flag DMA_ClearFlag(DMA2_FLAG_GL1); // Enable ADC2 ADC_Cmd(ADC2, ENABLE); // Enable DMA for ADC ADC_DMACmd(ADC2, ENABLE); // wait for ADRDY while (!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY)); // Start first ADC2 Software Conversion ADC_StartConversion(ADC2); } void ADC3Init(void) { // Analogue input pins have already been configured in DefinePins() RCC_ADCCLKConfig(RCC_ADC34PLLCLK_Div128); // Enable ADC3/4 clocks RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC34, ENABLE); // Calibration procedure // Set ADC voltage regulator on ADC3->CR |= ADC_CR_ADVREGEN_0; // Insert delay equal to 10 µs core_timer_delay_us(10); // Set calibration mode ADC3->CR &= (~ADC_CR_ADCALDIF); ADC3->CR |= ADC_CalibrationMode_Single; // Start calibration ADC3->CR |= ADC_CR_ADCAL; // Wait until calibration is complete while (ADC3->CR & ADC_CR_ADCAL); ADC_Cmd(ADC3, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC3, &ADC_CommonInitStructure); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 8; ADC_Init(ADC3, &ADC_InitStructure); // ADC3 regular channel configuration ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_7Cycles5); // B3 8S Input ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 2, ADC_SampleTime_7Cycles5); // B3 7S Input ADC_RegularChannelConfig(ADC3, ADC_Channel_13, 3, ADC_SampleTime_7Cycles5); // B3 6S Input ADC_RegularChannelConfig(ADC3, ADC_Channel_6, 4, ADC_SampleTime_7Cycles5); // B3 5S Input ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 5, ADC_SampleTime_7Cycles5); // B3 4S Input ADC_RegularChannelConfig(ADC3, ADC_Channel_14, 6, ADC_SampleTime_7Cycles5); // B3 3S Input ADC_RegularChannelConfig(ADC3, ADC_Channel_15, 7, ADC_SampleTime_7Cycles5); // B3 2S Input ADC_RegularChannelConfig(ADC3, ADC_Channel_16, 8, ADC_SampleTime_7Cycles5); // B3 1S Input // DMA1 channel3 configuration DMA_Cmd(DMA1_Channel3, DISABLE); DMA_DeInit(DMA1_Channel3); DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (U32)&(ADC3->DR); // Source address DMA_InitStructure.DMA_MemoryBaseAddr = (U32)&sysData.Dataset3.BattVoltages.ADCValues; // Destination address DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 8; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(DMA1_Channel3, &DMA_InitStructure); // Enable DMA1 Channel3 transfer DMA_Cmd(DMA1_Channel3, ENABLE); // Clear DMA1_Ch3 global flag DMA_ClearFlag(DMA1_FLAG_GL3); // Enable ADC3 ADC_Cmd(ADC3, ENABLE); // Enable DMA for ADC ADC_DMACmd(ADC3, ENABLE); // wait for ADRDY while (!ADC_GetFlagStatus(ADC3, ADC_FLAG_RDY)); // Start first ADC3 Software Conversion ADC_StartConversion(ADC3); } void ADC4Init(void) { // Analogue input pins have already been configured in DefinePins() // Configure the ADC/DMA clocks - already done in ADC1Init() // Calibration procedure // Set ADC voltage regulator on ADC4->CR |= ADC_CR_ADVREGEN_0; // Insert delay equal to 10 µs core_timer_delay_us(10); // Set calibration mode ADC4->CR &= (~ADC_CR_ADCALDIF); ADC4->CR |= ADC_CalibrationMode_Single; // Start calibration ADC4->CR |= ADC_CR_ADCAL; // Wait until calibration is complete while (ADC4->CR & ADC_CR_ADCAL); ADC_Cmd(ADC4, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC4, &ADC_CommonInitStructure); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 12; ADC_Init(ADC4, &ADC_InitStructure); // ADC4 regular channel configuration ADC_RegularChannelConfig(ADC4, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5); // B1 Curr Measure Input ADC_RegularChannelConfig(ADC4, ADC_Channel_2, 2, ADC_SampleTime_7Cycles5); // B2 Curr Measure Input ADC_RegularChannelConfig(ADC4, ADC_Channel_3, 3, ADC_SampleTime_7Cycles5); // B3 Curr Measure Input ADC_RegularChannelConfig(ADC4, ADC_Channel_4, 4, ADC_SampleTime_7Cycles5); // B4 Curr Measure Input ADC_RegularChannelConfig(ADC4, ADC_Channel_11, 5, ADC_SampleTime_7Cycles5); // Temperature1 Input ADC_RegularChannelConfig(ADC4, ADC_Channel_10, 6, ADC_SampleTime_7Cycles5); // Temperature2 Input ADC_RegularChannelConfig(ADC4, ADC_Channel_9, 7, ADC_SampleTime_7Cycles5); // Temperature3 Input ADC_RegularChannelConfig(ADC4, ADC_Channel_8, 8, ADC_SampleTime_7Cycles5); // Temperature4 Input ADC_RegularChannelConfig(ADC4, ADC_Channel_7, 9, ADC_SampleTime_7Cycles5); // Temperature5 Input ADC_RegularChannelConfig(ADC4, ADC_Channel_13, 10, ADC_SampleTime_7Cycles5); // Temperature6 Input ADC_RegularChannelConfig(ADC4, ADC_Channel_12, 11, ADC_SampleTime_7Cycles5); // Temperature7 Input ADC_RegularChannelConfig(ADC4, ADC_Channel_5, 12, ADC_SampleTime_7Cycles5); // Temperature8 Input // DMA1 channel4 configuration DMA_Cmd(DMA1_Channel4, DISABLE); DMA_DeInit(DMA1_Channel3); DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (U32)&(ADC4->DR); // Source address DMA_InitStructure.DMA_MemoryBaseAddr = (U32)&sysData.CurrentTempADC; // Destination address DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 12; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(DMA1_Channel4, &DMA_InitStructure); // Enable DMA1 Channel4 transfer DMA_Cmd(DMA1_Channel4, ENABLE); // Clear DMA1_Ch4 global flag DMA_ClearFlag(DMA1_FLAG_GL4); // Enable ADC4 ADC_Cmd(ADC4, ENABLE); // Enable DMA for ADC ADC_DMACmd(ADC4, ENABLE); // wait for ADRDY while (!ADC_GetFlagStatus(ADC4, ADC_FLAG_RDY)); // Start first ADC4 Software Conversion ADC_StartConversion(ADC4); }