272 lines
10 KiB
C
272 lines
10 KiB
C
#include "McuBspAdc.h"
|
||
|
||
static void McuBspAdc00IntHandler(void);
|
||
static void McuBspAdc01IntHandler(void);
|
||
static void McuBspAdc10IntHandler(void);
|
||
|
||
static uint32_t gMcuAdcValue[20] = {0}; //最多20个AD值
|
||
|
||
static uint8_t gMcuAdc00WorkStart = 0; // ADC00序列开始采样标志
|
||
static uint8_t gMcuAdc00WorkEnd = 0; // ADC00序列结束采样标志
|
||
|
||
static uint8_t gMcuAdc01WorkStart = 0; // ADC01序列开始采样标志
|
||
static uint8_t gMcuAdc01WorkEnd = 0; // ADC01序列结束采样标志
|
||
|
||
static uint8_t gMcuAdc10WorkStart = 0; // ADC10序列开始采样标志
|
||
static uint8_t gMcuAdc10WorkEnd = 0; // ADC10序列结束采样标志
|
||
|
||
/* ADC从属于哪个具体的ADC序列 */
|
||
static uint8_t gMcuAdcBelong[20] = {MCU_BSP_ADC0_BELONG, MCU_BSP_ADC1_BELONG, MCU_BSP_ADC2_BELONG,
|
||
MCU_BSP_ADC3_BELONG, MCU_BSP_ADC4_BELONG, MCU_BSP_ADC5_BELONG,
|
||
MCU_BSP_ADC6_BELONG, MCU_BSP_ADC7_BELONG, MCU_BSP_ADC8_BELONG,
|
||
MCU_BSP_ADC9_BELONG, MCU_BSP_ADC10_BELONG, MCU_BSP_ADC11_BELONG,
|
||
MCU_BSP_ADC12_BELONG, MCU_BSP_ADC13_BELONG, MCU_BSP_ADC14_BELONG,
|
||
MCU_BSP_ADC15_BELONG, MCU_BSP_ADC16_BELONG, MCU_BSP_ADC17_BELONG,
|
||
MCU_BSP_ADC18_BELONG, MCU_BSP_ADC19_BELONG};
|
||
static uint8_t gMcuBspAdcInitFlag = 0; // 初始化完成标志位
|
||
|
||
int McuBspAdcInit(void)
|
||
{
|
||
if (gMcuBspAdcInitFlag == 1) {
|
||
return 0;
|
||
}
|
||
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
|
||
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC1);
|
||
if (MCU_BSP_ADC0_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC0_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC0_GPIO, MCU_BSP_ADC0_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC1_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC1_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC1_GPIO, MCU_BSP_ADC1_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC2_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC2_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC2_GPIO, MCU_BSP_ADC2_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC3_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC3_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC3_GPIO, MCU_BSP_ADC3_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC4_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC4_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC4_GPIO, MCU_BSP_ADC4_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC5_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC5_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC5_GPIO, MCU_BSP_ADC5_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC6_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC6_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC6_GPIO, MCU_BSP_ADC6_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC7_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC7_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC7_GPIO, MCU_BSP_ADC7_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC8_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC8_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC8_GPIO, MCU_BSP_ADC8_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC9_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC9_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC9_GPIO, MCU_BSP_ADC9_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC10_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC10_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC10_GPIO, MCU_BSP_ADC10_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC11_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC11_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC11_GPIO, MCU_BSP_ADC11_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC12_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC12_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC12_GPIO, MCU_BSP_ADC12_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC13_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC13_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC13_GPIO, MCU_BSP_ADC13_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC14_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC14_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC14_GPIO, MCU_BSP_ADC14_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC15_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC15_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC15_GPIO, MCU_BSP_ADC15_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC16_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC16_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC16_GPIO, MCU_BSP_ADC16_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC17_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC17_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC17_GPIO, MCU_BSP_ADC17_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC18_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC18_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC18_GPIO, MCU_BSP_ADC18_GPIO_PIN);
|
||
}
|
||
if (MCU_BSP_ADC19_USE == 1) {
|
||
SysCtlPeripheralEnable(MCU_BSP_ADC19_GPIO_CLK);
|
||
GPIOPinTypeADC(MCU_BSP_ADC19_GPIO, MCU_BSP_ADC19_GPIO_PIN);
|
||
}
|
||
/* 只使用了这三个序列 */
|
||
ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0); // 第一个0是序列值,可占有8个通道,后面一个0是优先级
|
||
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 1); // 第一个1是序列值,可占有4个通道,后面一个1是优先级。
|
||
ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_PROCESSOR, 0); //
|
||
|
||
ADCHardwareOversampleConfigure(ADC0_BASE, 8); // 硬件获取8次取平均值
|
||
ADCHardwareOversampleConfigure(ADC1_BASE, 8); // 硬件获取8次取平均值
|
||
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 0, MCU_BSP_ADC0_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 1, MCU_BSP_ADC1_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 2, MCU_BSP_ADC2_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 3, MCU_BSP_ADC3_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 4, MCU_BSP_ADC4_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 5, MCU_BSP_ADC5_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 6, MCU_BSP_ADC6_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 0, 7, MCU_BSP_ADC7_CHANNEL | ADC_CTL_IE | ADC_CTL_END);
|
||
|
||
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, MCU_BSP_ADC8_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 1, 1, MCU_BSP_ADC9_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 1, 2, MCU_BSP_ADC10_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC0_BASE, 1, 3, MCU_BSP_ADC11_CHANNEL | ADC_CTL_IE | ADC_CTL_END);
|
||
|
||
//ADCSequenceStepConfigure(ADC1_BASE, 0, 0, MCU_BSP_ADC12_CHANNEL);
|
||
//ADCSequenceStepConfigure(ADC1_BASE, 0, 1, MCU_BSP_ADC13_CHANNEL);
|
||
//ADCSequenceStepConfigure(ADC1_BASE, 0, 2, MCU_BSP_ADC14_CHANNEL);
|
||
//ADCSequenceStepConfigure(ADC1_BASE, 0, 3, MCU_BSP_ADC15_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC1_BASE, 0, 0, MCU_BSP_ADC16_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC1_BASE, 0, 1, MCU_BSP_ADC17_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC1_BASE, 0, 2, MCU_BSP_ADC18_CHANNEL);
|
||
ADCSequenceStepConfigure(ADC1_BASE, 0, 3, MCU_BSP_ADC19_CHANNEL | ADC_CTL_IE | ADC_CTL_END);
|
||
|
||
BSP_IntVectSet((CPU_INT08U)BSP_INT_ID_ADC0_0, (CPU_FNCT_VOID)McuBspAdc00IntHandler);
|
||
BSP_IntVectSet((CPU_INT08U)BSP_INT_ID_ADC0_1, (CPU_FNCT_VOID)McuBspAdc01IntHandler);
|
||
BSP_IntVectSet((CPU_INT08U)BSP_INT_ID_ADC1_0, (CPU_FNCT_VOID)McuBspAdc10IntHandler);
|
||
|
||
BSP_IntEn(BSP_INT_ID_ADC0_0);
|
||
BSP_IntEn(BSP_INT_ID_ADC0_1);
|
||
BSP_IntEn(BSP_INT_ID_ADC1_0);
|
||
|
||
ADCIntEnable(ADC0_BASE, 0);
|
||
ADCIntEnable(ADC0_BASE, 1);
|
||
ADCIntEnable(ADC1_BASE, 0);
|
||
|
||
IntEnable(INT_ADC0SS0);
|
||
IntEnable(INT_ADC0SS1);
|
||
IntEnable(INT_ADC1SS0);
|
||
|
||
ADCSequenceEnable(ADC0_BASE, 0);
|
||
ADCSequenceEnable(ADC0_BASE, 1);
|
||
ADCSequenceEnable(ADC1_BASE, 0);
|
||
|
||
ADCIntClear(ADC0_BASE, 0);
|
||
ADCIntClear(ADC0_BASE, 1);
|
||
ADCIntClear(ADC1_BASE, 0);
|
||
return 0;
|
||
}
|
||
|
||
/* 获取ADC的采样值 */
|
||
int McuBspAdcGetValue(uint8_t AdcCh, float *Value)
|
||
{
|
||
if (Value == NULL) {
|
||
return -1;
|
||
}
|
||
|
||
switch(gMcuAdcBelong[AdcCh]) {
|
||
case MCU_BSP_ADC_TO_ADC0_0:
|
||
while (gMcuAdc00WorkStart != 0){
|
||
|
||
}
|
||
gMcuAdc00WorkStart = 1;
|
||
gMcuAdc00WorkEnd = 0;
|
||
ADCIntClear(ADC0_BASE, 0);
|
||
ADCProcessorTrigger(ADC0_BASE, 0);
|
||
/* 等待采样完成 */
|
||
while (gMcuAdc00WorkEnd == 0) {
|
||
|
||
}
|
||
break;
|
||
case MCU_BSP_ADC_TO_ADC0_1:
|
||
while (gMcuAdc01WorkStart != 0){
|
||
|
||
}
|
||
gMcuAdc01WorkStart = 1;
|
||
gMcuAdc01WorkEnd = 0;
|
||
ADCIntClear(ADC0_BASE, 1);
|
||
ADCProcessorTrigger(ADC0_BASE, 1);
|
||
/* 等待采样完成 */
|
||
while (gMcuAdc01WorkEnd == 0) {
|
||
|
||
}
|
||
break;
|
||
case MCU_BSP_ADC_TO_ADC0_2:
|
||
break;
|
||
case MCU_BSP_ADC_TO_ADC0_3:
|
||
break;
|
||
case MCU_BSP_ADC_TO_ADC1_0:
|
||
while (gMcuAdc10WorkStart != 0){
|
||
|
||
}
|
||
gMcuAdc10WorkStart = 1;
|
||
gMcuAdc10WorkEnd = 0;
|
||
ADCIntClear(ADC1_BASE, 0);
|
||
ADCProcessorTrigger(ADC1_BASE, 0);
|
||
/* 等待采样完成 */
|
||
while (gMcuAdc10WorkEnd == 0) {
|
||
|
||
}
|
||
break;
|
||
case MCU_BSP_ADC_TO_ADC1_1:
|
||
break;
|
||
case MCU_BSP_ADC_TO_ADC1_2:
|
||
break;
|
||
case MCU_BSP_ADC_TO_ADC1_3:
|
||
break;
|
||
}
|
||
*Value = gMcuAdcValue[AdcCh] * 3300.0f / 4096.0f; // 3300毫伏是板级电路的参考电压。
|
||
return 0;
|
||
}
|
||
|
||
static void McuBspAdc00IntHandler(void)
|
||
{
|
||
CPU_SR_ALLOC();
|
||
OSIntEnter(); //通知系统进入OS中断
|
||
OS_ENTER_CRITICAL();
|
||
ADCIntClear(ADC0_BASE, 0);
|
||
ADCSequenceDataGet(ADC0_BASE, 0, &gMcuAdcValue[0]);
|
||
gMcuAdc00WorkStart = 0;
|
||
gMcuAdc00WorkEnd = 1;
|
||
OS_EXIT_CRITICAL();
|
||
OSIntExit(); //
|
||
}
|
||
|
||
static void McuBspAdc01IntHandler(void)
|
||
{
|
||
CPU_SR_ALLOC();
|
||
OSIntEnter(); //通知系统进入OS中断
|
||
OS_ENTER_CRITICAL();
|
||
ADCIntClear(ADC0_BASE, 1);
|
||
ADCSequenceDataGet(ADC0_BASE, 1, &gMcuAdcValue[8]);
|
||
gMcuAdc01WorkStart = 0;
|
||
gMcuAdc01WorkEnd = 1;
|
||
OS_EXIT_CRITICAL();
|
||
OSIntExit(); //
|
||
}
|
||
|
||
static void McuBspAdc10IntHandler(void)
|
||
{
|
||
CPU_SR_ALLOC();
|
||
OSIntEnter(); //通知系统进入OS中断
|
||
OS_ENTER_CRITICAL();
|
||
ADCIntClear(ADC1_BASE, 0);
|
||
ADCSequenceDataGet(ADC1_BASE, 0, &gMcuAdcValue[16]);
|
||
gMcuAdc10WorkStart = 0;
|
||
gMcuAdc10WorkEnd = 1;
|
||
OS_EXIT_CRITICAL();
|
||
OSIntExit(); //
|
||
}
|