hal.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <stdlib.h>
  4. #include <stm32wbxx_ll_tim.h>
  5. #include "tama.h"
  6. #define TAG_HAL "TamaLIB"
  7. static void* tama_p1_hal_malloc(u32_t size) {
  8. return malloc(size);
  9. }
  10. static void tama_p1_hal_free(void* ptr) {
  11. free(ptr);
  12. }
  13. static void tama_p1_hal_halt(void) {
  14. g_ctx->halted = true;
  15. }
  16. static bool_t tama_p1_hal_is_log_enabled(log_level_t level) {
  17. switch(level) {
  18. case LOG_ERROR:
  19. return true;
  20. case LOG_INFO:
  21. return true;
  22. case LOG_MEMORY:
  23. return false;
  24. case LOG_CPU:
  25. return false;
  26. default:
  27. return false;
  28. }
  29. }
  30. static void tama_p1_hal_log(log_level_t level, char* buff, ...) {
  31. if(!tama_p1_hal_is_log_enabled(level)) return;
  32. FuriString* string = furi_string_alloc();
  33. va_list args;
  34. va_start(args, buff);
  35. furi_string_cat_vprintf(string, buff, args);
  36. va_end(args);
  37. switch(level) {
  38. case LOG_ERROR:
  39. FURI_LOG_E(TAG_HAL, "%s", furi_string_get_cstr(string));
  40. break;
  41. case LOG_INFO:
  42. FURI_LOG_I(TAG_HAL, "%s", furi_string_get_cstr(string));
  43. break;
  44. case LOG_MEMORY:
  45. break;
  46. case LOG_CPU:
  47. FURI_LOG_D(TAG_HAL, "%s", furi_string_get_cstr(string));
  48. break;
  49. default:
  50. FURI_LOG_D(TAG_HAL, "%s", furi_string_get_cstr(string));
  51. break;
  52. }
  53. furi_string_free(string);
  54. }
  55. static void tama_p1_hal_sleep_until(timestamp_t ts) {
  56. while(true) {
  57. uint32_t count = LL_TIM_GetCounter(TIM2);
  58. uint32_t delay = ts - count;
  59. // FURI_LOG_D(TAG, "delay: %x", delay);
  60. // Stolen from furi_delay_until_tick
  61. if(delay != 0 && 0 == (delay >> (8 * sizeof(uint32_t) - 1))) {
  62. // Not the best place to release mutex, but this is the only place we know whether
  63. // we're ahead or behind, otherwise around the step call we'll always have to
  64. // delay a tick and run more and more behind.
  65. furi_mutex_release(g_state_mutex);
  66. furi_delay_tick(1);
  67. while(furi_mutex_acquire(g_state_mutex, FuriWaitForever) != FuriStatusOk)
  68. furi_delay_tick(1);
  69. } else {
  70. break;
  71. }
  72. }
  73. }
  74. static timestamp_t tama_p1_hal_get_timestamp(void) {
  75. return LL_TIM_GetCounter(TIM2);
  76. }
  77. static void tama_p1_hal_update_screen(void) {
  78. // Do nothing, covered by main loop
  79. }
  80. static void tama_p1_hal_set_lcd_matrix(u8_t x, u8_t y, bool_t val) {
  81. if(val)
  82. g_ctx->framebuffer[y] |= 1 << x;
  83. else
  84. g_ctx->framebuffer[y] &= ~(1 << x);
  85. }
  86. static void tama_p1_hal_set_lcd_icon(u8_t icon, bool_t val) {
  87. if(val)
  88. g_ctx->icons |= 1 << icon;
  89. else
  90. g_ctx->icons &= ~(1 << icon);
  91. }
  92. static void tama_p1_hal_play_frequency(bool_t en) {
  93. if(en) {
  94. if(furi_hal_speaker_is_mine() || furi_hal_speaker_acquire(30)) {
  95. furi_hal_speaker_start(g_ctx->frequency, 0.5f);
  96. }
  97. } else {
  98. if(furi_hal_speaker_is_mine()) {
  99. furi_hal_speaker_stop();
  100. furi_hal_speaker_release();
  101. }
  102. }
  103. g_ctx->buzzer_on = en;
  104. }
  105. static void tama_p1_hal_set_frequency(u32_t freq) {
  106. g_ctx->frequency = freq / 10.0F;
  107. if(g_ctx->buzzer_on) tama_p1_hal_play_frequency(true);
  108. }
  109. static int tama_p1_hal_handler(void) {
  110. // Do nothing
  111. return 0;
  112. }
  113. void tama_p1_hal_init(hal_t* hal) {
  114. hal->malloc = tama_p1_hal_malloc;
  115. hal->free = tama_p1_hal_free;
  116. hal->halt = tama_p1_hal_halt;
  117. hal->is_log_enabled = tama_p1_hal_is_log_enabled;
  118. hal->log = tama_p1_hal_log;
  119. hal->sleep_until = tama_p1_hal_sleep_until;
  120. hal->get_timestamp = tama_p1_hal_get_timestamp;
  121. hal->update_screen = tama_p1_hal_update_screen;
  122. hal->set_lcd_matrix = tama_p1_hal_set_lcd_matrix;
  123. hal->set_lcd_icon = tama_p1_hal_set_lcd_icon;
  124. hal->set_frequency = tama_p1_hal_set_frequency;
  125. hal->play_frequency = tama_p1_hal_play_frequency;
  126. hal->handler = tama_p1_hal_handler;
  127. }