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-27 19:40:11 +02:00
|
|
|
#include <asm_inline.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>
|
|
|
|
|
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
|
2015-11-17 23:08:45 +01:00
|
|
|
*
|
2015-07-01 23:22:39 +02:00
|
|
|
* The current executing vector is found in the IPSR register. We consider the
|
2015-11-17 23:08:45 +01:00
|
|
|
* IRQs (exception 16 and up), and the SVC, 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)
|
|
|
|
{
|
|
|
|
uint32_t vector = _IpsrGet();
|
|
|
|
|
2015-11-17 23:08:45 +01:00
|
|
|
/*
|
|
|
|
* IRQs + PendSV (14) + SVC (11) + SYSTICK (15) are interrupts.
|
|
|
|
* Vectors 12 and 13 are reserved, we'll never be in there
|
2016-12-28 17:47:14 +01:00
|
|
|
* On ARMv6-M there is no nested execution bit, so we check exception 3,
|
|
|
|
* hard fault, to a detect a nested exception.
|
2015-11-17 23:08:45 +01:00
|
|
|
*/
|
2016-12-31 15:09:41 +01:00
|
|
|
#if defined(CONFIG_ARMV6_M)
|
2016-12-28 17:47:14 +01:00
|
|
|
return (vector > 10) || (vector == 3);
|
2016-12-31 15:41:19 +01:00
|
|
|
#elif defined(CONFIG_ARMV7_M)
|
2017-01-25 15:32:54 +01:00
|
|
|
return (vector > 10) ||
|
|
|
|
(vector && !(SCB->ICSR & SCB_ICSR_RETTOBASE_Msk));
|
2016-12-31 14:18:25 +01:00
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
2016-12-31 15:09:41 +01:00
|
|
|
#endif /* CONFIG_ARMV6_M */
|
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
|
|
|
|
|
|
|
_ScbUsageFaultEnable();
|
|
|
|
_ScbBusFaultEnable();
|
|
|
|
_ScbMemFaultEnable();
|
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)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_ARMV6_M)
|
|
|
|
#elif defined(CONFIG_ARMV7_M)
|
|
|
|
/* Reset all faults */
|
|
|
|
_ScbMemFaultAllFaultsReset();
|
|
|
|
_ScbBusFaultAllFaultsReset();
|
|
|
|
_ScbUsageFaultAllFaultsReset();
|
|
|
|
|
|
|
|
_ScbHardFaultAllFaultsReset();
|
|
|
|
#else
|
|
|
|
#error Unknown ARM architecture
|
|
|
|
#endif /* CONFIG_ARMV6_M */
|
|
|
|
}
|
|
|
|
|
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_ */
|