api-hal-subghz.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /* End */
  29. { 0, 0 },
  30. };
  31. static const uint8_t api_hal_subghz_preset_2fsk_packet_patable[8] = {
  32. 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  33. };
  34. void api_hal_subghz_init() {
  35. hal_gpio_init(&gpio_rf_sw_0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  36. hal_gpio_init(&gpio_rf_sw_1, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  37. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  38. // Reset and shutdown
  39. cc1101_reset(device);
  40. // Prepare GD0 for power on self test
  41. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  42. // GD0 low
  43. cc1101_write_reg(device, CC1101_IOCFG0, CC1101IocfgHW);
  44. while(hal_gpio_read(&gpio_cc1101_g0) != false);
  45. // GD0 high
  46. cc1101_write_reg(device, CC1101_IOCFG0, CC1101IocfgHW | CC1101_IOCFG_INV);
  47. while(hal_gpio_read(&gpio_cc1101_g0) != true);
  48. // Reset GD0 to floating state
  49. cc1101_write_reg(device, CC1101_IOCFG0, CC1101IocfgHighImpedance);
  50. hal_gpio_init(&gpio_cc1101_g0, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  51. // Turn off oscillator
  52. cc1101_shutdown(device);
  53. api_hal_spi_device_return(device);
  54. }
  55. void api_hal_subghz_dump_state() {
  56. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  57. printf(
  58. "[api_hal_subghz] cc1101 chip %d, version %d\r\n",
  59. cc1101_get_partnumber(device),
  60. cc1101_get_version(device)
  61. );
  62. api_hal_spi_device_return(device);
  63. }
  64. void api_hal_subghz_load_preset(ApiHalSubGhzPreset preset) {
  65. if(preset == ApiHalSubGhzPresetOokAsync) {
  66. api_hal_subghz_load_registers(api_hal_subghz_preset_ook_async_regs);
  67. api_hal_subghz_load_patable(api_hal_subghz_preset_ook_async_patable);
  68. } else if(preset == ApiHalSubGhzPreset2FskPacket) {
  69. api_hal_subghz_load_registers(api_hal_subghz_preset_2fsk_packet_regs);
  70. api_hal_subghz_load_patable(api_hal_subghz_preset_2fsk_packet_patable);
  71. }
  72. }
  73. void api_hal_subghz_load_registers(const uint8_t data[][2]) {
  74. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  75. cc1101_reset(device);
  76. uint32_t i = 0;
  77. while (data[i][0]) {
  78. cc1101_write_reg(device, data[i][0], data[i][1]);
  79. i++;
  80. }
  81. api_hal_spi_device_return(device);
  82. }
  83. void api_hal_subghz_load_patable(const uint8_t data[8]) {
  84. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  85. cc1101_set_pa_table(device, data);
  86. api_hal_spi_device_return(device);
  87. }
  88. void api_hal_subghz_write_packet(const uint8_t* data, uint8_t size) {
  89. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  90. cc1101_flush_tx(device);
  91. cc1101_write_fifo(device, data, size);
  92. api_hal_spi_device_return(device);
  93. }
  94. void api_hal_subghz_read_packet(uint8_t* data, uint8_t size) {
  95. }
  96. void api_hal_subghz_shutdown() {
  97. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  98. // Reset and shutdown
  99. cc1101_shutdown(device);
  100. api_hal_spi_device_return(device);
  101. }
  102. void api_hal_subghz_reset() {
  103. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  104. cc1101_reset(device);
  105. api_hal_spi_device_return(device);
  106. }
  107. void api_hal_subghz_idle() {
  108. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  109. cc1101_switch_to_idle(device);
  110. api_hal_spi_device_return(device);
  111. }
  112. void api_hal_subghz_rx() {
  113. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  114. cc1101_switch_to_rx(device);
  115. api_hal_spi_device_return(device);
  116. }
  117. void api_hal_subghz_tx() {
  118. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  119. cc1101_switch_to_tx(device);
  120. api_hal_spi_device_return(device);
  121. }
  122. float api_hal_subghz_get_rssi() {
  123. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  124. int32_t rssi_dec = cc1101_get_rssi(device);
  125. api_hal_spi_device_return(device);
  126. float rssi = rssi_dec;
  127. if(rssi_dec >= 128) {
  128. rssi = ((rssi - 256.0f) / 2.0f) - 74.0f;
  129. } else {
  130. rssi = (rssi / 2.0f) - 74.0f;
  131. }
  132. return rssi;
  133. }
  134. uint32_t api_hal_subghz_set_frequency(uint32_t value) {
  135. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  136. // Compensate rounding
  137. if (value % cc1101_get_frequency_step(device) > (cc1101_get_frequency_step(device) / 2)) {
  138. value += cc1101_get_frequency_step(device);
  139. }
  140. uint32_t real_frequency = cc1101_set_frequency(device, value);
  141. cc1101_calibrate(device);
  142. api_hal_spi_device_return(device);
  143. return real_frequency;
  144. }
  145. void api_hal_subghz_set_path(ApiHalSubGhzPath path) {
  146. if (path == ApiHalSubGhzPath1) {
  147. hal_gpio_write(&gpio_rf_sw_0, 0);
  148. hal_gpio_write(&gpio_rf_sw_1, 1);
  149. } else if (path == ApiHalSubGhzPath2) {
  150. hal_gpio_write(&gpio_rf_sw_0, 1);
  151. hal_gpio_write(&gpio_rf_sw_1, 0);
  152. } else if (path == ApiHalSubGhzPath3) {
  153. hal_gpio_write(&gpio_rf_sw_0, 1);
  154. hal_gpio_write(&gpio_rf_sw_1, 1);
  155. } else if (path == ApiHalSubGhzPathIsolate) {
  156. hal_gpio_write(&gpio_rf_sw_0, 0);
  157. hal_gpio_write(&gpio_rf_sw_1, 0);
  158. } else {
  159. furi_check(0);
  160. }
  161. }