@@ -258,36 +258,39 @@ int hal_interrupt_set_direct_handler(IRQn_Type irqn, hal_interrupt_direct_handle
258
258
return 1 ;
259
259
}
260
260
261
+ int error = 0 ;
261
262
int32_t state = HAL_disable_irq ();
262
263
263
264
if (handler == nullptr && (flags & HAL_INTERRUPT_DIRECT_FLAG_RESTORE)) {
264
- // Restore
265
+ // Restore old handler only if one was backed up
265
266
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 ;
269
- } else {
270
-
271
- // If there is currently a backup: Return error
272
- if (hal_interrupts_handler_backup[IRQN_TO_IDX (irqn)]){
273
- return 1 ;
267
+ if (old_handler) {
268
+ __NVIC_SetVector (irqn, (uint32_t )old_handler);
269
+ hal_interrupts_handler_backup[IRQN_TO_IDX (irqn)] = 0 ;
274
270
}
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;
271
+ } else {
272
+ // If there is currently a handler backup: Return error
273
+ if (hal_interrupts_handler_backup[IRQN_TO_IDX (irqn)]) {
274
+ error = SYSTEM_ERROR_NOT_SUPPORTED;
275
+ } else {
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
+ }
280
281
}
281
282
}
282
283
283
- if (flags & HAL_INTERRUPT_DIRECT_FLAG_DISABLE) {
284
- // Disable
285
- // __NVIC_DisableIRQ(irqn); // actually disable the interrupt (ie like systick!?)
286
- } else if (flags & HAL_INTERRUPT_DIRECT_FLAG_ENABLE) {
287
- __NVIC_SetVector (irqn, (uint32_t )handler);
284
+ if (!error) {
285
+ if (flags & HAL_INTERRUPT_DIRECT_FLAG_DISABLE) {
286
+ // Disable
287
+ __NVIC_DisableIRQ (irqn);
288
+ } else if (flags & HAL_INTERRUPT_DIRECT_FLAG_ENABLE) {
289
+ __NVIC_SetVector (irqn, (uint32_t )handler);
290
+ }
288
291
}
289
292
290
293
HAL_enable_irq (state);
291
294
292
- return 0 ;
295
+ return error ;
293
296
}
0 commit comments