/** * This method sends a byte to the SPI bus * @param Chr is the byte to be sent. * @return ERR_OK always ***************************************************************************/ byte SPI1_SendChar(byte Chr) { int i=0; byte ToSend=0; //SCLK setRegBits(GPIO_E_DR,0x10); wait(4); for(i=7;i>=0;i--) { ToSend=(Chr >>i) & 0x1; //SCLK clrRegBits(GPIO_E_DR,0x10); wait(4); if (ToSend==0x1) { //MOSI setRegBits(GPIO_E_DR,0x20); } else { //MOSI clrRegBits(GPIO_E_DR,0x20); } wait(1); //SCLK setRegBits(GPIO_E_DR,0x10); wait(4); } }
/** * This method turns on the specified LED ***************************************************************************/ void turn_led_on(byte number) { if (number==0) clrRegBits(GPIO_A_DR,0x30); else clrRegBits(GPIO_A_DR,0xC0); }
/** * This method inits the SPI interface ***************************************************************************/ void SPI1_Init(void) { //MISO clrRegBits(GPIO_E_DDR,0x40); clrRegBits(GPIO_E_PER,0x40); //MOSI setRegBits(GPIO_E_DDR,0x20); clrRegBits(GPIO_E_PER,0x20); //SCLK setRegBits(GPIO_E_DDR,0x10); clrRegBits(GPIO_E_PER,0x10); }
/** * Enables the PWM pad and clears fault pins. * @return ERR_OK always. */ void PWM_B_outputPadEnable (word mask) { mask &= 0x3F00; clrRegBits(PWMB_PMOUT, mask); setRegBits(PWMB_PMOUT, 0x8000); //PAD_EN=1 reset_faults_PWMB(); }
/* ** =================================================================== ** Method : Hall_U_SetEdge (component ExtInt) ** ** Description : ** Sets the edge type for this bean that generates the ** interrupt. ** Parameters : ** NAME - DESCRIPTION ** edge - Edge type: ** 0 - falling edge ** 1 - rising edge ** 2 - both edges ** 3 - low level ** 4 - high level ** Returns : ** --- - Error code, possible codes: ** ERR_OK - OK ** ERR_RANGE - Value is out of range ** =================================================================== */ byte Hall_U_SetEdge(byte edge) { if (edge > 1) { /* If parameter is out of range */ return ERR_RANGE; /* ....then return error */ } clrRegBits(GPIO_F_IEN,Hall_U_PIN_MASK); /* Disable interrupt "INT_GPIO_F" */ if (edge == 0) { setRegBits(GPIO_F_IPOL,Hall_U_PIN_MASK); /* Set the falling edge */ } else { clrRegBits(GPIO_F_IPOL,Hall_U_PIN_MASK); /* Set the rising edge */ } setReg(GPIO_F_IEDGE,Hall_U_PIN_MASK); /* Clear flag */ setRegBits(GPIO_F_IEN,Hall_U_PIN_MASK); /* Enable interrupt "INT_GPIO_F" */ return ERR_OK; }
/** * initializes the counter/timer. the timer is initialized w/ 1ms period. */ void TI1_init (void) { /* TMRA3_CTRL: CM=0,PCS=0,SCS=0,ONCE=0,LENGTH=1,DIR=0,Co_INIT=0,OM=0 */ setReg (TMRA3_CTRL, 0x20); /* Stop all functions of the timer */ /* TMRA3_SCR: TCF=0,TCFIE=1,TOF=0,TOFIE=0,IEF=0,IEFIE=0,IPS=0,INPUT=0,Capture_Mode=0,MSTR=0,EEOF=0,VAL=0,FORCE=0,OPS=0,OEN=0 */ setReg (TMRA3_SCR, 0x4000); setReg (TMRA3_LOAD, 0); /* Reset load register */ setReg (TMRA3_CMP1, 39999); /* Store appropriate value to the compare register according to the selected high speed CPU mode */ clrRegBits (TMRA3_CTRL, 0x1e00); setRegBits (TMRA3_CTRL, 4096); /* Set prescaler register according to the selected high speed CPU mode */ setReg (TMRA3_CNTR, 0); /* Reset counter */ clrRegBits (TMRA3_CTRL, 0xe000); setRegBits (TMRA3_CTRL, 0x2000); /* counter on! */ }
void FaultInterruptDisable(byte axis) { if (axis<=1) { clrRegBits (PWMA_PMFCTL,PWMA_PMFCTL_FIE0_MASK); // clrRegBits (PWMA_PMFCTL,PWMA_PMFCTL_FIE1_MASK); // clrRegBits (PWMA_PMFCTL,PWMA_PMFCTL_FIE2_MASK); clrRegBits (PWMA_PMFCTL,PWMA_PMFCTL_FIE3_MASK); } else { clrRegBits (PWMB_PMFCTL,PWMB_PMFCTL_FIE0_MASK); // clrRegBits (PWMB_PMFCTL,PWMB_PMFCTL_FIE1_MASK); // clrRegBits (PWMB_PMFCTL,PWMB_PMFCTL_FIE2_MASK); clrRegBits (PWMB_PMFCTL,PWMB_PMFCTL_FIE3_MASK); } }
/* ** =================================================================== ** Method : Bits1_ClrBit (component BitsIO) ** ** Description : ** This method clears (sets to zero) the specified bit ** of the output value. ** [ It is the same as "PutBit(Bit,FALSE);" ] ** a) direction = Input : sets the specified bit to "0"; ** this operation will be shown on ** output after the direction has ** beenswitched to output ** (SetDir(TRUE);) ** b) direction = Output : directly writes "0" to the ** appropriate pin ** Parameters : ** NAME - DESCRIPTION ** Bit - Number of the bit to clear (0 to 1) ** Returns : Nothing ** =================================================================== */ void Bits1_ClrBit(byte Bit) { register byte Mask=Bits1_GetMsk(Bit); /* Temporary variable - bit mask */ if (Mask) { /* Is bit mask correct? */ Shadow_GPIO_B_DATA &= ~Mask; /* Clear appropriate bit in shadow variable */ clrRegBits(GPIO_B_DATA,Mask); /* Clear appropriate bit on port */ } }
/* ** =================================================================== ** Method : LED_SetDir (component BitIO) ** ** Description : ** This method sets direction of the bean. ** Parameters : ** NAME - DESCRIPTION ** Dir - Direction to set (FALSE or TRUE) ** FALSE = Input, TRUE = Output ** Returns : Nothing ** =================================================================== */ void LED_SetDir(bool Dir) { if (Dir) { /* Is given direction output? */ setReg(GPIO_A_DR,((getReg(GPIO_A_DR)) & ~LED_PIN_MASK) | (Shadow_GPIO_A_DR & LED_PIN_MASK)); /* Restore correct value of output from shadow variable */ setRegBits(GPIO_A_DDR,LED_PIN_MASK); /* Set direction to output */ } else { /* Is given direction input? */ clrRegBits(GPIO_A_DDR,LED_PIN_MASK); /* Set direction to input */ } }
/* ** =================================================================== ** Method : Bits1_SetDir (component BitsIO) ** ** Description : ** This method sets direction of the bean. ** Parameters : ** NAME - DESCRIPTION ** Dir - Direction to set (FALSE or TRUE) ** FALSE = Input, TRUE = Output ** Returns : Nothing ** =================================================================== */ void Bits1_SetDir(bool Dir) { if (Dir) { /* Is given direction output? */ setReg(GPIO_B_DATA,((getReg(GPIO_B_DATA)) & ~Bits1_PIN_MASK)|(Shadow_GPIO_B_DATA & Bits1_PIN_MASK)); /* Restore correct value of output from shadow variable */ setRegBits(GPIO_B_DDIR,Bits1_PIN_MASK); /* Set direction to output */ } else { /* Is direction input? */ clrRegBits(GPIO_B_DDIR,Bits1_PIN_MASK); /* Set direction to input */ } }
/* ** =================================================================== ** Method : LED_PutVal (component BitIO) ** ** Description : ** This method writes the new output value. ** a) direction = Input : sets the new output value; ** this operation will be shown on ** output after the direction has ** been switched to output ** (SetDir(TRUE);) ** b) direction = Output : directly writes the value to the ** appropriate pin ** Parameters : ** NAME - DESCRIPTION ** Val - Output value. Possible values: ** FALSE - logical "0" (Low level) ** TRUE - logical "1" (High level) ** Returns : Nothing ** =================================================================== */ void LED_PutVal(bool Val) { if (Val) { /* Is it one to be written? */ Shadow_GPIO_A_DR |= LED_PIN_MASK; /* Set bit in shadow variable */ setRegBits(GPIO_A_DR,LED_PIN_MASK); /* Set bit on port */ } else { /* Is it zero to be written? */ Shadow_GPIO_A_DR &= ~LED_PIN_MASK; /* Clear bit in shadow variable */ clrRegBits(GPIO_A_DR,LED_PIN_MASK); /* Clear bit on port */ } }
/** * sets the clock prescaler. * @param presc is the prescaler value in range 0-3 that mean divisors 1 to 8. * @return ERR_OK if successful. */ byte PWMC1_setPrescaler(byte presc) { if (presc < 4) { clrRegBits (PWMB_PMCTL, 0x00c0); setRegBits (PWMB_PMCTL, (presc << 6)); ///setRegBitGroup(PWMB_PMCTL, PRSC, presc); return ERR_OK; } else return ERR_RANGE; }
//********************************************************* void Init_Hall_Effect_1(void) { clrRegBits(GPIO_D_DDR,GPIO_D2); // HX2 clrRegBits(GPIO_D_PER,GPIO_D2); clrRegBits(GPIO_D_DDR,GPIO_D3); // HY2 clrRegBits(GPIO_D_PER,GPIO_D3); clrRegBits(GPIO_D_DDR,GPIO_D4); // HZ2 clrRegBits(GPIO_D_PER,GPIO_D4); }
//********************************************************* void Init_Hall_Effect_0(void) { clrRegBits(GPIO_B_DDR,GPIO_B2); // HX1 clrRegBits(GPIO_B_PER,GPIO_B2); clrRegBits(GPIO_B_DDR,GPIO_B3); // HY1 clrRegBits(GPIO_B_PER,GPIO_B3); clrRegBits(GPIO_B_DDR,GPIO_B4); // HZ1 clrRegBits(GPIO_B_PER,GPIO_B4); }
/** * initializes the PWM module w/ 25KHz indipendent mode. * * CENTER ALIGNED **************************************************************************************/ void PWM_B_init(void) { // write protect off clrRegBits (PWMB_PMCFG, PWMB_PMCFG_WP_MASK); // PWMB_PMCTL: LDFQ=0,HALF=0,IPOL2=0,IPOL1=0,IPOL0=0,PRSC=0,PWMRIE=0,PWMF=0,ISENS=0,LDOK=0,PWMEN=0 setReg (PWMB_PMCTL, 0); // PWMB_PMOUT: PAD_EN=0,??=0,OUTCTL=0,??=0,??=0,OUT=0 setReg(PWMB_PMOUT, 0); // PWMB_PMCCR: ENHA=1,??=0,MSK=0,??=0,??=0,VLMODE=0,??=0,SWP45=0,SWP23=0,SWP01=0 setReg(PWMB_PMCCR, 32768); // PWMB_PMCFG: ??=0,??=0,??=0,EDG=0,??=0,TOPNEG45=0,TOPNEG23=0,TOPNEG01=0,??=0,BOTNEG45=0,BOTNEG23=0,BOTNEG01=0,INDEP45=0,INDEP23=0,INDEP01=0,WP=0 setReg(PWMB_PMCFG, 0x0); // PWMB_PMDEADTM: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,PWMDT=4 setReg (PWMB_PMDEADTM, DEAD_TIME); // PWMB_PWMVAL0: PWMVAL=MIN_DUTY setReg(PWMB_PWMVAL0, MIN_DUTY); // PWMB_PWMVAL1: PWMVAL=MAX_DUTY setReg(PWMB_PWMVAL1, MAX_DUTY); // PWMB_PWMVAL2: PWMVAL=MIN_DUTY setReg(PWMB_PWMVAL2, MIN_DUTY); // PWMB_PWMVAL3: PWMVAL=MAX_DUTY setReg(PWMB_PWMVAL3, MAX_DUTY); // PWMB_PWMVAL4: PWMVAL=MIN_DUTY setReg(PWMB_PWMVAL4, MIN_DUTY); // PWMB_PWMVAL5: PWMVAL=MAX_DUTY setReg(PWMB_PWMVAL5, MAX_DUTY); // PWMB_PWMCM: ??=0,PWMCM=PWMFREQ setReg(PWMB_PWMCM, PWMFREQ); setRegBits(PWMB_PMCTL, 0x2); /* Load counter and modulo registers into buffers */ /* PWMB_PMCTL: PWMEN=1 */ setRegBits(PWMB_PMCTL, 0x1); /* Run counter */ /* PWMB_PMCTL: PWMF=1 */ setRegBits(PWMB_PMCTL, 0x20); /* Enable reload interrupt */ // write protect on setRegBits (PWMB_PMCFG, PWMB_PMCFG_WP_MASK); }
/* ** =================================================================== ** Method : HWEnDi (component SynchroMaster) ** ** Description : ** Enables or disables the peripheral(s) associated with the bean. ** The method is called automatically as a part of the Enable and ** Disable methods and several internal methods. ** This method is internal. It is used by Processor Expert only. ** =================================================================== */ static void HWEnDi(void) { if (EnUser) { /* Enable device? */ setRegBit(QSPI0_SCTRL,SPE); /* Enable device */ setRegBits(GPIO_C_PEREN,0x0200); /* Switch pin to peripheral */ if (SerFlag & FULL_TX) { /* Is any char in transmit buffer? */ setReg(QSPI0_DXMIT,BufferWrite); /* Store char to transmitter register */ SerFlag &= ~FULL_TX; /* Zeroize FULL_TX flag */ } } else { clrRegBits(GPIO_C_PEREN,0x0200); /* Switch pin to GPIO */ clrRegBit(QSPI0_SCTRL,SPE); /* Disable device */ } }
/* * enables triggered sequential mode synchronous with the * PWM generation signal. */ byte AD_enableIntTriggerB(void) { if (ad_ModeFlgB != IDLE) /* Is the device in running mode? */ return ERR_BUSY; /// starts sampling in triggered sequential mode /// synchro with PWM generation. setRegBits (ADCB_ADCR1, 0x04); clrRegBits (ADCB_ADCR1, 0x03); ad_ModeFlgB = MEASURE; /* Set state of device to the measure mode */ HWEnDiB(); return ERR_OK; }
/* ** =================================================================== ** Method : Bits1_PutBit (component BitsIO) ** ** Description : ** This method writes the new value to the specified bit ** of the output value. ** a) direction = Input : sets the value of the specified ** bit; this operation will be ** shown on output after the ** direction has been switched to ** output (SetDir(TRUE);) ** b) direction = Output : directly writes the value of the ** bit to the appropriate pin ** Parameters : ** NAME - DESCRIPTION ** Bit - Number of the bit (0 to 1) ** Val - New value of the bit (FALSE or TRUE) ** FALSE = "0" or "Low", TRUE = "1" or "High" ** Returns : Nothing ** =================================================================== */ void Bits1_PutBit(byte Bit, bool Val) { register byte Mask=Bits1_GetMsk(Bit); /* Temporary variable - bit mask */ if (Mask) { /* Is bit mask correct? */ if (Val) { /* Is it one to be written? */ Shadow_GPIO_B_DATA |= Mask; /* Set appropriate bit in shadow variable */ setRegBits(GPIO_B_DATA,Mask); /* Set appropriate bit on port */ } else { /* Is it zero to be written? */ Shadow_GPIO_B_DATA &= ~Mask; /* Clear appropriate bit in shadow variable */ clrRegBits(GPIO_B_DATA,Mask); /* Clear appropriate bit on port */ } } }
/** * This method performs one measurement on channel B * * @param wait waits for result to be ready. * @return ERR_OK after a successful sampling, ERR_BUSY if the device is * already running a conversion. * **************************************************************************************/ byte AD_measureB(bool wait) { if (ad_ModeFlgB != IDLE) return ERR_BUSY; /* sequential once mode */ clrRegBits (ADCB_ADCR1, 0x00); ad_ModeFlgB = MEASURE; HWEnDiB(); /* wait on the interrupt */ if (wait) while (ad_ModeFlgB == MEASURE) {} return ERR_OK; }
void PWMAReload_Interrupt(void) { //clear the interrupt flag of the pwm reload interrupt clrRegBits(PWMA_PMCTL, PWMA_PMCTL_PWMF_MASK); //read the hall sensors status0=HALLSENSOR0; if (old_status0!= status0) { if (status0 == DIRECTION_TABLE[old_status0]) { comm_enc[0]++; // phase_changed[0]=1; // write mask to PWM Channel Control Register PWMState[0]= pTable0[status0]; tmp = getReg(PWMA_PMOUT) & 0x8000; val=tmp | PWMState[0].MaskOut | (PWMState[0].Mask<<8); setReg(PWMA_PMOUT,val); old_status0 = status0; } else if (status0 == DIRECTION_TABLE_INV[old_status0]) { comm_enc[0]--; // phase_changed[0]=1; // write mask to PWM Channel Control Register PWMState[0]= pTable0[status0]; tmp = getReg(PWMA_PMOUT) & 0x8000; val=tmp | PWMState[0].MaskOut | (PWMState[0].Mask<<8); setReg(PWMA_PMOUT,val); old_status0 = status0; } else { hall_error[0]=HALL_ERROR_TABLE; PWM_outputPadDisable(0); #ifdef DEBUG_CAN_MSG can_printf("HALL ERROR 0"); #endif } } }
/** * starts the acquisition on channel A. * **************************************************************************************/ static void HWEnDiA(void) { if (ad_ModeFlgA) /* Launch measurement? */ { OutFlgA = FALSE; /* Measured values are available */ /* Trigger mode? */ if (getRegBit (ADCA_ADCR1, SMODE2)) { setRegBit (ADCA_ADCR1, SYNC); /* Use sync input to initiate a conversion */ clrRegBit (ADCA_ADCR1, STOP); /* Normal operation mode */ } else { /* Set normal operation mode and sync input disabled */ clrRegBits (ADCA_ADCR1, ADCA_ADCR1_SYNC_MASK | ADCA_ADCR1_STOP_MASK); setRegBit (ADCA_ADCR1, START); /* Launching of conversion */ } } else { setRegBit (ADCA_ADCR1, STOP); /* Stop command issued */ } }
/** * This method reads a byte from the SPI bus * @param Chr is the read byte * @return ERR_OK always ***************************************************************************/ byte SPI1_RecvChar(byte *Chr) { int i=0; byte ToRead=0; *Chr=0; //SCLK setRegBits(GPIO_E_DR,0x10); wait(4); for(i=7;i>=0;i--) { //SCLK clrRegBits(GPIO_E_DR,0x10); wait(4); ToRead= getRegBits(GPIO_E_DR,0x40); wait(1); *Chr |=(ToRead & 1)<<i; //SCLK setRegBits(GPIO_E_DR,0x10); wait(4); } wait(4); //SCLK setRegBits(GPIO_E_DR,0x10); }
/** * This method inits the LED interface for the 4DC motor board ***************************************************************************/ void init_leds(void) { setRegBits(GPIO_A_DDR,0xF0); clrRegBits(GPIO_A_PER,0xF0); setRegBits(GPIO_A_DR, 0xF0); }
void TD0_Disable(void) { clrRegBits (TMRD0_CTRL, 0x2000); }