Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cc2500.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * CC2500 helper routines
  3. */
  4. #ifdef DEBUG
  5. //#define DEBUG_STATUS
  6. #endif
  7. #include "spi.h"
  8. #include "timer.h"
  9. #include "main.h"
  10. #include "cc2500.h"
  11. #ifdef DEBUG_STATUS
  12. static void cc2500PrintStatusByte(uint8_t status) {
  13. static int16_t lastStatusByte = -1;
  14. //if ((status & ~CC2500_STATUS_FIFO_BYTES_AVAILABLE_BM) != (lastStatusByte & ~CC2500_STATUS_FIFO_BYTES_AVAILABLE_BM)) {
  15. if (status != lastStatusByte) {
  16. lastStatusByte = status;
  17. debugWrite("\nStatus change: 0x");
  18. debugHex(status);
  19. debugWrite("\n");
  20. if (status & CC2500_STATUS_CHIP_RDYn_BM) {
  21. debugWrite(" Power/Crystal NOT ready!\n");
  22. }
  23. debugWrite(" State: ");
  24. switch (status & CC2500_STATUS_STATE_BM) {
  25. case CC2500_STATE_IDLE:
  26. debugWrite("IDLE");
  27. break;
  28. case CC2500_STATE_RX:
  29. debugWrite("RX");
  30. break;
  31. case CC2500_STATE_TX:
  32. debugWrite("TX");
  33. break;
  34. case CC2500_STATE_FSTXON:
  35. debugWrite("Freq Synth TX on");
  36. break;
  37. case CC2500_STATE_CALIBRATE:
  38. debugWrite("Freq Synth Calibration");
  39. break;
  40. case CC2500_STATE_SETTLING:
  41. debugWrite("PLL settling");
  42. break;
  43. case CC2500_STATE_RX_OVERFLOW:
  44. debugWrite("RX FIFO Overflow");
  45. break;
  46. case CC2500_STATE_TX_UNDERFLOW:
  47. debugWrite("TX FIFO Underflow");
  48. break;
  49. default:
  50. debugWrite("UNKNOWN");
  51. break;
  52. }
  53. debugWrite("\n");
  54. debugWrite(" FIFO Bytes available: 0x");
  55. debugHex(status & CC2500_STATUS_FIFO_BYTES_AVAILABLE_BM);
  56. debugWrite("\n\n");
  57. }
  58. }
  59. #endif
  60. void cc2500ReadRegisterMulti(uint8_t address, uint8_t data[], uint8_t length) {
  61. CS_off;
  62. #ifdef DEBUG_STATUS
  63. uint8_t status = spiWrite(address);
  64. #else
  65. spiWrite(address);
  66. #endif
  67. for (uint8_t i = 0; i < length; i++) {
  68. data[i] = spiRead();
  69. }
  70. CS_on;
  71. #ifdef DEBUG_STATUS
  72. cc2500PrintStatusByte(status);
  73. #endif
  74. }
  75. void cc2500WriteRegisterMulti(uint8_t address, const uint8_t data[], uint8_t length) {
  76. CS_off;
  77. #ifdef DEBUG_STATUS
  78. uint8_t status = spiWrite(CC2500_WRITE_BURST | address);
  79. #else
  80. spiWrite(CC2500_WRITE_BURST | address);
  81. #endif
  82. for (uint8_t i = 0; i < length; i++) {
  83. spiWrite(data[i]);
  84. }
  85. CS_on;
  86. #ifdef DEBUG_STATUS
  87. cc2500PrintStatusByte(status);
  88. #endif
  89. }
  90. void cc2500WriteReg(uint8_t address, uint8_t data) {
  91. CS_off;
  92. #ifdef DEBUG_STATUS
  93. uint8_t status = spiWrite(address);
  94. #else
  95. spiWrite(address);
  96. #endif
  97. NOP();
  98. spiWrite(data);
  99. CS_on;
  100. #ifdef DEBUG_STATUS
  101. cc2500PrintStatusByte(status);
  102. #endif
  103. }
  104. uint8_t cc2500ReadReg(uint8_t address) {
  105. CS_off;
  106. if ((address >= 0x30) && (address <= 0x3D)) {
  107. // Status Registers need a burst read
  108. address |= CC2500_READ_BURST;
  109. } else {
  110. address |= CC2500_READ_SINGLE;
  111. }
  112. #ifdef DEBUG_STATUS
  113. uint8_t status = spiWrite(address);
  114. #else
  115. spiWrite(address);
  116. #endif
  117. uint8_t result = spiRead();
  118. CS_on;
  119. #ifdef DEBUG_STATUS
  120. cc2500PrintStatusByte(status);
  121. #endif
  122. return result;
  123. }
  124. void cc2500Strobe(uint8_t address) {
  125. CS_off;
  126. #ifdef DEBUG_STATUS
  127. uint8_t status = spiWrite(address);
  128. #else
  129. spiWrite(address);
  130. #endif
  131. CS_on;
  132. #ifdef DEBUG_STATUS
  133. cc2500PrintStatusByte(status);
  134. #endif
  135. }
  136. void cc2500ResetChip(void) {
  137. // Toggle chip select signal
  138. CS_on;
  139. delay_us(30);
  140. CS_off;
  141. delay_us(30);
  142. CS_on;
  143. delay_us(45);
  144. cc2500Strobe(CC2500_SRES);
  145. delay_ms(100);
  146. }