@@ -36,6 +36,7 @@ using namespace particle;
36
36
#endif
37
37
38
38
extern uintptr_t link_ram_interrupt_vectors_location[];
39
+ static uint32_t hal_interrupts_handler_backup[MAX_VECTOR_TABLE_NUM] = {};
39
40
40
41
namespace {
41
42
@@ -258,20 +259,32 @@ int hal_interrupt_set_direct_handler(IRQn_Type irqn, hal_interrupt_direct_handle
258
259
}
259
260
260
261
int32_t state = HAL_disable_irq ();
261
- volatile uint32_t * isrs = (volatile uint32_t *)&link_ram_interrupt_vectors_location;
262
262
263
263
if (handler == nullptr && (flags & HAL_INTERRUPT_DIRECT_FLAG_RESTORE)) {
264
264
// Restore
265
- // HAL_Core_Restore_Interrupt(irqn);
265
+ uint32_t old_handler = hal_interrupts_handler_backup[IRQN_TO_IDX (irqn)];
266
+ __NVIC_SetVector (irqn, (uint32_t )old_handler);
267
+
268
+ hal_interrupts_handler_backup[IRQN_TO_IDX (irqn)] = 0 ;
266
269
} else {
267
- isrs[IRQN_TO_IDX (irqn)] = (uint32_t )handler;
270
+
271
+ // If there is currently a backup: Return error
272
+ if (hal_interrupts_handler_backup[IRQN_TO_IDX (irqn)]){
273
+ return 1 ;
274
+ }
275
+
276
+ // If there is a current handler, back it up
277
+ uint32_t current_handler = __NVIC_GetVector (irqn);
278
+ if (current_handler) {
279
+ hal_interrupts_handler_backup[IRQN_TO_IDX (irqn)] = current_handler;
280
+ }
268
281
}
269
282
270
283
if (flags & HAL_INTERRUPT_DIRECT_FLAG_DISABLE) {
271
284
// Disable
272
- // sd_nvic_DisableIRQ (irqn);
285
+ // __NVIC_DisableIRQ (irqn); // actually disable the interrupt (ie like systick!?)
273
286
} else if (flags & HAL_INTERRUPT_DIRECT_FLAG_ENABLE) {
274
- // sd_nvic_EnableIRQ (irqn);
287
+ __NVIC_SetVector (irqn, ( uint32_t )handler );
275
288
}
276
289
277
290
HAL_enable_irq (state);
0 commit comments