Mirf.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * Mirf
  3. *
  4. * Additional bug fixes and improvements
  5. * 11/03/2011:
  6. * Switched spi library.
  7. * 07/13/2010:
  8. * Added example to read a register
  9. * 11/12/2009:
  10. * Fix dataReady() to work correctly
  11. * Renamed keywords to keywords.txt ( for IDE ) and updated keyword list
  12. * Fixed client example code to timeout after one second and try again
  13. * when no response received from server
  14. * By: Nathan Isburgh <nathan@mrroot.net>
  15. * $Id: mirf.cpp 67 2010-07-13 13:25:53Z nisburgh $
  16. *
  17. *
  18. * An Ardunio port of:
  19. * http://www.tinkerer.eu/AVRLib/nRF24L01
  20. *
  21. * Significant changes to remove depencence on interupts and auto ack support.
  22. *
  23. * Aaron Shrimpton <aaronds@gmail.com>
  24. *
  25. */
  26. /*
  27. Copyright (c) 2007 Stefan Engelke <mbox@stefanengelke.de>
  28. Permission is hereby granted, free of charge, to any person
  29. obtaining a copy of this software and associated documentation
  30. files (the "Software"), to deal in the Software without
  31. restriction, including without limitation the rights to use, copy,
  32. modify, merge, publish, distribute, sublicense, and/or sell copies
  33. of the Software, and to permit persons to whom the Software is
  34. furnished to do so, subject to the following conditions:
  35. The above copyright notice and this permission notice shall be
  36. included in all copies or substantial portions of the Software.
  37. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  38. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  39. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  40. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  41. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  42. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  44. DEALINGS IN THE SOFTWARE.
  45. $Id: mirf.cpp 67 2010-07-13 13:25:53Z nisburgh $
  46. */
  47. #include "Mirf.h"
  48. // Defines for setting the MiRF registers for transmitting or receiving mode
  49. Nrf24l Mirf = Nrf24l();
  50. Nrf24l::Nrf24l(){
  51. cePin = 8;
  52. csnPin = 7;
  53. channel = 1;
  54. payload = 16;
  55. spi = NULL;
  56. }
  57. void Nrf24l::transferSync(uint8_t *dataout,uint8_t *datain,uint8_t len){
  58. uint8_t i;
  59. for(i = 0;i < len;i++){
  60. datain[i] = spi->transfer(dataout[i]);
  61. }
  62. }
  63. void Nrf24l::transmitSync(uint8_t *dataout,uint8_t len){
  64. uint8_t i;
  65. for(i = 0;i < len;i++){
  66. spi->transfer(dataout[i]);
  67. }
  68. }
  69. void Nrf24l::init()
  70. // Initializes pins to communicate with the MiRF module
  71. // Should be called in the early initializing phase at startup.
  72. {
  73. pinMode(cePin,OUTPUT);
  74. pinMode(csnPin,OUTPUT);
  75. ceLow();
  76. csnHi();
  77. // Initialize spi module
  78. spi->begin();
  79. }
  80. void Nrf24l::config()
  81. // Sets the important registers in the MiRF module and powers the module
  82. // in receiving mode
  83. // NB: channel and payload must be set now.
  84. {
  85. // Set RF channel
  86. configRegister(RF_CH,channel);
  87. // Set length of incoming payload
  88. configRegister(RX_PW_P0, payload);
  89. configRegister(RX_PW_P1, payload);
  90. // Start receiver
  91. powerUpRx();
  92. flushRx();
  93. }
  94. void Nrf24l::setRADDR(uint8_t * adr)
  95. // Sets the receiving address
  96. {
  97. ceLow();
  98. writeRegister(RX_ADDR_P1,adr,mirf_ADDR_LEN);
  99. ceHi();
  100. }
  101. void Nrf24l::setTADDR(uint8_t * adr)
  102. // Sets the transmitting address
  103. {
  104. /*
  105. * RX_ADDR_P0 must be set to the sending addr for auto ack to work.
  106. */
  107. writeRegister(RX_ADDR_P0,adr,mirf_ADDR_LEN);
  108. writeRegister(TX_ADDR,adr,mirf_ADDR_LEN);
  109. }
  110. extern bool Nrf24l::dataReady()
  111. // Checks if data is available for reading
  112. {
  113. // See note in getData() function - just checking RX_DR isn't good enough
  114. uint8_t status = getStatus();
  115. // We can short circuit on RX_DR, but if it's not set, we still need
  116. // to check the FIFO for any pending packets
  117. if ( status & (1 << RX_DR) ) return 1;
  118. return !rxFifoEmpty();
  119. }
  120. extern bool Nrf24l::rxFifoEmpty(){
  121. uint8_t fifoStatus;
  122. readRegister(FIFO_STATUS,&fifoStatus,sizeof(fifoStatus));
  123. return (fifoStatus & (1 << RX_EMPTY));
  124. }
  125. extern void Nrf24l::getData(uint8_t * data)
  126. // Reads payload bytes into data array
  127. {
  128. csnLow(); // Pull down chip select
  129. spi->transfer( R_RX_PAYLOAD ); // Send cmd to read rx payload
  130. transferSync(data,data,payload); // Read payload
  131. csnHi(); // Pull up chip select
  132. // NVI: per product spec, p 67, note c:
  133. // "The RX_DR IRQ is asserted by a new packet arrival event. The procedure
  134. // for handling this interrupt should be: 1) read payload through SPI,
  135. // 2) clear RX_DR IRQ, 3) read FIFO_STATUS to check if there are more
  136. // payloads available in RX FIFO, 4) if there are more data in RX FIFO,
  137. // repeat from step 1)."
  138. // So if we're going to clear RX_DR here, we need to check the RX FIFO
  139. // in the dataReady() function
  140. configRegister(STATUS,(1<<RX_DR)); // Reset status register
  141. }
  142. void Nrf24l::configRegister(uint8_t reg, uint8_t value)
  143. // Clocks only one byte into the given MiRF register
  144. {
  145. csnLow();
  146. spi->transfer(W_REGISTER | (REGISTER_MASK & reg));
  147. spi->transfer(value);
  148. csnHi();
  149. }
  150. void Nrf24l::readRegister(uint8_t reg, uint8_t * value, uint8_t len)
  151. // Reads an array of bytes from the given start position in the MiRF registers.
  152. {
  153. csnLow();
  154. spi->transfer(R_REGISTER | (REGISTER_MASK & reg));
  155. transferSync(value,value,len);
  156. csnHi();
  157. }
  158. void Nrf24l::writeRegister(uint8_t reg, uint8_t * value, uint8_t len)
  159. // Writes an array of bytes into inte the MiRF registers.
  160. {
  161. csnLow();
  162. spi->transfer(W_REGISTER | (REGISTER_MASK & reg));
  163. transmitSync(value,len);
  164. csnHi();
  165. }
  166. void Nrf24l::send(uint8_t * value)
  167. // Sends a data package to the default address. Be sure to send the correct
  168. // amount of bytes as configured as payload on the receiver.
  169. {
  170. uint8_t status;
  171. status = getStatus();
  172. while (PTX) {
  173. status = getStatus();
  174. if((status & ((1 << TX_DS) | (1 << MAX_RT)))){
  175. PTX = 0;
  176. break;
  177. }
  178. } // Wait until last paket is send
  179. ceLow();
  180. powerUpTx(); // Set to transmitter mode , Power up
  181. csnLow(); // Pull down chip select
  182. spi->transfer( FLUSH_TX ); // Write cmd to flush tx fifo
  183. csnHi(); // Pull up chip select
  184. csnLow(); // Pull down chip select
  185. spi->transfer( W_TX_PAYLOAD ); // Write cmd to write payload
  186. transmitSync(value,payload); // Write payload
  187. csnHi(); // Pull up chip select
  188. ceHi(); // Start transmission
  189. }
  190. /**
  191. * isSending.
  192. *
  193. * Test if chip is still sending.
  194. * When sending has finished return chip to listening.
  195. *
  196. */
  197. bool Nrf24l::isSending(){
  198. uint8_t status;
  199. if(PTX){
  200. status = getStatus();
  201. /*
  202. * if sending successful (TX_DS) or max retries exceded (MAX_RT).
  203. */
  204. if((status & ((1 << TX_DS) | (1 << MAX_RT)))){
  205. powerUpRx();
  206. return false;
  207. }
  208. return true;
  209. }
  210. return false;
  211. }
  212. uint8_t Nrf24l::getStatus(){
  213. uint8_t rv;
  214. readRegister(STATUS,&rv,1);
  215. return rv;
  216. }
  217. void Nrf24l::powerUpRx(){
  218. PTX = 0;
  219. ceLow();
  220. configRegister(CONFIG, mirf_CONFIG | ( (1<<PWR_UP) | (1<<PRIM_RX) ) );
  221. ceHi();
  222. configRegister(STATUS,(1 << TX_DS) | (1 << MAX_RT));
  223. }
  224. void Nrf24l::flushRx(){
  225. csnLow();
  226. spi->transfer( FLUSH_RX );
  227. csnHi();
  228. }
  229. void Nrf24l::powerUpTx(){
  230. PTX = 1;
  231. configRegister(CONFIG, mirf_CONFIG | ( (1<<PWR_UP) | (0<<PRIM_RX) ) );
  232. }
  233. void Nrf24l::ceHi(){
  234. digitalWrite(cePin,HIGH);
  235. }
  236. void Nrf24l::ceLow(){
  237. digitalWrite(cePin,LOW);
  238. }
  239. void Nrf24l::csnHi(){
  240. digitalWrite(csnPin,HIGH);
  241. }
  242. void Nrf24l::csnLow(){
  243. digitalWrite(csnPin,LOW);
  244. }
  245. void Nrf24l::powerDown(){
  246. ceLow();
  247. configRegister(CONFIG, mirf_CONFIG );
  248. }