|
发表于 2020-4-17 21:45:44
|
显示全部楼层
- /* INCLUDES ----------------------------------------------------------------- */
- #include "includes.h"
- #include "analogio.h"
- /**
- * @defgroup AnalogIO
- * @brief Analog input and output
- * @{
- */
- /* TYPEDEFS ----------------------------------------------------------------- */
- /**
- * @brief Analog Port and Pin define
- */
- typedef struct
- {
- GPIO_TypeDef* GPIOx;
- uint16_t GPIO_Pin;
- }AIO_PortPin_t;
- /* MACROS ------------------------------------------------------------------ */
- /* CONSTANTS --------------------------------------------------------------- */
- // PC0_ADC_IN10(W_AD1), PC1_ADC_IN11(W_AD2), PC2_ADC_IN12(W_AD3), PC3_ADC_IN13(W_AD4)
- // PA4_ADC_IN4(W_AD5), PA5_ADC_IN5(W_AD6), PA6_ADC_IN6(W_AD7), PA7_ADC_IN7(W_AD8),
- static const AIO_PortPin_t AIN_PORT_PIN[AIO_CH_NUM_IN] = {
- {GPIOC, GPIO_Pin_0}, {GPIOC, GPIO_Pin_1}, {GPIOC, GPIO_Pin_2}, {GPIOC, GPIO_Pin_3},
- {GPIOA, GPIO_Pin_4}, {GPIOA, GPIO_Pin_5}, {GPIOA, GPIO_Pin_6}, {GPIOA, GPIO_Pin_7},
- };
- /* GLOBAL VARIABLES --------------------------------------------------------- */
- /* GLOBAL FUNCTIONS --------------------------------------------------------- */
- /* LOCAL VARIABLES ---------------------------------------------------------- */
- // ADC sample DMA buffer must >= 3
- #define AIN_BUFF_SIZE 8
- static volatile uint16_t s_usAinBuffer[AIN_BUFF_SIZE][AIO_CH_NUM_IN];
- /* LOCAL FUNCTIONS ---------------------------------------------------------- */
- static void aio_sort_bubble(uint16_t *pbuf, uint8_t len);
- static uint16_t aio_sample_avg(uint16_t *pbuf, uint8_t len);
- static void AIO_InLowLevelInit(void);
- static void AIO_OutLowLevelInit(void);
- /**
- * @brief Init
- * @param None
- * @return Init status
- * @retval AIO_OK Init success
- */
- AIO_ERROR_t AIO_Init(void)
- {
- AIO_InLowLevelInit();
- AIO_OutLowLevelInit();
- return AIO_OK;
- }
- /**
- * @brief Start or Stop analog in sample
- * @param status 1-Start,0-Stop
- * @retval None
- */
- void AIO_SetInStatus(uint8_t status)
- {
- if (status)
- {
- AIO_InLowLevelInit();
- }
- else
- {
- ADC_DeInit(ADC1);
- DMA_DeInit(DMA1_Channel1);
- }
- }
- /**
- * @brief Get analog in voltage collect
- * @param[in] AIN_TYPE_INx: (AIO_TYPE_IN_MIN < x < AIO_TYPE_IN_MAX)
- * @param[out] VOL_MV: mv sampled signal, max 3300mV
- * @retval AIO_OK Success
- */
- AIO_ERROR_t AIO_GetInVoltage(AIO_TYPE_t AIO_TYPE_INx, uint16_t *VOL_MV)
- {
- uint8_t ofs;
- uint32_t val;
- uint16_t buff[AIN_BUFF_SIZE];
- if ((AIO_TYPE_INx <= AIO_TYPE_IN_MIN) || (AIO_TYPE_INx >= AIO_TYPE_IN_MAX) || (AIN_BUFF_SIZE <= 2))
- return AIO_ERROR_Param;
- ofs = AIO_TYPE_INx - AIO_TYPE_IN1;
- for (uint8_t i = 0; i < AIN_BUFF_SIZE; i++)
- buff[i] = s_usAinBuffer[i][ofs];
- aio_sort_bubble(&buff[0], AIN_BUFF_SIZE);
- val = aio_sample_avg(&buff[AIN_BUFF_SIZE / 4], AIN_BUFF_SIZE / 2);
- val = (val * 3300ul) / 0xFFF;
- if (VOL_MV)
- {
- *VOL_MV = val;
- return AIO_OK;
- }
- return AIO_ERROR_Param;
- }
- /**
- * @brief Set analog out voltage collect
- *
- * @param[in] AIN_TYPE_OUTx: (AIO_TYPE_OUT_MIN < x < AIO_TYPE_OUT_MAX)
- * @param[in] vol: mv out signal, max 3000mV
- *
- * @retval 0 Success
- */
- uint8_t AIO_SetOutVoltage(AIO_TYPE_t AIO_TYPE_OUTx, uint16_t vol)
- {
- if ((AIO_TYPE_OUTx <= AIO_TYPE_OUT_MIN) || (AIO_TYPE_OUTx >= AIO_TYPE_OUT_MAX) || vol > 3300)
- return 1;
- if (vol > 3000)
- return 1;
- vol = ((uint32_t)vol * 0xFFFul / 3000ul);
- (void)vol;
- return 0;
- }
- /**
- * @brief Bubble sort sample value
- * @param[in] pbuf: data buffer point
- * @param[in] len: data buffer length bytes
- * @retval None
- */
- static void aio_sort_bubble(uint16_t *pbuf, uint8_t len)
- {
- uint8_t i, j;
- uint16_t temp;
- for (i = 0; i < len - 1; i++)
- {
- for (j = 0; j < len - 1 - i; j++)
- {
- if (pbuf[j] > pbuf[j + 1])
- {
- temp = pbuf[j];
- pbuf[j] = pbuf[j + 1];
- pbuf[j + 1] = temp;
- }
- }
- }
- }
- /**
- * @brief Sample average value
- * @param[in] pbuf: data buffer point
- * @param[in] len: data buffer length
- * @retval None
- */
- static uint16_t aio_sample_avg(uint16_t *pbuf, uint8_t len)
- {
- uint32_t sum = 0;
- for (uint8_t i = 0; i < len; i++)
- sum += pbuf[i];
- return (sum / len);
- }
- /**
- * @brief ADC In low level init
- * @param None
- * @return None
- */
- static void AIO_InLowLevelInit(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- // Configure ADC1 Channel pin as analog input
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC, ENABLE);
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- for (uint8_t i = 0; i < AIO_CH_NUM_IN; i++)
- {
- GPIO_InitStructure.GPIO_Pin = AIN_PORT_PIN[i].GPIO_Pin;
- GPIO_Init(AIN_PORT_PIN[i].GPIOx, &GPIO_InitStructure);
- }
- // DMA1 channel1 configuration
- lib_memset(s_usAinBuffer, 0, sizeof(s_usAinBuffer));
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- DMA_DeInit(DMA1_Channel1);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&s_usAinBuffer;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = AIN_BUFF_SIZE * AIO_CH_NUM_IN;
- 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_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
- // Enable Transfer Complete interrupt
- DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
- //DMA_SetCurrDataCounter(DMA1_Channel1, 0);
- DMA_Cmd(DMA1_Channel1, ENABLE);
- // Enable the HSI oscillator
- //RCC_HSICmd(ENABLE);
- // Check that HSI oscillator is ready
- //while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
- // ADC clock
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- // ADC Init
- ADC_DeInit(ADC1);
- ADC_StructInit(&ADC_InitStructure);
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfConversion = AIO_CH_NUM_IN;
- ADC_Init(ADC1, &ADC_InitStructure);
- // ADC regular channel configuration
- // ADC_IN10,11,12,13
- // ADC_IN4,5,6,7
- ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_4Cycles);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_4Cycles);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_4Cycles);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_4Cycles);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_4Cycles);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_4Cycles);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_4Cycles);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_4Cycles);
- //ADC_RegularChannelConfig(ADC1, ADC_Channel_Vrefint, 9, ADC_SampleTime_16Cycles);
- //ADC_TempSensorVrefintCmd(ENABLE);
- // Enable the request after last transfer for DMA Circular mode
- ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
- // Enable ADC DMA
- ADC_DMACmd(ADC1, ENABLE);
- // Enable ADC
- ADC_Cmd(ADC1, ENABLE);
- // Wait until the ADC1 is ready
- while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET);
- // Start ADC Software Conversion
- ADC_SoftwareStartConv(ADC1);
- }
- /**
- * @brief ADC Out low level init
- * @param None
- * @return None
- */
- static void AIO_OutLowLevelInit(void)
- {
- }
- /**
- * @brief This function handles DMA1 Channel 1 interrupt request.
- * @param None
- * @retval None
- */
- void DMA1_Channel1_IRQHandler(void)
- {
- // DMA1 Channel1 Transfer Complete
- if(DMA_GetITStatus(DMA1_IT_TC1))
- {
- ADC_Cmd(ADC1, DISABLE);
- ADC_DMACmd(ADC1, DISABLE);
- DMA_Cmd(DMA1_Channel1, DISABLE);
- // Clear DMA1 Channel1 interrupt pending bits
- DMA_ClearITPendingBit(DMA1_IT_GL1);
- }
- }
- /**
- * @}
- */
复制代码 |
|