![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Handheld controller (RF) for Pi Swarm system
Fork of Pi_Swarm_Handheld_Controller by
alpha433.cpp@0:d63a63feb104, 2014-06-10 (annotated)
- Committer:
- jah128
- Date:
- Tue Jun 10 11:05:23 2014 +0000
- Revision:
- 0:d63a63feb104
Handheld controller for Pi Swarm (old code)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jah128 | 0:d63a63feb104 | 1 | /* University of York Robot Lab m3pi Library: 433MHz Alpha Transceiver |
jah128 | 0:d63a63feb104 | 2 | * |
jah128 | 0:d63a63feb104 | 3 | * (C) Dr James Hilder, Dept. Electronics & Computer Science, University of York |
jah128 | 0:d63a63feb104 | 4 | * |
jah128 | 0:d63a63feb104 | 5 | * October 2013 |
jah128 | 0:d63a63feb104 | 6 | * |
jah128 | 0:d63a63feb104 | 7 | * Designed for use with the enhanced MBED sensor board |
jah128 | 0:d63a63feb104 | 8 | * |
jah128 | 0:d63a63feb104 | 9 | * Based on code developed by Tobias Dipper, University of Stuttgart |
jah128 | 0:d63a63feb104 | 10 | * |
jah128 | 0:d63a63feb104 | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
jah128 | 0:d63a63feb104 | 12 | * of this software and associated documentation files (the "Software"), to deal |
jah128 | 0:d63a63feb104 | 13 | * in the Software without restriction, including without limitation the rights |
jah128 | 0:d63a63feb104 | 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jah128 | 0:d63a63feb104 | 15 | * copies of the Software, and to permit persons to whom the Software is |
jah128 | 0:d63a63feb104 | 16 | * furnished to do so, subject to the following conditions: |
jah128 | 0:d63a63feb104 | 17 | * |
jah128 | 0:d63a63feb104 | 18 | * The above copyright notice and this permission notice shall be included in |
jah128 | 0:d63a63feb104 | 19 | * all copies or substantial portions of the Software. |
jah128 | 0:d63a63feb104 | 20 | * |
jah128 | 0:d63a63feb104 | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jah128 | 0:d63a63feb104 | 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jah128 | 0:d63a63feb104 | 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jah128 | 0:d63a63feb104 | 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jah128 | 0:d63a63feb104 | 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jah128 | 0:d63a63feb104 | 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
jah128 | 0:d63a63feb104 | 27 | * THE SOFTWARE. |
jah128 | 0:d63a63feb104 | 28 | */ |
jah128 | 0:d63a63feb104 | 29 | |
jah128 | 0:d63a63feb104 | 30 | #include "mbed.h" |
jah128 | 0:d63a63feb104 | 31 | #include "alpha433.h" |
jah128 | 0:d63a63feb104 | 32 | #include "main.h" |
jah128 | 0:d63a63feb104 | 33 | |
jah128 | 0:d63a63feb104 | 34 | // Variables |
jah128 | 0:d63a63feb104 | 35 | |
jah128 | 0:d63a63feb104 | 36 | //Serial pc(USBTX, USBRX); |
jah128 | 0:d63a63feb104 | 37 | //DigitalOut tx_led(LED2); |
jah128 | 0:d63a63feb104 | 38 | //DigitalOut rx_led(LED3); |
jah128 | 0:d63a63feb104 | 39 | DigitalOut irqled(LED4); |
jah128 | 0:d63a63feb104 | 40 | Timeout reset_timeout; |
jah128 | 0:d63a63feb104 | 41 | |
jah128 | 0:d63a63feb104 | 42 | |
jah128 | 0:d63a63feb104 | 43 | char cRFStatus = 0; |
jah128 | 0:d63a63feb104 | 44 | |
jah128 | 0:d63a63feb104 | 45 | signed short ssTransmitCount = 0; |
jah128 | 0:d63a63feb104 | 46 | signed short ssTransmitPointer = 0; |
jah128 | 0:d63a63feb104 | 47 | char cTXBuffer[64]; |
jah128 | 0:d63a63feb104 | 48 | |
jah128 | 0:d63a63feb104 | 49 | signed short ssReceiveCount = 0; |
jah128 | 0:d63a63feb104 | 50 | signed short ssReceivePointer = 0; |
jah128 | 0:d63a63feb104 | 51 | char cRXBuffer[64]; |
jah128 | 0:d63a63feb104 | 52 | |
jah128 | 0:d63a63feb104 | 53 | char cDataAvailable = 0; |
jah128 | 0:d63a63feb104 | 54 | |
jah128 | 0:d63a63feb104 | 55 | Alpha433::Alpha433(PinName mosi, PinName miso, PinName sck, PinName fss, PinName nirq) : Stream("alpha433"), _spi(mosi,miso,sck), _fss(fss), _nirq_test(nirq), _nirq(nirq) { |
jah128 | 0:d63a63feb104 | 56 | |
jah128 | 0:d63a63feb104 | 57 | } |
jah128 | 0:d63a63feb104 | 58 | |
jah128 | 0:d63a63feb104 | 59 | Alpha433::Alpha433() : Stream("alpha433"), _spi(p5,p6,p7), _fss(p8), _nirq_test(p11), _nirq(p11) { |
jah128 | 0:d63a63feb104 | 60 | |
jah128 | 0:d63a63feb104 | 61 | } |
jah128 | 0:d63a63feb104 | 62 | |
jah128 | 0:d63a63feb104 | 63 | |
jah128 | 0:d63a63feb104 | 64 | |
jah128 | 0:d63a63feb104 | 65 | |
jah128 | 0:d63a63feb104 | 66 | // RF Send Data |
jah128 | 0:d63a63feb104 | 67 | // |
jah128 | 0:d63a63feb104 | 68 | // Eg: |
jah128 | 0:d63a63feb104 | 69 | // unsigned char message[32]; |
jah128 | 0:d63a63feb104 | 70 | // unsigned char count; |
jah128 | 0:d63a63feb104 | 71 | // count = snprintf(message, 32, "Hello: %i", 42); |
jah128 | 0:d63a63feb104 | 72 | // sendString(count, message); |
jah128 | 0:d63a63feb104 | 73 | unsigned long Alpha433::sendString(char cCount, char* cBuffer) |
jah128 | 0:d63a63feb104 | 74 | { |
jah128 | 0:d63a63feb104 | 75 | //pc.printf("SendString called"); |
jah128 | 0:d63a63feb104 | 76 | char i = 0; |
jah128 | 0:d63a63feb104 | 77 | if(cRFStatus == ALPHA433_MODE_TRANSMITTING) {// RF already transmitting |
jah128 | 0:d63a63feb104 | 78 | pc.printf("Error: Already transmitting\n"); |
jah128 | 0:d63a63feb104 | 79 | |
jah128 | 0:d63a63feb104 | 80 | return 1; // Error |
jah128 | 0:d63a63feb104 | 81 | |
jah128 | 0:d63a63feb104 | 82 | } |
jah128 | 0:d63a63feb104 | 83 | |
jah128 | 0:d63a63feb104 | 84 | if(cCount > 62) {// Amount of data to high |
jah128 | 0:d63a63feb104 | 85 | pc.printf("Error: Too much tx data\n"); |
jah128 | 0:d63a63feb104 | 86 | |
jah128 | 0:d63a63feb104 | 87 | return 2; // Error |
jah128 | 0:d63a63feb104 | 88 | |
jah128 | 0:d63a63feb104 | 89 | } |
jah128 | 0:d63a63feb104 | 90 | if(cCount == 0) {// No Data |
jah128 | 0:d63a63feb104 | 91 | pc.printf("Error: No tx data\n"); |
jah128 | 0:d63a63feb104 | 92 | return 3; // Error |
jah128 | 0:d63a63feb104 | 93 | } |
jah128 | 0:d63a63feb104 | 94 | cTXBuffer[i] = cCount; |
jah128 | 0:d63a63feb104 | 95 | |
jah128 | 0:d63a63feb104 | 96 | unsigned char checksum_byte = 0; |
jah128 | 0:d63a63feb104 | 97 | for(i=0; i<cCount; i++) {// make a copy |
jah128 | 0:d63a63feb104 | 98 | cTXBuffer[i+1] = cBuffer[i]; |
jah128 | 0:d63a63feb104 | 99 | checksum_byte ^= cBuffer[i]; |
jah128 | 0:d63a63feb104 | 100 | } |
jah128 | 0:d63a63feb104 | 101 | cTXBuffer[cCount+1] = checksum_byte; |
jah128 | 0:d63a63feb104 | 102 | //pc.printf("Message: \"%s\" Checksum: %2X\n",cBuffer,checksum_byte); |
jah128 | 0:d63a63feb104 | 103 | ssTransmitCount = cCount+3; // add count and checksum |
jah128 | 0:d63a63feb104 | 104 | ssTransmitPointer = -6; |
jah128 | 0:d63a63feb104 | 105 | cRFStatus = ALPHA433_MODE_SWITCHING; |
jah128 | 0:d63a63feb104 | 106 | disableReceiver(); |
jah128 | 0:d63a63feb104 | 107 | enableTransmitter(); |
jah128 | 0:d63a63feb104 | 108 | cRFStatus = ALPHA433_MODE_TRANSMITTING; |
jah128 | 0:d63a63feb104 | 109 | |
jah128 | 0:d63a63feb104 | 110 | //pc.printf("Transmitting %d bytes\n",ssTransmitCount); |
jah128 | 0:d63a63feb104 | 111 | |
jah128 | 0:d63a63feb104 | 112 | while(ssTransmitPointer <= ssTransmitCount){ |
jah128 | 0:d63a63feb104 | 113 | while(_nirq_test); |
jah128 | 0:d63a63feb104 | 114 | |
jah128 | 0:d63a63feb104 | 115 | if(ssTransmitPointer < -2) |
jah128 | 0:d63a63feb104 | 116 | _write(0xB8AA); // send sync |
jah128 | 0:d63a63feb104 | 117 | else if(ssTransmitPointer == -2) |
jah128 | 0:d63a63feb104 | 118 | _write(0xB82D); // send first part of the fifo pattern; |
jah128 | 0:d63a63feb104 | 119 | else if(ssTransmitPointer == -1) |
jah128 | 0:d63a63feb104 | 120 | _write(0xB8D4); // send second part of the fifo pattern; |
jah128 | 0:d63a63feb104 | 121 | else if(ssTransmitPointer == ssTransmitCount) |
jah128 | 0:d63a63feb104 | 122 | _write(0xB800); // send dummy byte |
jah128 | 0:d63a63feb104 | 123 | else |
jah128 | 0:d63a63feb104 | 124 | _write(0xB800 | cTXBuffer[ssTransmitPointer]); // send data |
jah128 | 0:d63a63feb104 | 125 | ssTransmitPointer++; |
jah128 | 0:d63a63feb104 | 126 | } |
jah128 | 0:d63a63feb104 | 127 | |
jah128 | 0:d63a63feb104 | 128 | _write(0xB800); // send dummy byte, maybe redundant |
jah128 | 0:d63a63feb104 | 129 | disableTransmitter(); |
jah128 | 0:d63a63feb104 | 130 | enableReceiver(); |
jah128 | 0:d63a63feb104 | 131 | ssReceivePointer = 0; |
jah128 | 0:d63a63feb104 | 132 | cRFStatus = ALPHA433_MODE_RECEIVING; |
jah128 | 0:d63a63feb104 | 133 | return 0; |
jah128 | 0:d63a63feb104 | 134 | } |
jah128 | 0:d63a63feb104 | 135 | |
jah128 | 0:d63a63feb104 | 136 | // Enable RF Transmitter |
jah128 | 0:d63a63feb104 | 137 | void Alpha433::enableTransmitter(void) |
jah128 | 0:d63a63feb104 | 138 | { |
jah128 | 0:d63a63feb104 | 139 | //pc.printf("Enable TX\n"); |
jah128 | 0:d63a63feb104 | 140 | //RFCommand(0x8229); |
jah128 | 0:d63a63feb104 | 141 | _write(0x8229); |
jah128 | 0:d63a63feb104 | 142 | //tx_led = 1; |
jah128 | 0:d63a63feb104 | 143 | } |
jah128 | 0:d63a63feb104 | 144 | |
jah128 | 0:d63a63feb104 | 145 | // Disable RF Transmitter |
jah128 | 0:d63a63feb104 | 146 | void Alpha433::disableTransmitter(void) |
jah128 | 0:d63a63feb104 | 147 | { |
jah128 | 0:d63a63feb104 | 148 | //pc.printf("Disable TX\n"); |
jah128 | 0:d63a63feb104 | 149 | //RFCommand(0x8209); |
jah128 | 0:d63a63feb104 | 150 | _write(0x8209); |
jah128 | 0:d63a63feb104 | 151 | // tx_led = 0; |
jah128 | 0:d63a63feb104 | 152 | |
jah128 | 0:d63a63feb104 | 153 | } |
jah128 | 0:d63a63feb104 | 154 | |
jah128 | 0:d63a63feb104 | 155 | |
jah128 | 0:d63a63feb104 | 156 | // Enable RF Receiver |
jah128 | 0:d63a63feb104 | 157 | void Alpha433::enableReceiver(void) |
jah128 | 0:d63a63feb104 | 158 | { |
jah128 | 0:d63a63feb104 | 159 | //pc.printf("Enable RX\n"); |
jah128 | 0:d63a63feb104 | 160 | //RFCommand(0x8288); |
jah128 | 0:d63a63feb104 | 161 | _write(0x8288); |
jah128 | 0:d63a63feb104 | 162 | // rx_led = 1; |
jah128 | 0:d63a63feb104 | 163 | enableFifoFill(); |
jah128 | 0:d63a63feb104 | 164 | } |
jah128 | 0:d63a63feb104 | 165 | |
jah128 | 0:d63a63feb104 | 166 | // Disable RF Receiver |
jah128 | 0:d63a63feb104 | 167 | void Alpha433::disableReceiver(void) |
jah128 | 0:d63a63feb104 | 168 | { |
jah128 | 0:d63a63feb104 | 169 | //pc.printf("Disable RX\n"); |
jah128 | 0:d63a63feb104 | 170 | //RFCommand(0x8208); |
jah128 | 0:d63a63feb104 | 171 | _write(0x8208); |
jah128 | 0:d63a63feb104 | 172 | // rx_led = 0; |
jah128 | 0:d63a63feb104 | 173 | disableFifoFill(); |
jah128 | 0:d63a63feb104 | 174 | } |
jah128 | 0:d63a63feb104 | 175 | |
jah128 | 0:d63a63feb104 | 176 | // SSI FiFo Clear |
jah128 | 0:d63a63feb104 | 177 | void Alpha433::clearBuffer(void) |
jah128 | 0:d63a63feb104 | 178 | { |
jah128 | 0:d63a63feb104 | 179 | while(_read(0xB000) != 0); |
jah128 | 0:d63a63feb104 | 180 | } |
jah128 | 0:d63a63feb104 | 181 | |
jah128 | 0:d63a63feb104 | 182 | // Reset RF |
jah128 | 0:d63a63feb104 | 183 | void Alpha433::rf_reset(void) |
jah128 | 0:d63a63feb104 | 184 | { |
jah128 | 0:d63a63feb104 | 185 | // Chip must be deselected |
jah128 | 0:d63a63feb104 | 186 | _fss = 1; |
jah128 | 0:d63a63feb104 | 187 | |
jah128 | 0:d63a63feb104 | 188 | // Setup the spi for 16 bit data, high steady state clock, second edge capture, with a 1MHz clock rate |
jah128 | 0:d63a63feb104 | 189 | _spi.format(16,0); //Was 16,3 |
jah128 | 0:d63a63feb104 | 190 | _spi.frequency(2000000); |
jah128 | 0:d63a63feb104 | 191 | _nirq.mode(PullUp); |
jah128 | 0:d63a63feb104 | 192 | _nirq.fall(this,&Alpha433::interrupt); |
jah128 | 0:d63a63feb104 | 193 | // Select the device by seting chip select low |
jah128 | 0:d63a63feb104 | 194 | _fss = 0; |
jah128 | 0:d63a63feb104 | 195 | //pc.printf("End reset\n"); |
jah128 | 0:d63a63feb104 | 196 | |
jah128 | 0:d63a63feb104 | 197 | } |
jah128 | 0:d63a63feb104 | 198 | |
jah128 | 0:d63a63feb104 | 199 | void Alpha433::timeout(void) |
jah128 | 0:d63a63feb104 | 200 | { |
jah128 | 0:d63a63feb104 | 201 | pc.printf("Error on read; resetting chip\n"); |
jah128 | 0:d63a63feb104 | 202 | rf_init(); |
jah128 | 0:d63a63feb104 | 203 | } |
jah128 | 0:d63a63feb104 | 204 | |
jah128 | 0:d63a63feb104 | 205 | // Initialise RF |
jah128 | 0:d63a63feb104 | 206 | void Alpha433::rf_init(void) |
jah128 | 0:d63a63feb104 | 207 | { |
jah128 | 0:d63a63feb104 | 208 | |
jah128 | 0:d63a63feb104 | 209 | pc.printf("Init start\n"); |
jah128 | 0:d63a63feb104 | 210 | |
jah128 | 0:d63a63feb104 | 211 | rf_reset(); // RF Hardware Reset |
jah128 | 0:d63a63feb104 | 212 | _write(0x0000); // read status to cancel prior interrupt |
jah128 | 0:d63a63feb104 | 213 | pc.printf("Start setup\n"); |
jah128 | 0:d63a63feb104 | 214 | |
jah128 | 0:d63a63feb104 | 215 | _write(0x8000 | ALPHA433_FREQUENCY | ALPHA433_CRYSTAL_LOAD | ALPHA433_USE_FIFO); |
jah128 | 0:d63a63feb104 | 216 | _write(0x9000 | ALPHA433_PIN20 | ALPHA433_VDI_RESPONSE | ALPHA433_BANDWIDTH | ALPHA433_LNA_GAIN | ALPHA433_RSSI); |
jah128 | 0:d63a63feb104 | 217 | _write(0xC228 | ALPHA433_CLOCK_RECOVERY | ALPHA433_FILTER | ALPHA433_DQD); |
jah128 | 0:d63a63feb104 | 218 | _write(0xCA00 | ALPHA433_FIFO_LEVEL | ALPHA433_FIFO_FILL | ALPHA433_HI_SENS_RESET); |
jah128 | 0:d63a63feb104 | 219 | _write(0xC400 | ALPHA433_AFC_MODE | ALPHA433_AFC_RANGE | ALPHA433_AFC_FINE_MODE | ALPHA433_AFC); |
jah128 | 0:d63a63feb104 | 220 | _write(0x9800 | ALPHA433_MOD_POLARITY | ALPHA433_MOD_FREQUENCY | ALPHA433_TX_POWER); |
jah128 | 0:d63a63feb104 | 221 | _write(0xC000 | ALPHA433_CLK_OUT | ALPHA433_LOW_BAT); |
jah128 | 0:d63a63feb104 | 222 | |
jah128 | 0:d63a63feb104 | 223 | enableReceiver(); |
jah128 | 0:d63a63feb104 | 224 | //pc.printf("End setup\n"); |
jah128 | 0:d63a63feb104 | 225 | |
jah128 | 0:d63a63feb104 | 226 | ssReceivePointer = 0; |
jah128 | 0:d63a63feb104 | 227 | reset_timeout.attach(this,&Alpha433::timeout,TIMEOUT); |
jah128 | 0:d63a63feb104 | 228 | pc.printf("Init end\n"); |
jah128 | 0:d63a63feb104 | 229 | cRFStatus = ALPHA433_MODE_RECEIVING; |
jah128 | 0:d63a63feb104 | 230 | |
jah128 | 0:d63a63feb104 | 231 | } |
jah128 | 0:d63a63feb104 | 232 | |
jah128 | 0:d63a63feb104 | 233 | |
jah128 | 0:d63a63feb104 | 234 | // RF Interrupt |
jah128 | 0:d63a63feb104 | 235 | void Alpha433::interrupt(void) |
jah128 | 0:d63a63feb104 | 236 | { |
jah128 | 0:d63a63feb104 | 237 | if(cRFStatus == ALPHA433_MODE_RECEIVING){ |
jah128 | 0:d63a63feb104 | 238 | irqled=1; |
jah128 | 0:d63a63feb104 | 239 | //Add reset timeout |
jah128 | 0:d63a63feb104 | 240 | reset_timeout.detach(); |
jah128 | 0:d63a63feb104 | 241 | reset_timeout.attach(this,&Alpha433::timeout,0.5); |
jah128 | 0:d63a63feb104 | 242 | pc.printf("Rec. ISR\n"); |
jah128 | 0:d63a63feb104 | 243 | int res = _read(0x0000); |
jah128 | 0:d63a63feb104 | 244 | if(res==0) res = _read(0x0000); |
jah128 | 0:d63a63feb104 | 245 | char read_failure = 0; |
jah128 | 0:d63a63feb104 | 246 | |
jah128 | 0:d63a63feb104 | 247 | if (res & (ALPHA433_STATUS_TX_NEXT_BYTE | ALPHA433_STATUS_FIFO_LIMIT_REACHED)) { // RF: waiting for next Byte OR FIFO full |
jah128 | 0:d63a63feb104 | 248 | pc.printf("Receiving"); |
jah128 | 0:d63a63feb104 | 249 | cRXBuffer[ssReceivePointer] = _read(0xB000) & 0xFF; // get data |
jah128 | 0:d63a63feb104 | 250 | if(ssReceivePointer == 0) { |
jah128 | 0:d63a63feb104 | 251 | ssReceiveCount = cRXBuffer[0]; |
jah128 | 0:d63a63feb104 | 252 | |
jah128 | 0:d63a63feb104 | 253 | if((ssReceiveCount == 0) || (ssReceiveCount > 62)) { // error amount of data |
jah128 | 0:d63a63feb104 | 254 | read_failure=1; |
jah128 | 0:d63a63feb104 | 255 | pc.printf("Error amount of RX data: %d\n",ssReceiveCount); |
jah128 | 0:d63a63feb104 | 256 | reset_timeout.detach(); |
jah128 | 0:d63a63feb104 | 257 | reset_timeout.attach(this,&Alpha433::timeout,TIMEOUT); |
jah128 | 0:d63a63feb104 | 258 | } else ssReceiveCount += 2; // add count + checksum |
jah128 | 0:d63a63feb104 | 259 | } |
jah128 | 0:d63a63feb104 | 260 | if(!read_failure){ |
jah128 | 0:d63a63feb104 | 261 | ssReceivePointer++; |
jah128 | 0:d63a63feb104 | 262 | if (ssReceivePointer > ssReceiveCount) { // End transmission |
jah128 | 0:d63a63feb104 | 263 | disableFifoFill(); |
jah128 | 0:d63a63feb104 | 264 | enableFifoFill(); |
jah128 | 0:d63a63feb104 | 265 | irqled=0; |
jah128 | 0:d63a63feb104 | 266 | reset_timeout.detach(); |
jah128 | 0:d63a63feb104 | 267 | reset_timeout.attach(this,&Alpha433::timeout,TIMEOUT); |
jah128 | 0:d63a63feb104 | 268 | ssReceivePointer = 0; |
jah128 | 0:d63a63feb104 | 269 | dataAvailable(cRXBuffer[0], &cRXBuffer[1]); |
jah128 | 0:d63a63feb104 | 270 | } |
jah128 | 0:d63a63feb104 | 271 | }else{ |
jah128 | 0:d63a63feb104 | 272 | disableFifoFill(); |
jah128 | 0:d63a63feb104 | 273 | enableFifoFill(); |
jah128 | 0:d63a63feb104 | 274 | ssReceivePointer = 0; |
jah128 | 0:d63a63feb104 | 275 | reset_timeout.detach(); |
jah128 | 0:d63a63feb104 | 276 | reset_timeout.attach(this,&Alpha433::timeout,TIMEOUT); |
jah128 | 0:d63a63feb104 | 277 | } |
jah128 | 0:d63a63feb104 | 278 | } |
jah128 | 0:d63a63feb104 | 279 | } |
jah128 | 0:d63a63feb104 | 280 | } |
jah128 | 0:d63a63feb104 | 281 | |
jah128 | 0:d63a63feb104 | 282 | // RF Set Datarate |
jah128 | 0:d63a63feb104 | 283 | void Alpha433::setDatarate(unsigned long ulValue) |
jah128 | 0:d63a63feb104 | 284 | { |
jah128 | 0:d63a63feb104 | 285 | unsigned long ulRateCmd; |
jah128 | 0:d63a63feb104 | 286 | if(ulValue < 3000) ulRateCmd = 0x0080 | (10000000 / 29 / 8 / ulValue) - 1; |
jah128 | 0:d63a63feb104 | 287 | else ulRateCmd = 0x0000 | (10000000 / 29 / 1 / ulValue) - 1; |
jah128 | 0:d63a63feb104 | 288 | _write(0xC600 | ulRateCmd); |
jah128 | 0:d63a63feb104 | 289 | } |
jah128 | 0:d63a63feb104 | 290 | |
jah128 | 0:d63a63feb104 | 291 | // RF Set Frequency |
jah128 | 0:d63a63feb104 | 292 | void Alpha433::setFrequency(unsigned long ulValue) |
jah128 | 0:d63a63feb104 | 293 | { |
jah128 | 0:d63a63feb104 | 294 | unsigned long ulRateCmd; |
jah128 | 0:d63a63feb104 | 295 | |
jah128 | 0:d63a63feb104 | 296 | #if (ALPHA433_FREQUENCY == ALPHA433_FREQUENCY_315) |
jah128 | 0:d63a63feb104 | 297 | ulRateCmd = (ulValue - 10000000 * 1 * 31) * 4 / 10000; |
jah128 | 0:d63a63feb104 | 298 | |
jah128 | 0:d63a63feb104 | 299 | #elif (ALPHA433_FREQUENCY == ALPHA433_FREQUENCY_433) |
jah128 | 0:d63a63feb104 | 300 | ulRateCmd = (ulValue - 10000000 * 1 * 43) * 4 / 10000; |
jah128 | 0:d63a63feb104 | 301 | |
jah128 | 0:d63a63feb104 | 302 | #elif (ALPHA433_FREQUENCY == ALPHA433_FREQUENCY_868) |
jah128 | 0:d63a63feb104 | 303 | ulRateCmd = (ulValue - 10000000 * 2 * 43) * 4 / 10000; |
jah128 | 0:d63a63feb104 | 304 | |
jah128 | 0:d63a63feb104 | 305 | #elif (ALPHA433_FREQUENCY == ALPHA433_FREQUENCY_915) |
jah128 | 0:d63a63feb104 | 306 | ulRateCmd = (ulValue - 10000000 * 3 * 30) * 4 / 10000; |
jah128 | 0:d63a63feb104 | 307 | #endif |
jah128 | 0:d63a63feb104 | 308 | |
jah128 | 0:d63a63feb104 | 309 | _write(0xA000 | ulRateCmd); |
jah128 | 0:d63a63feb104 | 310 | } |
jah128 | 0:d63a63feb104 | 311 | |
jah128 | 0:d63a63feb104 | 312 | |
jah128 | 0:d63a63feb104 | 313 | |
jah128 | 0:d63a63feb104 | 314 | // Enable RF Receiver FiFo fill |
jah128 | 0:d63a63feb104 | 315 | void Alpha433::enableFifoFill(void) |
jah128 | 0:d63a63feb104 | 316 | { |
jah128 | 0:d63a63feb104 | 317 | _write(0xCA00 | ALPHA433_FIFO_LEVEL | ALPHA433_FIFO_FILL | ALPHA433_HI_SENS_RESET | 0x0002); |
jah128 | 0:d63a63feb104 | 318 | while((_read(0x0000) & ALPHA433_STATUS_FIFO_EMPTY) == 0); |
jah128 | 0:d63a63feb104 | 319 | } |
jah128 | 0:d63a63feb104 | 320 | |
jah128 | 0:d63a63feb104 | 321 | // Disable RF Receiver FiFo fill |
jah128 | 0:d63a63feb104 | 322 | void Alpha433::disableFifoFill(void) |
jah128 | 0:d63a63feb104 | 323 | { |
jah128 | 0:d63a63feb104 | 324 | _write(0xCA00 | ALPHA433_FIFO_LEVEL | ALPHA433_FIFO_FILL | ALPHA433_HI_SENS_RESET); |
jah128 | 0:d63a63feb104 | 325 | } |
jah128 | 0:d63a63feb104 | 326 | |
jah128 | 0:d63a63feb104 | 327 | // Handle new RF Data |
jah128 | 0:d63a63feb104 | 328 | void Alpha433::dataAvailable(char cCount, char* cBuffer) |
jah128 | 0:d63a63feb104 | 329 | { |
jah128 | 0:d63a63feb104 | 330 | char rstring [cCount+1]; |
jah128 | 0:d63a63feb104 | 331 | char checksum = 0; |
jah128 | 0:d63a63feb104 | 332 | int i; |
jah128 | 0:d63a63feb104 | 333 | for(i=0;i<cCount;i++){ |
jah128 | 0:d63a63feb104 | 334 | rstring[i]=cBuffer[i]; |
jah128 | 0:d63a63feb104 | 335 | checksum ^= rstring[i]; |
jah128 | 0:d63a63feb104 | 336 | } |
jah128 | 0:d63a63feb104 | 337 | rstring[cCount]=0; |
jah128 | 0:d63a63feb104 | 338 | if (cBuffer[cCount] != checksum){ |
jah128 | 0:d63a63feb104 | 339 | pc.printf("Received [%d] \"%s\" (checksum failed: expected %02X, received %02X)%02X %02X\n",cCount,rstring,checksum,cBuffer[cCount],cBuffer[cCount-1],cBuffer[cCount+1]); |
jah128 | 0:d63a63feb104 | 340 | }else { |
jah128 | 0:d63a63feb104 | 341 | pc.printf("Received [%d] \"%s\" (checksum passed)\n",cCount,rstring); |
jah128 | 0:d63a63feb104 | 342 | handleData(rstring, cCount); |
jah128 | 0:d63a63feb104 | 343 | } |
jah128 | 0:d63a63feb104 | 344 | } |
jah128 | 0:d63a63feb104 | 345 | |
jah128 | 0:d63a63feb104 | 346 | |
jah128 | 0:d63a63feb104 | 347 | int Alpha433::readStatusByte() |
jah128 | 0:d63a63feb104 | 348 | { |
jah128 | 0:d63a63feb104 | 349 | pc.printf("Reading status byte\n"); |
jah128 | 0:d63a63feb104 | 350 | return _read(0x0000); |
jah128 | 0:d63a63feb104 | 351 | } |
jah128 | 0:d63a63feb104 | 352 | |
jah128 | 0:d63a63feb104 | 353 | //-----PRIVATE FUNCTIONS----- |
jah128 | 0:d63a63feb104 | 354 | |
jah128 | 0:d63a63feb104 | 355 | void Alpha433::_write(int address) { |
jah128 | 0:d63a63feb104 | 356 | _fss=0; //select the deivce |
jah128 | 0:d63a63feb104 | 357 | _spi.write(address); //write the address of where the data is to be written first |
jah128 | 0:d63a63feb104 | 358 | //pc.printf("Write data: %04X\n",address); |
jah128 | 0:d63a63feb104 | 359 | _fss=1; //deselect the device |
jah128 | 0:d63a63feb104 | 360 | } |
jah128 | 0:d63a63feb104 | 361 | |
jah128 | 0:d63a63feb104 | 362 | int Alpha433::_read(int address) { |
jah128 | 0:d63a63feb104 | 363 | int _data; |
jah128 | 0:d63a63feb104 | 364 | _fss=0; //select the device |
jah128 | 0:d63a63feb104 | 365 | _data = _spi.write(address); //select the register |
jah128 | 0:d63a63feb104 | 366 | //pc.printf("Read data: %04X\n",_data); |
jah128 | 0:d63a63feb104 | 367 | _fss=1; //deselect the device |
jah128 | 0:d63a63feb104 | 368 | return _data; //return the data |
jah128 | 0:d63a63feb104 | 369 | |
jah128 | 0:d63a63feb104 | 370 | } |
jah128 | 0:d63a63feb104 | 371 | |
jah128 | 0:d63a63feb104 | 372 | int Alpha433::_putc (int c) { |
jah128 | 0:d63a63feb104 | 373 | return(c); |
jah128 | 0:d63a63feb104 | 374 | } |
jah128 | 0:d63a63feb104 | 375 | |
jah128 | 0:d63a63feb104 | 376 | int Alpha433::_getc (void) { |
jah128 | 0:d63a63feb104 | 377 | char r = 0; |
jah128 | 0:d63a63feb104 | 378 | return(r); |
jah128 | 0:d63a63feb104 | 379 | } |