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

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 * Original version 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 * Pin connections should be as follows for Arduino:
heroic 0:db163b6f1592 20 *
heroic 0:db163b6f1592 21 * DR1 = 2 (digital pin 2)
heroic 0:db163b6f1592 22 * CE = 3
heroic 0:db163b6f1592 23 * CS = 4
heroic 0:db163b6f1592 24 * CLK = 5
heroic 0:db163b6f1592 25 * DAT = 6
heroic 0:db163b6f1592 26 *
heroic 0:db163b6f1592 27 */
heroic 0:db163b6f1592 28
heroic 0:db163b6f1592 29 #include "mbed.h"
heroic 0:db163b6f1592 30
heroic 0:db163b6f1592 31 #define NRF2401_BUFFER_SIZE 25
heroic 0:db163b6f1592 32
heroic 0:db163b6f1592 33 class Nrf2401
heroic 0:db163b6f1592 34 {
heroic 0:db163b6f1592 35 public:
heroic 0:db163b6f1592 36
heroic 0:db163b6f1592 37 // properties
heroic 0:db163b6f1592 38
heroic 0:db163b6f1592 39 volatile unsigned char data[NRF2401_BUFFER_SIZE];
heroic 0:db163b6f1592 40 volatile unsigned int remoteAddress;
heroic 0:db163b6f1592 41 volatile unsigned int localAddress;
heroic 0:db163b6f1592 42 volatile unsigned char dataRate;
heroic 0:db163b6f1592 43 volatile unsigned char channel;
heroic 0:db163b6f1592 44 volatile unsigned char power;
heroic 0:db163b6f1592 45 volatile unsigned char mode;
heroic 0:db163b6f1592 46
heroic 0:db163b6f1592 47 // methods
heroic 0:db163b6f1592 48
heroic 1:049f6cb8b160 49 Nrf2401(PinName n_DR1, PinName n_CE, PinName n_CS, PinName n_CLK, PinName n_DAT);
heroic 0:db163b6f1592 50 void rxMode(unsigned char messageSize=0);
heroic 0:db163b6f1592 51 void txMode(unsigned char messageSize=0);
heroic 0:db163b6f1592 52 void write(unsigned char dataByte);
heroic 0:db163b6f1592 53 void write(unsigned char* dataBuffer=0);
heroic 0:db163b6f1592 54 void read(unsigned char* dataBuffer=0);
heroic 0:db163b6f1592 55 bool available(void);
heroic 0:db163b6f1592 56
heroic 0:db163b6f1592 57 // you shouldn't need to use anything below this point..
heroic 0:db163b6f1592 58 private:
heroic 0:db163b6f1592 59 volatile unsigned char payloadSize;
heroic 0:db163b6f1592 60 volatile unsigned char configuration[15];
heroic 0:db163b6f1592 61 void configure(void);
heroic 0:db163b6f1592 62 void loadConfiguration(bool modeSwitchOnly=false);
heroic 0:db163b6f1592 63 void loadByte(unsigned char byte);
heroic 1:049f6cb8b160 64 inline void select_chip(void) {
heroic 1:049f6cb8b160 65 _cs = 1;
heroic 1:049f6cb8b160 66 }
heroic 1:049f6cb8b160 67
heroic 1:049f6cb8b160 68 inline void deselect_chip(void) {
heroic 1:049f6cb8b160 69 _cs = 0;
heroic 1:049f6cb8b160 70 }
heroic 1:049f6cb8b160 71
heroic 1:049f6cb8b160 72 inline void enable_chip(void) {
heroic 1:049f6cb8b160 73 _ce = 1;
heroic 1:049f6cb8b160 74 }
heroic 1:049f6cb8b160 75
heroic 1:049f6cb8b160 76 inline void disable_chip(void) {
heroic 1:049f6cb8b160 77 _ce = 0;
heroic 1:049f6cb8b160 78 }
heroic 1:049f6cb8b160 79
heroic 1:049f6cb8b160 80 inline void cycle_clock(void) {
heroic 1:049f6cb8b160 81 _clk = 1;
heroic 1:049f6cb8b160 82 wait_us(1);
heroic 1:049f6cb8b160 83 _clk = 0;
heroic 1:049f6cb8b160 84 }
heroic 1:049f6cb8b160 85
heroic 1:049f6cb8b160 86 inline void tx_data_lo(void) {
heroic 1:049f6cb8b160 87 _dat.output();
heroic 1:049f6cb8b160 88 _dat = 1;
heroic 1:049f6cb8b160 89 }
heroic 1:049f6cb8b160 90
heroic 1:049f6cb8b160 91 inline void tx_data_hi(void) {
heroic 1:049f6cb8b160 92 _dat.output();
heroic 1:049f6cb8b160 93 _dat = 1;
heroic 1:049f6cb8b160 94 }
heroic 1:049f6cb8b160 95
heroic 1:049f6cb8b160 96 inline int rx_data_hi(void) {
heroic 1:049f6cb8b160 97 _dat.input();
heroic 1:049f6cb8b160 98 return _dat;
heroic 1:049f6cb8b160 99 }
heroic 1:049f6cb8b160 100
heroic 1:049f6cb8b160 101 inline int data_ready(void) {
heroic 1:049f6cb8b160 102 return _dr1;
heroic 1:049f6cb8b160 103 }
heroic 0:db163b6f1592 104
heroic 0:db163b6f1592 105 DigitalOut _ce, _cs, _clk;
heroic 0:db163b6f1592 106 DigitalIn _dr1;
heroic 0:db163b6f1592 107 DigitalInOut _dat;
heroic 0:db163b6f1592 108 };