bool timer_set(long cycles, bool start) { unsigned int divider = cycles, prescaler_bit = 0, prescaler = 1, old_irq; if(cycles < 1) return false; if(start && pfn_unregister != NULL) { pfn_unregister(); pfn_unregister = NULL; } /* Increase prescale values starting from 0 to make the cycle count fit */ while(divider > 65535 && prescaler <= 1024) { prescaler <<= 2; /* 1, 4, 16, 64, 256, 1024 */ prescaler_bit++; divider = cycles / prescaler; } old_irq = disable_irq_save(); __tcu_stop_counter(1); if(start) { __tcu_disable_pwm_output(1); __tcu_mask_half_match_irq(1); __tcu_unmask_full_match_irq(1); /* EXTAL clock = CFG_EXTAL (12Mhz in most targets) */ __tcu_select_extalclk(1); } REG_TCU_TCSR(1) = (REG_TCU_TCSR(1) & ~TCU_TCSR_PRESCALE_MASK) | (prescaler_bit << TCU_TCSR_PRESCALE_BIT); REG_TCU_TCNT(1) = 0; REG_TCU_TDHR(1) = 0; REG_TCU_TDFR(1) = divider; __tcu_clear_full_match_flag(1); if(start) { system_enable_irq(IRQ_TCU1); __tcu_start_counter(1); } restore_irq(old_irq); return true; }
static void set_backlight(int val) { if(val == old_val) return; __tcu_disable_pwm_output(BACKLIGHT_PWM); __tcu_stop_counter(BACKLIGHT_PWM); __tcu_set_count(BACKLIGHT_PWM, 0); __tcu_set_half_data(BACKLIGHT_PWM, logtable[val - 1]); __tcu_set_full_data(BACKLIGHT_PWM, 256); __tcu_start_counter(BACKLIGHT_PWM); __tcu_enable_pwm_output(BACKLIGHT_PWM); old_val = val; }
/* This function will delay sdelay ms*/ static void sd_mdelay(int sdelay) { __tcu_set_count(0,0); __tcu_disable_pwm_output(0); __tcu_select_rtcclk(0); __tcu_select_clk_div1(0); __tcu_mask_half_match_irq(0); __tcu_mask_full_match_irq(0); REG_TCU_TDFR(0) = 32; // 1 ms __tcu_start_counter(0); while(1) { if(REG_TCU_TCNT(0) > sdelay) break; } __tcu_stop_counter(0); __tcu_stop_timer_clock(0); }
bool _backlight_init(void) { __gpio_as_pwm(BACKLIGHT_PWM); __tcu_start_timer_clock(BACKLIGHT_PWM); __tcu_stop_counter(BACKLIGHT_PWM); __tcu_init_pwm_output_low(BACKLIGHT_PWM); __tcu_set_pwm_output_shutdown_graceful(BACKLIGHT_PWM); __tcu_disable_pwm_output(BACKLIGHT_PWM); __tcu_select_extalclk(BACKLIGHT_PWM); /* 12 MHz */ __tcu_select_clk_div64(BACKLIGHT_PWM); /* 187.5 kHz */ __tcu_mask_half_match_irq(BACKLIGHT_PWM); __tcu_mask_full_match_irq(BACKLIGHT_PWM); __tcu_set_count(BACKLIGHT_PWM, 0); __tcu_set_half_data(BACKLIGHT_PWM, 0); __tcu_set_full_data(BACKLIGHT_PWM, 256); return true; }
void mdelay (unsigned int ms) { int i; __tcu_disable_pwm_output(3); __tcu_mask_half_match_irq(3); __tcu_mask_full_match_irq(3); __tcu_select_extalclk(3); __tcu_select_clk_div16(3); REG_TCU_TDFR(3) = CFG_EXTAL / 16 / 1000; REG_TCU_TDHR(3) = CFG_EXTAL / 16 / 1000; __tcu_set_count(3, 0); __tcu_start_counter(3); for (i = 0; i < ms; i++) { __tcu_clear_full_match_flag(3); while (!__tcu_full_match_flag(3)); } __tcu_stop_counter(3); }
void timer_stop(void) { unsigned int old_irq = disable_irq_save(); __tcu_stop_counter(1); restore_irq(old_irq); }
static void set_backlight_off(void) { __tcu_disable_pwm_output(BACKLIGHT_PWM); __tcu_stop_counter(BACKLIGHT_PWM); }