Library for the (decidedly not as good as the 24L01+) Nordic 2401A radio.
Nrf2401A.cpp@0:db163b6f1592, 2013-09-20 (annotated)
- Committer:
- heroic
- Date:
- Fri Sep 20 20:55:24 2013 +0000
- Revision:
- 0:db163b6f1592
- Child:
- 1:049f6cb8b160
Establishing commit. Ported to mbed.
Who changed what in which revision?
User | Revision | Line number | New 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 | * 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 "Nrf2401.h" |
heroic | 0:db163b6f1592 | 30 | |
heroic | 0:db163b6f1592 | 31 | inline void select_chip(void) { |
heroic | 0:db163b6f1592 | 32 | _ce = 1; |
heroic | 0:db163b6f1592 | 33 | } |
heroic | 0:db163b6f1592 | 34 | |
heroic | 0:db163b6f1592 | 35 | inline void deselect_chip(void) { |
heroic | 0:db163b6f1592 | 36 | _ce = 0; |
heroic | 0:db163b6f1592 | 37 | } |
heroic | 0:db163b6f1592 | 38 | |
heroic | 0:db163b6f1592 | 39 | inline void enable_chip(void) { |
heroic | 0:db163b6f1592 | 40 | _ce = 1; |
heroic | 0:db163b6f1592 | 41 | } |
heroic | 0:db163b6f1592 | 42 | |
heroic | 0:db163b6f1592 | 43 | inline void disable_chip(void) { |
heroic | 0:db163b6f1592 | 44 | _ce = 0; |
heroic | 0:db163b6f1592 | 45 | } |
heroic | 0:db163b6f1592 | 46 | |
heroic | 0:db163b6f1592 | 47 | inline void cycle_clock(void) { |
heroic | 0:db163b6f1592 | 48 | _clk = 1; |
heroic | 0:db163b6f1592 | 49 | delay_us(1); |
heroic | 0:db163b6f1592 | 50 | _clk = 0; |
heroic | 0:db163b6f1592 | 51 | } |
heroic | 0:db163b6f1592 | 52 | |
heroic | 0:db163b6f1592 | 53 | inline void tx_data_lo(void) { |
heroic | 0:db163b6f1592 | 54 | _dat.output(); |
heroic | 0:db163b6f1592 | 55 | _dat = 1; |
heroic | 0:db163b6f1592 | 56 | } |
heroic | 0:db163b6f1592 | 57 | |
heroic | 0:db163b6f1592 | 58 | inline void tx_data_hi(void) { |
heroic | 0:db163b6f1592 | 59 | _dat.output(); |
heroic | 0:db163b6f1592 | 60 | _dat = 1; |
heroic | 0:db163b6f1592 | 61 | } |
heroic | 0:db163b6f1592 | 62 | |
heroic | 0:db163b6f1592 | 63 | inline int rx_data_hi(void) { |
heroic | 0:db163b6f1592 | 64 | _dat.input(); |
heroic | 0:db163b6f1592 | 65 | return _dat; |
heroic | 0:db163b6f1592 | 66 | } |
heroic | 0:db163b6f1592 | 67 | |
heroic | 0:db163b6f1592 | 68 | inline int data_ready(void) { |
heroic | 0:db163b6f1592 | 69 | return _dr1; |
heroic | 0:db163b6f1592 | 70 | } |
heroic | 0:db163b6f1592 | 71 | |
heroic | 0:db163b6f1592 | 72 | Nrf2401::Nrf2401(PinName n_DR1, PinName n_CE, PinName n_CS, PinName n_CLK, PinName n_DAT) : |
heroic | 0:db163b6f1592 | 73 | _dr1(n_DR1), _ce(n_CE), _cs(n_CS), _clk(n_CLK), _clk(n_CLK), _dat(n_DAT) |
heroic | 0:db163b6f1592 | 74 | { |
heroic | 0:db163b6f1592 | 75 | _dat.output(); |
heroic | 0:db163b6f1592 | 76 | |
heroic | 0:db163b6f1592 | 77 | remoteAddress = 0; |
heroic | 0:db163b6f1592 | 78 | localAddress = 0; |
heroic | 0:db163b6f1592 | 79 | payloadSize = 0; |
heroic | 0:db163b6f1592 | 80 | dataRate = 1; |
heroic | 0:db163b6f1592 | 81 | channel = 111; |
heroic | 0:db163b6f1592 | 82 | power = 3; |
heroic | 0:db163b6f1592 | 83 | mode = 0; |
heroic | 0:db163b6f1592 | 84 | |
heroic | 0:db163b6f1592 | 85 | configuration[7] = 234; |
heroic | 0:db163b6f1592 | 86 | configuration[8] = 223; |
heroic | 0:db163b6f1592 | 87 | configuration[9] = 212; |
heroic | 0:db163b6f1592 | 88 | |
heroic | 0:db163b6f1592 | 89 | disable_chip(); |
heroic | 0:db163b6f1592 | 90 | deselect_chip(); |
heroic | 0:db163b6f1592 | 91 | } |
heroic | 0:db163b6f1592 | 92 | |
heroic | 0:db163b6f1592 | 93 | void Nrf2401::rxMode(unsigned char messageSize) |
heroic | 0:db163b6f1592 | 94 | { |
heroic | 0:db163b6f1592 | 95 | mode = 1; |
heroic | 0:db163b6f1592 | 96 | if(messageSize) payloadSize = messageSize, configure(); |
heroic | 0:db163b6f1592 | 97 | else configuration[14] |= 1, loadConfiguration(true); |
heroic | 0:db163b6f1592 | 98 | enable_chip(); |
heroic | 0:db163b6f1592 | 99 | delay_us(250); |
heroic | 0:db163b6f1592 | 100 | } |
heroic | 0:db163b6f1592 | 101 | |
heroic | 0:db163b6f1592 | 102 | void Nrf2401::txMode(unsigned char messageSize) |
heroic | 0:db163b6f1592 | 103 | { |
heroic | 0:db163b6f1592 | 104 | mode = 0; |
heroic | 0:db163b6f1592 | 105 | if(messageSize) payloadSize = messageSize, configure(); |
heroic | 0:db163b6f1592 | 106 | else configuration[14] &= ~1, loadConfiguration(true); |
heroic | 0:db163b6f1592 | 107 | delay_us(250); |
heroic | 0:db163b6f1592 | 108 | } |
heroic | 0:db163b6f1592 | 109 | |
heroic | 0:db163b6f1592 | 110 | void Nrf2401::write(unsigned char* dataBuffer) |
heroic | 0:db163b6f1592 | 111 | { |
heroic | 0:db163b6f1592 | 112 | if(!dataBuffer) dataBuffer = (unsigned char*) data; |
heroic | 0:db163b6f1592 | 113 | enable_chip(); |
heroic | 0:db163b6f1592 | 114 | delay_us(5); |
heroic | 0:db163b6f1592 | 115 | loadByte(configuration[7]); |
heroic | 0:db163b6f1592 | 116 | loadByte(configuration[8]); |
heroic | 0:db163b6f1592 | 117 | loadByte(configuration[9]); |
heroic | 0:db163b6f1592 | 118 | loadByte(remoteAddress >> 8); |
heroic | 0:db163b6f1592 | 119 | loadByte(remoteAddress); |
heroic | 0:db163b6f1592 | 120 | for(int i=0; i<payloadSize; i++) loadByte(dataBuffer[i]); |
heroic | 0:db163b6f1592 | 121 | disable_chip(); |
heroic | 0:db163b6f1592 | 122 | delay_us(250); |
heroic | 0:db163b6f1592 | 123 | } |
heroic | 0:db163b6f1592 | 124 | |
heroic | 0:db163b6f1592 | 125 | void Nrf2401::write(unsigned char dataByte) |
heroic | 0:db163b6f1592 | 126 | { |
heroic | 0:db163b6f1592 | 127 | data[0] = dataByte; |
heroic | 0:db163b6f1592 | 128 | write(); |
heroic | 0:db163b6f1592 | 129 | } |
heroic | 0:db163b6f1592 | 130 | |
heroic | 0:db163b6f1592 | 131 | void Nrf2401::read(unsigned char* dataBuffer) |
heroic | 0:db163b6f1592 | 132 | { |
heroic | 0:db163b6f1592 | 133 | if(!dataBuffer) dataBuffer = (unsigned char*) data; |
heroic | 0:db163b6f1592 | 134 | disable_chip(); |
heroic | 0:db163b6f1592 | 135 | _delay_ms(2); |
heroic | 0:db163b6f1592 | 136 | for(int i=0; i<payloadSize; i++) |
heroic | 0:db163b6f1592 | 137 | { |
heroic | 0:db163b6f1592 | 138 | dataBuffer[i] = 0; |
heroic | 0:db163b6f1592 | 139 | for(int n=7; n>-1; n--) |
heroic | 0:db163b6f1592 | 140 | { |
heroic | 0:db163b6f1592 | 141 | if(RX_DATA_HI) dataBuffer[i] |= (1 << n); |
heroic | 0:db163b6f1592 | 142 | delay_us(1); |
heroic | 0:db163b6f1592 | 143 | cycle_clock(); |
heroic | 0:db163b6f1592 | 144 | } |
heroic | 0:db163b6f1592 | 145 | } |
heroic | 0:db163b6f1592 | 146 | enable_chip(); |
heroic | 0:db163b6f1592 | 147 | delay_us(1); |
heroic | 0:db163b6f1592 | 148 | } |
heroic | 0:db163b6f1592 | 149 | |
heroic | 0:db163b6f1592 | 150 | bool Nrf2401::available(void) |
heroic | 0:db163b6f1592 | 151 | { |
heroic | 0:db163b6f1592 | 152 | return data_ready(); |
heroic | 0:db163b6f1592 | 153 | } |
heroic | 0:db163b6f1592 | 154 | |
heroic | 0:db163b6f1592 | 155 | //// you shouldn't need to call directly any of the methods below this point... |
heroic | 0:db163b6f1592 | 156 | |
heroic | 0:db163b6f1592 | 157 | void Nrf2401::configure(void) |
heroic | 0:db163b6f1592 | 158 | { |
heroic | 0:db163b6f1592 | 159 | configuration[1] = payloadSize << 3; |
heroic | 0:db163b6f1592 | 160 | configuration[10] = localAddress >> 8; |
heroic | 0:db163b6f1592 | 161 | configuration[11] = localAddress; |
heroic | 0:db163b6f1592 | 162 | configuration[12] = 163; |
heroic | 0:db163b6f1592 | 163 | configuration[13] = power | (dataRate << 5) | 76; |
heroic | 0:db163b6f1592 | 164 | configuration[14] = mode | (channel << 1); |
heroic | 0:db163b6f1592 | 165 | loadConfiguration(); |
heroic | 0:db163b6f1592 | 166 | } |
heroic | 0:db163b6f1592 | 167 | |
heroic | 0:db163b6f1592 | 168 | void Nrf2401::loadConfiguration(bool modeSwitchOnly) |
heroic | 0:db163b6f1592 | 169 | { |
heroic | 0:db163b6f1592 | 170 | disable_chip(); |
heroic | 0:db163b6f1592 | 171 | select_chip(); |
heroic | 0:db163b6f1592 | 172 | delay_us(5); |
heroic | 0:db163b6f1592 | 173 | if(modeSwitchOnly) loadByte(configuration[14]); |
heroic | 0:db163b6f1592 | 174 | else for(int i=0; i<15; i++) loadByte(configuration[i]); |
heroic | 0:db163b6f1592 | 175 | deselect_chip(); |
heroic | 0:db163b6f1592 | 176 | } |
heroic | 0:db163b6f1592 | 177 | |
heroic | 0:db163b6f1592 | 178 | void Nrf2401::loadByte(unsigned char byte) |
heroic | 0:db163b6f1592 | 179 | { |
heroic | 0:db163b6f1592 | 180 | for(int i=7; i>-1; i--) |
heroic | 0:db163b6f1592 | 181 | { |
heroic | 0:db163b6f1592 | 182 | if((byte & (1 << i)) == 0) tx_data_lo(); |
heroic | 0:db163b6f1592 | 183 | else tx_data_hi(); |
heroic | 0:db163b6f1592 | 184 | delay_us(1); |
heroic | 0:db163b6f1592 | 185 | cycle_clock(); |
heroic | 0:db163b6f1592 | 186 | } |
heroic | 0:db163b6f1592 | 187 | } |