api-hal-interrupt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "api-hal-interrupt.h"
  2. #include <furi.h>
  3. #include <main.h>
  4. #include <stm32wbxx_ll_tim.h>
  5. volatile ApiHalInterruptISR api_hal_tim_tim2_isr = NULL;
  6. #define API_HAL_INTERRUPT_DMA_COUNT 2
  7. #define API_HAL_INTERRUPT_DMA_CHANNELS_COUNT 8
  8. volatile ApiHalInterruptISR api_hal_dma_channel_isr[API_HAL_INTERRUPT_DMA_COUNT][API_HAL_INTERRUPT_DMA_CHANNELS_COUNT] = {0};
  9. void api_hal_interrupt_init() {
  10. NVIC_SetPriority(RCC_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
  11. NVIC_EnableIRQ(RCC_IRQn);
  12. NVIC_SetPriority(TAMP_STAMP_LSECSS_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
  13. NVIC_EnableIRQ(TAMP_STAMP_LSECSS_IRQn);
  14. NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 5, 0));
  15. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  16. FURI_LOG_I("FuriHalInterrupt", "Init OK");
  17. }
  18. void api_hal_interrupt_set_timer_isr(TIM_TypeDef* timer, ApiHalInterruptISR isr) {
  19. if (timer == TIM2) {
  20. if (isr) {
  21. furi_assert(api_hal_tim_tim2_isr == NULL);
  22. } else {
  23. furi_assert(api_hal_tim_tim2_isr != NULL);
  24. }
  25. api_hal_tim_tim2_isr = isr;
  26. } else {
  27. furi_check(0);
  28. }
  29. }
  30. void api_hal_interrupt_set_dma_channel_isr(DMA_TypeDef* dma, uint32_t channel, ApiHalInterruptISR isr) {
  31. --channel; // Pascal
  32. furi_check(dma);
  33. furi_check(channel < API_HAL_INTERRUPT_DMA_CHANNELS_COUNT);
  34. if (dma == DMA1) {
  35. api_hal_dma_channel_isr[0][channel] = isr;
  36. } else if (dma == DMA1) {
  37. api_hal_dma_channel_isr[1][channel] = isr;
  38. } else {
  39. furi_check(0);
  40. }
  41. }
  42. extern void api_interrupt_call(InterruptType type, void* hw);
  43. /* ST HAL symbols */
  44. /* Comparator trigger event */
  45. void HAL_COMP_TriggerCallback(COMP_HandleTypeDef* hcomp) {
  46. api_interrupt_call(InterruptTypeComparatorTrigger, hcomp);
  47. }
  48. /* Timer update event */
  49. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
  50. api_interrupt_call(InterruptTypeTimerUpdate, htim);
  51. }
  52. /* Timer 2 */
  53. void TIM2_IRQHandler(void) {
  54. if (api_hal_tim_tim2_isr) {
  55. api_hal_tim_tim2_isr();
  56. } else {
  57. HAL_TIM_IRQHandler(&htim2);
  58. }
  59. }
  60. /* DMA 1 */
  61. void DMA1_Channel1_IRQHandler(void) {
  62. if (api_hal_dma_channel_isr[0][0]) api_hal_dma_channel_isr[0][0]();
  63. }
  64. void DMA1_Channel2_IRQHandler(void) {
  65. if (api_hal_dma_channel_isr[0][1]) api_hal_dma_channel_isr[0][1]();
  66. }
  67. void DMA1_Channel3_IRQHandler(void) {
  68. if (api_hal_dma_channel_isr[0][2]) api_hal_dma_channel_isr[0][2]();
  69. }
  70. void DMA1_Channel4_IRQHandler(void) {
  71. if (api_hal_dma_channel_isr[0][3]) api_hal_dma_channel_isr[0][3]();
  72. }
  73. void DMA1_Channel5_IRQHandler(void) {
  74. if (api_hal_dma_channel_isr[0][4]) api_hal_dma_channel_isr[0][4]();
  75. }
  76. void DMA1_Channel6_IRQHandler(void) {
  77. if (api_hal_dma_channel_isr[0][5]) api_hal_dma_channel_isr[0][5]();
  78. }
  79. void DMA1_Channel7_IRQHandler(void) {
  80. if (api_hal_dma_channel_isr[0][6]) api_hal_dma_channel_isr[0][6]();
  81. }
  82. void DMA1_Channel8_IRQHandler(void) {
  83. if (api_hal_dma_channel_isr[0][7]) api_hal_dma_channel_isr[0][7]();
  84. }
  85. /* DMA 2 */
  86. void DMA2_Channel1_IRQHandler(void) {
  87. if (api_hal_dma_channel_isr[1][0]) api_hal_dma_channel_isr[1][0]();
  88. }
  89. void DMA2_Channel2_IRQHandler(void) {
  90. if (api_hal_dma_channel_isr[1][1]) api_hal_dma_channel_isr[1][1]();
  91. }
  92. void DMA2_Channel3_IRQHandler(void) {
  93. if (api_hal_dma_channel_isr[1][2]) api_hal_dma_channel_isr[1][2]();
  94. }
  95. void DMA2_Channel4_IRQHandler(void) {
  96. if (api_hal_dma_channel_isr[1][3]) api_hal_dma_channel_isr[1][3]();
  97. }
  98. void DMA2_Channel5_IRQHandler(void) {
  99. if (api_hal_dma_channel_isr[1][4]) api_hal_dma_channel_isr[1][4]();
  100. }
  101. void DMA2_Channel6_IRQHandler(void) {
  102. if (api_hal_dma_channel_isr[1][5]) api_hal_dma_channel_isr[1][5]();
  103. }
  104. void DMA2_Channel7_IRQHandler(void) {
  105. if (api_hal_dma_channel_isr[1][6]) api_hal_dma_channel_isr[1][6]();
  106. }
  107. void DMA2_Channel8_IRQHandler(void) {
  108. if (api_hal_dma_channel_isr[1][7]) api_hal_dma_channel_isr[1][7]();
  109. }
  110. void TAMP_STAMP_LSECSS_IRQHandler(void) {
  111. if (LL_RCC_IsActiveFlag_LSECSS()) {
  112. LL_RCC_ClearFlag_LSECSS();
  113. if (!LL_RCC_LSE_IsReady()) {
  114. FURI_LOG_E("FuriHalInterrupt", "LSE CSS fired: resetting system");
  115. NVIC_SystemReset();
  116. } else {
  117. FURI_LOG_E("FuriHalInterrupt", "LSE CSS fired: but LSE is alive");
  118. }
  119. }
  120. }
  121. void RCC_IRQHandler(void) {
  122. }
  123. void NMI_Handler(void) {
  124. if (LL_RCC_IsActiveFlag_HSECSS()) {
  125. LL_RCC_ClearFlag_HSECSS();
  126. FURI_LOG_E("FuriHalInterrupt", "HSE CSS fired: resetting system");
  127. NVIC_SystemReset();
  128. }
  129. }
  130. void HardFault_Handler(void) {
  131. if ((*(volatile uint32_t *)CoreDebug_BASE) & (1 << 0)) {
  132. __asm("bkpt 1");
  133. }
  134. while (1) {}
  135. }
  136. void MemManage_Handler(void) {
  137. __asm("bkpt 1");
  138. while (1) {}
  139. }
  140. void BusFault_Handler(void) {
  141. __asm("bkpt 1");
  142. while (1) {}
  143. }
  144. void UsageFault_Handler(void) {
  145. __asm("bkpt 1");
  146. while (1) {}
  147. }
  148. void DebugMon_Handler(void) {
  149. }