api-hal-subghz.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "api-hal-subghz.h"
  2. #include <stm32wbxx_ll_gpio.h>
  3. #include <api-hal-gpio.h>
  4. #include <api-hal-spi.h>
  5. #include <cc1101.h>
  6. #include <stdio.h>
  7. #include "main.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. LL_GPIO_SetPinMode(RF_SW_0_GPIO_Port, RF_SW_0_Pin, LL_GPIO_MODE_OUTPUT);
  36. LL_GPIO_SetPinSpeed(RF_SW_0_GPIO_Port, RF_SW_0_Pin, LL_GPIO_SPEED_FREQ_LOW);
  37. LL_GPIO_SetPinOutputType(RF_SW_0_GPIO_Port, RF_SW_0_Pin, LL_GPIO_OUTPUT_PUSHPULL);
  38. LL_GPIO_SetPinMode(RF_SW_1_GPIO_Port, RF_SW_1_Pin, LL_GPIO_MODE_OUTPUT);
  39. LL_GPIO_SetPinSpeed(RF_SW_1_GPIO_Port, RF_SW_1_Pin, LL_GPIO_SPEED_FREQ_LOW);
  40. LL_GPIO_SetPinOutputType(RF_SW_1_GPIO_Port, RF_SW_1_Pin, LL_GPIO_OUTPUT_PUSHPULL);
  41. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  42. // Reset and shutdown
  43. cc1101_reset(device);
  44. cc1101_write_reg(device, CC1101_IOCFG0, 0x2E); // High impedance 3-state
  45. cc1101_shutdown(device);
  46. api_hal_spi_device_return(device);
  47. }
  48. void api_hal_subghz_dump_state() {
  49. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  50. printf(
  51. "[api_hal_subghz] cc1101 chip %d, version %d\r\n",
  52. cc1101_get_partnumber(device),
  53. cc1101_get_version(device)
  54. );
  55. api_hal_spi_device_return(device);
  56. }
  57. void api_hal_subghz_load_preset(ApiHalSubGhzPreset preset) {
  58. if(preset == ApiHalSubGhzPresetOokAsync) {
  59. api_hal_subghz_load_registers(api_hal_subghz_preset_ook_async_regs);
  60. api_hal_subghz_load_patable(api_hal_subghz_preset_ook_async_patable);
  61. } else if(preset == ApiHalSubGhzPreset2FskPacket) {
  62. api_hal_subghz_load_registers(api_hal_subghz_preset_2fsk_packet_regs);
  63. api_hal_subghz_load_patable(api_hal_subghz_preset_2fsk_packet_patable);
  64. }
  65. }
  66. void api_hal_subghz_load_registers(const uint8_t data[][2]) {
  67. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  68. cc1101_reset(device);
  69. uint32_t i = 0;
  70. while (data[i][0]) {
  71. cc1101_write_reg(device, data[i][0], data[i][1]);
  72. i++;
  73. }
  74. api_hal_spi_device_return(device);
  75. }
  76. void api_hal_subghz_load_patable(const uint8_t data[8]) {
  77. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  78. cc1101_set_pa_table(device, data);
  79. api_hal_spi_device_return(device);
  80. }
  81. void api_hal_subghz_write_packet(const uint8_t* data, uint8_t size) {
  82. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  83. cc1101_flush_tx(device);
  84. cc1101_write_fifo(device, data, size);
  85. api_hal_spi_device_return(device);
  86. }
  87. void api_hal_subghz_read_packet(uint8_t* data, uint8_t size) {
  88. }
  89. void api_hal_subghz_shutdown() {
  90. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  91. // Reset and shutdown
  92. cc1101_shutdown(device);
  93. api_hal_spi_device_return(device);
  94. }
  95. void api_hal_subghz_reset() {
  96. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  97. cc1101_reset(device);
  98. api_hal_spi_device_return(device);
  99. }
  100. void api_hal_subghz_idle() {
  101. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  102. cc1101_switch_to_idle(device);
  103. api_hal_spi_device_return(device);
  104. }
  105. void api_hal_subghz_rx() {
  106. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  107. cc1101_switch_to_rx(device);
  108. api_hal_spi_device_return(device);
  109. }
  110. void api_hal_subghz_tx() {
  111. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  112. cc1101_switch_to_tx(device);
  113. api_hal_spi_device_return(device);
  114. }
  115. float api_hal_subghz_get_rssi() {
  116. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  117. int32_t rssi_dec = cc1101_get_rssi(device);
  118. api_hal_spi_device_return(device);
  119. float rssi = rssi_dec;
  120. if(rssi_dec >= 128) {
  121. rssi = ((rssi - 256.0f) / 2.0f) - 74.0f;
  122. } else {
  123. rssi = (rssi / 2.0f) - 74.0f;
  124. }
  125. return rssi;
  126. }
  127. uint32_t api_hal_subghz_set_frequency(uint32_t value) {
  128. const ApiHalSpiDevice* device = api_hal_spi_device_get(ApiHalSpiDeviceIdSubGhz);
  129. // Compensate rounding
  130. if (value % cc1101_get_frequency_step(device) > (cc1101_get_frequency_step(device) / 2)) {
  131. value += cc1101_get_frequency_step(device);
  132. }
  133. uint32_t real_frequency = cc1101_set_frequency(device, value);
  134. cc1101_calibrate(device);
  135. api_hal_spi_device_return(device);
  136. return real_frequency;
  137. }
  138. void api_hal_subghz_set_path(ApiHalSubGhzPath path) {
  139. if (path == ApiHalSubGhzPath1) {
  140. LL_GPIO_ResetOutputPin(RF_SW_0_GPIO_Port, RF_SW_0_Pin);
  141. LL_GPIO_SetOutputPin(RF_SW_1_GPIO_Port, RF_SW_1_Pin);
  142. } else if (path == ApiHalSubGhzPath2) {
  143. LL_GPIO_SetOutputPin(RF_SW_0_GPIO_Port, RF_SW_0_Pin);
  144. LL_GPIO_ResetOutputPin(RF_SW_1_GPIO_Port, RF_SW_1_Pin);
  145. } else if (path == ApiHalSubGhzPath3) {
  146. LL_GPIO_SetOutputPin(RF_SW_0_GPIO_Port, RF_SW_0_Pin);
  147. LL_GPIO_SetOutputPin(RF_SW_1_GPIO_Port, RF_SW_1_Pin);
  148. } else if (path == ApiHalSubGhzPathIsolate) {
  149. LL_GPIO_ResetOutputPin(RF_SW_0_GPIO_Port, RF_SW_0_Pin);
  150. LL_GPIO_ResetOutputPin(RF_SW_1_GPIO_Port, RF_SW_1_Pin);
  151. } else {
  152. }
  153. }