| Remote Access Control |

# comms.c

Go to the documentation of this file.

    00001 // This file has been prepared for Doxygen automatic documentation generation.
    00048 #include "comms.h"
    00049 #include "common.h"
    00050 #include "config.h"
    00051 #include "timer.h"
    00052 #include "radio.h"
    00053
    00054
    00055
    00056 #ifdef USE_UART
    00057
    00058
    00059
    00061 #define BAUD_RATE 19200
    00063 #define BRREG_VALUE ((CPU_F/16/BAUD_RATE)-1)
    00064
    00065
    00066
    00068 static volatile byte * messageBufferPtr;
    00070 static volatile byte messageBytesLeft;
    00072 static volatile byte messageByteCount;
    00073
    00074
    00075
    00077 #pragma vector = USART_RX_vect
    00078 __interrupt void receiveHandler(void)
    00079 {
    00080         // Store incoming byte in buffer and move ptr.
    00081         *messageBufferPtr++ = UDR0;
    00082         ++messageByteCount;
    00083
    00084         // Is buffer full?
    00085         if( --messageBytesLeft == 0 ) {
    00086                 // Disable receive interrupts.
    00087                 UCSR0B &= ~(1<<RXCIE0);
    00088         } else {
    00089                 // Restart timeout.
    00090                 startShortTimeout();
    00091         }
    00092 }
    00093
    00094
    00095
    00096 bool initReceiver(void)
    00097 {
    00098         // Pull up input line.
    00099         PORTD |= (1<<PD0);
    00100
    00101         // Initialize UART receiver.
    00102         UCSR0B = (1<<RXEN0);
    00103         UBRR0 = BRREG_VALUE;
    00104
    00105         return true; // Always succeeds.
    00106 }
    00107
    00108
    00109
    00110 void enableReception( byte messageSize )
    00111 {
    00112         messageBufferPtr = (byte *) &receiveBuffer;
    00113         messageBytesLeft = messageSize;
    00114         messageByteCount = 0;
    00115         UCSR0A |= (1<<RXC0);
    00116         UCSR0B |= (1<<RXCIE0);
    00117 }
    00118
    00119
    00120
    00121 bool messageComplete(void)
    00122 {
    00123         return messageBytesLeft == 0;
    00124 }
    00125
    00126
    00127
    00128 byte messageBytesReceived(void)
    00129 {
    00130         // Don't count preamble.
    00131         if( messageByteCount >= 1 ) {
    00132                 return messageByteCount - 1;
    00133         } else {
    00134                 return 0;
    00135         }
    00136 }
    00137
    00138
    00139
    00140 bool receiverTimedOut(void)
    00141 {
    00142         if( shortTimeout == true && !messageComplete() ) {
    00143                 // Disable reception.
    00144                 UCSR0B &= ~(1<<RXCIE0);
    00145                 // Indicate timeout.
    00146                 return true;
    00147         } else {
    00148                 return false;
    00149         }
    00150 }
    00151
    00152
    00153
    00154 #else
    00155
    00156
    00157
    00159 static volatile byte * messageBufferPtr;
    00161 static volatile byte messageBytesLeft;
    00163 static volatile byte messageByteCount;
    00165 static volatile byte messageCurrentByte;
    00167 static volatile byte messageBitCount;
    00169 static volatile bool messageWaitForStartBit;
    00170
    00171
    00172
    00174 #pragma vector = RF_CLOCK_PCINT_VECT
    00175 __interrupt void rxClockLineHandler(void)
    00176 {
    00177         // Return if it was not a rising edge in clock input pin.
    00178         if( GET_RX_CLOCK() == 0 ) {
    00179                 return;
    00180         }
    00181
    00182         // Check that data is 0 if we are waiting for start bit.
    00183         if( messageWaitForStartBit ) {
    00184                 if( GET_RX_DATA() == 0 ) {
    00185                         messageWaitForStartBit = false;
    00186                         startShortTimeout();
    00187                 }
    00188                 return;
    00189         }
    00190
    00191         // Insert bit into current byte and copy to buffer if 8 bits are received.
    00192         messageCurrentByte <<= 1;
    00193   #ifdef INVERTED_MANCHESTER
    00194         if( GET_RX_DATA() != 0 ) {
    00195   #else
    00196         if( GET_RX_DATA() == 0 ) {
    00197   #endif
    00198                 messageCurrentByte |= 0x01;
    00199         }
    00200         if( ++messageBitCount == 8 ) {
    00201                 *messageBufferPtr++ = messageCurrentByte;
    00202                 messageCurrentByte = 0;
    00203                 messageBitCount = 0;
    00204                 ++messageByteCount;
    00205
    00206                 // Disable further interrupts if all bytes are received,
    00207                 // else restart the timeout for next byte.
    00208                 if( --messageBytesLeft == 0 ) {
    00209                         RF_CLOCK_PCINT_MASK_REG &= ~(1<<RF_CLOCK_PCINT_MASK_BIT);
    00210                         rx_WriteOFF( false );
    00211                 } else {
    00212                         startShortTimeout();
    00213                 }
    00214         }
    00215 }
    00216
    00217
    00218
    00219 bool initReceiver(void)
    00220 {
    00221         INIT_RX_DATA();
    00222         INIT_RX_CLOCK();
    00223
    00224         PCICR |= (1<<RF_CLOCK_PCINT_ENABLE_BIT);
    00225
    00226         // Consult receiver datasheet for details on settings.
    00227         rx_OPMODE_t opmode;
    00228         opmode.baudRateRange = 2; // XLim = 3.2 - 5.6 kBaud
    00229         opmode.bitCheck = 1; // 3 valid 1-bits required to wake up receiver.
    00230 #ifdef FSK
    00231         opmode.amplitudeModulation = false;
    00232 #endif
    00233 #ifdef ASK
    00234         opmode.amplitudeModulation = true;
    00235 #endif
    00236         opmode.sleepValue = 1; // ~2ms sleep.
    00237         opmode.sleepExtension = false;
    00238         opmode.noiseSuppression = true;
    00239
    00240         rx_LIMIT_t limit;
    00241         limit.limMin = RX_LIM_MIN; // 4800 Baud - 20%
    00242         limit.limMax = RX_LIM_MAX; // 4800 Baud + 20%
    00243
    00244         bool success = rx_WriteVerifySleep( &opmode, &limit );
    00245         return success;
    00246 }
    00247
    00248
    00249
    00250 void enableReception( byte messageSize )
    00251 {
    00252         messageBufferPtr = (byte *) &receiveBuffer;
    00253         messageBytesLeft = messageSize;
    00254         messageByteCount = 0;
    00255         messageCurrentByte = 0;
    00256         messageBitCount = 0;
    00257         messageWaitForStartBit = true;
    00258         RF_CLOCK_PCINT_MASK_REG |= (1<<RF_CLOCK_PCINT_MASK_BIT);
    00259 }
    00260
    00261
    00262
    00263 bool messageComplete(void)
    00264 {
    00265         return messageBytesLeft == 0;
    00266 }
    00267
    00268
    00269
    00270 byte messageBytesReceived(void)
    00271 {
    00272         return messageByteCount;
    00273 }
    00274
    00275
    00276
    00277 bool receiverTimedOut(void)
    00278 {
    00279         if( shortTimeout == true && !messageComplete() && !messageWaitForStartBit ) {
    00280                 // Disable reception.
    00281                 RF_CLOCK_PCINT_MASK_REG &= ~(1<<RF_CLOCK_PCINT_MASK_BIT);
    00282                 rx_WriteOFF( false );
    00283                 // Indicate timeout.
    00284                 return true;
    00285         } else {
    00286                 return false;
    00287         }
    00288 }
    00289
    00290
    00291
    00292 #endif
    00293

@DOC_TITLE@