static bool pwm_software_timerCallback(struct timer *timer, void *data) { int32_t ret; struct pwm_software *pwm = data; uint64_t value; if (pwm->oldvalue) { ret = gpioPin_clearPin(pwm->pin); CONFIG_ASSERT(ret >= 0); value = pwm->period - pwm->duty_cycle; PRINTF_ISR(i_set, "Clear Pin and set Waittime to %llu\n", value); ret = timer_oneshot(pwm->timer, value); CONFIG_ASSERT(ret >= 0); } else { ret = gpioPin_setPin(pwm->pin); CONFIG_ASSERT(ret >= 0); /** * Update duty Cycle at begin of PWM */ if (pwm->updated) { pwm->duty_cycle = pwm->duty_cycleNew; pwm->updated = false; } PRINTF_ISR(i_clear, "Set Pin and set Waittime to %llu\n", pwm->duty_cycle); ret = timer_oneshot(pwm->timer, pwm->duty_cycle); CONFIG_ASSERT(ret >= 0); } pwm->oldvalue = !pwm->oldvalue; return false; }
void thread_a(void) { while (1) { printk("Timer launched !\n"); #ifdef DEBUG_IO_SLEEP #ifdef SOFT_TIMER timer_soft_oneshot(5000, &callback, NULL); #else timer_oneshot(200, &callback, NULL); #endif printk("Sleeping..."); pio_set_value(GPIOA_BASE, 3); usleep(200000); pio_clear_value(GPIOA_BASE, 3); printk("Waking up !\n"); #else pio_set_value(GPIOA_BASE, 3); #ifdef SOFT_TIMER timer_soft_oneshot(5000, &callback, NULL); #else timer_oneshot(200, &callback, NULL); #endif printk("Sleeping..."); pio_set_value(GPIOC_BASE, 7); usleep(100000); pio_clear_value(GPIOC_BASE, 7); printk("Waking up !\n"); #endif /* DEBUG_IO_SLEEP */ } }
int32_t timerInit(struct gpio_pin *p) { int32_t ret; pin = p; timer = timer_init(0, 64, 20000, 700); CONFIG_ASSERT(timer != NULL); ret = timer_setOverflowCallback(timer, &irqhandle, NULL); CONFIG_ASSERT(ret == 0); CONFIG_ASSERT(timer_oneshot(timer, n) == 0); return 0; }
PWM_SET_DUTY_CYCLE(software, p, us) { struct pwm_software *pwm = (struct pwm_software *) p; uint32_t ret; if (!pwm->period) { goto software_pwm_setDutyCycle_error0; } /* Stop Timer */ if (us == 0) { PRINTF("Disable Timer Duty Cycle is 0\n"); ret = gpioPin_clearPin(pwm->pin); if (ret < 0) { goto software_pwm_setDutyCycle_error0; } pwm->running = false; ret = timer_stop(pwm->timer); if (ret < 0) { goto software_pwm_setDutyCycle_error0; } } else if (pwm->running) { /* only update on change */ if (pwm->duty_cycle != us) { PRINTF("Enable Timer Update Duty Cycle is %llu\n", us); pwm->updated = true; pwm->duty_cycleNew = us; } } else { PRINTF("Enable Timer Duty Cycle is %llu\n", us); pwm->duty_cycle = us; pwm->updated = false; pwm->running = true; pwm->oldvalue = true; ret = gpioPin_setPin(pwm->pin); if (ret < 0) { goto software_pwm_setDutyCycle_error0; } ret = timer_oneshot(pwm->timer, pwm->duty_cycle); if (ret < 0) { goto software_pwm_setDutyCycle_error0; } } return 0; software_pwm_setDutyCycle_error0: return -1; }
static bool irqhandle(struct timer *timer, void *data) { (void) data; #if 1 #ifndef CONFIG_ASSERT_DISABLED CONFIG_ASSERT(gpioPin_togglePin(pin) == 0); #else gpioPin_togglePin(pin); #endif #endif if (up) { n+=100; } else { n-=100; } if (n >= 1500) { up = false; } else if (n <= 500) { up = true; } CONFIG_ASSERT(timer_oneshot(timer, n) == 0); return false; }