Example #1
0
void TIM2_init() {
  uint32_t SystemCoreClock = 72000000;
  uint16_t prescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
  rcc_clk_enable(RCC_TIMER2);
  /* Time base configuration */
  timer_pause(TIMER2);
  timer_set_prescaler(TIMER2, prescalerValue);
  timer_set_reload(TIMER2, 29); // 800kHz
  
  /* Timing Mode configuration: Channel 1 */
  timer_set_mode(TIMER2, 1, TIMER_OUTPUT_COMPARE);
  timer_set_compare(TIMER2, 1, 8);
  timer_oc_set_mode(TIMER2, 1, TIMER_OC_MODE_FROZEN, ~TIMER_OC_PE);

  /* Timing Mode configuration: Channel 2 */
  timer_set_mode(TIMER2, 2, TIMER_OUTPUT_COMPARE);
  timer_set_compare(TIMER2, 2, 17);
  timer_oc_set_mode(TIMER2, 2, TIMER_OC_MODE_PWM_1, ~TIMER_OC_PE);
  //timer_resume(TIMER2);


  timer_attach_interrupt(TIMER2, TIMER_UPDATE_INTERRUPT, TIM2_IRQHandler);
  /* configure TIM2 interrupt */
  nvic_irq_set_priority(NVIC_TIMER2, 2);
  nvic_irq_enable(NVIC_TIMER2);
}
Example #2
0
/**
\brief Schedule the callback to be called in some specified time.

The delay is expressed relative to the last compare event. It doesn't matter
how long it took to call this function after the last compare, the timer will
expire precisely delayTicks after the last one.

The only possible problem is that it took so long to call this function that
the delay specified is shorter than the time already elapsed since the last
compare. In that case, this function triggers the interrupt to fire right away.

This means that the interrupt may fire a bit off, but this inaccuracy does not
propagate to subsequent timers.

\param delayTicks Number of ticks before the timer expired, relative to the
                  last compare event.
*/
void bsp_timer_scheduleIn(PORT_TIMER_WIDTH delayTicks) {
   PORT_TIMER_WIDTH newCompareValue;
   PORT_TIMER_WIDTH temp_last_compare_value;
   PORT_TIMER_WIDTH current_value;

   temp_last_compare_value = bsp_timer_vars.last_compare_value;

   newCompareValue      =  bsp_timer_vars.last_compare_value+delayTicks;
   bsp_timer_vars.last_compare_value   =  newCompareValue;

   current_value=timer_get_current_value(TIMER_NUM2);
   if (delayTicks<current_value-temp_last_compare_value) {
      // we're already too late, schedule the ISR right now manually

      // setting the interrupt flag triggers an interrupt
      // TODO .. check that this works correctly.

	   timer_set_compare(TIMER_NUM2,TIMER_COMPARE_REG0,delayTicks+current_value);
	   timer_enable(TIMER_NUM2); //in case not enabled
	   //
   } else {
      // this is the normal case, have timer expire at newCompareValue
	  timer_set_compare(TIMER_NUM2,TIMER_COMPARE_REG0,newCompareValue);
	  timer_enable(TIMER_NUM2); //in case not enabled
   }
}
Example #3
0
void radiotimer_setPeriod(PORT_RADIOTIMER_WIDTH period) {
	radiotimer_vars.period=(PORT_RADIOTIMER_WIDTH)period;
	timer_reset(TIMER_NUM3);
	timer_enable(TIMER_NUM3);
	radiotimer_vars.counter_slot_val=radiotimer_getValue(); //it is 0 always. remove that..
	timer_set_compare(TIMER_NUM3, TIMER_COMPARE_REG0,  radiotimer_vars.counter_slot_val+period); //the period timer is controlled by the compare 0 register
}
/* Start interval timer TIMER (0-3).  The timer will issue the
   corresponding INTCMD interrupt RATE times per second.
   This function does not enable the interrupt.  */
