furi-hal-bt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <furi-hal-bt.h>
  2. #include <app_entry.h>
  3. #include <ble.h>
  4. #include <stm32wbxx.h>
  5. #include <shci.h>
  6. #include <cmsis_os2.h>
  7. #include <gap.h>
  8. void furi_hal_bt_init() {
  9. // Explicitly tell that we are in charge of CLK48 domain
  10. HAL_HSEM_FastTake(CFG_HW_CLK48_CONFIG_SEMID);
  11. // Start Core2, init HCI and start GAP/GATT
  12. APPE_Init();
  13. }
  14. bool furi_hal_bt_init_app() {
  15. return gap_init();
  16. }
  17. void furi_hal_bt_start_advertising() {
  18. if(gap_get_state() == GapStateIdle) {
  19. gap_start_advertising();
  20. }
  21. }
  22. void furi_hal_bt_stop_advertising() {
  23. if(furi_hal_bt_is_active()) {
  24. gap_stop_advertising();
  25. while(furi_hal_bt_is_active()) {
  26. osDelay(1);
  27. }
  28. }
  29. }
  30. void furi_hal_bt_dump_state(string_t buffer) {
  31. BleGlueStatus status = APPE_Status();
  32. if (status == BleGlueStatusStarted) {
  33. uint8_t HCI_Version;
  34. uint16_t HCI_Revision;
  35. uint8_t LMP_PAL_Version;
  36. uint16_t Manufacturer_Name;
  37. uint16_t LMP_PAL_Subversion;
  38. tBleStatus ret = hci_read_local_version_information(
  39. &HCI_Version, &HCI_Revision, &LMP_PAL_Version, &Manufacturer_Name, &LMP_PAL_Subversion
  40. );
  41. string_cat_printf(buffer,
  42. "Ret: %d, HCI_Version: %d, HCI_Revision: %d, LMP_PAL_Version: %d, Manufacturer_Name: %d, LMP_PAL_Subversion: %d",
  43. ret, HCI_Version, HCI_Revision, LMP_PAL_Version, Manufacturer_Name, LMP_PAL_Subversion
  44. );
  45. } else {
  46. string_cat_printf(buffer, "BLE not ready");
  47. }
  48. }
  49. bool furi_hal_bt_is_alive() {
  50. BleGlueStatus status = APPE_Status();
  51. return (status == BleGlueStatusBroken) || (status == BleGlueStatusStarted);
  52. }
  53. bool furi_hal_bt_is_active() {
  54. return gap_get_state() > GapStateIdle;
  55. }
  56. bool furi_hal_bt_wait_startup() {
  57. uint16_t counter = 0;
  58. while (!(APPE_Status() == BleGlueStatusStarted || APPE_Status() == BleGlueStatusBroken)) {
  59. osDelay(10);
  60. counter++;
  61. if (counter > 1000) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. bool furi_hal_bt_lock_flash(bool erase_flag) {
  68. if (!furi_hal_bt_wait_startup()) {
  69. return false;
  70. }
  71. while (HAL_HSEM_FastTake(CFG_HW_FLASH_SEMID) != HAL_OK) {
  72. osDelay(1);
  73. }
  74. HAL_FLASH_Unlock();
  75. if(erase_flag) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);
  76. while(LL_FLASH_IsActiveFlag_OperationSuspended()) {
  77. osDelay(1);
  78. };
  79. __disable_irq();
  80. return true;
  81. }
  82. void furi_hal_bt_unlock_flash(bool erase_flag) {
  83. __enable_irq();
  84. if(erase_flag) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);
  85. HAL_FLASH_Lock();
  86. HAL_HSEM_Release(CFG_HW_FLASH_SEMID, HSEM_CPU1_COREID);
  87. }
  88. void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power) {
  89. aci_hal_set_tx_power_level(0, power);
  90. aci_hal_tone_start(channel, 0);
  91. }
  92. void furi_hal_bt_stop_tone_tx() {
  93. aci_hal_tone_stop();
  94. }
  95. void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate) {
  96. hci_le_enhanced_transmitter_test(channel, 0x25, pattern, datarate);
  97. }
  98. void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate) {
  99. hci_le_enhanced_receiver_test(channel, datarate, 0);
  100. }
  101. uint16_t furi_hal_bt_stop_packet_test() {
  102. uint16_t num_of_packets = 0;
  103. hci_le_test_end(&num_of_packets);
  104. return num_of_packets;
  105. }
  106. void furi_hal_bt_start_rx(uint8_t channel) {
  107. aci_hal_rx_start(channel);
  108. }
  109. float furi_hal_bt_get_rssi() {
  110. float val;
  111. uint8_t rssi_raw[3];
  112. if (aci_hal_read_raw_rssi(rssi_raw) != BLE_STATUS_SUCCESS) {
  113. return 0.0f;
  114. }
  115. // Some ST magic with rssi
  116. uint8_t agc = rssi_raw[2] & 0xFF;
  117. int rssi = (((int)rssi_raw[1] << 8) & 0xFF00) + (rssi_raw[0] & 0xFF);
  118. if(rssi == 0 || agc > 11) {
  119. val = -127.0;
  120. } else {
  121. val = agc * 6.0f - 127.0f;
  122. while(rssi > 30) {
  123. val += 6.0;
  124. rssi >>=1;
  125. }
  126. val += (417 * rssi + 18080) >> 10;
  127. }
  128. return val;
  129. }
  130. uint32_t furi_hal_bt_get_transmitted_packets() {
  131. uint32_t packets = 0;
  132. aci_hal_le_tx_test_packet_number(&packets);
  133. return packets;
  134. }
  135. void furi_hal_bt_stop_rx() {
  136. aci_hal_rx_stop();
  137. }