api-interrupt-mgr.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "api-interrupt-mgr.h"
  2. #include <cmsis_os2.h>
  3. #include <furi.h>
  4. static volatile InterruptCallbackItem callback_list[InterruptTypeLast];
  5. bool api_interrupt_init() {
  6. for(uint8_t i = 0; i < InterruptTypeLast; i++) {
  7. callback_list[i].callback = NULL;
  8. callback_list[i].context = NULL;
  9. callback_list[i].ready = false;
  10. }
  11. return true;
  12. }
  13. void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context) {
  14. furi_assert(type < InterruptTypeLast);
  15. furi_check(callback_list[type].callback == NULL);
  16. callback_list[type].callback = callback;
  17. callback_list[type].context = context;
  18. __DMB();
  19. callback_list[type].ready = true;
  20. }
  21. void api_interrupt_remove(InterruptCallback callback, InterruptType type) {
  22. furi_assert(type < InterruptTypeLast);
  23. if(callback_list[type].callback != NULL) {
  24. furi_check(callback_list[type].callback == callback);
  25. }
  26. callback_list[type].ready = false;
  27. __DMB();
  28. callback_list[type].callback = NULL;
  29. callback_list[type].context = NULL;
  30. }
  31. void api_interrupt_enable(InterruptCallback callback, InterruptType type) {
  32. furi_assert(type < InterruptTypeLast);
  33. furi_check(callback_list[type].callback == callback);
  34. callback_list[type].ready = true;
  35. __DMB();
  36. }
  37. void api_interrupt_disable(InterruptCallback callback, InterruptType type) {
  38. furi_assert(type < InterruptTypeLast);
  39. furi_check(callback_list[type].callback == callback);
  40. callback_list[type].ready = false;
  41. __DMB();
  42. }
  43. void api_interrupt_call(InterruptType type, void* hw) {
  44. // that executed in interrupt ctx so mutex don't needed
  45. // but we need to check ready flag
  46. furi_assert(type < InterruptTypeLast);
  47. if(callback_list[type].callback != NULL) {
  48. if(callback_list[type].ready) {
  49. callback_list[type].callback(hw, callback_list[type].context);
  50. }
  51. }
  52. }