/*
********************************************************************************
* uC/OS-II
* AD采样驱动程序设计
* ARM Cortex-M3 Port
*
* File : ADCxDrv.C
* Version : V1.0
* By : 王宏强
*
* For : Stm32f10x
* Mode : Thumb2
* Toolchain :
* RealView Microcontroller Development Kit (MDK)
* Keil uVision
* Description : 定时器驱动
* 占用ADCx(ADC1,ADC2)
*
* 1,DMA规则模式(可靠性低,多路用此模式) 加宏定义 #define ADC_DMA
* 2,4路以下,用注入模式(可靠性高,占资源少)
*
* ADCxOpen
* ADCxClose
* ADCxWrite
* ADCxRead
* ADCxIoCtl
* ADCxInstall
* ADCxNuinstall
* Date : 2012.05.22
*******************************************************************************/
#include "ADCxDrv.h"
//DMA采样缓冲区
static volatile INT16U ADC_ConvertedValueTab[MAX_AD_SAMPLE_COUNTER] = {0};
static INT16U ADCxBuff[CHANNEL_COUNT] = {0}; //缓冲区数据平均值
static INT16U index = 0;
#ifdef UCOSII
static OS_EVENT *adcSem;
static INT8U err;
#endif
//总采样时间(单位ms) = 读样个数 * 采样1个值所用时间 / 72mHz * 1000
//static INT16U sampingTime = (INT16U)(CHANNEL_COUNT * ADCx_SAMPLE_COUNT *
// 239 * 5 / 9e3 + 1);
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*******************************************************************************
* Function Name :INT16U GetSampleTemp(INT16U order)
* Description :获取采样到的数据,并进行平均
* Input :order:通道序列号
* Output :返回本通道 采样平均值
* Other :
* Date :2012.05.23 14:48:23
*******************************************************************************/
static INT16U GetSampleValue(INT16U order)
{
u32 sum = 0;
u16 i = order;
if (order >= CHANNEL_COUNT) return 0; //序列号超出范围
for (i = order; i < MAX_AD_SAMPLE_COUNTER; i+=CHANNEL_COUNT)
{
sum += ADC_ConvertedValueTab[i];
}
sum /= ADCx_SAMPLE_COUNT;
return (u16)sum;
}
void StartAdc(FunctionalState stat)
{
if (stat == ENABLE) index = 0;
ADC_ITConfig(ADCx, ADC_IT_JEOC, stat);
ADC_Cmd(ADCx, stat);
}
/*******************************************************************************
* Function Name :static INT32S ADCxOpen(void *pd)
* Description :
* Input :
* Output :
* Other :
* Date :2012.05.23 10:25:06
*******************************************************************************/
static INT32S ADCxOpen(void *pd)
{
GPIO_InitTypeDef GPIO_InitStructure;
INT32U rccApb = 0;
INT16U gpioPin = 0;
/* Enable peripheral clocks ----------------------------------------------*/
/* Enable DMA1 and DMA2 clocks */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMAx, ENABLE);
#if ADCx_GPIOX_1_EN
rccApb |= RCC_APBXPeriph_GPIOX_1;
#endif
#if ADCx_GPIOX_2_EN
rccApb |= RCC_APBXPeriph_GPIOX_2;
#endif
#if ADCx_GPIOX_3_EN
rccApb |= RCC_APBXPeriph_GPIOX_3;
#endif
rccApb |= RCC_APBXPeriph_ADCx;
RCC_APB2PeriphClockCmd(rccApb, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
#if ADCx_GPIOX_1_EN
gpioPin = 0;
#if ADCx_CHANNEL0_EN
gpioPin |= ADCx_GPIOX_PIN_CH0;
#endif
#if ADCx_CHANNEL1_EN
gpioPin |= ADCx_GPIOX_PIN_CH1;
#endif
#if ADCx_CHANNEL2_EN
gpioPin |= ADCx_GPIOX_PIN_CH2;
#endif
#if ADCx_CHANNEL3_EN
gpioPin |= ADCx_GPIOX_PIN_CH3;
#endif
#if ADCx_CHANNEL4_EN
gpioPin |= ADCx_GPIOX_PIN_CH4;
#endif
#if ADCx_CHANNEL5_EN
gpioPin |= ADCx_GPIOX_PIN_CH5;
#endif
#if ADCx_CHANNEL6_EN
gpioPin |= ADCx_GPIOX_PIN_CH6;
#endif
#if ADCx_CHANNEL7_EN
gpioPin |= ADCx_GPIOX_PIN_CH7;
#endif
GPIO_InitStructure.GPIO_Pin = gpioPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADCx_GPIOX_1, &GPIO_InitStructure);
#endif
#if ADCx_GPIOX_2_EN
gpioPin = 0;
#if ADCx_CHANNEL8_EN
gpioPin |= ADCx_GPIOX_PIN_CH8;
#endif
#if ADCx_CHANNEL9_EN
gpioPin |= ADCx_GPIOX_PIN_CH9;
#endif
GPIO_InitStructure.GPIO_Pin = gpioPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADCx_GPIOX_2, &GPIO_InitStructure);
#endif
#if ADCx_GPIOX_3_EN
gpioPin = 0;
#if ADCx_CHANNEL10_EN
gpioPin |= ADCx_GPIOX_PIN_CH10;
#endif
#if ADCx_CHANNEL11_EN
gpioPin |= ADCx_GPIOX_PIN_CH11;
#endif
#if ADCx_CHANNEL12_EN
gpioPin |= ADCx_GPIOX_PIN_CH12;
#endif
#if ADCx_CHANNEL13_EN
gpioPin |= ADCx_GPIOX_PIN_CH13;
#endif
#if ADCx_CHANNEL14_EN
gpioPin |= ADCx_GPIOX_PIN_CH14;
#endif
#if ADCx_CHANNEL15_EN
gpioPin |= ADCx_GPIOX_PIN_CH15;
#endif
GPIO_InitStructure.GPIO_Pin = gpioPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADCx_GPIOX_3, &GPIO_InitStructure);
#endif
/* ADCx configuration ---------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = CHANNEL_COUNT;
ADC_Init(ADCx, &ADC_InitStructure);
#ifdef ADC_DMA
/* ADCx regular channels configuration */
#if ADCx_CHANNEL0_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_0, ORDER_CH0, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL1_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_1, ORDER_CH1, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL2_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_2, ORDER_CH2, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL3_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_3, ORDER_CH3, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL4_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_4, ORDER_CH4, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL5_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_5, ORDER_CH5, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL6_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_6, ORDER_CH6, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL7_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_7, ORDER_CH7, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL8_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_8, ORDER_CH8, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL9_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_9, ORDER_CH9, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL10_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_10, ORDER_CH10, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL11_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_11, ORDER_CH11, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL12_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_12, ORDER_CH12, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL13_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_13, ORDER_CH13, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL14_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_14, ORDER_CH14, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL15_EN
ADC_RegularChannelConfig(ADCx, ADC_Channel_15, ORDER_CH15, ADC_SampleTime_239Cycles5);
#endif
/* DMA1 channel1 configuration -------------------------------------------*/
DMA_DeInit(DMAx_Channelx);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADCx_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValueTab;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = (u32)MAX_AD_SAMPLE_COUNTER; //存储的个数
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_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMAx_Channelx, &DMA_InitStructure);
/* Enable ADCx DMA */
ADC_DMACmd(ADCx, ENABLE);
/* Enable DMA1 channel1 */
DMA_Cmd(DMAx_Channelx, ENABLE);
#else
/* Set injected sequencer length */
ADC_InjectedSequencerLengthConfig(ADC1, CHANNEL_COUNT);
#if ADCx_CHANNEL0_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_0, ORDER_CH0, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL1_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_1, ORDER_CH1, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL2_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_2, ORDER_CH2, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL3_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_3, ORDER_CH3, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL4_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_4, ORDER_CH4, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL5_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_5, ORDER_CH5, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL6_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_6, ORDER_CH6, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL7_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_7, ORDER_CH7, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL8_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_8, ORDER_CH8, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL9_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_9, ORDER_CH9, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL10_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_10, ORDER_CH10, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL11_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_11, ORDER_CH11, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL12_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_12, ORDER_CH12, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL13_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_13, ORDER_CH13, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL14_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_14, ORDER_CH14, ADC_SampleTime_239Cycles5);
#endif
#if ADCx_CHANNEL15_EN
ADC_InjectedChannelConfig(ADCx, ADC_Channel_15, ORDER_CH15, ADC_SampleTime_239Cycles5);
#endif
ADC_AutoInjectedConvCmd(ADCx, ENABLE);
ADC_ITConfig(ADCx, ADC_IT_JEOC, ENABLE);
/* Configure and enable ADC interrupt */
NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
StartAdc(DISABLE);
#endif
/* Enable ADCx */
ADC_Cmd(ADCx, ENABLE);
/* Enable ADCx reset calibaration register */
ADC_ResetCalibration(ADCx);
/* Check the end of ADCx reset calibration register */
while(ADC_GetResetCalibrationStatus(ADCx));
/* Start ADCx calibaration */
ADC_StartCalibration(ADCx);
/* Check the end of ADCx calibration */
while(ADC_GetCalibrationStatus(ADCx));
#ifdef UCOSII
adcSem = OSSemCreate(0);
#endif
return (INT32S)DRV_NO_ERR;
}
/*******************************************************************************
* Function Name :static INT32S ADCxClose(void *pd)
* Description :
* Input :
* Output :
* Other :
* Date :2012.05.24 09:10:25
*******************************************************************************/
static INT32S ADCxClose(void *pd)
{
ADC_SoftwareStartConvCmd(ADCx, DISABLE);
/* Enable DMA1 channel1 */
DMA_Cmd(DMAx_Channelx, DISABLE);
/* Enable ADCx DMA */
ADC_DMACmd(ADCx, DISABLE);
StartAdc(DISABLE);
return (INT32S)DRV_NO_ERR;
}
/*******************************************************************************
* Function Name :static INT32S ADCxWrite(INT8S *buffer, INT32U lenToWrite, INT8U waitType)
* Description :无效
* Input :
* Output :
* Other :
* Date :2012.05.23 14:19:47
*******************************************************************************/
static INT32S ADCxWrite(INT8S *buffer, INT32U lenToWrite, INT8U waitType)
{
return (INT32S)DRV_NO_ERR;
}
/*******************************************************************************
* Function Name :static INT32S ADCxRead(INT8S *buffer, INT32U blen, INT32U lenToRead, INT8U waitType)
* Description :读取采样到的数据
* Input :*buffer:采样缓冲。lenToRead:采取长度(单位是字节)
* Output :
* Other :
* Date :2012.05.23 14:19:49
*******************************************************************************/
static INT32S ADCxRead(INT8S *buffer, INT32U blen, INT32U lenToRead, INT8U waitType)
{
int i = 0;
if (lenToRead > sizeof(ADCxBuff))
return (INT32S)DRV_READ_FAIL;
for (i = 0; i < CHANNEL_COUNT; i++)
{
ADCxBuff[i] = GetSampleValue(i);
}
memcpy(buffer, ADCxBuff, lenToRead);
return (INT32S)DRV_NO_ERR;
}
/*******************************************************************************
* Function Name :static INT32S ADCxIoCtl(INT32U too, void *pd)
* Description :ADCX采样控制
* Input :too: 1-停止 AD采样
* 2-开始 AD采样 延迟直接退出。
* 3-开始 并等待采样缓冲填满后 停止采样。(UCOSII 系统下)
* Output :
* Other :
* Date :2012.05.23 14:19:51
*******************************************************************************/
static INT32S ADCxIoCtl(INT32U too, void *pd)
{
switch (too)
{
case 1 : StartAdc(DISABLE); break;
case 2 : StartAdc(ENABLE); break;
#ifdef UCOSII
case 3 :
StartAdc(ENABLE);
OSSemPend(adcSem, 0, &err);
break;
#endif
default : return(INT32S)DRV_CTRL_FAIL;
}
return (INT32S)DRV_NO_ERR;
}
/*******************************************************************************
* Function Name :INT32S ADCxInstall(UDFOperationsType *op)
* Description :安装ADCx驱动
* Input :
* Output :
* Other :
* Date :2012.05.13
*******************************************************************************/
INT32S ADCxInstall(UDFOperationsType *op)
{
op->devOpen = ADCxOpen;
op->devClose = ADCxClose;
op->devWrite = ADCxWrite;
op->devRead = ADCxRead;
op->devIoctl = ADCxIoCtl;
return (INT32S)DRV_NO_ERR;
}
/*******************************************************************************
* Function Name :INT32S ADCxNuinstall(UDFOperationsType *op)
* Description :卸载ADCx驱动
* Input :
* Output :
* Other :
* Date :2012.05.13
*******************************************************************************/
INT32S ADCxNuinstall(UDFOperationsType *op)
{
INT32S res = (INT32S)DRV_NO_ERR;
void *pd = NULL;
if (op->devClose != NULL)
res = op->devClose(pd);
op->devOpen = NULL;
op->devClose = NULL;
op->devWrite = NULL;
op->devRead = NULL;
op->devIoctl = NULL;
return res;
}
/*******************************************************************************
* Function Name :void ADC_IRQHandler(void)
* Description :ADC中断函数
* Input :
* Output :
* Other :
* Date :2012.05.24 14:49:49
*******************************************************************************/
void ADC_IRQHandler(void)
{
#ifdef UCOSII
OSIntEnter();
#endif
/* Clear ADC1 EOC pending interrupt bit */
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC); //清除规则采样中断
if (index >= MAX_AD_SAMPLE_COUNTER)
{
StartAdc(DISABLE);
#ifdef UCOSII
OSSemPost(adcSem);
#endif
}
else
{
#if CHANNEL_COUNT > 0
ADC_ConvertedValueTab[index++] = ADC_GetInjectedConversionValue(ADCx, ADC_InjectedChannel_1);
#endif
#if CHANNEL_COUNT > 1
ADC_ConvertedValueTab[index++] = ADC_GetInjectedConversionValue(ADCx, ADC_InjectedChannel_2);
#endif
#if CHANNEL_COUNT > 2
ADC_ConvertedValueTab[index++] = ADC_GetInjectedConversionValue(ADCx, ADC_InjectedChannel_3);
#endif
#if CHANNEL_COUNT > 3
ADC_ConvertedValueTab[index++] = ADC_GetInjectedConversionValue(ADCx, ADC_InjectedChannel_4);
#endif
}
#ifdef UCOSII
OSIntExit();
#endif
}