Naze32 clone with Frysky receiver
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cc2500.c 3.6KB

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