api-hal-subghz.c 6.6 KB

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