ENEL400 / AlohaTransceiver

Dependencies:   L2Frame crc

Dependents:   LoRaBaseStation LoRaTerminal

Committer:
rba90
Date:
Fri Aug 05 03:07:43 2016 +0000
Revision:
15:0dc9df48687a
Parent:
11:3e1ff29da71a
Child:
17:c6e2e2cd6e5f
add more api interface

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 0:e2ccabf3f30c 1 #include "AlohaTransceiver.h"
rba90 0:e2ccabf3f30c 2 #include "mbed.h"
rba90 0:e2ccabf3f30c 3 #include "radio.h"
rba90 0:e2ccabf3f30c 4 #include "AlohaFrame.h"
rba90 15:0dc9df48687a 5 #include "RingBuffer.h"
rba90 0:e2ccabf3f30c 6
rba90 0:e2ccabf3f30c 7 // declear the type of radio state
rba90 0:e2ccabf3f30c 8 typedef enum
rba90 0:e2ccabf3f30c 9 {
rba90 0:e2ccabf3f30c 10 LOWPOWER = 0,
rba90 0:e2ccabf3f30c 11 IDLE,
rba90 0:e2ccabf3f30c 12
rba90 0:e2ccabf3f30c 13 RX,
rba90 0:e2ccabf3f30c 14 RX_TIMEOUT,
rba90 0:e2ccabf3f30c 15 RX_ERROR,
rba90 0:e2ccabf3f30c 16
rba90 0:e2ccabf3f30c 17 TX,
rba90 0:e2ccabf3f30c 18 TX_TIMEOUT,
rba90 0:e2ccabf3f30c 19
rba90 0:e2ccabf3f30c 20 CAD,
rba90 0:e2ccabf3f30c 21 CAD_DONE
rba90 0:e2ccabf3f30c 22 }AppStates_t;
rba90 0:e2ccabf3f30c 23
rba90 0:e2ccabf3f30c 24 // radio driver related variables
rba90 0:e2ccabf3f30c 25 static uint16_t BufferSize;
rba90 0:e2ccabf3f30c 26 static uint8_t Buffer[BUFFER_SIZE];
rba90 0:e2ccabf3f30c 27
rba90 0:e2ccabf3f30c 28 static int16_t RssiValue;
rba90 0:e2ccabf3f30c 29 static int8_t SnrValue;
rba90 0:e2ccabf3f30c 30
rba90 0:e2ccabf3f30c 31 static volatile AppStates_t State;
rba90 0:e2ccabf3f30c 32 static RadioEvents_t RadioEvents;
rba90 0:e2ccabf3f30c 33
rba90 0:e2ccabf3f30c 34 // callback functions for radio driver
rba90 0:e2ccabf3f30c 35 void OnTxDone();
rba90 0:e2ccabf3f30c 36 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
rba90 0:e2ccabf3f30c 37 void OnTxTimeout();
rba90 0:e2ccabf3f30c 38 void OnRxTimeout();
rba90 0:e2ccabf3f30c 39 void OnRxError();
rba90 0:e2ccabf3f30c 40 void OnFhssChangeChannel( uint8_t channelIndex );
rba90 0:e2ccabf3f30c 41 void OnCadDone();
rba90 0:e2ccabf3f30c 42
rba90 0:e2ccabf3f30c 43 // radio driver
rba90 0:e2ccabf3f30c 44 #ifdef BUILD_FOR_BS
rba90 0:e2ccabf3f30c 45 SX1276MB1xAS Radio( NULL );
rba90 0:e2ccabf3f30c 46 #endif
rba90 0:e2ccabf3f30c 47
rba90 0:e2ccabf3f30c 48 #ifdef BUILD_FOR_TERMINAL
rba90 0:e2ccabf3f30c 49 SX1276inAir Radio( NULL );
rba90 0:e2ccabf3f30c 50 #endif
rba90 0:e2ccabf3f30c 51
rba90 0:e2ccabf3f30c 52
rba90 0:e2ccabf3f30c 53 /*
rba90 0:e2ccabf3f30c 54 * Abstract interface for accessing radio driver
rba90 0:e2ccabf3f30c 55 */
rba90 0:e2ccabf3f30c 56
rba90 11:3e1ff29da71a 57 AlohaTransceiver::AlohaTransceiver(uint8_t id)
rba90 0:e2ccabf3f30c 58 {
rba90 11:3e1ff29da71a 59 // store unique device id
rba90 11:3e1ff29da71a 60 deviceId = id;
rba90 11:3e1ff29da71a 61
rba90 0:e2ccabf3f30c 62 // configure properties
rba90 0:e2ccabf3f30c 63 #if USE_MODEM_LORA == 1
rba90 0:e2ccabf3f30c 64 Settings.Power = TX_OUTPUT_POWER;
rba90 0:e2ccabf3f30c 65 Settings.Bandwidth = LORA_BANDWIDTH;
rba90 0:e2ccabf3f30c 66 Settings.Datarate = LORA_SPREADING_FACTOR;
rba90 0:e2ccabf3f30c 67 Settings.Coderate = LORA_CODINGRATE;
rba90 0:e2ccabf3f30c 68 Settings.PreambleLen = LORA_PREAMBLE_LENGTH;
rba90 0:e2ccabf3f30c 69 Settings.SymbolTimeout = LORA_SYMBOL_TIMEOUT;
rba90 0:e2ccabf3f30c 70 Settings.FixLen = LORA_FIX_LENGTH_PAYLOAD_ON;
rba90 0:e2ccabf3f30c 71 Settings.PayloadLen = 0;
rba90 0:e2ccabf3f30c 72 Settings.CrcOn = LORA_CRC_ENABLED;
rba90 0:e2ccabf3f30c 73 Settings.FreqHopOn = LORA_FHSS_ENABLED;
rba90 0:e2ccabf3f30c 74 Settings.HopPeriod = LORA_NB_SYMB_HOP;
rba90 0:e2ccabf3f30c 75 Settings.IqInverted = LORA_IQ_INVERSION_ON;
rba90 0:e2ccabf3f30c 76 Settings.RxContinuous = true;
rba90 0:e2ccabf3f30c 77 Settings.TxTimeout = TX_TIMEOUT_VALUE;
rba90 0:e2ccabf3f30c 78
rba90 0:e2ccabf3f30c 79 #elif USE_MODEM_FSK == 1
rba90 0:e2ccabf3f30c 80 // TODO: Complete settings for FSK mode
rba90 0:e2ccabf3f30c 81 #error "FSK not implemented"
rba90 0:e2ccabf3f30c 82 #else
rba90 0:e2ccabf3f30c 83 #error "Please define a modem in the compiler options."
rba90 0:e2ccabf3f30c 84 #endif
rba90 0:e2ccabf3f30c 85 }
rba90 0:e2ccabf3f30c 86
rba90 0:e2ccabf3f30c 87 AlohaTransceiver::~AlohaTransceiver()
rba90 0:e2ccabf3f30c 88 {
rba90 0:e2ccabf3f30c 89
rba90 0:e2ccabf3f30c 90 }
rba90 0:e2ccabf3f30c 91
rba90 6:f545f5aa7de3 92 void AlohaTransceiver::boardInit()
rba90 0:e2ccabf3f30c 93 {
rba90 0:e2ccabf3f30c 94 // configure callback functions
rba90 0:e2ccabf3f30c 95 RadioEvents.TxDone = OnTxDone;
rba90 0:e2ccabf3f30c 96 RadioEvents.RxDone = OnRxDone;
rba90 0:e2ccabf3f30c 97 RadioEvents.RxError = OnRxError;
rba90 0:e2ccabf3f30c 98 RadioEvents.TxTimeout = OnTxTimeout;
rba90 0:e2ccabf3f30c 99 RadioEvents.RxTimeout = OnRxTimeout;
rba90 0:e2ccabf3f30c 100 Radio.Init( &RadioEvents );
rba90 0:e2ccabf3f30c 101
rba90 0:e2ccabf3f30c 102 // verify the connection with the board
rba90 0:e2ccabf3f30c 103 while( Radio.Read( REG_VERSION ) == 0x00 )
rba90 0:e2ccabf3f30c 104 {
rba90 0:e2ccabf3f30c 105 printf( "Radio could not be detected!\n\r" );
rba90 0:e2ccabf3f30c 106 wait( 1 );
rba90 0:e2ccabf3f30c 107 }
rba90 0:e2ccabf3f30c 108 printf("RadioRegVersion: %d\r\n", Radio.Read( REG_VERSION ));
rba90 0:e2ccabf3f30c 109 }
rba90 0:e2ccabf3f30c 110
rba90 0:e2ccabf3f30c 111 void AlohaTransceiver::updateSettings()
rba90 0:e2ccabf3f30c 112 {
rba90 6:f545f5aa7de3 113 Radio.SetChannel( RF_FREQUENCY );
rba90 0:e2ccabf3f30c 114 #if USE_MODEM_LORA == 1
rba90 0:e2ccabf3f30c 115
rba90 0:e2ccabf3f30c 116 Radio.SetTxConfig( MODEM_LORA, Settings.Power, 0, Settings.Bandwidth,
rba90 0:e2ccabf3f30c 117 Settings.Datarate, Settings.Coderate,
rba90 0:e2ccabf3f30c 118 Settings.PreambleLen, Settings.FixLen,
rba90 0:e2ccabf3f30c 119 Settings.CrcOn, Settings.FreqHopOn, Settings.HopPeriod,
rba90 0:e2ccabf3f30c 120 Settings.IqInverted, Settings.TxTimeout );
rba90 0:e2ccabf3f30c 121
rba90 0:e2ccabf3f30c 122 Radio.SetRxConfig( MODEM_LORA, Settings.Bandwidth, Settings.Datarate,
rba90 0:e2ccabf3f30c 123 Settings.Coderate, 0, Settings.PreambleLen,
rba90 0:e2ccabf3f30c 124 Settings.SymbolTimeout, Settings.FixLen, Settings.PayloadLen,
rba90 0:e2ccabf3f30c 125 Settings.CrcOn, Settings.FreqHopOn, Settings.HopPeriod,
rba90 0:e2ccabf3f30c 126 Settings.IqInverted, Settings.RxContinuous );
rba90 0:e2ccabf3f30c 127
rba90 0:e2ccabf3f30c 128 #elif USE_MODEM_FSK == 1
rba90 0:e2ccabf3f30c 129 #error "FSK not implemented"
rba90 0:e2ccabf3f30c 130 #else
rba90 0:e2ccabf3f30c 131 #error "Please define a modem in the compiler options."
rba90 0:e2ccabf3f30c 132 #endif
rba90 0:e2ccabf3f30c 133 }
rba90 0:e2ccabf3f30c 134
rba90 6:f545f5aa7de3 135 void AlohaTransceiver::enable()
rba90 6:f545f5aa7de3 136 {
rba90 6:f545f5aa7de3 137 // entering passive receiver mode
rba90 10:065a4b58c6ff 138 Radio.Rx( 0 );
rba90 6:f545f5aa7de3 139 }
rba90 6:f545f5aa7de3 140
rba90 0:e2ccabf3f30c 141 void AlohaTransceiver::poll()
rba90 0:e2ccabf3f30c 142 {
rba90 0:e2ccabf3f30c 143 switch( State )
rba90 0:e2ccabf3f30c 144 {
rba90 0:e2ccabf3f30c 145 case RX:
rba90 5:c3741633dc6f 146 {
rba90 0:e2ccabf3f30c 147 // create new frame instance
rba90 0:e2ccabf3f30c 148 AlohaFrame frame(Buffer, BufferSize);
rba90 0:e2ccabf3f30c 149
rba90 11:3e1ff29da71a 150 // check destination
rba90 11:3e1ff29da71a 151 // if the destination is the device id, then processing, otherwise drop the packet and continue
rba90 11:3e1ff29da71a 152 // listening
rba90 11:3e1ff29da71a 153 if (frame.getDestinationAddress() == (deviceId & 0x0f))
rba90 0:e2ccabf3f30c 154 {
rba90 11:3e1ff29da71a 155 // check registered callback function
rba90 11:3e1ff29da71a 156 // execute callback functions if registered
rba90 11:3e1ff29da71a 157 if (AlohaTypeCallbackTable[frame.getType()] != NULL)
rba90 11:3e1ff29da71a 158 {
rba90 11:3e1ff29da71a 159 AlohaTypeCallbackTable[frame.getType()](&frame);
rba90 11:3e1ff29da71a 160 }
rba90 0:e2ccabf3f30c 161 }
rba90 0:e2ccabf3f30c 162
rba90 10:065a4b58c6ff 163 Radio.Rx( 0 );
rba90 0:e2ccabf3f30c 164 State = LOWPOWER;
rba90 0:e2ccabf3f30c 165 break;
rba90 0:e2ccabf3f30c 166 }
rba90 0:e2ccabf3f30c 167 case TX:
rba90 0:e2ccabf3f30c 168 {
rba90 10:065a4b58c6ff 169 Radio.Rx( 0 );
rba90 0:e2ccabf3f30c 170 State = LOWPOWER;
rba90 0:e2ccabf3f30c 171 break;
rba90 0:e2ccabf3f30c 172 }
rba90 0:e2ccabf3f30c 173 case RX_TIMEOUT:
rba90 0:e2ccabf3f30c 174 {
rba90 10:065a4b58c6ff 175 Radio.Rx( 0 );
rba90 0:e2ccabf3f30c 176 State = LOWPOWER;
rba90 0:e2ccabf3f30c 177 break;
rba90 0:e2ccabf3f30c 178 }
rba90 0:e2ccabf3f30c 179 case RX_ERROR:
rba90 0:e2ccabf3f30c 180 {
rba90 10:065a4b58c6ff 181 Radio.Rx( 0 );
rba90 0:e2ccabf3f30c 182 State = LOWPOWER;
rba90 0:e2ccabf3f30c 183 break;
rba90 0:e2ccabf3f30c 184 }
rba90 0:e2ccabf3f30c 185 case TX_TIMEOUT:
rba90 0:e2ccabf3f30c 186 {
rba90 10:065a4b58c6ff 187 Radio.Rx( 0 );
rba90 0:e2ccabf3f30c 188 State = LOWPOWER;
rba90 0:e2ccabf3f30c 189 break;
rba90 0:e2ccabf3f30c 190 }
rba90 0:e2ccabf3f30c 191 case LOWPOWER:
rba90 0:e2ccabf3f30c 192 {
rba90 0:e2ccabf3f30c 193 break;
rba90 0:e2ccabf3f30c 194 }
rba90 0:e2ccabf3f30c 195 default:
rba90 0:e2ccabf3f30c 196 {
rba90 0:e2ccabf3f30c 197 State = LOWPOWER;
rba90 0:e2ccabf3f30c 198 break;
rba90 0:e2ccabf3f30c 199 }
rba90 0:e2ccabf3f30c 200 }
rba90 0:e2ccabf3f30c 201 }
rba90 0:e2ccabf3f30c 202
rba90 0:e2ccabf3f30c 203 void AlohaTransceiver::send(uint8_t *buffer, int length)
rba90 0:e2ccabf3f30c 204 {
rba90 0:e2ccabf3f30c 205 Radio.Send(buffer, length);
rba90 10:065a4b58c6ff 206 State = LOWPOWER;
rba90 0:e2ccabf3f30c 207 }
rba90 0:e2ccabf3f30c 208
rba90 15:0dc9df48687a 209 void AlohaTransceiver::send(AlohaFrame *frame)
rba90 15:0dc9df48687a 210 {
rba90 15:0dc9df48687a 211
rba90 15:0dc9df48687a 212 }
rba90 15:0dc9df48687a 213
rba90 0:e2ccabf3f30c 214 void AlohaTransceiver::registerType(AlohaFrame::AlohaType_t type, aloha_callback_func f)
rba90 0:e2ccabf3f30c 215 {
rba90 0:e2ccabf3f30c 216 AlohaTypeCallbackTable[type] = f;
rba90 0:e2ccabf3f30c 217 }
rba90 0:e2ccabf3f30c 218
rba90 0:e2ccabf3f30c 219 void AlohaTransceiver::deRegisterType(AlohaFrame::AlohaType_t type, aloha_callback_func f)
rba90 0:e2ccabf3f30c 220 {
rba90 0:e2ccabf3f30c 221 AlohaTypeCallbackTable[type] = NULL;
rba90 0:e2ccabf3f30c 222 }
rba90 0:e2ccabf3f30c 223
rba90 2:fa264e48d5f7 224 int16_t AlohaTransceiver::getRssi()
rba90 2:fa264e48d5f7 225 {
rba90 2:fa264e48d5f7 226 return RssiValue;
rba90 2:fa264e48d5f7 227 }
rba90 2:fa264e48d5f7 228
rba90 2:fa264e48d5f7 229 int8_t AlohaTransceiver::getSnr()
rba90 2:fa264e48d5f7 230 {
rba90 2:fa264e48d5f7 231 return SnrValue;
rba90 2:fa264e48d5f7 232 }
rba90 2:fa264e48d5f7 233
rba90 11:3e1ff29da71a 234 uint8_t AlohaTransceiver::getDeviceId()
rba90 11:3e1ff29da71a 235 {
rba90 11:3e1ff29da71a 236 return deviceId;
rba90 11:3e1ff29da71a 237 }
rba90 11:3e1ff29da71a 238
rba90 11:3e1ff29da71a 239 void AlohaTransceiver::setDeviceId(uint8_t id)
rba90 11:3e1ff29da71a 240 {
rba90 11:3e1ff29da71a 241 deviceId = id;
rba90 11:3e1ff29da71a 242 }
rba90 11:3e1ff29da71a 243
rba90 8:4bda842f73d4 244 #if USE_MODEM_LORA == 1
rba90 8:4bda842f73d4 245 AlohaTransceiver::LoRaSettings_t *AlohaTransceiver::getSettings()
rba90 8:4bda842f73d4 246 {
rba90 8:4bda842f73d4 247 return &Settings;
rba90 8:4bda842f73d4 248 }
rba90 8:4bda842f73d4 249
rba90 8:4bda842f73d4 250 #elif USE_MODEM_FSK == 1
rba90 8:4bda842f73d4 251 AlohaTransceiver::FskSettings_t *AlohaTransceiver::getSettings()
rba90 8:4bda842f73d4 252 {
rba90 8:4bda842f73d4 253 return &Settings;
rba90 8:4bda842f73d4 254 }
rba90 8:4bda842f73d4 255 #else
rba90 8:4bda842f73d4 256 #error "Please define a modem in the compiler options."
rba90 8:4bda842f73d4 257 #endif
rba90 8:4bda842f73d4 258
rba90 0:e2ccabf3f30c 259 void OnTxDone( void )
rba90 0:e2ccabf3f30c 260 {
rba90 0:e2ccabf3f30c 261 Radio.Sleep( );
rba90 0:e2ccabf3f30c 262 State = TX;
rba90 0:e2ccabf3f30c 263 printf( "> OnTxDone\n\r" );
rba90 0:e2ccabf3f30c 264 }
rba90 0:e2ccabf3f30c 265
rba90 0:e2ccabf3f30c 266 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
rba90 0:e2ccabf3f30c 267 {
rba90 0:e2ccabf3f30c 268 Radio.Sleep( );
rba90 0:e2ccabf3f30c 269
rba90 0:e2ccabf3f30c 270 // safeguard: if size exceeded maximum buffer size, it will cause memory overflow
rba90 0:e2ccabf3f30c 271 BufferSize = size ? BUFFER_SIZE : size <= BUFFER_SIZE;
rba90 0:e2ccabf3f30c 272
rba90 0:e2ccabf3f30c 273 memcpy( Buffer, payload, BufferSize );
rba90 0:e2ccabf3f30c 274 RssiValue = rssi;
rba90 0:e2ccabf3f30c 275 SnrValue = snr;
rba90 0:e2ccabf3f30c 276 State = RX;
rba90 0:e2ccabf3f30c 277 printf( "> OnRxDone\n\r" );
rba90 0:e2ccabf3f30c 278 }
rba90 0:e2ccabf3f30c 279
rba90 0:e2ccabf3f30c 280 void OnTxTimeout( void )
rba90 0:e2ccabf3f30c 281 {
rba90 0:e2ccabf3f30c 282 Radio.Sleep( );
rba90 0:e2ccabf3f30c 283 State = TX_TIMEOUT;
rba90 0:e2ccabf3f30c 284 printf( "> OnTxTimeout\n\r" );
rba90 0:e2ccabf3f30c 285 }
rba90 0:e2ccabf3f30c 286
rba90 0:e2ccabf3f30c 287 void OnRxTimeout( void )
rba90 0:e2ccabf3f30c 288 {
rba90 0:e2ccabf3f30c 289 Radio.Sleep( );
rba90 0:e2ccabf3f30c 290 Buffer[ BufferSize ] = 0;
rba90 0:e2ccabf3f30c 291 State = RX_TIMEOUT;
rba90 0:e2ccabf3f30c 292 printf( "> OnRxTimeout\n\r" );
rba90 0:e2ccabf3f30c 293 }
rba90 0:e2ccabf3f30c 294
rba90 0:e2ccabf3f30c 295 void OnRxError( void )
rba90 0:e2ccabf3f30c 296 {
rba90 0:e2ccabf3f30c 297 Radio.Sleep( );
rba90 0:e2ccabf3f30c 298 State = RX_ERROR;
rba90 0:e2ccabf3f30c 299 printf( "> OnRxError\n\r" );
rba90 0:e2ccabf3f30c 300 }