1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/devicetree.h>
10 
11 #include <zephyr/drivers/edac.h>
12 
13 #include <zephyr/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 
main(void)33 int main(void)
34 {
35 	const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(ibecc));
36 
37 	if (!device_is_ready(dev)) {
38 		printk("%s: device not ready.\n", dev->name);
39 		return 0;
40 	}
41 
42 	if (edac_notify_callback_set(dev, notification_callback)) {
43 		LOG_ERR("Cannot set notification callback");
44 		return 0;
45 	}
46 
47 	LOG_INF("EDAC shell application initialized");
48 	return 0;
49 }
50 
thread_function(void)51 void thread_function(void)
52 {
53 	LOG_DBG("Thread started");
54 
55 	while (true) {
56 		if (atomic_cas(&handled, true, false)) {
57 			printk("Got notification about IBECC event\n");
58 			k_sleep(K_MSEC(300));
59 		}
60 	}
61 }
62 
63 K_THREAD_DEFINE(thread_id, STACKSIZE, thread_function, NULL, NULL, NULL,
64 		PRIORITY, 0, 0);
65