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)16void 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)22int 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)45void 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 main(void)58int main(void) 59 { 60 /* Set up led and led timer */ 61 if (!gpio_is_ready_dt(&led)) { 62 printk("Device not ready\n"); 63 return 0; 64 } 65 66 int err = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); 67 68 if (err) { 69 printk("Error configuring LED\n"); 70 return 0; 71 } 72 k_timer_start(&led_timer, K_NO_WAIT, K_SECONDS(1)); 73 74 /* Set up debug monitor */ 75 err = debug_mon_enable(); 76 if (err) { 77 printk("Error enabling monitor mode:\n" 78 "Cannot enable DBM when CPU is in Debug mode"); 79 return 0; 80 } 81 82 /* Enter a breakpoint */ 83 __asm("bkpt"); 84 return 0; 85 } 86