1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr.h>
8 #include <device.h>
9 #include <devicetree.h>
10 
11 #include <drivers/edac.h>
12 
13 #include <logging/log.h>
14 LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
15 
16 #define STACKSIZE	1024
17 #define PRIORITY	7
18 
19 static atomic_t handled;
20 
21 /**
22  * Callback called from ISR. Runs in supervisor mode
23  */
notification_callback(const struct device * dev,void * data)24 static void notification_callback(const struct device *dev, void *data)
25 {
26 	/* Special care need to be taken for NMI callback:
27 	 * delayed_work, mutex and semaphores are not working stable
28 	 * here, using integer increment for now
29 	 */
30 	atomic_set(&handled, true);
31 }
32 
33 #define DEVICE_NAME DT_LABEL(DT_NODELABEL(ibecc))
34 
main(void)35 void main(void)
36 {
37 	const struct device *dev;
38 
39 	dev = device_get_binding(DEVICE_NAME);
40 	if (!dev) {
41 		LOG_ERR("Cannot open EDAC device: %s", DEVICE_NAME);
42 		return;
43 	}
44 
45 	if (edac_notify_callback_set(dev, notification_callback)) {
46 		LOG_ERR("Cannot set notification callback");
47 		return;
48 	}
49 
50 	LOG_INF("EDAC shell application initialized");
51 }
52 
thread_function(void)53 void thread_function(void)
54 {
55 	LOG_DBG("Thread started");
56 
57 	while (true) {
58 		if (atomic_cas(&handled, true, false)) {
59 			printk("Got notification about IBECC event\n");
60 			k_sleep(K_MSEC(300));
61 		}
62 	}
63 }
64 
65 K_THREAD_DEFINE(thread_id, STACKSIZE, thread_function, NULL, NULL, NULL,
66 		PRIORITY, 0, 0);
67