cc1101.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "cc1101.h"
  2. #include <cmsis_os2.h>
  3. #include <api-hal-delay.h>
  4. #include <assert.h>
  5. #include <string.h>
  6. CC1101Status cc1101_strobe(const ApiHalSpiDevice* device, uint8_t strobe) {
  7. uint8_t tx[1] = { strobe };
  8. CC1101Status rx[1] = { 0 };
  9. hal_gpio_write(device->chip_select, false);
  10. while(hal_gpio_read(device->bus->miso));
  11. api_hal_spi_bus_trx(device->bus, tx, (uint8_t*)rx, 1, CC1101_TIMEOUT);
  12. hal_gpio_write(device->chip_select, true);
  13. assert(rx[0].CHIP_RDYn == 0);
  14. return rx[0];
  15. }
  16. CC1101Status cc1101_write_reg(const ApiHalSpiDevice* device, uint8_t reg, uint8_t data) {
  17. uint8_t tx[2] = { reg, data };
  18. CC1101Status rx[2] = { 0 };
  19. hal_gpio_write(device->chip_select, false);
  20. while(hal_gpio_read(device->bus->miso));
  21. api_hal_spi_bus_trx(device->bus, tx, (uint8_t*)rx, 2, CC1101_TIMEOUT);
  22. hal_gpio_write(device->chip_select, true);
  23. assert((rx[0].CHIP_RDYn|rx[1].CHIP_RDYn) == 0);
  24. return rx[1];
  25. }
  26. CC1101Status cc1101_read_reg(const ApiHalSpiDevice* device, uint8_t reg, uint8_t* data) {
  27. assert(sizeof(CC1101Status) == 1);
  28. uint8_t tx[2] = { reg|CC1101_READ, 0};
  29. CC1101Status rx[2] = { 0 };
  30. hal_gpio_write(device->chip_select, false);
  31. while(hal_gpio_read(device->bus->miso));
  32. api_hal_spi_bus_trx(device->bus, tx, (uint8_t*)rx, 2, CC1101_TIMEOUT);
  33. hal_gpio_write(device->chip_select, true);
  34. assert((rx[0].CHIP_RDYn) == 0);
  35. *data = *(uint8_t*)&rx[1];
  36. return rx[0];
  37. }
  38. uint8_t cc1101_get_partnumber(const ApiHalSpiDevice* device) {
  39. uint8_t partnumber=0;
  40. cc1101_read_reg(device, CC1101_STATUS_PARTNUM|CC1101_BURST, &partnumber);
  41. return partnumber;
  42. }
  43. uint8_t cc1101_get_version(const ApiHalSpiDevice* device) {
  44. uint8_t version=0;
  45. cc1101_read_reg(device, CC1101_STATUS_VERSION|CC1101_BURST, &version);
  46. return version;
  47. }
  48. uint8_t cc1101_get_rssi(const ApiHalSpiDevice* device) {
  49. uint8_t rssi=0;
  50. cc1101_read_reg(device, CC1101_STATUS_RSSI|CC1101_BURST, &rssi);
  51. return rssi;
  52. }
  53. void cc1101_reset(const ApiHalSpiDevice* device) {
  54. hal_gpio_write(device->chip_select, false);
  55. delay_us(1000);
  56. hal_gpio_write(device->chip_select, true);
  57. delay_us(1000);
  58. cc1101_strobe(device, CC1101_STROBE_SRES);
  59. }
  60. CC1101Status cc1101_get_status(const ApiHalSpiDevice* device) {
  61. return cc1101_strobe(device, CC1101_STROBE_SNOP);
  62. }
  63. void cc1101_shutdown(const ApiHalSpiDevice* device) {
  64. cc1101_strobe(device, CC1101_STROBE_SPWD);
  65. }
  66. void cc1101_calibrate(const ApiHalSpiDevice* device) {
  67. cc1101_strobe(device, CC1101_STROBE_SCAL);
  68. }
  69. void cc1101_switch_to_idle(const ApiHalSpiDevice* device) {
  70. cc1101_strobe(device, CC1101_STROBE_SIDLE);
  71. }
  72. void cc1101_switch_to_rx(const ApiHalSpiDevice* device) {
  73. cc1101_strobe(device, CC1101_STROBE_SRX);
  74. }
  75. void cc1101_switch_to_tx(const ApiHalSpiDevice* device) {
  76. cc1101_strobe(device, CC1101_STROBE_STX);
  77. }
  78. void cc1101_flush_rx(const ApiHalSpiDevice* device) {
  79. cc1101_strobe(device, CC1101_STROBE_SFRX);
  80. }
  81. void cc1101_flush_tx(const ApiHalSpiDevice* device) {
  82. cc1101_strobe(device, CC1101_STROBE_SFTX);
  83. }
  84. uint32_t cc1101_set_frequency(const ApiHalSpiDevice* device, uint32_t value) {
  85. uint64_t real_value = (uint64_t)value * CC1101_FDIV / CC1101_QUARTZ;
  86. // Sanity check
  87. assert((real_value & CC1101_FMASK) == real_value);
  88. cc1101_write_reg(device, CC1101_FREQ2, (real_value >> 16) & 0xFF);
  89. cc1101_write_reg(device, CC1101_FREQ1, (real_value >> 8 ) & 0xFF);
  90. cc1101_write_reg(device, CC1101_FREQ0, (real_value >> 0 ) & 0xFF);
  91. uint64_t real_frequency = real_value * CC1101_QUARTZ / CC1101_FDIV;
  92. return (uint32_t)real_frequency;
  93. }
  94. uint32_t cc1101_set_intermediate_frequency(const ApiHalSpiDevice* device, uint32_t value) {
  95. uint64_t real_value = value * CC1101_IFDIV / CC1101_QUARTZ;
  96. assert((real_value & 0xFF) == real_value);
  97. cc1101_write_reg(device, CC1101_FSCTRL0, (real_value >> 0 ) & 0xFF);
  98. uint64_t real_frequency = real_value * CC1101_QUARTZ / CC1101_IFDIV;
  99. return (uint32_t)real_frequency;
  100. }
  101. void cc1101_set_pa_table(const ApiHalSpiDevice* device, const uint8_t value[8]) {
  102. uint8_t tx[9] = { CC1101_PATABLE | CC1101_BURST };
  103. CC1101Status rx[9] = { 0 };
  104. memcpy(&tx[1], &value[0], 8);
  105. hal_gpio_write(device->chip_select, false);
  106. while(hal_gpio_read(device->bus->miso));
  107. api_hal_spi_bus_trx(device->bus, tx, (uint8_t*)rx, sizeof(rx), CC1101_TIMEOUT);
  108. hal_gpio_write(device->chip_select, true);
  109. assert((rx[0].CHIP_RDYn|rx[8].CHIP_RDYn) == 0);
  110. }
  111. uint8_t cc1101_write_fifo(const ApiHalSpiDevice* device, const uint8_t* data, uint8_t size) {
  112. uint8_t buff_tx[64];
  113. uint8_t buff_rx[64];
  114. buff_tx[0] = CC1101_FIFO | CC1101_BURST;
  115. memcpy(&buff_tx[1], data, size);
  116. // Start transaction
  117. hal_gpio_write(device->chip_select, false);
  118. // Wait IC to become ready
  119. while(hal_gpio_read(device->bus->miso));
  120. // Tell IC what we want
  121. api_hal_spi_bus_trx(device->bus, buff_tx, (uint8_t*) buff_rx, size + 1, CC1101_TIMEOUT);
  122. // Finish transaction
  123. hal_gpio_write(device->chip_select, true);
  124. return size;
  125. }
  126. uint8_t cc1101_read_fifo(const ApiHalSpiDevice* device, uint8_t* data, uint8_t* size) {
  127. uint8_t buff_tx[64];
  128. buff_tx[0] = CC1101_FIFO | CC1101_READ | CC1101_BURST;
  129. uint8_t buff_rx[2];
  130. // Start transaction
  131. hal_gpio_write(device->chip_select, false);
  132. // Wait IC to become ready
  133. while(hal_gpio_read(device->bus->miso));
  134. // First byte - packet length
  135. api_hal_spi_bus_trx(device->bus, buff_tx, buff_rx, 2, CC1101_TIMEOUT);
  136. *size = buff_rx[2];
  137. api_hal_spi_bus_trx(device->bus, &buff_tx[1], data, *size, CC1101_TIMEOUT);
  138. cc1101_flush_rx(device);
  139. hal_gpio_write(device->chip_select, true);
  140. return *size;
  141. }