void microblaze_timer_configure (unsigned timer, unsigned rate)
{
	unsigned count;

	/* 
	 * Work out timer compare value for a given clock freq 
	 * and interrupt rate
	 */
	count = CONFIG_CPU_CLOCK_FREQ / rate;

	/* Do the actual hardware timer initialization:  */

	/* Enable timer, enable interrupt generation, and put into count down mode */
	/* Set the compare counter */
	timer_set_compare(MICROBLAZE_TIMER_BASE_ADDR, timer, count);

	/* Reset timer and clear interrupt */
	timer_set_csr(MICROBLAZE_TIMER_BASE_ADDR, timer, 
			TIMER_INTERRUPT | TIMER_RESET);

	/* start the timer */
	timer_set_csr(MICROBLAZE_TIMER_BASE_ADDR, timer, 
			TIMER_ENABLE | TIMER_ENABLE_INTR | TIMER_RELOAD | TIMER_DOWN_COUNT);

}
Example #5
0
void Dynamixel::begin(int baud) {
	//TxDString("[DXL]start begin\r\n");

	afio_remap(AFIO_REMAP_USART1);//USART1 -> DXL
	afio_cfg_debug_ports(AFIO_DEBUG_FULL_SWJ_NO_NJRST);
#ifdef BOARD_CM900  //Engineering version case

	 gpio_set_mode(PORT_ENABLE_TXD, PIN_ENABLE_TXD, GPIO_OUTPUT_PP);
	 gpio_set_mode(PORT_ENABLE_RXD, PIN_ENABLE_RXD, GPIO_OUTPUT_PP);
	 gpio_write_bit(PORT_ENABLE_TXD, PIN_ENABLE_TXD, 0 );// TX Disable
	 gpio_write_bit(PORT_ENABLE_RXD, PIN_ENABLE_RXD, 1 );// RX Enable
#else
	 gpio_set_mode(PORT_TXRX_DIRECTION, PIN_TXRX_DIRECTION, GPIO_OUTPUT_PP);
	 gpio_write_bit(PORT_TXRX_DIRECTION, PIN_TXRX_DIRECTION, 0 );// RX Enable
#endif
	 timer_set_mode(TIMER2, TIMER_CH1, TIMER_OUTPUT_COMPARE);

	 timer_pause(TIMER2);

	 uint16 ovf = timer_get_reload(TIMER2);
	 timer_set_count(TIMER2, min(0, ovf));

	 timer_set_reload(TIMER2, 30000);//set overflow

	 ovf = timer_get_reload(TIMER2);
	 timer_set_compare(TIMER2, TIMER_CH1, min(1000, ovf));

	 timer_attach_interrupt(TIMER2, TIMER_CH1, TIM2_IRQHandler);

	 timer_generate_update(TIMER2);
	 timer_resume(TIMER2);

	 dxl_initialize(0, baud);
}
Example #6
0
/**
 * Non-blocking piezo/headphone beep
 */
