common_defines.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include "core_defines.h"
  3. #include <stdbool.h>
  4. #include <FreeRTOS.h>
  5. #include <task.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <cmsis_compiler.h>
  10. #ifndef FURI_IS_IRQ_MASKED
  11. #define FURI_IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
  12. #endif
  13. #ifndef FURI_IS_IRQ_MODE
  14. #define FURI_IS_IRQ_MODE() (__get_IPSR() != 0U)
  15. #endif
  16. #ifndef FURI_IS_ISR
  17. #define FURI_IS_ISR() (FURI_IS_IRQ_MODE() || FURI_IS_IRQ_MASKED())
  18. #endif
  19. #ifndef FURI_CRITICAL_ENTER
  20. #define FURI_CRITICAL_ENTER() \
  21. uint32_t __isrm = 0; \
  22. bool __from_isr = FURI_IS_ISR(); \
  23. bool __kernel_running = (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING); \
  24. if(__from_isr) { \
  25. __isrm = taskENTER_CRITICAL_FROM_ISR(); \
  26. } else if(__kernel_running) { \
  27. taskENTER_CRITICAL(); \
  28. } else { \
  29. __disable_irq(); \
  30. }
  31. #endif
  32. #ifndef FURI_CRITICAL_EXIT
  33. #define FURI_CRITICAL_EXIT() \
  34. if(__from_isr) { \
  35. taskEXIT_CRITICAL_FROM_ISR(__isrm); \
  36. } else if(__kernel_running) { \
  37. taskEXIT_CRITICAL(); \
  38. } else { \
  39. __enable_irq(); \
  40. }
  41. #endif
  42. static inline bool furi_is_irq_context() {
  43. bool irq = false;
  44. BaseType_t state;
  45. if(FURI_IS_IRQ_MODE()) {
  46. /* Called from interrupt context */
  47. irq = true;
  48. } else {
  49. /* Get FreeRTOS scheduler state */
  50. state = xTaskGetSchedulerState();
  51. if(state != taskSCHEDULER_NOT_STARTED) {
  52. /* Scheduler was started */
  53. if(FURI_IS_IRQ_MASKED()) {
  54. /* Interrupts are masked */
  55. irq = true;
  56. }
  57. }
  58. }
  59. /* Return context, 0: thread context, 1: IRQ context */
  60. return (irq);
  61. }
  62. #ifdef __cplusplus
  63. }
  64. #endif