1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/sys/printk.h>
10 #include <cmsis_core.h>
11 #include <zephyr/arch/arm/exception.h>
12 
13 #define LED0_NODE DT_ALIAS(led0)
14 static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
15 
timer_handler(struct k_timer * timer)16 void timer_handler(struct k_timer *timer)
17 {
18 	gpio_pin_toggle_dt(&led);
19 }
20 K_TIMER_DEFINE(led_timer, timer_handler, NULL);
21 
debug_mon_enable(void)22 int debug_mon_enable(void)
23 {
24 	/*
25 	 * Cannot enable monitor mode if C_DEBUGEN bit is set. This bit can only be
26 	 * altered from debug access port. It is cleared on power-on-reset.
27 	 */
28 	bool is_in_halting_mode = (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 1;
29 
30 	if (is_in_halting_mode) {
31 		return -1;
32 	}
33 
34 	/* Enable monitor mode debugging by setting MON_EN bit of DEMCR */
35 	CoreDebug->DEMCR |= CoreDebug_DEMCR_MON_EN_Msk;
36 	return 0;
37 }
38 
39 /*
40  * Interrupt handler that will be called each time the device
41  * enters a breakpoint while debugging in monitor mode.
42  * With CONFIG_CORTEX_M_DEBUG_MONITOR_HOOK enabled this exception is set to lowest
43  * possible priority (IRQ_PRIO_LOWEST).
44  */
z_arm_debug_monitor(void)45 void z_arm_debug_monitor(void)
46 {
47 	/*
48 	 * Implement the logic or use a ready implementation available for your platform
49 	 * (ie. SEGGER implementation for their debug probes)
50 	 */
51 	printk("Entered debug monitor interrupt\n");
52 
53 	/* Spin in breakpoint. Other, higher-priority interrupts will continue to execute */
54 	while (true) {
55 		;
56 	}
57 }
58 
main(void)59 int main(void)
60 {
61 	/* Set up led and led timer */
62 	if (!gpio_is_ready_dt(&led)) {
63 		printk("Device not ready\n");
64 		return 0;
65 	}
66 
67 	int err = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
68 
69 	if (err) {
70 		printk("Error configuring LED\n");
71 		return 0;
72 	}
73 	k_timer_start(&led_timer, K_NO_WAIT, K_SECONDS(1));
74 
75 	/* Set up debug monitor */
76 	err = debug_mon_enable();
77 	if (err) {
78 		printk("Error enabling monitor mode:\n"
79 			"Cannot enable DBM when CPU is in Debug mode");
80 		return 0;
81 	}
82 
83 	/* Enter a breakpoint */
84 	__asm("bkpt");
85 	return 0;
86 }
87