2222#include <stdbool.h>
2323#include <stdint.h>
2424#include "cpu_conf.h"
25+ #include "kernel_defines.h"
26+ #include "debug_irq_disable.h"
2527
2628#ifdef __cplusplus
2729extern "C" {
2830#endif
2931
32+ /**
33+ * @brief Start SysTick timer to measure time spent with IRQ disabled
34+ */
35+ static inline void _irq_debug_start_count (void )
36+ {
37+ SysTick -> VAL = 0 ;
38+ SysTick -> LOAD = SysTick_LOAD_RELOAD_Msk ;
39+ SysTick -> CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk ;
40+ }
41+
42+ /**
43+ * @brief Stop SysTick timer, return time spent with IRQ disabled
44+ */
45+ static inline uint32_t _irq_debug_stop_count (void )
46+ {
47+ uint32_t ticks = SysTick_LOAD_RELOAD_Msk - SysTick -> VAL ;
48+ SysTick -> CTRL = 0 ;
49+ return ticks ;
50+ }
51+
3052/**
3153 * @brief Disable all maskable interrupts
3254 */
@@ -35,6 +57,10 @@ unsigned int irq_disable(void)
3557{
3658 uint32_t mask = __get_PRIMASK ();
3759
60+ if ((mask == 0 ) && IS_USED (MODULE_DEBUG_IRQ_DISABLE )) {
61+ _irq_debug_start_count ();
62+ }
63+
3864 __disable_irq ();
3965 return mask ;
4066}
@@ -55,10 +81,28 @@ unsigned int irq_enable(void)
5581 * @brief Restore the state of the IRQ flags
5682 */
5783static inline __attribute__((always_inline ))
84+ #if !IS_USED (MODULE_DEBUG_IRQ_DISABLE )
5885void irq_restore (unsigned int state )
5986{
6087 __set_PRIMASK (state );
6188}
89+ #else
90+ void _irq_restore (unsigned int state , const char * file , unsigned line )
91+ {
92+ uint32_t ticks = 0 ;
93+
94+ if (state == 0 ) {
95+ ticks = _irq_debug_stop_count ();
96+ }
97+
98+ __set_PRIMASK (state );
99+
100+ if (ticks ) {
101+ debug_irq_disable_print (file , line , ticks );
102+ }
103+ }
104+ #define irq_restore (state ) _irq_restore(state, __FILE__, __LINE__);
105+ #endif /* MODULE_DEBUG_IRQ_DISABLE */
62106
63107/**
64108 * @brief See if IRQs are currently enabled
0 commit comments