api-hal-interrupt.c 5.3 KB

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