void motorSetup() { /* Set pin mode for Hbridge output pins */ MAP_PinTypeGPIO(AIN1, PIN_MODE_0, false); /* Ain 1 */ MAP_PinTypeGPIO(AIN2, PIN_MODE_0, false); /* Bin 1 */ MAP_PinTypeGPIO(BIN1, PIN_MODE_0, false); /* Bin 2 */ MAP_PinTypeGPIO(BIN2, PIN_MODE_0, false); /* Ain 2 */ /* Get port name and bin number from GPIO number (TI lookup table) */ GPIO_IF_GetPortNPin(AIN1x, &port_ain1, &pin_ain1); GPIO_IF_GetPortNPin(AIN2x, &port_ain2, &pin_ain2); GPIO_IF_GetPortNPin(BIN1x, &port_bin1, &pin_bin1); GPIO_IF_GetPortNPin(BIN2x, &port_bin2, &pin_bin2); /* Set pin direction */ GPIODirModeSet(port_ain1, pin_ain1, 1); GPIODirModeSet(port_ain2, pin_ain2, 1); GPIODirModeSet(port_bin1, pin_bin1, 1); GPIODirModeSet(port_bin2, pin_bin2, 1); /* Set value to write to PIN */ bitA1 = 1 << (AIN1x % 8); bitA2 = 1 << (AIN2x % 8); bitB1 = 1 << (BIN1x % 8); bitB2 = 1 << (BIN2x % 8); // Enable timer A peripheral MAP_PRCMPeripheralClkEnable(PRCM_TIMERA0, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA0); // Split channels and configure for periodic interrupts MAP_TimerConfigure(TIMERA0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_PERIODIC); MAP_TimerPrescaleSet(TIMERA0_BASE, TIMER_A, 0); MAP_TimerPrescaleSet(TIMERA0_BASE, TIMER_B, 0); // Set compare interrupt MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMA_MATCH); MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMB_MATCH); // Configure compare interrupt, start with 0 speed MAP_TimerMatchSet(TIMERA0_BASE, TIMER_A, 0); MAP_TimerMatchSet(TIMERA0_BASE, TIMER_B, 0); // Set timeout interrupt MAP_TimerIntRegister(TIMERA0_BASE, TIMER_A, TimerBaseIntHandlerA); MAP_TimerIntRegister(TIMERA0_BASE, TIMER_B, TimerBaseIntHandlerB); MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMA_TIMEOUT); MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMB_TIMEOUT); // Turn on timers MAP_TimerLoadSet(TIMERA0_BASE, TIMER_A, MOTOR_PRESCALER); MAP_TimerLoadSet(TIMERA0_BASE, TIMER_B, MOTOR_PRESCALER); MAP_TimerEnable(TIMERA0_BASE, TIMER_A); MAP_TimerEnable(TIMERA0_BASE, TIMER_B); }
void motorSetup() { pinMode(AIN1x, OUTPUT); pinMode(AIN2x, OUTPUT); pinMode(BIN1x, OUTPUT); pinMode(BIN2x, OUTPUT); bitA1 = digitalPinToBitMask(AIN1x); bitA2 = digitalPinToBitMask(AIN2x); bitB1 = digitalPinToBitMask(BIN1x); bitB2 = digitalPinToBitMask(BIN2x); portA1 = digitalPinToPort(AIN1x); portA2 = digitalPinToPort(AIN2x); portB1 = digitalPinToPort(BIN1x); portB2 = digitalPinToPort(BIN2x); baseA1 = (uint32_t) portBASERegister(portA1); baseA2 = (uint32_t) portBASERegister(portA2); baseB1 = (uint32_t) portBASERegister(portB1); baseB2 = (uint32_t) portBASERegister(portB2); // Enable timer A peripheral MAP_PRCMPeripheralClkEnable(PRCM_TIMERA0, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA0); // Split channels and configure for periodic interrupts MAP_TimerConfigure(TIMERA0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC | TIMER_CFG_B_PERIODIC); MAP_TimerPrescaleSet(TIMERA0_BASE, TIMER_A, 0); MAP_TimerPrescaleSet(TIMERA0_BASE, TIMER_B, 0); // Set compare interrupt MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMA_MATCH); MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMB_MATCH); // Configure compare interrupt, start with 0 speed MAP_TimerMatchSet(TIMERA0_BASE, TIMER_A, 0); MAP_TimerMatchSet(TIMERA0_BASE, TIMER_B, 0); // Set timeout interrupt MAP_TimerIntRegister(TIMERA0_BASE, TIMER_A, TimerBaseIntHandlerA); MAP_TimerIntRegister(TIMERA0_BASE, TIMER_B, TimerBaseIntHandlerB); MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMA_TIMEOUT); MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMB_TIMEOUT); // Turn on timers MAP_TimerLoadSet(TIMERA0_BASE, TIMER_A, MOTOR_PRESCALER); MAP_TimerLoadSet(TIMERA0_BASE, TIMER_B, MOTOR_PRESCALER); MAP_TimerEnable(TIMERA0_BASE, TIMER_A); MAP_TimerEnable(TIMERA0_BASE, TIMER_B); }
//**************************************************************************** // //! The delay function uses timer to implement the delay time in milliseconds //! //! \param time in millisecond // //! \return void //**************************************************************************** static void delay(int time_ms) { // Initialize Timer 0B as one-shot down counter. MAP_PRCMPeripheralClkEnable(PRCM_TIMERA0, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA0); MAP_TimerConfigure(TIMERA0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_ONE_SHOT); MAP_TimerPrescaleSet(TIMERA0_BASE, TIMER_B, PRESCALE); //Load the value in milisecond MAP_TimerLoadSet(TIMERA0_BASE, TIMER_B, MILLISECONDS_TO_TICKS(time_ms)); // Enable the timer MAP_TimerEnable(TIMERA0_BASE, TIMER_B); //Stall during debug MAP_TimerControlStall(TIMERA0_BASE, TIMER_B, 1); // Enable interrupt upon Time-out MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMB_TIMEOUT); // Clear Interrupt Flag MAP_TimerIntClear(TIMERA0_BASE, MAP_TimerIntStatus(TIMERA0_BASE, true)); //Wait until timer time-out while (MAP_TimerIntStatus(TIMERA0_BASE, true) != TIMER_TIMB_TIMEOUT){} //Disable the timer MAP_TimerDisable(TIMERA0_BASE, TIMER_B); //Disable Interrupt MAP_TimerIntDisable(TIMERA0_BASE, TIMER_TIMB_TIMEOUT); MAP_TimerIntUnregister(TIMERA0_BASE, TIMER_B); }
//**************************************************************************** // //! Setup the timer in PWM mode //! //! \param ulBase is the base address of the timer to be configured //! \param ulTimer is the timer to be setup (TIMER_A or TIMER_B) //! \param ulConfig is the timer configuration setting //! \param ucInvert is to select the inversion of the output //! //! This function //! 1. The specified timer is setup to operate as PWM //! //! \return None. // //**************************************************************************** void SetupTimerPWMMode(unsigned long ulBase, unsigned long ulTimer, unsigned long ulConfig, unsigned char ucInvert) { // // Set GPT - Configured Timer in PWM mode. // MAP_TimerConfigure(ulBase,ulConfig); MAP_TimerPrescaleSet(ulBase,ulTimer,0); // // Inverting the timer output if required // MAP_TimerControlLevel(ulBase,ulTimer,ucInvert); // // Load value set to ~0.5 ms time period // MAP_TimerLoadSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD); // // Match value set so as to output level 0 // MAP_TimerMatchSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD); }
void tone(uint8_t pin, unsigned int frequency, unsigned long duration) { /* Use TIMERA0B since it is not on any pin */ tone_timer = digitalPinToTimer(pin); if(tone_timer == NOT_ON_TIMER) return; if(tone_state != 0 && pin != current_pin) return; g_duration = duration; current_pin = pin; tone_state = 2; MAP_PRCMPeripheralClkEnable(PRCM_TIMERA0, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA0); MAP_TimerConfigure(TIMERA0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PERIODIC); MAP_TimerIntRegister(TIMERA0_BASE, TIMER_B, ToneIntHandler); MAP_TimerIntEnable(TIMERA0_BASE, TIMER_TIMB_TIMEOUT); MAP_TimerPrescaleSet(TIMERA0_BASE, TIMER_B, 7); MAP_TimerLoadSet(TIMERA0_BASE, TIMER_B, (F_CPU / 8) / 1000); MAP_TimerEnable(TIMERA0_BASE, TIMER_B); PWMWrite(pin, 256, 128, frequency); }
//***************************************************************************** // //! Initializing the Timer //! //! \param ePeripheral is the peripheral which need to be initialized. //! \param ulBase is the base address for the timer. //! \param ulConfig is the configuration for the timer. //! \param ulTimer selects amoung the TIMER_A or TIMER_B or TIMER_BOTH. //! \param ulValue is the timer prescale value which must be between 0 and //! 255 (inclusive) for 16/32-bit timers and between 0 and 65535 (inclusive) //! for 32/64-bit timers. //! This function //! 1. Enables and reset the peripheral for the timer. //! 2. Configures and set the prescale value for the timer. //! //! \return none // //***************************************************************************** void Timer_IF_Init( unsigned long ePeripheral, unsigned long ulBase, unsigned long ulConfig, unsigned long ulTimer, unsigned long ulValue) { // // Initialize GPT A0 (in 32 bit mode) as periodic down counter. // MAP_PRCMPeripheralClkEnable(ePeripheral, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(ePeripheral); MAP_TimerConfigure(ulBase,ulConfig); MAP_TimerPrescaleSet(ulBase,ulTimer,ulValue); }
static void EmitCtrlInit(void) { s_ulTimerEmitCtrl = TIMERA1_BASE; MAP_PRCMPeripheralClkEnable(PRCM_TIMERA1,PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA1); MAP_TimerConfigure(s_ulTimerEmitCtrl,TIMER_CFG_PERIODIC); MAP_TimerPrescaleSet(s_ulTimerEmitCtrl,TIMER_A,0); //MAP_TimerIntRegister(s_ulTimerEmitCtrl,TIMER_A,TimerEmitCtrlHandler); osi_InterruptRegister(INT_TIMERA1A, TimerEmitCtrlHandler, 32); MAP_TimerIntEnable(s_ulTimerEmitCtrl,TIMER_TIMA_TIMEOUT); }
void controller_setup(){ // Enable timer A peripheral MAP_PRCMPeripheralClkEnable(PRCM_TIMERA1, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA1); // Configure one channel for periodic interrupts MAP_TimerConfigure(TIMERA1_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC); MAP_TimerPrescaleSet(TIMERA1_BASE, TIMER_A, IMU_CONTROLLER_PRESCALER); // Set timeout interrupt MAP_TimerIntRegister(TIMERA1_BASE, TIMER_A, ControllerIntHandler); MAP_TimerIntEnable(TIMERA1_BASE, TIMER_TIMA_TIMEOUT); // Turn on timers MAP_TimerLoadSet(TIMERA1_BASE, TIMER_A, IMU_CONTROLLER_STARTUP); MAP_TimerEnable(TIMERA1_BASE, TIMER_A); }
void OneMsTaskTimer::start(uint32_t timer_index) { uint32_t load = (F_CPU / 1000); //// !!!! count = 0; overflowing = 0; // Base address for first timer g_ulBase = TIMERA0_BASE + (timer_index <<12); // Configuring the timers MAP_PRCMPeripheralClkEnable(PRCM_TIMERA0 + timer_index, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA0 + timer_index); MAP_TimerConfigure(g_ulBase,TIMER_CFG_PERIODIC); MAP_TimerPrescaleSet(g_ulBase,TIMER_A,0); // Setup the interrupts for the timer timeouts. MAP_TimerIntRegister(g_ulBase, TIMER_A, OneMsTaskTimer_int); MAP_TimerIntEnable(g_ulBase, TIMER_TIMA_TIMEOUT); // Turn on the timers MAP_TimerLoadSet(g_ulBase,TIMER_A, load); // Enable the GPT MAP_TimerEnable(g_ulBase,TIMER_A); }
void odometer_controller_setup(void){ //acc_value_startup = get_accelerometer_default_offset(); // Enable timer A peripheral MAP_PRCMPeripheralClkEnable(PRCM_TIMERA3, PRCM_RUN_MODE_CLK); MAP_PRCMPeripheralReset(PRCM_TIMERA3); // Configure one channel for periodic interrupts MAP_TimerConfigure(TIMERA3_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC); MAP_TimerPrescaleSet(TIMERA3_BASE, TIMER_A, ODOMETER_CONTROLLER_PRESCALER); // Set timeout interrupt MAP_TimerIntRegister(TIMERA3_BASE, TIMER_A, OdometerControllerIntHandler); MAP_TimerIntEnable(TIMERA3_BASE, TIMER_TIMA_TIMEOUT); // Turn on timers MAP_TimerLoadSet(TIMERA3_BASE, TIMER_A, ODOMETER_CONTROLLER_STARTUP); MAP_TimerEnable(TIMERA3_BASE, TIMER_A); }
void PWMWrite(uint8_t pin, uint32_t analog_res, uint32_t duty, uint32_t freq) { analog_res = analog_res * 1000; freq; uint32_t load = (F_CPU / freq) * 1000; uint32_t match = load / (analog_res / duty); match = match; load = load / 1000; uint16_t prescaler = load >> 16; uint16_t prescaler_match = match >> 16; uint8_t timer = digitalPinToTimer(pin); if(timer == NOT_ON_TIMER) return; MAP_PRCMPeripheralClkEnable(PRCM_TIMERA0 + (timer/2), PRCM_RUN_MODE_CLK); uint16_t pnum = digitalPinToPinNum(pin); switch(timer) { /* PWM0/1 */ case TIMERA0A: case TIMERA0B: MAP_PinTypeTimer(pnum, PIN_MODE_5); break; /* PWM2/3 */ case TIMERA1A: case TIMERA1B: MAP_PinTypeTimer(pnum, PIN_MODE_9); break; /* PWM4/5 */ case TIMERA2A: case TIMERA2B: MAP_PinTypeTimer(pnum, PIN_MODE_3); break; /* PWM6/7 */ case TIMERA3A: case TIMERA3B: MAP_PinTypeTimer(pnum, PIN_MODE_3); break; } uint32_t base = TIMERA0_BASE + ((timer/2) << 12); /* FIXME: If B is already opperational and configure A, B get's messed up. */ MAP_TimerConfigure(base, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM); uint16_t timerab = timer % 2 ? TIMER_B : TIMER_A; MAP_TimerPrescaleSet(base, timerab, prescaler); MAP_TimerPrescaleMatchSet(base, timerab, prescaler_match); MAP_TimerControlLevel(base, timerab, 1); MAP_TimerLoadSet(base, timerab, load); MAP_TimerMatchSet(base, timerab, match); MAP_TimerEnable(base, timerab); }