1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/mbox.h>
9 #include <hal/nrf_ipc.h>
10 #include <zephyr/drivers/ipm.h>
11 #include <zephyr/sys/printk.h>
12 #include <zephyr/drivers/timer/nrf_rtc_timer.h>
13 #include <zephyr/logging/log.h>
14 LOG_MODULE_REGISTER(main);
15 
16 #if (CONFIG_SOC_SERIES_BSIM_NRFXX)
17 extern uint32_t shared_cell_buffer;
18 static uint32_t shared_cell = (uintptr_t)&shared_cell_buffer;
19 #else
20 static uint32_t shared_cell = 0x20070000;
21 #endif
22 
sync_callback(void)23 static void sync_callback(void)
24 {
25 	int32_t offset = z_nrf_rtc_timer_nrf53net_offset_get();
26 
27 	__ASSERT(offset >= 0, "Synchronization should be completed");
28 
29 	uint32_t timestamp = sys_clock_tick_get_32() + offset;
30 	uint32_t app_timestamp = *(volatile uint32_t *)shared_cell;
31 
32 	LOG_INF("Local timestamp: %u, application core timestamp: %u",
33 		timestamp, app_timestamp);
34 }
35 
mbox_callback(const struct device * dev,uint32_t channel,void * user_data,struct mbox_msg * data)36 static void mbox_callback(const struct device *dev, uint32_t channel,
37 			  void *user_data, struct mbox_msg *data)
38 {
39 	sync_callback();
40 }
41 
mbox_init(void)42 static int mbox_init(void)
43 {
44 	const struct device *dev;
45 	int err;
46 
47 	dev = COND_CODE_1(CONFIG_MBOX, (DEVICE_DT_GET(DT_NODELABEL(mbox))), (NULL));
48 	if (dev == NULL) {
49 		return -ENODEV;
50 	}
51 
52 	err = mbox_register_callback(dev, 2, mbox_callback, NULL);
53 	if (err < 0) {
54 		return err;
55 	}
56 
57 	return mbox_set_enabled(dev, 2, true);
58 }
59 
main(void)60 int main(void)
61 {
62 	int err;
63 
64 	LOG_INF("Synchronization using mbox driver");
65 	err = mbox_init();
66 	if (err < 0) {
67 		LOG_ERR("Failed to initialize sync RTC listener (err:%d)", err);
68 	}
69 	return 0;
70 }
71