2015-04-11 01:44:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-04 16:09:39 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Exception/interrupt context helpers for Cortex-M CPUs
|
|
|
|
*
|
2015-10-20 18:42:33 +02:00
|
|
|
* Exception/interrupt context helpers.
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
#ifndef _ARM_CORTEXM_ISR__H_
|
|
|
|
#define _ARM_CORTEXM_ISR__H_
|
|
|
|
|
2015-05-28 19:56:47 +02:00
|
|
|
#include <arch/cpu.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2016-01-22 18:38:49 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
#ifdef _ASMLANGUAGE
|
|
|
|
|
|
|
|
/* nothing */
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-01-18 19:28:52 +01:00
|
|
|
#include <arch/arm/cortex_m/cmsis.h>
|
2017-04-19 21:53:48 +02:00
|
|
|
#include <irq_offload.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_IRQ_OFFLOAD
|
|
|
|
extern volatile irq_offload_routine_t offload_routine;
|
|
|
|
#endif
|
2017-01-18 19:28:52 +01:00
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Find out if running in an ISR context
|
2017-04-19 21:53:48 +02:00
|
|
|
*
|
2015-07-01 23:22:39 +02:00
|
|
|
* The current executing vector is found in the IPSR register. We consider the
|
2017-04-19 21:53:48 +02:00
|
|
|
* IRQs (exception 16 and up), and the PendSV and SYSTICK exceptions to be
|
|
|
|
* interrupts. Taking a fault within an exception is also considered in
|
2015-07-01 23:22:39 +02:00
|
|
|
* interrupt context.
|
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return 1 if in ISR, 0 if not.
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
static ALWAYS_INLINE int _IsInIsr(void)
|
|
|
|
{
|
2018-03-12 10:45:02 +01:00
|
|
|
u32_t vector = __get_IPSR();
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2017-04-19 21:53:48 +02:00
|
|
|
/* IRQs + PendSV (14) + SYSTICK (15) are interrupts. */
|
|
|
|
return (vector > 13)
|
|
|
|
#ifdef CONFIG_IRQ_OFFLOAD
|
|
|
|
/* Only non-NULL if currently running an offloaded function */
|
|
|
|
|| offload_routine != NULL
|
|
|
|
#endif
|
2018-02-06 23:47:58 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
2017-04-20 00:23:51 +02:00
|
|
|
/* On ARMv6-M there is no nested execution bit, so we check
|
|
|
|
* exception 3, hard fault, to a detect a nested exception.
|
|
|
|
*/
|
|
|
|
|| (vector == 3)
|
2018-02-06 23:47:58 +01:00
|
|
|
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
|
2017-04-20 00:23:51 +02:00
|
|
|
/* If not in thread mode, and if RETTOBASE bit in ICSR is 0,
|
|
|
|
* then there are preempted active exceptions to execute.
|
|
|
|
*/
|
|
|
|
#ifndef CONFIG_BOARD_QEMU_CORTEX_M3
|
|
|
|
/* The polarity of RETTOBASE is incorrectly flipped in
|
|
|
|
* all but the very latest master tip of QEMU's NVIC driver,
|
|
|
|
* see commit "armv7m: Rewrite NVIC to not use any GIC code".
|
|
|
|
* Until QEMU 2.9 is released, and the SDK is updated to
|
|
|
|
* include it, skip this check in QEMU.
|
|
|
|
*/
|
|
|
|
|| (vector && !(SCB->ICSR & SCB_ICSR_RETTOBASE_Msk))
|
|
|
|
#endif /* CONFIG_BOARD_QEMU_CORTEX_M3 */
|
2016-12-31 14:18:25 +01:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2018-02-06 23:47:58 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
2017-04-20 00:23:51 +02:00
|
|
|
;
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 19:28:52 +01:00
|
|
|
#define _EXC_SVC_PRIO 0
|
|
|
|
#define _EXC_FAULT_PRIO 0
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Setup system exceptions
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
|
|
|
* Set exception priorities to conform with the BASEPRI locking mechanism.
|
|
|
|
* Set PendSV priority to lowest possible.
|
|
|
|
*
|
|
|
|
* Enable fault exceptions.
|
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return N/A
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
static ALWAYS_INLINE void _ExcSetup(void)
|
|
|
|
{
|
2017-01-18 19:28:52 +01:00
|
|
|
NVIC_SetPriority(PendSV_IRQn, 0xff);
|
2016-12-13 20:55:11 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_CPU_CORTEX_M_HAS_BASEPRI
|
2017-01-18 19:28:52 +01:00
|
|
|
NVIC_SetPriority(SVCall_IRQn, _EXC_SVC_PRIO);
|
2016-12-13 20:55:11 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_CPU_CORTEX_M_HAS_PROGRAMMABLE_FAULT_PRIOS
|
2017-01-18 19:28:52 +01:00
|
|
|
NVIC_SetPriority(MemoryManagement_IRQn, _EXC_FAULT_PRIO);
|
|
|
|
NVIC_SetPriority(BusFault_IRQn, _EXC_FAULT_PRIO);
|
|
|
|
NVIC_SetPriority(UsageFault_IRQn, _EXC_FAULT_PRIO);
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2017-01-25 15:45:11 +01:00
|
|
|
/* Enable Usage, Mem, & Bus Faults */
|
|
|
|
SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk |
|
|
|
|
SCB_SHCSR_BUSFAULTENA_Msk;
|
2016-12-13 20:55:11 +01:00
|
|
|
#endif
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 16:12:00 +01:00
|
|
|
/**
|
|
|
|
* @brief Clear Fault exceptions
|
|
|
|
*
|
|
|
|
* Clear out exceptions for Mem, Bus, Usage and Hard Faults
|
|
|
|
*
|
|
|
|
* @return N/A
|
|
|
|
*/
|
|
|
|
static ALWAYS_INLINE void _ClearFaults(void)
|
|
|
|
{
|
2018-02-06 23:47:58 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
|
|
|
|
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
|
2017-01-25 16:12:00 +01:00
|
|
|
/* Reset all faults */
|
2017-01-25 16:33:03 +01:00
|
|
|
SCB->CFSR = SCB_CFSR_USGFAULTSR_Msk |
|
|
|
|
SCB_CFSR_MEMFAULTSR_Msk |
|
|
|
|
SCB_CFSR_BUSFAULTSR_Msk;
|
2017-01-25 16:12:00 +01:00
|
|
|
|
2017-01-25 16:33:03 +01:00
|
|
|
/* Clear all Hard Faults - HFSR is write-one-to-clear */
|
|
|
|
SCB->HFSR = 0xffffffff;
|
2017-01-25 16:12:00 +01:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2018-02-06 23:47:58 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
|
2017-01-25 16:12:00 +01:00
|
|
|
}
|
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
#endif /* _ASMLANGUAGE */
|
|
|
|
|
2016-01-22 18:38:49 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
#endif /* _ARM_CORTEXM_ISR__H_ */
|