|
6 | 6 |
|
7 | 7 | #include <zephyr/kernel.h> |
8 | 8 | #include <zephyr/drivers/counter.h> |
9 | | -#include <zephyr/drivers/gpio.h> |
10 | 9 |
|
11 | 10 | #define ALARM_CHANNEL_ID (0) |
12 | | -#define LED0_NODE DT_ALIAS(led0) |
13 | | -static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); |
14 | 11 | const struct device *const counter_dev = DEVICE_DT_GET(DT_ALIAS(counter)); |
15 | | -static bool state = true; |
16 | | - |
17 | 12 | struct counter_alarm_cfg counter_cfg; |
18 | 13 |
|
19 | | -void counter_handler(const struct device *counter_dev, uint8_t id, uint32_t ticks, void *user_data) |
| 14 | +void counter_handler(const struct device *counter_dev, uint8_t chan_id, |
| 15 | + uint32_t ticks, void *user_data) |
20 | 16 | { |
21 | | - counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID, user_data); |
22 | | - gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); |
23 | | - for (int i = 1000000UL; i > 0; i--) { |
24 | | - if (state == true) { |
25 | | - state = false; |
26 | | - gpio_pin_set_dt(&led, 1); |
27 | | - } else { |
28 | | - state = true; |
29 | | - gpio_pin_set_dt(&led, 0); |
30 | | - } |
31 | | - } |
32 | | - gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE); |
| 17 | + k_busy_wait(500000); |
| 18 | + counter_stop(counter_dev); |
33 | 19 | } |
34 | 20 |
|
35 | 21 | int main(void) |
36 | 22 | { |
37 | | - counter_start(counter_dev); |
38 | | - |
39 | | - counter_cfg.flags = 0; |
40 | | - counter_cfg.ticks = counter_us_to_ticks(counter_dev, 1000000UL); |
41 | | - counter_cfg.callback = counter_handler; |
42 | | - counter_cfg.user_data = &counter_cfg; |
| 23 | + while (1) { |
| 24 | + k_msleep(1500); |
| 25 | + counter_start(counter_dev); |
43 | 26 |
|
44 | | - counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID, &counter_cfg); |
| 27 | + counter_cfg.flags = 0; |
| 28 | + counter_cfg.ticks = counter_us_to_ticks(counter_dev, 1000000UL); |
| 29 | + counter_cfg.callback = counter_handler; |
| 30 | + counter_cfg.user_data = &counter_cfg; |
45 | 31 |
|
46 | | - while (1) { |
47 | | - k_sleep(K_FOREVER); |
| 32 | + counter_set_channel_alarm(counter_dev, ALARM_CHANNEL_ID, &counter_cfg); |
| 33 | + k_msleep(1500); |
48 | 34 | } |
| 35 | + |
49 | 36 | return 0; |
50 | 37 | } |
0 commit comments