cc1101.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_get_frequency_step(const ApiHalSpiDevice* device) {
  95. return CC1101_QUARTZ / CC1101_FDIV;
  96. }
  97. uint32_t cc1101_set_frequency_offset(const ApiHalSpiDevice* device, uint32_t value) {
  98. uint64_t real_value = value * CC1101_IFDIV / CC1101_QUARTZ;
  99. assert((real_value & 0xFF) == real_value);
  100. cc1101_write_reg(device, CC1101_FSCTRL0, (real_value >> 0 ) & 0xFF);
  101. uint64_t real_frequency = real_value * CC1101_QUARTZ / CC1101_IFDIV;
  102. return (uint32_t)real_frequency;
  103. }
  104. uint32_t cc1101_get_frequency_offset_step(const ApiHalSpiDevice* device) {
  105. return CC1101_QUARTZ / CC1101_IFDIV;
  106. }
  107. void cc1101_set_pa_table(const ApiHalSpiDevice* device, const uint8_t value[8]) {
  108. uint8_t tx[9] = { CC1101_PATABLE | CC1101_BURST };
  109. CC1101Status rx[9] = { 0 };
  110. memcpy(&tx[1], &value[0], 8);
  111. hal_gpio_write(device->chip_select, false);
  112. while(hal_gpio_read(device->bus->miso));
  113. api_hal_spi_bus_trx(device->bus, tx, (uint8_t*)rx, sizeof(rx), CC1101_TIMEOUT);
  114. hal_gpio_write(device->chip_select, true);
  115. assert((rx[0].CHIP_RDYn|rx[8].CHIP_RDYn) == 0);
  116. }
  117. uint8_t cc1101_write_fifo(const ApiHalSpiDevice* device, const uint8_t* data, uint8_t size) {
  118. uint8_t buff_tx[64];
  119. uint8_t buff_rx[64];
  120. buff_tx[0] = CC1101_FIFO | CC1101_BURST;
  121. memcpy(&buff_tx[1], data, size);
  122. // Start transaction
  123. hal_gpio_write(device->chip_select, false);
  124. // Wait IC to become ready
  125. while(hal_gpio_read(device->bus->miso));
  126. // Tell IC what we want
  127. api_hal_spi_bus_trx(device->bus, buff_tx, (uint8_t*) buff_rx, size + 1, CC1101_TIMEOUT);
  128. // Finish transaction
  129. hal_gpio_write(device->chip_select, true);
  130. return size;
  131. }
  132. uint8_t cc1101_read_fifo(const ApiHalSpiDevice* device, uint8_t* data, uint8_t* size) {
  133. uint8_t buff_tx[64];
  134. buff_tx[0] = CC1101_FIFO | CC1101_READ | CC1101_BURST;
  135. uint8_t buff_rx[2];
  136. // Start transaction
  137. hal_gpio_write(device->chip_select, false);
  138. // Wait IC to become ready
  139. while(hal_gpio_read(device->bus->miso));
  140. // First byte - packet length
  141. api_hal_spi_bus_trx(device->bus, buff_tx, buff_rx, 2, CC1101_TIMEOUT);
  142. *size = buff_rx[2];
  143. api_hal_spi_bus_trx(device->bus, &buff_tx[1], data, *size, CC1101_TIMEOUT);
  144. cc1101_flush_rx(device);
  145. hal_gpio_write(device->chip_select, true);
  146. return *size;
  147. }