api-hal-subghz.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "api-hal-subghz.h"
  2. #include <api-hal-gpio.h>
  3. #include <api-hal-spi.h>
  4. #include <api-hal-resources.h>
  5. #include <furi.h>
  6. #include <cc1101.h>
  7. #include <stdio.h>
  8. static const uint8_t api_hal_subghz_preset_ook_async_regs[][2] = {
  9. /* Base setting */
  10. { CC1101_IOCFG0, 0x0D }, // GD0 as async serial data output/input
  11. { CC1101_FSCTRL1, 0x06 }, // Set IF 26m/2^10*2=2.2MHz
  12. { CC1101_MCSM0, 0x18 }, // Autocalibrate on idle to TRX, ~150us OSC guard time
  13. /* Async OOK Specific things */
  14. { CC1101_MDMCFG2, 0x30 }, // ASK/OOK, No preamble/sync
  15. { CC1101_PKTCTRL0, 0x32 }, // Async, no CRC, Infinite
  16. { CC1101_FREND0, 0x01 }, // OOK/ASK PATABLE
  17. /* End */
  18. { 0, 0 },
  19. };
  20. static const uint8_t api_hal_subghz_preset_ook_async_patable[8] = {
  21. 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  22. };
  23. static const uint8_t api_hal_subghz_preset_2fsk_packet_regs[][2] = {
  24. /* Base setting */
  25. { CC1101_IOCFG0, 0x06 }, // GD0 as async serial data output/input
  26. { CC1101_FSCTRL1, 0x06 }, // Set IF 26m/2^10*2=2.2MHz
  27. { CC1101_MCSM0, 0x18 }, // Autocalibrate on idle to TRX, ~150us OSC guard time
  28. { CC1101_TEST2, 0x81},
  29. { CC1101_TEST1, 0x35},
  30. { CC1101_TEST0, 0x09},
  31. /* End */
  32. { 0, 0 },
  33. };
  34. static const uint8_t api_hal_subghz_preset_2fsk_packet_patable[8] = {
  35. 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  36. };
  37. void api_hal_subghz_init() {
  38. hal_gpio_init(&gpio_rf_sw_0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  39. hal_gpio_init(&gpio_rf_sw_1, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  40. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  41. // Reset and shutdown
  42. cc1101_reset(device);
  43. // Prepare GD0 for power on self test
  44. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  45. // GD0 low
  46. cc1101_write_reg(device, CC1101_IOCFG0, CC1101IocfgHW);
  47. while(hal_gpio_read(&gpio_cc1101_g0) != false);
  48. // GD0 high
  49. cc1101_write_reg(device, CC1101_IOCFG0, CC1101IocfgHW | CC1101_IOCFG_INV);
  50. while(hal_gpio_read(&gpio_cc1101_g0) != true);
  51. // Reset GD0 to floating state
  52. cc1101_write_reg(device, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  53. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  54. // Turn off oscillator
  55. cc1101_shutdown(device);
  56. api_hal_spi_device_return(device);
  57. }
  58. void api_hal_subghz_dump_state() {
  59. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  60. printf(
  61. "[api_hal_subghz] cc1101 chip %d, version %d\r\n",
  62. cc1101_get_partnumber(device),
  63. cc1101_get_version(device)
  64. );
  65. api_hal_spi_device_return(device);
  66. }
  67. void api_hal_subghz_load_preset(ApiHalSubGhzPreset preset) {
  68. if(preset == ApiHalSubGhzPresetOokAsync) {
  69. api_hal_subghz_load_registers(api_hal_subghz_preset_ook_async_regs);
  70. api_hal_subghz_load_patable(api_hal_subghz_preset_ook_async_patable);
  71. } else if(preset == ApiHalSubGhzPreset2FskPacket) {
  72. api_hal_subghz_load_registers(api_hal_subghz_preset_2fsk_packet_regs);
  73. api_hal_subghz_load_patable(api_hal_subghz_preset_2fsk_packet_patable);
  74. }
  75. }
  76. uint8_t api_hal_subghz_get_status() {
  77. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  78. CC1101StatusRaw st;
  79. st.status = cc1101_get_status(device);
  80. api_hal_spi_device_return(device);
  81. return st.status_raw;
  82. }
  83. void api_hal_subghz_load_registers(const uint8_t data[][2]) {
  84. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  85. cc1101_reset(device);
  86. uint32_t i = 0;
  87. while (data[i][0]) {
  88. cc1101_write_reg(device, data[i][0], data[i][1]);
  89. i++;
  90. }
  91. api_hal_spi_device_return(device);
  92. }
  93. void api_hal_subghz_load_patable(const uint8_t data[8]) {
  94. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  95. cc1101_set_pa_table(device, data);
  96. api_hal_spi_device_return(device);
  97. }
  98. void api_hal_subghz_write_packet(const uint8_t* data, uint8_t size) {
  99. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  100. cc1101_flush_tx(device);
  101. cc1101_write_fifo(device, data, size);
  102. api_hal_spi_device_return(device);
  103. }
  104. void api_hal_subghz_flush_rx() {
  105. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  106. cc1101_flush_rx(device);
  107. api_hal_spi_device_return(device);
  108. }
  109. void api_hal_subghz_read_packet(uint8_t* data, uint8_t* size) {
  110. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  111. cc1101_read_fifo(device, data, size);
  112. api_hal_spi_device_return(device);
  113. }
  114. void api_hal_subghz_shutdown() {
  115. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  116. // Reset and shutdown
  117. cc1101_shutdown(device);
  118. api_hal_spi_device_return(device);
  119. }
  120. void api_hal_subghz_reset() {
  121. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  122. cc1101_reset(device);
  123. api_hal_spi_device_return(device);
  124. }
  125. void api_hal_subghz_idle() {
  126. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  127. cc1101_switch_to_idle(device);
  128. api_hal_spi_device_return(device);
  129. }
  130. void api_hal_subghz_rx() {
  131. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  132. cc1101_switch_to_rx(device);
  133. api_hal_spi_device_return(device);
  134. }
  135. void api_hal_subghz_tx() {
  136. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  137. cc1101_switch_to_tx(device);
  138. api_hal_spi_device_return(device);
  139. }
  140. float api_hal_subghz_get_rssi() {
  141. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  142. int32_t rssi_dec = cc1101_get_rssi(device);
  143. api_hal_spi_device_return(device);
  144. float rssi = rssi_dec;
  145. if(rssi_dec >= 128) {
  146. rssi = ((rssi - 256.0f) / 2.0f) - 74.0f;
  147. } else {
  148. rssi = (rssi / 2.0f) - 74.0f;
  149. }
  150. return rssi;
  151. }
  152. uint32_t api_hal_subghz_set_frequency(uint32_t value) {
  153. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  154. // Compensate rounding
  155. if (value % cc1101_get_frequency_step(device) > (cc1101_get_frequency_step(device) / 2)) {
  156. value += cc1101_get_frequency_step(device);
  157. }
  158. uint32_t real_frequency = cc1101_set_frequency(device, value);
  159. cc1101_calibrate(device);
  160. api_hal_spi_device_return(device);
  161. return real_frequency;
  162. }
  163. void api_hal_subghz_set_path(ApiHalSubGhzPath path) {
  164. if (path == ApiHalSubGhzPath433) {
  165. hal_gpio_write(&gpio_rf_sw_0, 0);
  166. hal_gpio_write(&gpio_rf_sw_1, 1);
  167. } else if (path == ApiHalSubGhzPath315) {
  168. hal_gpio_write(&gpio_rf_sw_0, 1);
  169. hal_gpio_write(&gpio_rf_sw_1, 0);
  170. } else if (path == ApiHalSubGhzPath868) {
  171. hal_gpio_write(&gpio_rf_sw_0, 1);
  172. hal_gpio_write(&gpio_rf_sw_1, 1);
  173. } else if (path == ApiHalSubGhzPathIsolate) {
  174. hal_gpio_write(&gpio_rf_sw_0, 0);
  175. hal_gpio_write(&gpio_rf_sw_1, 0);
  176. } else {
  177. furi_check(0);
  178. }
  179. }