1 /*
2  * Copyright (c) 2022 Microchip Technology Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <soc.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/device.h>
11 #include <zephyr/drivers/led.h>
12 #include <zephyr/logging/log_ctrl.h>
13 #include <zephyr/logging/log.h>
14 #include <zephyr/sys/sys_io.h>
15 LOG_MODULE_DECLARE(led, CONFIG_LED_LOG_LEVEL);
16 
17 #define K_WAIT_DELAY          100u
18 
19 struct dev_info {
20 	const struct device *dev;
21 	uint32_t base_addr;
22 };
23 
24 #define XEC_BBLED_DEV(node_id)		\
25 	{ .dev = DEVICE_DT_GET(node_id), .base_addr = (uint32_t)DT_REG_ADDR(node_id) },
26 
27 static const struct dev_info led_dev_table[] = {
28 	DT_FOREACH_STATUS_OKAY(microchip_xec_bbled, XEC_BBLED_DEV)
29 };
30 
print_bbled_regs(mem_addr_t bbled_base)31 static void print_bbled_regs(mem_addr_t bbled_base)
32 {
33 	uint32_t r = 0;
34 
35 	if (!bbled_base) {
36 		return;
37 	}
38 
39 	LOG_INF("BBLED @ 0x%lx", bbled_base);
40 
41 	r = sys_read32(bbled_base);
42 	LOG_INF("config = 0x%x", r);
43 	r = sys_read32(bbled_base + 4U);
44 	LOG_INF("limits = 0x%x", r);
45 	r = sys_read32(bbled_base + 8U);
46 	LOG_INF("delay = 0x%x", r);
47 	r = sys_read32(bbled_base + 0xcU);
48 	LOG_INF("update_step_size = 0x%x", r);
49 	r = sys_read32(bbled_base + 0x10U);
50 	LOG_INF("update_interval = 0x%x", r);
51 	r = sys_read32(bbled_base + 0x14U);
52 	LOG_INF("output_delay = 0x%x", r);
53 }
54 
led_test(void)55 int led_test(void)
56 {
57 	int ret = 0;
58 	size_t n = 0;
59 	uint32_t delay_on = 0, delay_off = 0;
60 
61 	/* Account for the time serial port is detected so log messages can
62 	 * be seen
63 	 */
64 	k_sleep(K_SECONDS(1));
65 
66 	LOG_INF("Microchip XEC EVB BBLED Sample");
67 
68 	for (n = 0; n < ARRAY_SIZE(led_dev_table); n++) {
69 		const struct device *dev = led_dev_table[n].dev;
70 		uint32_t base = led_dev_table[n].base_addr;
71 
72 		LOG_INF("BBLED instance %d @ %x", n, base);
73 		print_bbled_regs(base);
74 
75 		if (!device_is_ready(dev)) {
76 			LOG_ERR("%s: device not ready", dev->name);
77 			continue;
78 		}
79 
80 		LOG_INF("blink: T = 0.5 second, duty cycle = 0.5");
81 		delay_on = 250;
82 		delay_off = 250;
83 		ret = led_blink(dev, 0, delay_on, delay_off);
84 		if (ret) {
85 			LOG_ERR("LED blink API returned error %d", ret);
86 		}
87 		print_bbled_regs(base);
88 
89 		LOG_INF("Delay 5 seconds");
90 		k_sleep(K_SECONDS(5));
91 
92 		LOG_INF("blink: T = 3 seconds, duty cycle = 0.4");
93 		delay_on = 1200;
94 		delay_off = 1800;
95 		ret = led_blink(dev, 0, delay_on, delay_off);
96 		if (ret) {
97 			LOG_ERR("LED blink API returned error %d", ret);
98 		}
99 		print_bbled_regs(base);
100 
101 		LOG_INF("Delay 15 seconds");
102 		k_sleep(K_SECONDS(15));
103 
104 		LOG_INF("Set ON");
105 		ret = led_on(dev, 0);
106 		if (ret) {
107 			LOG_ERR("LED ON API returned error %d", ret);
108 		}
109 		print_bbled_regs(base);
110 
111 		LOG_INF("Delay 2 seconds");
112 		k_sleep(K_SECONDS(2));
113 
114 		LOG_INF("Set OFF");
115 		ret = led_off(dev, 0);
116 		if (ret) {
117 			LOG_ERR("LED OFF API returned error %d", ret);
118 		}
119 		print_bbled_regs(base);
120 
121 		LOG_INF("Delay 2 seconds");
122 		k_sleep(K_SECONDS(2));
123 	}
124 
125 	LOG_INF("LED test done");
126 
127 	return 0;
128 }
129 
main(void)130 int main(void)
131 {
132 	led_test();
133 	return 0;
134 }
135