furi-hal-bt.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <furi-hal-bt.h>
  2. #include <ble.h>
  3. #include <stm32wbxx.h>
  4. #include <shci.h>
  5. #include <cmsis_os2.h>
  6. #include <furi.h>
  7. osMutexId_t furi_hal_bt_core2_mtx = NULL;
  8. void furi_hal_bt_init() {
  9. furi_hal_bt_core2_mtx = osMutexNew(NULL);
  10. }
  11. static bool furi_hal_bt_wait_startup() {
  12. uint16_t counter = 0;
  13. while (!(ble_glue_get_status() == BleGlueStatusStarted || ble_glue_get_status() == BleGlueStatusBleStackMissing)) {
  14. osDelay(10);
  15. counter++;
  16. if (counter > 1000) {
  17. return false;
  18. }
  19. }
  20. return true;
  21. }
  22. bool furi_hal_bt_start_core2() {
  23. furi_assert(furi_hal_bt_core2_mtx);
  24. bool ret = false;
  25. osMutexAcquire(furi_hal_bt_core2_mtx, osWaitForever);
  26. // Explicitly tell that we are in charge of CLK48 domain
  27. HAL_HSEM_FastTake(CFG_HW_CLK48_CONFIG_SEMID);
  28. // Start Core2
  29. ble_glue_init();
  30. // Wait for Core2 start
  31. ret = furi_hal_bt_wait_startup();
  32. osMutexRelease(furi_hal_bt_core2_mtx);
  33. return ret;
  34. }
  35. bool furi_hal_bt_init_app(BleEventCallback event_cb, void* context) {
  36. furi_assert(event_cb);
  37. return gap_init(event_cb, context);
  38. }
  39. void furi_hal_bt_start_advertising() {
  40. if(gap_get_state() == GapStateIdle) {
  41. gap_start_advertising();
  42. }
  43. }
  44. void furi_hal_bt_stop_advertising() {
  45. if(furi_hal_bt_is_active()) {
  46. gap_stop_advertising();
  47. while(furi_hal_bt_is_active()) {
  48. osDelay(1);
  49. }
  50. }
  51. }
  52. void furi_hal_bt_set_data_event_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context) {
  53. serial_svc_set_callbacks(on_received_cb, on_sent_cb, context);
  54. }
  55. bool furi_hal_bt_tx(uint8_t* data, uint16_t size) {
  56. if(size > FURI_HAL_BT_PACKET_SIZE_MAX) {
  57. return false;
  58. }
  59. return serial_svc_update_tx(data, size);
  60. }
  61. bool furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size) {
  62. bool ret = false;
  63. BleGlueStatus status = ble_glue_get_status();
  64. if(status == BleGlueStatusUninitialized || BleGlueStatusStarted) {
  65. ble_app_get_key_storage_buff(key_buff_addr, key_buff_size);
  66. ret = true;
  67. }
  68. return ret;
  69. }
  70. void furi_hal_bt_set_key_storage_change_callback(BleGlueKeyStorageChangedCallback callback, void* context) {
  71. furi_assert(callback);
  72. ble_glue_set_key_storage_changed_callback(callback, context);
  73. }
  74. void furi_hal_bt_nvm_sram_sem_acquire() {
  75. while(HAL_HSEM_FastTake(CFG_HW_BLE_NVM_SRAM_SEMID) != HAL_OK) {
  76. osDelay(1);
  77. }
  78. }
  79. void furi_hal_bt_nvm_sram_sem_release() {
  80. HAL_HSEM_Release(CFG_HW_BLE_NVM_SRAM_SEMID, 0);
  81. }
  82. void furi_hal_bt_dump_state(string_t buffer) {
  83. BleGlueStatus status = ble_glue_get_status();
  84. if (status == BleGlueStatusStarted) {
  85. uint8_t HCI_Version;
  86. uint16_t HCI_Revision;
  87. uint8_t LMP_PAL_Version;
  88. uint16_t Manufacturer_Name;
  89. uint16_t LMP_PAL_Subversion;
  90. tBleStatus ret = hci_read_local_version_information(
  91. &HCI_Version, &HCI_Revision, &LMP_PAL_Version, &Manufacturer_Name, &LMP_PAL_Subversion
  92. );
  93. string_cat_printf(buffer,
  94. "Ret: %d, HCI_Version: %d, HCI_Revision: %d, LMP_PAL_Version: %d, Manufacturer_Name: %d, LMP_PAL_Subversion: %d",
  95. ret, HCI_Version, HCI_Revision, LMP_PAL_Version, Manufacturer_Name, LMP_PAL_Subversion
  96. );
  97. } else {
  98. string_cat_printf(buffer, "BLE not ready");
  99. }
  100. }
  101. bool furi_hal_bt_is_alive() {
  102. BleGlueStatus status = ble_glue_get_status();
  103. return (status == BleGlueStatusBleStackMissing) || (status == BleGlueStatusStarted);
  104. }
  105. bool furi_hal_bt_is_active() {
  106. return gap_get_state() > GapStateIdle;
  107. }
  108. static void furi_hal_bt_lock_flash_core2(bool erase_flag) {
  109. // Take flash controller ownership
  110. while (HAL_HSEM_FastTake(CFG_HW_FLASH_SEMID) != HAL_OK) {
  111. taskYIELD();
  112. }
  113. // Unlock flash operation
  114. HAL_FLASH_Unlock();
  115. // Erase activity notification
  116. if(erase_flag) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);
  117. while(true) {
  118. // Wait till flash controller become usable
  119. while(LL_FLASH_IsActiveFlag_OperationSuspended()) {
  120. taskYIELD();
  121. };
  122. // Just a little more love
  123. taskENTER_CRITICAL();
  124. // Actually we already have mutex for it, but specification is specification
  125. if (HAL_HSEM_IsSemTaken(CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID)) {
  126. taskEXIT_CRITICAL();
  127. continue;
  128. }
  129. // Take sempahopre and prevent core2 from anyting funky
  130. if (HAL_HSEM_FastTake(CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID) != HAL_OK) {
  131. taskEXIT_CRITICAL();
  132. continue;
  133. }
  134. break;
  135. }
  136. }
  137. void furi_hal_bt_lock_flash(bool erase_flag) {
  138. // Acquire dangerous ops mutex
  139. osMutexAcquire(furi_hal_bt_core2_mtx, osWaitForever);
  140. // If Core2 is running use IPC locking
  141. BleGlueStatus status = ble_glue_get_status();
  142. if(status == BleGlueStatusStarted || status == BleGlueStatusBleStackMissing) {
  143. furi_hal_bt_lock_flash_core2(erase_flag);
  144. } else {
  145. HAL_FLASH_Unlock();
  146. }
  147. }
  148. static void furi_hal_bt_unlock_flash_core2(bool erase_flag) {
  149. // Funky ops are ok at this point
  150. HAL_HSEM_Release(CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID, 0);
  151. // Task switching is ok
  152. taskEXIT_CRITICAL();
  153. // Doesn't make much sense, does it?
  154. while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) {
  155. taskYIELD();
  156. }
  157. // Erase activity over, core2 can continue
  158. if(erase_flag) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);
  159. // Lock flash controller
  160. HAL_FLASH_Lock();
  161. // Release flash controller ownership
  162. HAL_HSEM_Release(CFG_HW_FLASH_SEMID, 0);
  163. }
  164. void furi_hal_bt_unlock_flash(bool erase_flag) {
  165. // If Core2 is running use IPC locking
  166. BleGlueStatus status = ble_glue_get_status();
  167. if(status == BleGlueStatusStarted || status == BleGlueStatusBleStackMissing) {
  168. furi_hal_bt_unlock_flash_core2(erase_flag);
  169. } else {
  170. HAL_FLASH_Lock();
  171. }
  172. // Release dangerous ops mutex
  173. osMutexRelease(furi_hal_bt_core2_mtx);
  174. }
  175. void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power) {
  176. aci_hal_set_tx_power_level(0, power);
  177. aci_hal_tone_start(channel, 0);
  178. }
  179. void furi_hal_bt_stop_tone_tx() {
  180. aci_hal_tone_stop();
  181. }
  182. void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate) {
  183. hci_le_enhanced_transmitter_test(channel, 0x25, pattern, datarate);
  184. }
  185. void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate) {
  186. hci_le_enhanced_receiver_test(channel, datarate, 0);
  187. }
  188. uint16_t furi_hal_bt_stop_packet_test() {
  189. uint16_t num_of_packets = 0;
  190. hci_le_test_end(&num_of_packets);
  191. return num_of_packets;
  192. }
  193. void furi_hal_bt_start_rx(uint8_t channel) {
  194. aci_hal_rx_start(channel);
  195. }
  196. float furi_hal_bt_get_rssi() {
  197. float val;
  198. uint8_t rssi_raw[3];
  199. if (aci_hal_read_raw_rssi(rssi_raw) != BLE_STATUS_SUCCESS) {
  200. return 0.0f;
  201. }
  202. // Some ST magic with rssi
  203. uint8_t agc = rssi_raw[2] & 0xFF;
  204. int rssi = (((int)rssi_raw[1] << 8) & 0xFF00) + (rssi_raw[0] & 0xFF);
  205. if(rssi == 0 || agc > 11) {
  206. val = -127.0;
  207. } else {
  208. val = agc * 6.0f - 127.0f;
  209. while(rssi > 30) {
  210. val += 6.0;
  211. rssi >>=1;
  212. }
  213. val += (417 * rssi + 18080) >> 10;
  214. }
  215. return val;
  216. }
  217. uint32_t furi_hal_bt_get_transmitted_packets() {
  218. uint32_t packets = 0;
  219. aci_hal_le_tx_test_packet_number(&packets);
  220. return packets;
  221. }
  222. void furi_hal_bt_stop_rx() {
  223. aci_hal_rx_stop();
  224. }