1 /*
2 * Copyright (c) 2020 Siddharth Chandrasekaran <siddharth@embedjournal.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/mgmt/osdp.h>
11
12 /* The devicetree node identifier for the "led0" alias. */
13 #define LED0_NODE DT_ALIAS(led0)
14
15 #if !DT_NODE_HAS_STATUS_OKAY(LED0_NODE)
16 #error "BOARD does not define a debug LED"
17 #endif
18
19 static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET_OR(LED0_NODE, gpios, {0});
20
21 #define SLEEP_TIME_MS (20)
22 #define CNT_PER_SEC (1000 / SLEEP_TIME_MS)
23
cmd_handler(void * unused,struct osdp_cmd * p)24 int cmd_handler(void *unused, struct osdp_cmd *p)
25 {
26 printk("App received command %d\n", p->id);
27 return 0;
28 }
29
main(void)30 int main(void)
31 {
32 int ret, led_state;
33 uint32_t cnt = 0;
34
35 if (!gpio_is_ready_dt(&led0)) {
36 printk("LED0 GPIO port %s is not ready\n", led0.port->name);
37 return 0;
38 }
39
40 ret = gpio_pin_configure_dt(&led0, GPIO_OUTPUT_ACTIVE);
41 if (ret < 0) {
42 printk("Failed to configure gpio port %s pin %d\n",
43 led0.port->name, led0.pin);
44 return 0;
45 }
46
47 osdp_pd_set_command_callback(cmd_handler, NULL);
48
49 led_state = 0;
50 while (1) {
51 if ((cnt & 0x7f) == 0x7f) {
52 /* show a sign of life */
53 led_state = !led_state;
54 }
55 gpio_pin_set(led0.port, led0.pin, led_state);
56 k_msleep(SLEEP_TIME_MS);
57 cnt++;
58 }
59 return 0;
60 }
61