void buzzer_nonblocking_buzz(float time, bool piezo, bool headphones) {

	// No need to go further if both outputs are
	// false
  if (!(piezo || headphones))
	  return;
  piezo_out = piezo;
  headphones_out = headphones;
  buzz_time = 4100*time*2;

  // Configure timer2 to fire every N microseconds
  timer_pause(TIMER2);
  timer_set_prescaler(TIMER2,1);
  timer_set_reload(TIMER2,(125*CYCLES_PER_MICROSECOND)/2);

  // setup interrupt on channel 2
  timer_set_mode(TIMER2,TIMER_CH2,TIMER_OUTPUT_COMPARE);
  timer_set_compare(TIMER2,TIMER_CH2,MAX_RELOAD-1);
  timer_attach_interrupt(TIMER2,TIMER_CH2,buzzer_handler);

  // start timer2
  buzz_count=0;
  timer_generate_update(TIMER2); // refresh timer count, prescale, overflow
  timer_resume(TIMER2);
}
Example #7
0
static void timer_default_config(timer_dev *dev) {
    timer_adv_reg_map *regs = (dev->regs).adv;
    const uint16 full_overflow = 0xFFFF;
    const uint16 half_duty = 0x8FFF;

    timer_init(dev);
    timer_pause(dev);

    regs->CR1 = TIMER_CR1_ARPE;
    regs->PSC = 1;
    regs->SR = 0;
    regs->DIER = 0;
    regs->EGR = TIMER_EGR_UG;
    switch (dev->type) {
    case TIMER_ADVANCED:
        regs->BDTR = TIMER_BDTR_MOE | TIMER_BDTR_LOCK_OFF;
        // fall-through
    case TIMER_GENERAL:
        timer_set_reload(dev, full_overflow);
        for (uint8 channel = 1; channel <= 4; channel++) {
            if (timer_has_cc_channel(dev, channel)) {
                timer_set_compare(dev, channel, half_duty);
                timer_oc_set_mode(dev, channel, TIMER_OC_MODE_PWM_1,
                                  TIMER_OC_PE);
            }
        }
        // fall-through
    case TIMER_BASIC:
        break;
    }

    timer_generate_update(dev);
    timer_resume(dev);
}
Example #8
0
void speaker_setCompare(int channel, uint16 val) {
    uint16 ovf = speaker_getOverflow();

    //Selects the minimum of the two
    uint16 result = ((val)<(ovf)?(val):(ovf));

    timer_set_compare(SPEAKER_TIMER, (uint8)channel, result);
}
Example #9
0
void pwmWrite(uint8 pin, uint16 duty_cycle)
{
  if (pin >= BOARD_NR_GPIO_PINS) return;
  timer_dev *dev = PIN_MAP[pin].timer_device;
  if (dev == NULL || dev->type == TIMER_BASIC) return;

  timer_set_compare(dev, PIN_MAP[pin].timer_channel, duty_cycle);
}
Example #10
0
void radiotimer_schedule(PORT_RADIOTIMER_WIDTH offset) {
	uint32_t cur;
	PORT_RADIOTIMER_WIDTH current=radiotimer_vars.counter_slot_val;//references to the init of the current time slot.
	// offset when to fire
	//get current
	cur=current + offset;
	timer_set_compare(TIMER_NUM3, TIMER_COMPARE_REG1,cur); //this is controlled by the compare 1 register

}
Example #11
0
void pwmWrite(uint8 pin, uint16 duty_cycle) {
    if (pin >= BOARD_NR_GPIO_PINS) {
        return;
    }
    timer_dev *dev = PIN_MAP[pin].timer_device;
    uint8 cc_channel = PIN_MAP[pin].timer_channel;
    ASSERT(dev && cc_channel);
    timer_set_compare(dev, cc_channel, duty_cycle);
}
Example #12
0
void pwmWrite(uint8 pin, uint16 duty_cycle) 
{
    timer_dev *dev = PIN_MAP[pin].timer_device;
    uint16 duty_out;

    if (pin >= BOARD_NR_GPIO_PINS || dev == NULL || dev->type == TIMER_BASIC) {
        return;
    }

    if( duty_cycle > 1000 ) duty_cycle = 1000;

    duty_out = duty_cycle * 0xFFFF / 1000;

    timer_set_compare(dev, PIN_MAP[pin].timer_channel, duty_out);
}
Example #13
0
void DmxClass::begin(uint16 n) {
  SerialUSB.println("DMX begin");
  this->number_of_channels = n; // red, green, and blue are independent channels
  //SerialUSB.end();
  
  // initializes timer configurations
  timer_pause(this->dmx_timer);
  timer_set_prescaler(this->dmx_timer, 1); 
  timer_set_reload(this->dmx_timer, 288); // 4 us = 288 clock pulses @ 72MHz
  timer_generate_update(this->dmx_timer); // update new reload value
  timer_set_mode(this->dmx_timer, dmx_timer_ch, TIMER_OUTPUT_COMPARE);
  timer_set_compare(this->dmx_timer, dmx_timer_ch, 1); // test
  timer_attach_interrupt(this->dmx_timer, TIMER_CC1_INTERRUPT, dmx_handler_hack);
  timer_resume(this->dmx_timer);
}
Example #14
0
void analogWrite(uint8 pin, int val)
{
  if (pin >= BOARD_NR_GPIO_PINS) return;
  timer_dev *dev = PIN_MAP[pin].timer_device;
  if (dev == NULL || dev->type == TIMER_BASIC) return;

  // configure as PWM if not
  if ( TIMER_OC_MODE_PWM_1 != timer_oc_get_mode(dev, PIN_MAP[pin].timer_channel) )
  {
    pinMode(pin, PWM);
  }

  if (val > 255) val = 255;

  // map compare according to the current reload
  uint16 compare = map(val, 0, 255, 0, timer_get_reload(dev));

  timer_set_compare(dev, PIN_MAP[pin].timer_channel, compare);
}
Example #15
0
void buzzer_nonblocking_buzz(float time) {

  buzz_time = 4100*time*2;

  // Configure timer2 to fire every N microseconds
  timer_pause(TIMER2);
  timer_set_prescaler(TIMER2,1);
  timer_set_reload(TIMER2,(125*CYCLES_PER_MICROSECOND)/2);

  // setup interrupt on channel 2
  timer_set_mode(TIMER2,TIMER_CH2,TIMER_OUTPUT_COMPARE);
  timer_set_compare(TIMER2,TIMER_CH2,MAX_RELOAD-1);
  timer_attach_interrupt(TIMER2,TIMER_CH2,buzzer_handler);

  // start timer2
  buzz_count=0;
  timer_generate_update(TIMER2); // refresh timer count, prescale, overflow
  timer_resume(TIMER2);
}
Example #16
0
// Adafruit Modification to change PWM Period/Frequency
void pwmPeriod(uint8 pin, uint32 us)
{
  if (pin >= BOARD_NR_GPIO_PINS) return;
  timer_dev *dev = PIN_MAP[pin].timer_device;
  if (dev == NULL || dev->type == TIMER_BASIC) return;

  // Get timer's max speed in hz
  uint32 max_speed = rcc_dev_timer_clk_speed(dev->clk_id);

  // period in cpu cycles
  uint32 cycle = us * (max_speed / 1000000UL);

  uint16 prescaler = (uint16) (cycle / TIMER_MAX_RELOAD);
  uint16 reload    = (uint16) round(cycle / (prescaler+1));

  // Re-map compare to preserve duty cycle
  uint16 compare   =  timer_get_compare(dev, PIN_MAP[pin].timer_channel);

  compare = map(compare, 0, timer_get_reload(dev), 0, reload);

  timer_set_prescaler(dev, prescaler);
  timer_set_reload(dev, reload);
  timer_set_compare(dev, PIN_MAP[pin].timer_channel, compare);
}
void HardwareTimer::setCompare(int channel, uint16 val) {
    uint16 ovf = this->getOverflow();
    timer_set_compare(this->dev, (uint8)channel, min(val, ovf));
}