furi-hal-bt.c 7.6 KB

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