/** * @brief Configures the ADC multi-mode * @param hadc : pointer to a ADC_HandleTypeDef structure that contains * the configuration information for the specified ADC. * @param multimode : pointer to an ADC_MultiModeTypeDef structure that contains * the configuration information for multimode. * @retval HAL status */ HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode) { /* Check the parameters */ assert_param(IS_ADC_MODE(multimode->Mode)); assert_param(IS_ADC_DMA_ACCESS_MODE(multimode->DMAAccessMode)); assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay)); /* Process locked */ __HAL_LOCK(hadc); /* Set ADC mode */ ADC->CCR &= ~(ADC_CCR_MULTI); ADC->CCR |= multimode->Mode; /* Set the ADC DMA access mode */ ADC->CCR &= ~(ADC_CCR_DMA); ADC->CCR |= multimode->DMAAccessMode; /* Set delay between two sampling phases */ ADC->CCR &= ~(ADC_CCR_DELAY); ADC->CCR |= multimode->TwoSamplingDelay; /* Process unlocked */ __HAL_UNLOCK(hadc); /* Return function status */ return HAL_OK; }
void ADC_CommonInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct) { uint32_t tmpreg1 = 0; /* Check the parameters */ assert_param(IS_ADC_MODE(ADC_CommonInitStruct->ADC_Mode)); assert_param(IS_ADC_PRESCALER(ADC_CommonInitStruct->ADC_Prescaler)); assert_param(IS_ADC_DMA_ACCESS_MODE(ADC_CommonInitStruct->ADC_DMAAccessMode)); assert_param(IS_ADC_SAMPLING_DELAY(ADC_CommonInitStruct->ADC_TwoSamplingDelay)); /*---------------------------- ADC CCR Configuration -----------------*/ /* Get the ADC CCR value */ tmpreg1 = ADC->CCR; /* Clear MULTI, DELAY, DMA and ADCPRE bits */ tmpreg1 &= CR_CLEAR_MASK; /* Configure ADCx: Multi mode, Delay between two sampling time, ADC prescaler, and DMA access mode for multimode */ /* Set MULTI bits according to ADC_Mode value */ /* Set ADCPRE bits according to ADC_Prescaler value */ /* Set DMA bits according to ADC_DMAAccessMode value */ /* Set DELAY bits according to ADC_TwoSamplingDelay value */ tmpreg1 |= (uint32_t)(ADC_CommonInitStruct->ADC_Mode | ADC_CommonInitStruct->ADC_Prescaler | ADC_CommonInitStruct->ADC_DMAAccessMode | ADC_CommonInitStruct->ADC_TwoSamplingDelay); /* Write to ADC CCR */ ADC->CCR = tmpreg1; }
/******************************************************************************* * 函数名称: ADC_Init * 功能描述: 根据ADC_InitStruct 中指定的参数初始化ADCx 外围模块。 * 输入参数: (1)ADCx: 其中x 可以是1 2或3,用来选择ADC 外围模块. * (2)ADC_InitStruc:指向结构体ADC_InitTypeDef 的指针,该结构包括了指定ADC外围模块的配置信息。 * 输出参数: 无 * 返回参数: 无 ******************************************************************************/ void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct) { u32 tmpreg1 = 0; u8 tmpreg2 = 0; /* Check the parameters [检查参数 检查参数]*/ assert_param(IS_ADC_ALL_PERIPH(ADCx)); assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode)); assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode)); assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode)); assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv)); assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign)); assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel)); /*---------------------------- ADCx CR1 Configuration [ADCx CR1配置]-----------------*/ /* Get the ADCx CR1 value [得到ADCx CR1的值]*/ tmpreg1 = ADCx->CR1; /* Clear DUALMOD and SCAN bits [清除DUALMOD和SCAN位]*/ tmpreg1 &= CR1_CLEAR_Mask; /* Configure ADCx: Dual mode and scan conversion mode [配置ADCx:双重模式和扫描转换模式]*/ /* Set DUALMOD bits according to ADC_Mode value [设置DUALMOD位依照ADC模式值]*/ /* Set SCAN bit according to ADC_ScanConvMode value */ tmpreg1 |= (u32)(ADC_InitStruct->ADC_Mode | ((u32)ADC_InitStruct->ADC_ScanConvMode << 8)); /* Write to ADCx CR1 [写到ADCx CR1]*/ ADCx->CR1 = tmpreg1; /*---------------------------- ADCx CR2 Configuration [ADCx CR2配置]-----------------*/ /* Get the ADCx CR2 value [得到ADCx CR2的值]*/ tmpreg1 = ADCx->CR2; /* Clear CONT, ALIGN and EXTSEL bits [清除CONT,ALIGN和EXTSEL位]*/ tmpreg1 &= CR2_CLEAR_Mask; /* Configure ADCx: external trigger event and continuous conversion mode [配置ADCx:触发器时间和连续转换模式]*/ /* Set ALIGN bit according to ADC_DataAlign value [依照ADC_DataAlign的值设置ALIGN位]*/ /* Set EXTSEL bits according to ADC_ExternalTrigConv value [依照ADC_ExternalTrigConv的值设置EXTSEL位]*/ /* Set CONT bit according to ADC_ContinuousConvMode value [依照ADC_ContinuousConvMode的值设置CONT位]*/ tmpreg1 |= (u32)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv | ((u32)ADC_InitStruct->ADC_ContinuousConvMode << 1)); /* Write to ADCx CR2 [写入ADCx CR2]*/ ADCx->CR2 = tmpreg1; /*---------------------------- ADCx SQR1 Configuration [ADCx SQR1配置]-----------------*/ /* Get the ADCx SQR1 value [取得ADCx SQR1的值]*/ tmpreg1 = ADCx->SQR1; /* Clear L bits [清除L位]*/ tmpreg1 &= SQR1_CLEAR_Mask; /* Configure ADCx: regular channel sequence length [配置ADCx:规则信道长度]*/ /* Set L bits according to ADC_NbrOfChannel value [依照ADC_NbrOfChannel的值设置L位]*/ tmpreg2 |= (ADC_InitStruct->ADC_NbrOfChannel - 1); tmpreg1 |= ((u32)tmpreg2 << 20); /* Write to ADCx SQR1 [写入ADCx SQR1]*/ ADCx->SQR1 = tmpreg1; }
/** * @brief Initializes the ADCx peripheral according to the specified parameters * in the ADC_InitStruct. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral. * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains * the configuration information for the specified ADC peripheral. * @retval None */ void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct) { uint32_t tmpreg1 = 0; uint8_t tmpreg2 = 0; /* Check the parameters */ assert_param(IS_ADC_ALL_PERIPH(ADCx)); assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode)); assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode)); assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode)); assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv)); assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign)); assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel)); /*---------------------------- ADCx CR1 Configuration -----------------*/ /* Get the ADCx CR1 value */ tmpreg1 = ADCx->CR1; /* Clear DUALMOD and SCAN bits */ tmpreg1 &= CR1_CLEAR_Mask; /* Configure ADCx: Dual mode and scan conversion mode */ /* Set DUALMOD bits according to ADC_Mode value */ /* Set SCAN bit according to ADC_ScanConvMode value */ tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8)); /* Write to ADCx CR1 */ ADCx->CR1 = tmpreg1; /*---------------------------- ADCx CR2 Configuration -----------------*/ /* Get the ADCx CR2 value */ tmpreg1 = ADCx->CR2; /* Clear CONT, ALIGN and EXTSEL bits */ tmpreg1 &= CR2_CLEAR_Mask; /* Configure ADCx: external trigger event and continuous conversion mode */ /* Set ALIGN bit according to ADC_DataAlign value */ /* Set EXTSEL bits according to ADC_ExternalTrigConv value */ /* Set CONT bit according to ADC_ContinuousConvMode value */ tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv | ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1)); /* Write to ADCx CR2 */ ADCx->CR2 = tmpreg1; /*---------------------------- ADCx SQR1 Configuration -----------------*/ /* Get the ADCx SQR1 value */ tmpreg1 = ADCx->SQR1; /* Clear L bits */ tmpreg1 &= SQR1_CLEAR_Mask; /* Configure ADCx: regular channel sequence length */ /* Set L bits according to ADC_NbrOfChannel value */ tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1); tmpreg1 |= (uint32_t)tmpreg2 << 20; /* Write to ADCx SQR1 */ ADCx->SQR1 = tmpreg1; }
/** * @brief Enable ADC multimode and configure multimode parameters * @note Possibility to update parameters on the fly: * This function initializes multimode parameters, following * calls to this function can be used to reconfigure some parameters * of structure "ADC_MultiModeTypeDef" on the fly, without reseting * the ADCs (both ADCs of the common group). * The setting of these parameters is conditioned to ADC state. * For parameters constraints, see comments of structure * "ADC_MultiModeTypeDef". * @note To change back configuration from multimode to single mode, ADC must * be reset (using function HAL_ADC_Init() ). * @param hadc: ADC handle * @param multimode: Structure of ADC multimode configuration * @retval HAL status */ HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode) { HAL_StatusTypeDef tmp_hal_status = HAL_OK; ADC_HandleTypeDef tmphadcSlave; /* Check the parameters */ assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance)); assert_param(IS_ADC_MODE(multimode->Mode)); /* Process locked */ __HAL_LOCK(hadc); /* Set a temporary handle of the ADC slave associated to the ADC master */ ADC_MULTI_SLAVE(hadc, &tmphadcSlave); /* Parameters update conditioned to ADC state: */ /* Parameters that can be updated when ADC is disabled or enabled without */ /* conversion on going on regular group: */ /* - ADC master and ADC slave DMA configuration */ /* Parameters that can be updated only when ADC is disabled: */ /* - Multimode mode selection */ /* To optimize code, all multimode settings can be set when both ADCs of */ /* the common group are in state: disabled. */ if ((ADC_IS_ENABLE(hadc) == RESET) && (ADC_IS_ENABLE(&tmphadcSlave) == RESET) && (IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance)) ) { MODIFY_REG(hadc->Instance->CR1, ADC_CR1_DUALMOD , multimode->Mode ); } /* If one of the ADC sharing the same common group is enabled, no update */ /* could be done on neither of the multimode structure parameters. */ else { /* Update ADC state machine to error */ hadc->State = HAL_ADC_STATE_ERROR; tmp_hal_status = HAL_ERROR; } /* Process unlocked */ __HAL_UNLOCK(hadc); /* Return function status */ return tmp_hal_status; }
/** * @brief Initializes the ADCx peripheral according to the specified parameters * in the ADC_InitStruct. * @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral. * @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that * contains the configuration information for the specified * ADC peripheral. * @retval : None */ void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct) { uint32_t tmpreg1 = 0; /* Check the parameters */ assert_param(IS_ADC_ALL_PERIPH(ADCx)); assert_param(IS_ADC_MODE(ADC_InitStruct->ADC_Mode)); assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ScanConvMode)); assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode)); assert_param(IS_ADC_EXT_TRIG(ADC_InitStruct->ADC_ExternalTrigConv)); assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign)); assert_param(IS_ADC_REGULAR_LENGTH(ADC_InitStruct->ADC_NbrOfChannel)); /*---------------------------- ADCx ADCFG Configuration -----------------*/ /* Get the ADCx ADCFG value */ tmpreg1 = ADCx->ADCFG; /* Clear ADCPRE bits */ tmpreg1 &= ADCFG_CLEAR_Mask; /* Configure ADCx: AD convertion prescare*/ /* Set ADCPRE bit according to ADC_PRESCARE value */ tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_PRESCARE); /* Write to ADCx ADCFG */ ADCx->ADCFG = tmpreg1; /*---------------------------- ADCx ADCR Configuration -----------------*/ /* Get the ADCx ADCR value */ tmpreg1 = ADCx->ADCR; /* Clear ALIGN , ADMD, and TRGEN and TRGSEL bits */ tmpreg1 &= ADCR_CLEAR_Mask; /* Configure ADCx: external trigger event and AD conversion mode and ALIGN*/ /* Set ALIGN bit according to ADC_DataAlign value */ /* Set TRGEN bits according to ADC_TRGEN value */ /* Set TRGSEL bits according to ADC_ExternalTrigConv value */ /* Set ADMD bit according to ADC_Mode value */ /*tmpreg1 |= ((uint32_t)ADC_InitStruct->ADC_DataAlign) | ADC_InitStruct->ADC_ExternalTrigConv | ((uint32_t)ADC_InitStruct->ADC_Mode) | ((uint32_t)ADC_InitStruct->ADC_TRGEN);*/ tmpreg1 |= ((uint32_t)ADC_InitStruct->ADC_DataAlign) | ((uint32_t)ADC_InitStruct->ADC_Mode) | (((uint32_t)ADC_InitStruct->ADC_TRGEN)<<2); /* Write to ADCx ADCR */ ADCx->ADCR = tmpreg1; }
/** * @brief Configures the ADC multi-mode * @param hadc : pointer to a ADC_HandleTypeDef structure that contains * the configuration information for the specified ADC. * @param multimode : pointer to an ADC_MultiModeTypeDef structure that contains * the configuration information for multimode. * @retval HAL status */ HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode) { ADC_Common_TypeDef *tmpADC_Common; /* Check the parameters */ assert_param(IS_ADC_MODE(multimode->Mode)); assert_param(IS_ADC_DMA_ACCESS_MODE(multimode->DMAAccessMode)); assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay)); /* Process locked */ __HAL_LOCK(hadc); /* Pointer to the common control register to which is belonging hadc */ /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */ /* control register) */ tmpADC_Common = ADC_COMMON_REGISTER(hadc); /* Set ADC mode */ tmpADC_Common->CCR &= ~(ADC_CCR_MULTI); tmpADC_Common->CCR |= multimode->Mode; /* Set the ADC DMA access mode */ tmpADC_Common->CCR &= ~(ADC_CCR_DMA); tmpADC_Common->CCR |= multimode->DMAAccessMode; /* Set delay between two sampling phases */ tmpADC_Common->CCR &= ~(ADC_CCR_DELAY); tmpADC_Common->CCR |= multimode->TwoSamplingDelay; /* Process unlocked */ __HAL_UNLOCK(hadc); /* Return function status */ return HAL_OK; }