void buzzerbk::set(uint8_t status) //reading and mapping analog input function { portEnable(_port); if(status>1){status=1;} if(status<0){status=0;} if(setstate!=status){ //solo si no se repite write2BK(SLAVE_ADDRESS_BUZZER, ADDR_FREQ, 800); error=writeBK(SLAVE_ADDRESS_BUZZER, ADDR_MODE, (uint8_t)status); setstate=status; //actualiza el valor if(status==0){tonefreq =0;} //resetea la frecuencia } portEnable(0); }
void motorbk::set(uint8_t direction) //function to stop the motor { portEnable(_port); if (direction==0){ writeBK(SLAVE_ADDRESS_MOTOR,ADDR_VELOCITY_M,0); error=writeBK(SLAVE_ADDRESS_MOTOR,ADDR_DIRECTION_M,STOP); } else{ writeBK(SLAVE_ADDRESS_MOTOR,ADDR_VELOCITY_M,Velocity_saved); error=writeBK(SLAVE_ADDRESS_MOTOR,ADDR_DIRECTION_M,direction); } portEnable(0); }
void buzzerbk::beep(unsigned int t_on,unsigned int t_off) { if (t_on>65000) t_on=65000; if (t_off>65000) t_off=65000; portEnable(_port); writeBK(SLAVE_ADDRESS_BUZZER,ADDR_MODE,(byte)MODE_BEEP); write2BK(SLAVE_ADDRESS_BUZZER,ADDR_TON,t_on); error = write2BK(SLAVE_ADDRESS_BUZZER,ADDR_TOFF,t_off); setstate=1; //actualiza el valor portEnable(0); }
void motorbk::set(uint8_t direction, uint8_t speed) //function to set the motor in a certain speed and direction { portEnable(_port); if(motor_flag_usb == 0){ //battery connected Velocity_saved = speed; writeBK(SLAVE_ADDRESS_MOTOR,ADDR_VELOCITY_M,Velocity_saved); error=writeBK(SLAVE_ADDRESS_MOTOR,ADDR_DIRECTION_M,direction); } if(motor_flag_usb == 1){ //usb connected Velocity_saved = 0; writeBK(SLAVE_ADDRESS_MOTOR,ADDR_VELOCITY_M,Velocity_saved); error=writeBK(SLAVE_ADDRESS_MOTOR,ADDR_DIRECTION_M,STOP); } portEnable(0); }
void buzzerbk::playtone(unsigned int frequency) { portEnable(_port); if(frequency<60||frequency>20000) frequency = 0; else{ if(tonefreq != frequency){ write2BK(SLAVE_ADDRESS_BUZZER, ADDR_FREQ, frequency); writeBK(SLAVE_ADDRESS_BUZZER, ADDR_MODE,1); tonefreq = frequency; setstate=1; //actualiza el valor } } portEnable(0); }
void buzzerbk::playtone(unsigned int frequency, unsigned int duration) { portEnable(_port); if (duration>65000) duration=65000; if(frequency<60||frequency>20000) frequency = 0; else{ if(tonefreq != frequency){ write2BK(SLAVE_ADDRESS_BUZZER, ADDR_FREQ, frequency); error=write2BK(SLAVE_ADDRESS_BUZZER,ADDR_DURATION, duration); writeBK(SLAVE_ADDRESS_BUZZER, ADDR_MODE, (byte)MODE_PLAY1); tonefreq = frequency; setstate=1; //actualiza el valor } } portEnable(0); }
void ftmInit(int timer, void *callBack, ftmCfg_t *ftmCfg) { volatile ftm_t *ftm = getFtmHandle(timer); uint8_t chIdx; int freqPS; uint16_t mod; uint16_t cntin; uint32_t outmask = 0; uint32_t value; *ftmCtrl[timer].simScgcPtr |= ftmCtrl[timer].simScgcEnBit; #if 1 if (callBack) { ftmCtrl[timer].callBack = callBack; hwInstallISRHandler(ISR_FTM0 + timer, ftmCtrl[timer].isr); ftm->sc |= FTM_SC_TOIE_BIT; } #endif ftm->conf |= FTM_CONF_BDMMODE_FUNCTIONAL << FTM_CONF_BDMMODE_SHIFT; ftm->cntin = ftmCfg->initCount; ftmCtrl[timer].ftmMode = ftmCfg->mode; ftm->outmask = FTM_OUTMASK_ALL; switch (ftmCfg->mode) { case FTM_MODE_INPUT_CAPTURE: break; case FTM_MODE_QUADRATURE_DECODE: ftm->mod = ftmCfg->mod; if (timer != FTM_0) { /* TODO Add QD support on FTM_0 - check pinout */ ftm->sc |= FTM_SC_CLKS_EXTERNAL_CLOCK << FTM_SC_CLKS_SHIFT; if (portEnable(ftmCtrl[timer].port[0]) != ERROR) { ftm->qdctrl = FTM_QDCTRL_QUADEN_BIT; PORT_PCR(ftmCtrl[timer].port[0], ftmCtrl[timer].pinQDPhA) = PORT_IRQC_DISABLED | ftmCtrl[timer].pinQDMux; PORT_PCR(ftmCtrl[timer].port[0], ftmCtrl[timer].pinQDPhB) = PORT_IRQC_DISABLED | ftmCtrl[timer].pinQDMux; } switch (ftmCfg->quadCfg) { default: case FTM_QUAD_CONFIG_FILTER_NONE: ftm->qdctrl &= ~(FTM_QDCTRL_PHAFLTREN_BIT | FTM_QDCTRL_PHBFLTREN_BIT); break; case FTM_QUAD_CONFIG_FILTER_LOW: case FTM_QUAD_CONFIG_FILTER_MED: case FTM_QUAD_CONFIG_FILTER_HIGH: ftm->qdctrl |= FTM_QDCTRL_PHAFLTREN_BIT; ftm->qdctrl |= FTM_QDCTRL_PHBFLTREN_BIT; value = ftmCfg->quadCfg << FTM_FILTER_VALUE_CH0_SHIFT; value |= ftmCfg->quadCfg << FTM_FILTER_VALUE_CH1_SHIFT; ftm->filter = value; break; } } break; case FTM_MODE_OUTPUT_COMPARE: break; case FTM_MODE_PWM: freqPS = calcFreqPS(ftmCfg->pwmFreq); if (ftmCfg->pwmCfgBits & FTM_PWM_CFG_CENTER_ALINGNED) { ftmCtrl[timer].pwmAlign = PWM_CENTER_ALIGNED; /* The Freescale ref manual indicates that one should set * the FTM_SC_CPWMS_BIT for center aligned PWM. However, this * doesn't work in combined mode (the channels setup the realtive * triggering to center the pulse). */ if (ftmCfg->pwmCfgBits & FTM_PWM_CFG_COMBINED_MASK) { ftm->sc &= ~FTM_SC_CPWMS_BIT; } else { ftm->sc |= FTM_SC_CPWMS_BIT; } } else { ftmCtrl[timer].pwmAlign = PWM_EDGE_ALIGNED; ftm->sc &= ~FTM_SC_CPWMS_BIT; } ftm->synconf = ( FTM_SYNCONF_SYNCMODE_BIT | FTM_SYNCONF_SWWRBUF_BIT | FTM_SYNCONF_INVC_BIT | FTM_SYNCONF_SWINVC_BIT); ftm->sync = FTM_SYNC_CNTMAX_BIT; ftm->sync |= FTM_SYNC_SWSYNC_BIT; if (ftmCfg->pwmCfgBits & FTM_PWM_CFG_OUTPUT_MASK) { ftm->synconf |= FTM_SYNCONF_SWOM_BIT; ftm->sync |= FTM_SYNC_SYNCHOM_BIT; } if (ftmCfg->deadTime) { ftm->mode = FTM_MODE_WPDIS_BIT; ftm->deadtime = deadCounts(ftmCfg->deadTime); } /* need local mod as you can't read ftm->mod until it is latched over */ mod = freqToMod(ftmCfg->pwmFreq, freqPS); if (ftmCtrl[timer].pwmAlign == PWM_CENTER_ALIGNED) { cntin = -mod / 2; mod = mod / 2 - 1; } else { mod = mod - 1; cntin = 0; } ftm->mod = mod; ftm->cntin = cntin; ftm->qdctrl &= ~FTM_QDCTRL_QUADEN_BIT; ftm->exttrig = ftmCfg->triggerBits; for (chIdx = FTM_CH_0; chIdx < MAX_FTM_CH; chIdx++) { volatile uint32_t *ccsPtr; volatile uint32_t *cvPtr; int32_t combinedBit; int32_t complementaryBit; int32_t combinedIdx; uint8_t channel = ftmCfg->channels[chIdx] ; int activeLow = ftmCfg->activeLow[chIdx]; if (channel == FTM_CH_NONE) { break; } if (portEnable(ftmCtrl[timer].port[channel]) == ERROR) { assert(0); break; } PORT_PCR(ftmCtrl[timer].port[channel], ftmCtrl[timer].pinCh[channel]) = PORT_IRQC_DISABLED | ftmCtrl[timer].pinChMux[channel]; ftm->cnt = 0; ftm->mode = FTM_MODE_WPDIS_BIT; if (activeLow) { ftm->pol |= (1 << channel); } else { ftm->pol &= ~(1 << channel); } if (ftmCfg->pwmCfgBits & FTM_PWM_CFG_OUTPUT_MASK) { outmask |= (1 << channel); } else { outmask &= ~(1 << channel); } switch (channel) { case FTM_CH_0: ccsPtr = &ftm->c0cs; cvPtr = &ftm->c0v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_0_1; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_0_1; combinedIdx = PWM_COMBINED_CHS_0_1_IDX; break; case FTM_CH_1: ccsPtr = &ftm->c1cs; cvPtr = &ftm->c1v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_0_1; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_0_1; combinedIdx = PWM_COMBINED_CHS_0_1_IDX; break; case FTM_CH_2: ccsPtr = &ftm->c2cs; cvPtr = &ftm->c2v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_2_3; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_2_3; combinedIdx = PWM_COMBINED_CHS_2_3_IDX; break; case FTM_CH_3: ccsPtr = &ftm->c3cs; cvPtr = &ftm->c3v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_2_3; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_2_3; combinedIdx = PWM_COMBINED_CHS_2_3_IDX; break; case FTM_CH_4: ccsPtr = &ftm->c4cs; cvPtr = &ftm->c4v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_4_5; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_4_5; combinedIdx = PWM_COMBINED_CHS_4_5_IDX; break; case FTM_CH_5: ccsPtr = &ftm->c5cs; cvPtr = &ftm->c5v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_4_5; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_4_5; combinedIdx = PWM_COMBINED_CHS_4_5_IDX; break; case FTM_CH_6: ccsPtr = &ftm->c6cs; cvPtr = &ftm->c6v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_6_7; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_6_7; combinedIdx = PWM_COMBINED_CHS_6_7_IDX; break; case FTM_CH_7: ccsPtr = &ftm->c7cs; cvPtr = &ftm->c7v; combinedBit = FTM_PWM_CFG_COMBINED_MODE_CHS_6_7; complementaryBit = FTM_PWM_CFG_COMPLEMENTARY_CH_6_7; combinedIdx = PWM_COMBINED_CHS_6_7_IDX; break; default: assert(0); ccsPtr = NULL; cvPtr = NULL; break; } if (ccsPtr && cvPtr) { if (ftmCtrl[timer].pwmAlign == PWM_CENTER_ALIGNED) { *ccsPtr = FTM_CH_CS_ELSB_BIT; } else { *ccsPtr = FTM_CH_CS_MSB_BIT | FTM_CH_CS_ELSB_BIT; } #if 0 /* * TODO: It is possible to interrupt on each channel * but this quickly swamps the processor with interrupts. * I would like to add an implicit request for the * ch interrupts as separate command. * * Channel interrupts probably make more sense for the * input capture mode. * */ if (callBack) { // Maybe limit to first ch? && chIdx == 0) *ccsPtr |= FTM_CH_CS_CHIE_BIT; } #endif if (ftmCfg->pwmCfgBits & combinedBit) { uint32_t combine = ftm->combine; combine |= (FTM_COMBINED_BIT << (8 * combinedIdx)); combine |= (FTM_SYNCEN_BIT << (8 * combinedIdx)); if (ftmCfg->pwmCfgBits & complementaryBit) { combine |= (FTM_COMP_BIT << (8 * combinedIdx)); } else { combine &= ~(FTM_COMP_BIT << (8 * combinedIdx)); } if (ftmCfg->deadTime) { ftm->mode = FTM_MODE_WPDIS_BIT; combine |= (FTM_DEADTIME_BIT << (8 * combinedIdx)); } ftm->mode = FTM_MODE_WPDIS_BIT; ftm->combine = combine; } else { ftm->mode = FTM_MODE_WPDIS_BIT; ftm->combine &= ~(FTM_COMBINED_BIT << (8 * combinedIdx)); ftm->combine &= ~(FTM_COMP_BIT << (8 * combinedIdx)); ftm->combine |= (FTM_SYNCEN_BIT << (8 * combinedIdx)); } ftm->mode = FTM_MODE_WPDIS_BIT; ftmPwmWrite(timer, channel, ftmCfg->dutyScaled[chIdx], TRUE); } } ftm->mode |= FTM_MODE_INIT_BIT; ftm->outmask = outmask; ftm->pwmload |= FTM_PWMLOAD_LDOK_BIT | FTM_PWMLOAD_ALL_MASK; ftm->sync |= FTM_SYNC_SWSYNC_BIT; ftm->sc |= (FTM_SC_CLKS_BUS << FTM_SC_CLKS_SHIFT) | freqPS; break; } ftm->mode |= FTM_MODE_FTMEN_BIT; }