www.pudn.com > DSP2812formotorcontrol.rar > DSP281x_Hall3.c


/* ================================================================================== 
File name:       DSP281x_Hall3.C 
                     
Originator:	Digital Control Systems Group 
			Texas Instruments 
 
Description:  This file contains source for the Capture drivers for the F281X 
 
Target: TMS320F281x family 
     
===================================================================================== 
History: 
------------------------------------------------------------------------------------- 
 04-15-2005	Version 3.20: Using DSP281x v. 1.00 or higher  
------------------------------------------------------------------------------------*/ 
 
#include "DSP281x_Device.h" 
#include "DSP281x_Hall3.h" 
 
void  F281X_EV1_HALL3_Init(HALL3 *p) 
{ 
        F281X_EV1_HALL3_Determine_State(p); 
        p->HallGpioBuffer = p->HallGpio;       // Init with current CAP/GPIO logic levels 
 	    p->HallGpioAccepted = p->HallGpio;     // Init with current CAP/GPIO logic levels 
 
        EvaRegs.CAPCONA.all = HALL3_INIT_STATE;  // Set up capture units, CAP1-3 using GP timer 2 
        EvaRegs.CAPFIFOA.all = 0x1500;           // Write "01" each CAPxFIFO for EV to believe that there is already an entry in the FIFO 
 
        EALLOW;                       // Enable EALLOW 
        GpioMuxRegs.GPAMUX.all |= 0x0700;     // Set up the capture 1-3 pins to primary functions 
        EDIS;                         // Disable EALLOW 
 
} 
 
 
void F281X_EV1_HALL3_Read(HALL3 *p) 
{ 
 
       p->CapFlag = EvaRegs.EVAIFRC.all;  // Save capture flag register, convenient for Watch Window 
       if (p->CapFlag==0)                 // NO_EDGE_DETECTED: No hall signal edges detected on CAP1-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.  
          {   
            F281X_EV1_HALL3_Debounce(p); 
            p->CmtnTrigHall = p->EdgeDebounced;      // Set Commutation trigger here 
          } 
         else                                  // If current position is debounced, find match in table  
            F281X_EV1_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 CAP1-3 
       { 
         p->StallCount = 0xFFFF;               // On new edge, reset stall counter 
         EvaRegs.EVAIFRC.all = 0x0007;         // Clear all CAP1-3 Int-flags  
         F281X_EV1_HALL3_Determine_State(p);   // Since motor has moved, determine state (read HallGpio) 
         p->CapCounter += 1;                     // Increment running edge detection counter 
       }                     
} 
 
 
void F281X_EV1_HALL3_Determine_State(HALL3 *p) 
{ 
        EALLOW;                       // Enable EALLOW 
        // Configure CAP1-3 as GPIO-inputs (GPIO8-GPIO10) 
        GpioMuxRegs.GPAMUX.all &= 0xF8FF; 
		 
		// config GPIO8-GPIO10 as inputs 
		GpioMuxRegs.GPADIR.bit.GPIOA8 = 0; 
		GpioMuxRegs.GPADIR.bit.GPIOA9 = 0; 
		GpioMuxRegs.GPADIR.bit.GPIOA10 = 0; 
        EDIS;                         // Disable EALLOW 
         
        p->HallGpio = GpioDataRegs.GPADAT.all&0x0700;  // HallGpio.2-0 = GPIO10-GPIO8  
        p->HallGpio = p->HallGpio>>8; 
 
        EALLOW;                       // Enable EALLOW 
        GpioMuxRegs.GPAMUX.all |= 0x0700;     // Set up the CAP1-3 pins to primary functions 
        EDIS;                         // Disable EALLOW 
 
} 
 
 
void F281X_EV1_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 
         F281X_EV1_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 F281X_EV1_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 F281X_EV1_HALL3_Create_Map(HALL3 *p) 
{ 
   p->HallMap[p->HallMapPointer] = p->HallGpioAccepted;  // Save debounced GPIO to table. 
}