Library for the (decidedly not as good as the 24L01+) Nordic 2401A radio.

Fork of Nrf2401 by Heroic Robotics

Committer:
heroic
Date:
Fri Sep 20 21:47:44 2013 +0000
Revision:
1:049f6cb8b160
Parent:
0:db163b6f1592
Fix many bugs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
heroic 0:db163b6f1592 1 /*
heroic 0:db163b6f1592 2 * Nrf2401A.h
heroic 0:db163b6f1592 3 * A simplistic interface for using Sparkfun's Nrf2401A breakout boards with Arduino
heroic 0:db163b6f1592 4 *
heroic 0:db163b6f1592 5 * Ported to mbed by Jas Strong <jasmine@heroicrobotics.com>
heroic 0:db163b6f1592 6 *
heroic 0:db163b6f1592 7 * Original code for http://labs.ideo.com by Jesse Tane March 2009
heroic 0:db163b6f1592 8 *
heroic 0:db163b6f1592 9 * License:
heroic 0:db163b6f1592 10 * --------
heroic 0:db163b6f1592 11 * This is free software. You can redistribute it and/or modify it under
heroic 0:db163b6f1592 12 * the terms of Creative Commons Attribution 3.0 United States License.
heroic 0:db163b6f1592 13 * To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/
heroic 0:db163b6f1592 14 * or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
heroic 0:db163b6f1592 15 *
heroic 0:db163b6f1592 16 * Notes:
heroic 0:db163b6f1592 17 * ------
heroic 0:db163b6f1592 18 * For documentation on how to use this library, please visit http://www.arduino.cc/playground/Main/InterfacingWithHardware
heroic 0:db163b6f1592 19 *
heroic 0:db163b6f1592 20 */
heroic 0:db163b6f1592 21
heroic 1:049f6cb8b160 22 #include "Nrf2401A.h"
heroic 1:049f6cb8b160 23 #include "mbed.h"
heroic 0:db163b6f1592 24
heroic 0:db163b6f1592 25
heroic 0:db163b6f1592 26 Nrf2401::Nrf2401(PinName n_DR1, PinName n_CE, PinName n_CS, PinName n_CLK, PinName n_DAT) :
heroic 1:049f6cb8b160 27 _dr1(n_DR1), _ce(n_CE), _cs(n_CS), _clk(n_CLK), _dat(n_DAT)
heroic 0:db163b6f1592 28 {
heroic 0:db163b6f1592 29 _dat.output();
heroic 0:db163b6f1592 30
heroic 0:db163b6f1592 31 remoteAddress = 0;
heroic 0:db163b6f1592 32 localAddress = 0;
heroic 0:db163b6f1592 33 payloadSize = 0;
heroic 0:db163b6f1592 34 dataRate = 1;
heroic 0:db163b6f1592 35 channel = 111;
heroic 0:db163b6f1592 36 power = 3;
heroic 0:db163b6f1592 37 mode = 0;
heroic 0:db163b6f1592 38
heroic 0:db163b6f1592 39 configuration[7] = 234;
heroic 0:db163b6f1592 40 configuration[8] = 223;
heroic 0:db163b6f1592 41 configuration[9] = 212;
heroic 0:db163b6f1592 42
heroic 0:db163b6f1592 43 disable_chip();
heroic 0:db163b6f1592 44 deselect_chip();
heroic 0:db163b6f1592 45 }
heroic 0:db163b6f1592 46
heroic 0:db163b6f1592 47 void Nrf2401::rxMode(unsigned char messageSize)
heroic 0:db163b6f1592 48 {
heroic 0:db163b6f1592 49 mode = 1;
heroic 0:db163b6f1592 50 if(messageSize) payloadSize = messageSize, configure();
heroic 0:db163b6f1592 51 else configuration[14] |= 1, loadConfiguration(true);
heroic 0:db163b6f1592 52 enable_chip();
heroic 1:049f6cb8b160 53 wait_us(250);
heroic 0:db163b6f1592 54 }
heroic 0:db163b6f1592 55
heroic 0:db163b6f1592 56 void Nrf2401::txMode(unsigned char messageSize)
heroic 0:db163b6f1592 57 {
heroic 0:db163b6f1592 58 mode = 0;
heroic 0:db163b6f1592 59 if(messageSize) payloadSize = messageSize, configure();
heroic 0:db163b6f1592 60 else configuration[14] &= ~1, loadConfiguration(true);
heroic 1:049f6cb8b160 61 wait_us(250);
heroic 0:db163b6f1592 62 }
heroic 0:db163b6f1592 63
heroic 0:db163b6f1592 64 void Nrf2401::write(unsigned char* dataBuffer)
heroic 0:db163b6f1592 65 {
heroic 0:db163b6f1592 66 if(!dataBuffer) dataBuffer = (unsigned char*) data;
heroic 0:db163b6f1592 67 enable_chip();
heroic 1:049f6cb8b160 68 wait_us(5);
heroic 0:db163b6f1592 69 loadByte(configuration[7]);
heroic 0:db163b6f1592 70 loadByte(configuration[8]);
heroic 0:db163b6f1592 71 loadByte(configuration[9]);
heroic 0:db163b6f1592 72 loadByte(remoteAddress >> 8);
heroic 0:db163b6f1592 73 loadByte(remoteAddress);
heroic 0:db163b6f1592 74 for(int i=0; i<payloadSize; i++) loadByte(dataBuffer[i]);
heroic 0:db163b6f1592 75 disable_chip();
heroic 1:049f6cb8b160 76 wait_us(250);
heroic 0:db163b6f1592 77 }
heroic 0:db163b6f1592 78
heroic 0:db163b6f1592 79 void Nrf2401::write(unsigned char dataByte)
heroic 0:db163b6f1592 80 {
heroic 0:db163b6f1592 81 data[0] = dataByte;
heroic 0:db163b6f1592 82 write();
heroic 0:db163b6f1592 83 }
heroic 0:db163b6f1592 84
heroic 0:db163b6f1592 85 void Nrf2401::read(unsigned char* dataBuffer)
heroic 0:db163b6f1592 86 {
heroic 0:db163b6f1592 87 if(!dataBuffer) dataBuffer = (unsigned char*) data;
heroic 0:db163b6f1592 88 disable_chip();
heroic 1:049f6cb8b160 89 wait_ms(2);
heroic 0:db163b6f1592 90 for(int i=0; i<payloadSize; i++)
heroic 0:db163b6f1592 91 {
heroic 0:db163b6f1592 92 dataBuffer[i] = 0;
heroic 0:db163b6f1592 93 for(int n=7; n>-1; n--)
heroic 0:db163b6f1592 94 {
heroic 1:049f6cb8b160 95 if(rx_data_hi()) dataBuffer[i] |= (1 << n);
heroic 1:049f6cb8b160 96 wait_us(1);
heroic 0:db163b6f1592 97 cycle_clock();
heroic 0:db163b6f1592 98 }
heroic 0:db163b6f1592 99 }
heroic 0:db163b6f1592 100 enable_chip();
heroic 1:049f6cb8b160 101 wait_us(1);
heroic 0:db163b6f1592 102 }
heroic 0:db163b6f1592 103
heroic 0:db163b6f1592 104 bool Nrf2401::available(void)
heroic 0:db163b6f1592 105 {
heroic 0:db163b6f1592 106 return data_ready();
heroic 0:db163b6f1592 107 }
heroic 0:db163b6f1592 108
heroic 0:db163b6f1592 109 //// you shouldn't need to call directly any of the methods below this point...
heroic 0:db163b6f1592 110
heroic 0:db163b6f1592 111 void Nrf2401::configure(void)
heroic 0:db163b6f1592 112 {
heroic 0:db163b6f1592 113 configuration[1] = payloadSize << 3;
heroic 0:db163b6f1592 114 configuration[10] = localAddress >> 8;
heroic 0:db163b6f1592 115 configuration[11] = localAddress;
heroic 0:db163b6f1592 116 configuration[12] = 163;
heroic 0:db163b6f1592 117 configuration[13] = power | (dataRate << 5) | 76;
heroic 0:db163b6f1592 118 configuration[14] = mode | (channel << 1);
heroic 0:db163b6f1592 119 loadConfiguration();
heroic 0:db163b6f1592 120 }
heroic 0:db163b6f1592 121
heroic 0:db163b6f1592 122 void Nrf2401::loadConfiguration(bool modeSwitchOnly)
heroic 0:db163b6f1592 123 {
heroic 0:db163b6f1592 124 disable_chip();
heroic 0:db163b6f1592 125 select_chip();
heroic 1:049f6cb8b160 126 wait_us(5);
heroic 0:db163b6f1592 127 if(modeSwitchOnly) loadByte(configuration[14]);
heroic 0:db163b6f1592 128 else for(int i=0; i<15; i++) loadByte(configuration[i]);
heroic 0:db163b6f1592 129 deselect_chip();
heroic 0:db163b6f1592 130 }
heroic 0:db163b6f1592 131
heroic 0:db163b6f1592 132 void Nrf2401::loadByte(unsigned char byte)
heroic 0:db163b6f1592 133 {
heroic 0:db163b6f1592 134 for(int i=7; i>-1; i--)
heroic 0:db163b6f1592 135 {
heroic 0:db163b6f1592 136 if((byte & (1 << i)) == 0) tx_data_lo();
heroic 0:db163b6f1592 137 else tx_data_hi();
heroic 1:049f6cb8b160 138 wait_us(1);
heroic 0:db163b6f1592 139 cycle_clock();
heroic 0:db163b6f1592 140 }
heroic 0:db163b6f1592 141 }