Part Number:TMS320F280049C
ecap_dev.h
#ifndef __ECAP_DEV_H
#define __ECAP_DEV_H
#include "user_config.h"
#include "driverlib.h"
#include "device.h"
#include "ecap.h"
#include <stdint.h>
typedef struct
{
uint32_t base;
uint16_t preScalerValue;
uint32_t interruptNum;
XBAR_InputNum xbarInputNum;
uint16_t xbarInputPin;
uint32_t pinConfig;
GPIO_Direction pinIO;
GPIO_QualificationMode qualification;
ECAP_CaptureMode captureMode;
ECAP_Events event;
ECAP_EventPolarity eventPolarity[4];
ECAP_SyncOutMode syncOutMode;
ECAP_InputCaptureSignals input;
volatile uint32_t cap1Count;
volatile uint32_t cap2Count;
volatile uint32_t cap3Count;
volatile uint32_t cap4Count;
uint32_t intCount;
void (*ISR)(void);
float inputFrequency;
uint16_t ackGroup;
} ECAP_DevTypeDef;
void ECAP_DevInit(ECAP_DevTypeDef *pDev); /* eCAP设备初始化 */
#endif
ecap_dev.c
//
// Included Files
//
#include "ecap_dev.h"
//
// Defines
//
#define ECAP_DEV1_BASE ECAP1_BASE
#define ECAP_DEV1_INPUT_PRESCALER 31
#define ECAP_DEV1_INT_NUM INT_ECAP2
#define ECAP_DEV1_XBAR_INPUT_NUM XBAR_INPUT15
#define ECAP_DEV1_XBAR_INPUT_PIN 7
#define ECAP_DEV1_PIN_CONFIG GPIO_7_GPIO7
#define ECAP_DEV1_PIN_DIRECTION_MODE GPIO_DIR_MODE_IN
#define ECAP_DEV1_PIN_QUALIFICATION_MODE GPIO_QUAL_ASYNC
#define ECAP_DEV1_ISR ECAP1_ISR
#define ECAP_DEV1_ECAPTURE_MODE ECAP_ONE_SHOT_CAPTURE_MODE
#define ECAP_DEV1_EVENT ECAP_EVENT_4
#define ECAP_DEV1_EVENT1_POLARITY ECAP_EVNT_RISING_EDGE
#define ECAP_DEV1_EVENT2_POLARITY ECAP_EVNT_RISING_EDGE
#define ECAP_DEV1_EVENT3_POLARITY ECAP_EVNT_RISING_EDGE
#define ECAP_DEV1_EVENT4_POLARITY ECAP_EVNT_RISING_EDGE
#define ECAP_DEV1_INPUT_ECAPTURE_SIGNAL ECAP_INPUT_INPUTXBAR15
#define ECAP_DEV1_SYNC_OUT_MODE ECAP_SYNC_OUT_DISABLED
#define ECAP_DEV1_INTERRUPT_ACK_GROUP INTERRUPT_ACK_GROUP4
#define ECAP_DEV2_BASE ECAP2_BASE
#define ECAP_DEV2_INPUT_PRESCALER 31
#define ECAP_DEV2_INT_NUM INT_ECAP1
#define ECAP_DEV2_XBAR_INPUT_NUM XBAR_INPUT16
#define ECAP_DEV2_XBAR_INPUT_PIN 33
#define ECAP_DEV2_PIN_CONFIG GPIO_33_GPIO33
#define ECAP_DEV2_PIN_DIRECTION_MODE GPIO_DIR_MODE_IN
#define ECAP_DEV2_PIN_QUALIFICATION_MODE GPIO_QUAL_ASYNC
#define ECAP_DEV2_ISR ECAP2_ISR
#define ECAP_DEV2_ECAPTURE_MODE ECAP_ONE_SHOT_CAPTURE_MODE
#define ECAP_DEV2_EVENT ECAP_EVENT_4
#define ECAP_DEV2_EVENT1_POLARITY ECAP_EVNT_FALLING_EDGE
#define ECAP_DEV2_EVENT2_POLARITY ECAP_EVNT_FALLING_EDGE
#define ECAP_DEV2_EVENT3_POLARITY ECAP_EVNT_FALLING_EDGE
#define ECAP_DEV2_EVENT4_POLARITY ECAP_EVNT_FALLING_EDGE
#define ECAP_DEV2_INPUT_ECAPTURE_SIGNAL ECAP_INPUT_INPUTXBAR16
#define ECAP_DEV2_SYNC_OUT_MODE ECAP_SYNC_OUT_DISABLED
#define ECAP_DEV2_INTERRUPT_ACK_GROUP INTERRUPT_ACK_GROUP4
//
// Globals
//
ECAP_DevTypeDef gECAP_Dev[ECAP_DEV_NUM];
//
// Function Prototypes
//
void InitECAP(ECAP_DevTypeDef *pDev);
__interrupt void ECAP1_ISR(void);
__interrupt void ECAP2_ISR(void);
/* eCAP设备初始化 */
void ECAP_DevInit(ECAP_DevTypeDef *pDev)
{
if (pDev == &gECAP_Dev[0])
{
pDev->base = ECAP_DEV1_BASE;
pDev->preScalerValue = ECAP_DEV1_INPUT_PRESCALER;
pDev->interruptNum = ECAP_DEV1_INT_NUM;
pDev->xbarInputNum = ECAP_DEV1_XBAR_INPUT_NUM;
pDev->xbarInputPin = ECAP_DEV1_XBAR_INPUT_PIN;
pDev->pinConfig = ECAP_DEV1_PIN_CONFIG;
pDev->pinIO = ECAP_DEV1_PIN_DIRECTION_MODE;
pDev->qualification = ECAP_DEV1_PIN_QUALIFICATION_MODE;
pDev->ISR = ECAP_DEV1_ISR;
pDev->captureMode = ECAP_DEV1_ECAPTURE_MODE;
pDev->event = ECAP_DEV1_EVENT;
switch (pDev->event)
{
case ECAP_EVENT_1:
pDev->eventPolarity[0] = ECAP_DEV1_EVENT1_POLARITY;
break;
case ECAP_EVENT_2:
pDev->eventPolarity[0] = ECAP_DEV1_EVENT1_POLARITY;
pDev->eventPolarity[1] = ECAP_DEV1_EVENT2_POLARITY;
break;
case ECAP_EVENT_3:
pDev->eventPolarity[0] = ECAP_DEV1_EVENT1_POLARITY;
pDev->eventPolarity[1] = ECAP_DEV1_EVENT2_POLARITY;
pDev->eventPolarity[2] = ECAP_DEV1_EVENT3_POLARITY;
break;
case ECAP_EVENT_4:
pDev->eventPolarity[0] = ECAP_DEV1_EVENT1_POLARITY;
pDev->eventPolarity[1] = ECAP_DEV1_EVENT2_POLARITY;
pDev->eventPolarity[2] = ECAP_DEV1_EVENT3_POLARITY;
pDev->eventPolarity[3] = ECAP_DEV1_EVENT4_POLARITY;
break;
default:
break;
}
pDev->input = ECAP_DEV1_INPUT_ECAPTURE_SIGNAL;
pDev->syncOutMode = ECAP_DEV1_SYNC_OUT_MODE;
pDev->ackGroup = ECAP_DEV1_INTERRUPT_ACK_GROUP;
}
else if (pDev == &gECAP_Dev[1])
{
pDev->base = ECAP_DEV2_BASE;
pDev->preScalerValue = ECAP_DEV2_INPUT_PRESCALER;
pDev->interruptNum = ECAP_DEV2_INT_NUM;
pDev->xbarInputNum = ECAP_DEV2_XBAR_INPUT_NUM;
pDev->xbarInputPin = ECAP_DEV2_XBAR_INPUT_PIN;
pDev->pinConfig = ECAP_DEV2_PIN_CONFIG;
pDev->pinIO = ECAP_DEV2_PIN_DIRECTION_MODE;
pDev->qualification = ECAP_DEV2_PIN_QUALIFICATION_MODE;
pDev->ISR = ECAP_DEV2_ISR;
pDev->captureMode = ECAP_DEV2_ECAPTURE_MODE;
pDev->event = ECAP_DEV2_EVENT;
switch (pDev->event)
{
case ECAP_EVENT_1:
pDev->eventPolarity[0] = ECAP_DEV2_EVENT1_POLARITY;
break;
case ECAP_EVENT_2:
pDev->eventPolarity[0] = ECAP_DEV2_EVENT1_POLARITY;
pDev->eventPolarity[1] = ECAP_DEV2_EVENT2_POLARITY;
break;
case ECAP_EVENT_3:
pDev->eventPolarity[0] = ECAP_DEV2_EVENT1_POLARITY;
pDev->eventPolarity[1] = ECAP_DEV2_EVENT2_POLARITY;
pDev->eventPolarity[2] = ECAP_DEV2_EVENT3_POLARITY;
break;
case ECAP_EVENT_4:
pDev->eventPolarity[0] = ECAP_DEV2_EVENT1_POLARITY;
pDev->eventPolarity[1] = ECAP_DEV2_EVENT2_POLARITY;
pDev->eventPolarity[2] = ECAP_DEV2_EVENT3_POLARITY;
pDev->eventPolarity[3] = ECAP_DEV2_EVENT4_POLARITY;
break;
default:
break;
}
pDev->input = ECAP_DEV2_INPUT_ECAPTURE_SIGNAL;
pDev->syncOutMode = ECAP_DEV2_SYNC_OUT_MODE;
pDev->ackGroup = ECAP_DEV2_INTERRUPT_ACK_GROUP;
}
//
// Configure eCAP input GPIO
//
XBAR_setInputPin(pDev->xbarInputNum, pDev->xbarInputPin);
GPIO_setPinConfig(pDev->pinConfig);
GPIO_setDirectionMode(pDev->xbarInputPin, pDev->pinIO);
GPIO_setQualificationMode(pDev->xbarInputPin, pDev->qualification);
//
// Interrupts that are used in this example are re-mapped to ISR functions
// found within this file.
//
Interrupt_register(pDev->interruptNum, pDev->ISR);
//
// Configure eCAP
//
InitECAP(pDev);
//
// Initialize counters:
//
switch (pDev->event)
{
case ECAP_EVENT_1:
pDev->cap1Count = 0U;
break;
case ECAP_EVENT_2:
pDev->cap1Count = 0U;
pDev->cap2Count = 0U;
break;
case ECAP_EVENT_3:
pDev->cap1Count = 0U;
pDev->cap2Count = 0U;
pDev->cap3Count = 0U;
break;
case ECAP_EVENT_4:
pDev->cap1Count = 0U;
pDev->cap2Count = 0U;
pDev->cap3Count = 0U;
pDev->cap4Count = 0U;
break;
default:
break;
}
//
// Enable interrupts required for this example
//
Interrupt_enable(pDev->interruptNum);
}
//
// InitECAP – Configure eCAP
//
void InitECAP(ECAP_DevTypeDef *pDev)
{
//
// Disable ,clear all capture flags and interrupts
//
ECAP_disableInterrupt(pDev->base,
(ECAP_ISR_SOURCE_CAPTURE_EVENT_1 |
ECAP_ISR_SOURCE_CAPTURE_EVENT_2 |
ECAP_ISR_SOURCE_CAPTURE_EVENT_3 |
ECAP_ISR_SOURCE_CAPTURE_EVENT_4 |
ECAP_ISR_SOURCE_COUNTER_OVERFLOW |
ECAP_ISR_SOURCE_COUNTER_PERIOD |
ECAP_ISR_SOURCE_COUNTER_COMPARE));
ECAP_clearInterrupt(pDev->base,
(ECAP_ISR_SOURCE_CAPTURE_EVENT_1 |
ECAP_ISR_SOURCE_CAPTURE_EVENT_2 |
ECAP_ISR_SOURCE_CAPTURE_EVENT_3 |
ECAP_ISR_SOURCE_CAPTURE_EVENT_4 |
ECAP_ISR_SOURCE_COUNTER_OVERFLOW |
ECAP_ISR_SOURCE_COUNTER_PERIOD |
ECAP_ISR_SOURCE_COUNTER_COMPARE));
//
// Disable ECAP1-ECAP4 register loads
//
ECAP_disableTimeStampCapture(pDev->base);
//
// Configure eCAP
// Enable capture mode.
// One shot mode, stop capture at event 4.
// Set polarity of the events to rising, falling, rising, falling edge.
// Set capture in time difference mode.
// Select input from XBAR7.
// Enable eCAP module.
// Enable interrupt.
//
ECAP_stopCounter(pDev->base);
ECAP_enableCaptureMode(pDev->base);
ECAP_setEventPrescaler(pDev->base, pDev->preScalerValue); /* By-pass the prescaler */
ECAP_setCaptureMode(pDev->base, pDev->captureMode, pDev->event);
switch (pDev->event)
{
case ECAP_EVENT_1:
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_1, pDev->eventPolarity[0]);
ECAP_enableCounterResetOnEvent(pDev->base, ECAP_EVENT_1);
break;
case ECAP_EVENT_2:
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_1, pDev->eventPolarity[0]);
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_2, pDev->eventPolarity[1]);
ECAP_disableCounterResetOnEvent(pDev->base, ECAP_EVENT_1);
ECAP_enableCounterResetOnEvent(pDev->base, ECAP_EVENT_2);
break;
case ECAP_EVENT_3:
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_1, pDev->eventPolarity[0]);
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_2, pDev->eventPolarity[1]);
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_3, pDev->eventPolarity[2]);
ECAP_disableCounterResetOnEvent(pDev->base, ECAP_EVENT_1);
ECAP_disableCounterResetOnEvent(pDev->base, ECAP_EVENT_2);
ECAP_enableCounterResetOnEvent(pDev->base, ECAP_EVENT_3);
break;
case ECAP_EVENT_4:
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_1, pDev->eventPolarity[0]);
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_2, pDev->eventPolarity[1]);
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_3, pDev->eventPolarity[2]);
ECAP_setEventPolarity(pDev->base, ECAP_EVENT_4, pDev->eventPolarity[3]);
ECAP_disableCounterResetOnEvent(pDev->base, ECAP_EVENT_1);
ECAP_disableCounterResetOnEvent(pDev->base, ECAP_EVENT_2);
ECAP_disableCounterResetOnEvent(pDev->base, ECAP_EVENT_3);
ECAP_enableCounterResetOnEvent(pDev->base, ECAP_EVENT_4);
break;
default:
break;
}
ECAP_selectECAPInput(pDev->base, pDev->input);
ECAP_enableLoadCounter(pDev->base);
ECAP_setSyncOutMode(pDev->base, pDev->syncOutMode);
ECAP_startCounter(pDev->base);
ECAP_enableTimeStampCapture(pDev->base);
ECAP_reArm(pDev->base);
switch (pDev->event)
{
case ECAP_EVENT_1:
ECAP_enableInterrupt(pDev->base, ECAP_ISR_SOURCE_CAPTURE_EVENT_1);
break;
case ECAP_EVENT_2:
ECAP_enableInterrupt(pDev->base, ECAP_ISR_SOURCE_CAPTURE_EVENT_2);
break;
case ECAP_EVENT_3:
ECAP_enableInterrupt(pDev->base, ECAP_ISR_SOURCE_CAPTURE_EVENT_3);
break;
case ECAP_EVENT_4:
ECAP_enableInterrupt(pDev->base, ECAP_ISR_SOURCE_CAPTURE_EVENT_4);
break;
default:
break;
}
}
// #pragma CODE_SECTION(ECAP1_ISR, ".TI.ramfunc");
//
// eCAP 1 ISR
//
__interrupt void ECAP1_ISR(void)
{
switch (gECAP_Dev[0].event)
{
case ECAP_EVENT_1:
gECAP_Dev[0].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[0].inputFrequency = 40000000.0f / gECAP_Dev[0].cap1Count * gECAP_Dev[0].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[0].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_1); // Clear interrupt flags for more interrupts.
break;
case ECAP_EVENT_2:
gECAP_Dev[0].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[0].cap2Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_2); // Get the capture counts.
gECAP_Dev[0].inputFrequency = 80000000.0f / gECAP_Dev[0].cap2Count * gECAP_Dev[0].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[0].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_2);
break;
case ECAP_EVENT_3:
gECAP_Dev[0].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[0].cap2Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_2); // Get the capture counts.
gECAP_Dev[0].cap3Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_3); // Get the capture counts.
gECAP_Dev[0].inputFrequency = 120000000.0f / gECAP_Dev[0].cap3Count * gECAP_Dev[0].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[0].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_3);
break;
case ECAP_EVENT_4:
gECAP_Dev[0].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[0].cap2Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_2);
gECAP_Dev[0].cap3Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_3);
gECAP_Dev[0].cap4Count = ECAP_getEventTimeStamp(gECAP_Dev[0].base, ECAP_EVENT_4);
gECAP_Dev[0].inputFrequency = 160000000.0f / gECAP_Dev[0].cap4Count * gECAP_Dev[0].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[0].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_4);
break;
default:
break;
}
// gECAP_Dev[0].intCount++;
ECAP_clearGlobalInterrupt(gECAP_Dev[0].base);
ECAP_reArm(gECAP_Dev[0].base); // Start eCAP
Interrupt_clearACKGroup(gECAP_Dev[0].ackGroup); // Acknowledge the group interrupt for more interrupts.
}
// #pragma CODE_SECTION(ECAP2_ISR, ".TI.ramfunc");
//
// eCAP 2 ISR
//
__interrupt void ECAP2_ISR(void)
{
switch (gECAP_Dev[1].event)
{
case ECAP_EVENT_1:
gECAP_Dev[1].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[1].inputFrequency = 40000000.0f / gECAP_Dev[1].cap1Count * gECAP_Dev[1].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[1].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_1); // Clear interrupt flags for more interrupts.
break;
case ECAP_EVENT_2:
gECAP_Dev[1].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[1].cap2Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_2); // Get the capture counts.
gECAP_Dev[1].inputFrequency = 80000000.0f / gECAP_Dev[1].cap2Count * gECAP_Dev[1].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[1].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_2);
break;
case ECAP_EVENT_3:
gECAP_Dev[1].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[1].cap2Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_2); // Get the capture counts.
gECAP_Dev[1].cap3Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_3); // Get the capture counts.
gECAP_Dev[1].inputFrequency = 120000000.0f / gECAP_Dev[1].cap3Count * gECAP_Dev[1].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[1].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_3);
break;
case ECAP_EVENT_4:
gECAP_Dev[1].cap1Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_1); // Get the capture counts.
gECAP_Dev[1].cap2Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_2); // Get the capture counts.
gECAP_Dev[1].cap3Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_3); // Get the capture counts.
gECAP_Dev[1].cap4Count = ECAP_getEventTimeStamp(gECAP_Dev[1].base, ECAP_EVENT_4); // Get the capture counts.
gECAP_Dev[1].inputFrequency = 160000000.0f / gECAP_Dev[1].cap4Count * gECAP_Dev[1].preScalerValue; /* 输入信号频率 单位:Hz */
ECAP_enableInterrupt(gECAP_Dev[1].base, ECAP_ISR_SOURCE_CAPTURE_EVENT_4);
break;
default:
break;
}
// gECAP_Dev[1].intCount++;
ECAP_clearGlobalInterrupt(gECAP_Dev[1].base);
ECAP_reArm(gECAP_Dev[1].base); // Start eCAP
Interrupt_clearACKGroup(gECAP_Dev[1].ackGroup); // Acknowledge the group interrupt for more interrupts.
}
user_config.h
#ifndef __USER_CONFIG_H
#define __USER_CONFIG_H
/* lFC Device Config */
#define LFC_DEV_NUM 2
#define LIGHT_SENSOR1 gLFC_Dev[0]
#define LIGHT_SENSOR2 gLFC_Dev[1]
/* ECAP Device Config */
#define ECAP_DEV_NUM 2
#define LIGHT_CAP1 gECAP_Dev[0]
#define LIGHT_CAP2 gECAP_Dev[1]
extern void User_Init(void);
#endif
user_config.c
#include "ecap_dev.h"
#include "lfc_dev.h"
extern LFC_DevTypeDef gLFC_Dev[LFC_DEV_NUM];
extern ECAP_DevTypeDef gECAP_Dev[ECAP_DEV_NUM];
extern void EQEP_DevInit(void);
void User_Init(void)
{
LFC_DevInit(&LIGHT_SENSOR1); /* LIGHT_SENSOR1初始化 */
LFC_DevInit(&LIGHT_SENSOR2); /* LIGHT_SENSOR2初始化 */
ECAP_DevInit(&LIGHT_CAP1); /* LIGHT_CAP1初始化 */
ECAP_DevInit(&LIGHT_CAP2); /* LIGHT_CAP2初始化 */
//EQEP_DevInit();
}
zhen zhen:
经过上午的调试,已解决。是ECAP中断函数写错了,ECAP_enableInterrupt应为ECAP_clearInterrupt。我用的连续模式ECAP_CONTINUOUS_CAPTURE_MODE,中断函数里的
ECAP_reArm可以不用。这样就可以用两个ECAP模块测得两路脉冲信号了