📄 f280xhall3.c
字号:
/* ==================================================================================
File name: F280XHALL3.C
Originator: Digital Control Systems Group
Texas Instruments
Description: This file contains source for the Capture drivers for the F280X
Target: TMS320F280x family
=====================================================================================
History:
-------------------------------------------------------------------------------------
04-15-2005 Version 3.20: Using DSP280x v. 1.10 or higher
------------------------------------------------------------------------------------*/
#include "DSP280x_Device.h"
#include "f280xhall3.h"
void F280X_HALL3_Init(HALL3 *p)
{
F280X_HALL3_Determine_State(p);
p->HallGpioBuffer = p->HallGpio; // Init with current ECAP/GPIO logic levels
p->HallGpioAccepted = p->HallGpio; // Init with current ECAP/GPIO logic levels
// Init ECAP Control Registers 1 and 2 for ECAP1/2/3
ECap1Regs.ECCTL1.all = HALL3_ECCTL1_ECAP1_INIT_STATE;
ECap1Regs.ECCTL2.all = HALL3_ECCTL2_INIT_STATE;
ECap2Regs.ECCTL1.all = HALL3_ECCTL1_ECAP2_INIT_STATE;
ECap2Regs.ECCTL2.all = HALL3_ECCTL2_INIT_STATE;
ECap3Regs.ECCTL1.all = HALL3_ECCTL1_ECAP3_INIT_STATE;
ECap3Regs.ECCTL2.all = HALL3_ECCTL2_INIT_STATE;
// Forcing the Capture Event 1 flags to 1 to believe that there are already the captured edges
ECap1Regs.ECFRC.bit.CEVT1 = 1;
ECap2Regs.ECFRC.bit.CEVT1 = 1;
ECap3Regs.ECFRC.bit.CEVT1 = 1;
EALLOW; // Enable EALLOW
// Set up the ECAP1/2/3 pins to primary functions
GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 1; // GPIO24 is ECAP1
GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 1; // GPIO25 is ECAP2
GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 1; // GPIO26 is ECAP3
EDIS; // Disable EALLOW
}
void F280X_HALL3_Read(HALL3 *p)
{
// Save capture flag registers, convenient for Watch Window
p->CapFlag = 0;
if ((ECap1Regs.ECFLG.bit.CEVT1 != 0)||(ECap1Regs.ECFLG.bit.CEVT2 != 0))
p->CapFlag |= 1;
if ((ECap2Regs.ECFLG.bit.CEVT1 != 0)||(ECap2Regs.ECFLG.bit.CEVT2 != 0))
p->CapFlag |= 2;
if ((ECap3Regs.ECFLG.bit.CEVT1 != 0)||(ECap3Regs.ECFLG.bit.CEVT2 != 0))
p->CapFlag |= 4;
if (p->CapFlag==0) // NO_EDGE_DETECTED: No hall signal edges detected on ECAP1-3 (bits 0-2)
{
p->CmtnTrigHall = 0; // Reset trigger, it only handshakes with calling program.
if (p->EdgeDebounced==0) // If motor has not moved then debounce current position.
{
F280X_HALL3_Debounce(p);
p->CmtnTrigHall = p->EdgeDebounced; // Set Commutation trigger here
}
else // If current position is debounced, find match in table
F280X_HALL3_Next_State_Ptr(p); // and return pointer to current state. Ptr to be incremented
// by MOD6CNT after RET.
p->EdgeDebounced = 0; // Reset trigger
}
else // EDGE_DETECTED: Any hall signal edges detected on ECAP1-3
{
p->StallCount = 0xFFFF; // On new edge, reset stall counter
// Clear all ECAP1-3 Events 1/2 status flags
ECap1Regs.ECCLR.bit.CEVT1 = 1;
ECap1Regs.ECCLR.bit.CEVT2 = 1;
ECap2Regs.ECCLR.bit.CEVT1 = 1;
ECap2Regs.ECCLR.bit.CEVT2 = 1;
ECap3Regs.ECCLR.bit.CEVT1 = 1;
ECap3Regs.ECCLR.bit.CEVT2 = 1;
F280X_HALL3_Determine_State(p); // Since motor has moved, determine state (read HallGpio)
p->CapCounter += 1; // Increment running edge detection counter
}
}
void F280X_HALL3_Determine_State(HALL3 *p)
{
EALLOW; // Enable EALLOW
// Configure ECAP1/2/3 as GPIO-inputs (GPIO24/25/26)
GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 0; // GPIO24 is ECAP1
GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 0; // GPIO25 is ECAP2
GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 0; // GPIO26 is ECAP3
// config GPIO24/25/26 as inputs
GpioCtrlRegs.GPADIR.bit.GPIO24 = 0; // GPIO24 is ECAP1
GpioCtrlRegs.GPADIR.bit.GPIO25 = 0; // GPIO25 is ECAP2
GpioCtrlRegs.GPADIR.bit.GPIO26 = 0; // GPIO26 is ECAP3
EDIS; // Disable EALLOW
// HallGpio.2-0 = GPIO26/GPIO25/GPIO24
p->HallGpio = GpioDataRegs.GPADAT.bit.GPIO24;
p->HallGpio |= 2*GpioDataRegs.GPADAT.bit.GPIO25;
p->HallGpio |= 4*GpioDataRegs.GPADAT.bit.GPIO26;
EALLOW; // Enable EALLOW
// Set up the ECAP1/2/3 pins to primary functions
GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 1; // GPIO24 is ECAP1
GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 1; // GPIO25 is ECAP2
GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 1; // GPIO26 is ECAP3
EDIS; // Disable EALLOW
}
void F280X_HALL3_Debounce(HALL3 *p)
{
if (p->HallGpio == p->HallGpioAccepted) // GPIO_UNCHANGED: Current GPIO reading == debounced GPIO reading?
{
if (p->Revolutions <= 0) // Only create hall map during initial Revolutions
F280X_HALL3_Create_Map(p);
p->StallCount -= 1; // Decrement stall counter
if (p->StallCount == 0)
{
p->EdgeDebounced = 0x7FFF; // If motor has stalled, then user trigger to commutate
p->StallCount = 0xFFFF; // Reset counter to starting value
}
}
else // GPIO_CHANGED: If not zero, then the motor has moved to a new position.
{
if (p->HallGpio == p->HallGpioBuffer) // Current GPIO reading == previous GPIO reading?
{
if (p->DebounceCount >= p->DebounceAmount) // If equal, is current GPIO reading debounced?
{
p->HallGpioAccepted = p->HallGpioBuffer; // Current GPIO reading is now debounced
p->EdgeDebounced = 0x7FFF; // Edge/position debounced, trigger commutation
p->DebounceCount = 0; // Reset debounce counter
if (p->HallMapPointer==0)
p->Revolutions += 1; // Increment on every rev (HallMapPointer = 0)
}
else // DEBOUNCE_MORE
p->DebounceCount += 1; // Increment debounce counter
}
else // NEW_READING
{
p->HallGpioBuffer = p->HallGpio; // Save new reading and reset debounce counter
p->DebounceCount = 0;
}
}
}
void F280X_HALL3_Next_State_Ptr(HALL3 *p)
{
int16 i, HallPointer;
if (p->Revolutions>0) // Only run function after map has been created.
{
for (i=0;i<=5;i++) // Search for a match of current debounced GPIO position
{ // and the table entries.
if (p->HallMap[i] == p->HallGpioAccepted) // Match_Found
HallPointer = i;
}
p->HallMapPointer = HallPointer; // On match, save pointer position. Pointer will be incremented
} // by 1 since MOD6CNT will receive a positive trigger
} // and pointer as inputs.
void F280X_HALL3_Create_Map(HALL3 *p)
{
p->HallMap[p->HallMapPointer] = p->HallGpioAccepted; // Save debounced GPIO to table.
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -