stackMasterSlaveSync.c

Go to the documentation of this file.
00001 00061 // don't change the order of the includes! 00062 #include <chipcon/reg1010.h> 00063 00064 // include one of the two follwoing. cc1010eb.h for the evaluation board, and partCboard for the 00065 // teco partC board. 00066 //#include <chipcon/cc1010eb.h> 00067 #include <chipcon/partCboard.h> 00068 00069 #include <chipcon/hal.h> 00070 #include <chipcon/sdccutils.h> 00071 #include "acltypes.h" 00072 #include <chipcon/partCStack.h> 00073 00074 data byte i; 00075 00078 00079 #define DEBUG_USE_UART 00080 00085 #define DEBUG_USE_UART1_FOR_DUMPING 00086 00091 //#define DEBUG_DUMP_RSSI_SAMPLES 00092 00097 #define DEBUG_DUMP_RF_STATE 00098 00103 //#define DEBUG_DUMP_SYNC_VALUES 00104 00109 //#define DEBUG_DUMP_RF_STATISTICS 00110 00112 #define DEBUG_VERIFY_SCRAMBLING 00113 00114 00118 //#define DEBUG_ARBITRATION_TEST_0 00119 //#define DEBUG_ARBITRATION_TEST_PATTERN 00120 //#define DEBUG_ARBITRATION_TEST_COUNT 00121 00122 00127 #ifdef DEBUG_ARBITRATION_TEST_PATTERN 00128 #define DEBUG_ARBITRATION_TEST_ACTIVE 00129 #endif 00130 #ifdef DEBUG_ARBITRATION_TEST_COUNT 00131 #define DEBUG_ARBITRATION_TEST_ACTIVE 00132 #endif 00133 #ifdef DEBUG_ARBITRATION_TEST_0 00134 #define DEBUG_ARBITRATION_TEST_ACTIVE 00135 #endif 00136 00138 00141 // the possible states of the rf layer 00147 #define RF_STATE_MASTER BIN(00000001) 00148 00153 #define RF_STATE_ALONE BIN(00000010) 00154 00159 #define RF_STATE_IN_SYNC BIN(00000100) 00160 00166 #define RF_STATE_TRAFFIC_GROUP BIN(00001000) 00167 00172 #define RF_STATE_LONG_SEARCH_FOR_MASTER BIN(00010000) 00173 00180 #define RF_STATE_MASTER_RIVAL_TEST BIN(00100000) 00181 00183 #define RF_STATE_RUNNING BIN(10000000) 00184 00204 data volatile byte rfState= 0; 00205 00206 #ifdef DEBUG_USE_UART 00207 #ifdef DEBUG_DUMP_RF_STATE 00208 data volatile byte rfStateOld= 0xFF; 00209 #endif 00210 #endif 00211 00216 #define RF_SYNC_MAX_SLOT_COUNT 10 00217 00222 //#define RF_USE_ALONE_DETECTION 00223 00228 #define RF_ALONE_TEST_FAILED_THRESHOLD 167 00229 00241 #define RF_ALONE_DEEP_SLEEP 00242 00244 #define RF_PREAMBLE_LENGTH_TX 20 00245 00247 #define RF_PREAMBLE_LENGTH_RX 20 00248 00256 #define RF_MAX_SYNC_JITTER 5769ul 00257 00262 #define RF_SLOT_LENGTH_IN_US 36865ul 00263 00269 #define RF_MASTER_SEARCH_SLOTS_STARTUP 50 00270 00274 #define RF_MASTER_SEARCH_SLOTS_AFTER_ALONE 167 00275 00283 #define RF_MAX_FAILED_SYNC_COUNT 6 00284 00289 #define RF_MASTER_RIVAL_TEST_SYNC_COUNT 30 00290 00299 #define RF_SYNC_ADDITIONAL_OFFSET 0x89B3 00300 00314 #define RF_ARBITRATION_RSSI_THRESHOLD 0x78 00315 00323 #define RF_ALONE_TEST_RSSI_THRESHOLD 0xC0 00324 00330 #define RF_RENDEZVOUS_SYNC_START_HI 0x7C 00331 #define RF_RENDEZVOUS_SYNC_START_LO 0xC8 00332 00339 #define RF_RENDEZVOUS_DATA_PACKET_START_HI 0x93 00340 #define RF_RENDEZVOUS_DATA_PACKET_START_LO 0x12 00341 00347 #define RF_RENDEZVOUS_ARBITRATION_START_HI 0x71 00348 #define RF_RENDEZVOUS_ARBITRATION_START_LO 0x88 00349 00359 #define RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS 0x390 00360 00361 00362 00366 #define RF_BUFFER_SIZE 64 00367 00374 //#define RF_USE_TRAFFIC_INDICATION 00375 00380 #define RF_RENDEZVOUS_TRAFFIC_INDICATION_HI 0x92 00381 #define RF_RENDEZVOUS_TRAFFIC_INDICATION_LO 0x60 00382 00387 #define RF_RENDEZVOUS_ALONE_TEST_HI 0x8C 00388 #define RF_RENDEZVOUS_ALONE_TEST_LO 0xBA 00389 00391 static byte rfSlotCount= 0; 00392 00394 static byte rfSyncCount= 0; 00395 00397 static byte rfFailedSyncCount= 0; 00398 00403 data byte rfScramblerReg= BIN(01101100); 00404 00406 data unsigned long randomNumberReg; 00407 00413 unsigned short rfSlotsWithoutSignalReceived= 0; 00414 00420 data unsigned short tEndAvg= 0; 00422 //data unsigned long rfAverageTEndBuffer; 00423 00425 xdata TIMER_DATA periodDataSlot; 00426 xdata TIMER_DATA periodDataSyncByteReceiveTimeout; 00427 00429 xdata signed short moduloTimer0; 00430 00431 #ifdef DEBUG_USE_UART 00432 #ifdef DEBUG_DUMP_RSSI_SAMPLES 00433 00434 xdata byte rssiSamples[16]; 00436 unsigned short arbitrationGood= 0; 00438 unsigned short arbitrationBad= 0; 00439 #endif 00440 #endif 00441 00455 RF_RXTXPAIR_SETTINGS code RF_SETTINGS = { 00456 0xA0, 0x2F, 0x52, // Modem 0, 1 and 2 00457 0x76, 0x00, 0x00, // Freq A 00458 0x58, 0x7A, 0x8D, // Freq B 00459 0x01, 0xAB, // FSEP 1 and 0 00460 0x40, // PLL_RX 00461 0x30, // PLL_TX 00462 0x6C, // CURRENT_RX 00463 0xF3, // CURRENT_TX 00464 0x32, // FREND 00465 0x0F, // PA_POW 00466 0x00, // MATCH 00467 0x00, // PRESCALER 00468 }; 00470 xdata RF_RXTXPAIR_CALDATA RF_CALDATA; 00471 00482 xdata byte rfFieldStrength; 00483 00484 #ifdef RF_USE_ALONE_DETECTION 00485 #ifndef RF_ALONE_DEEP_SLEEP 00486 00492 xdata unsigned short rfSlotsInAloneModeLeft; 00493 #endif 00494 #endif 00495 00496 00497 00500 00501 #define LL_PROTOCOL_VERSION 4 00502 #define LL_HEADER_SIZE 12 00503 #define LL_PAYLOAD_SIZE 64 00504 #define LL_TAIL_SIZE 2 00505 #define LL_FRAME_MAX_SIZE (LL_HEADER_SIZE+LL_PAYLOAD_SIZE+LL_TAIL_SIZE) 00506 00507 #define LL_CRC16_POLY 0x1021 00508 00510 #define LL_CRC16_INIT 0xFFFF 00511 00513 #define LL_CRC_OK 0 00514 00516 xdata byte LL_sequence_no; 00517 00518 byte LL_slots_left; 00519 00521 #define LL_STATE_SEND_SUCCESS BIN(00000001) 00522 00523 #define LL_STATE_CTRL_MESSAGE_INSERTED BIN(00000010) 00524 00528 #define LL_STATE_NEED_TO_SEND BIN(00000100) 00529 00530 #define LL_STATE_RECEIVE_BUFFER_LOCKED BIN(00001000) 00531 00532 #define LL_STATE_NEW_DATA BIN(00010000) 00533 00534 #define LL_STATE_PACKET_JUST_SENT BIN(00100000) 00535 00536 #define LL_STATE_PACKET_JUST_RECEIVED BIN(01000000) 00537 00538 #define LL_STATE_RUNNING BIN(10000000) 00539 00559 data volatile byte llState= 0; 00560 data static byte llStateOld= 0xFF; 00561 00562 code nodeAddrType llEpromAddress= {10, 1, 0, 1, 10, 1, 2, 0}; 00564 00567 00569 #define ACL_SUBSCRIPTIONLIST_LENGTH 8 00570 00572 #define ACL_CONTROL_MESSAGES_TIMEOUT 30 00573 00575 #define ACL_TYPE_ACM_H ACL_TYPE_ACM_HI 00576 00577 #define ACL_TYPE_ACM_L ACL_TYPE_ACM_LO 00578 00579 00580 xdata byte ACL_subscriptions[ACL_SUBSCRIPTIONLIST_LENGTH][2]; 00581 xdata boolean ACL_send_buffer_locked; 00582 xdata boolean ACL_subscribe_all; 00583 xdata boolean ACL_ACM_answers; // answer on ACM messages like helo and so on 00584 xdata byte ACL_write_position; 00585 00586 xdata boolean ACL_ignore_control_messages; 00587 xdata boolean ACL_pass_control_messages; 00589 00595 00596 00597 xdata byte LL_header_received[LL_HEADER_SIZE]; 00599 xdata byte LL_payload_received[LL_PAYLOAD_SIZE]; 00601 xdata byte LL_payload_received_length; 00603 xdata byte LL_tail_received[LL_TAIL_SIZE]; 00605 00612 00613 00614 volatile xdata byte LL_header_receivebuffer[LL_HEADER_SIZE]; 00616 volatile xdata byte LL_payload_receivebuffer[LL_PAYLOAD_SIZE]; 00618 volatile xdata byte LL_payload_receivebuffer_length; 00620 volatile xdata byte LL_tail_receivebuffer[LL_TAIL_SIZE]; 00622 00629 00630 00631 xdata byte LL_payload_send[LL_PAYLOAD_SIZE]; 00633 xdata byte LL_payload_send_length; 00635 00642 00643 00644 xdata byte LL_header_sendbuffer[LL_HEADER_SIZE]; 00646 xdata byte LL_payload_sendbuffer[LL_PAYLOAD_SIZE]; 00648 xdata byte LL_payload_sendbuffer_length; 00650 xdata byte LL_tail_sendbuffer[LL_TAIL_SIZE]; 00652 00655 00656 xdata unsigned long statsRfSlotsInSync; 00657 00659 xdata unsigned long statsRfSlotsNeedToSend; 00661 xdata unsigned long statsRfArbitrationWon; 00662 00663 /* /// those of the statsRfSlotsNeedToSend where arbitration was lost 00664 xdata unsigned long statsRfArbitrationLost; 00666 xdata unsigned long statsRfDataPacketsReceivedNeedingToSend; 00667 00669 xdata unsigned long statsRfArbitrationSingalDetected; 00671 xdata unsigned long statsRfArbitrationNoSingalDetected; */ 00672 00674 xdata unsigned long statsRfDataPacketsReceivedNotNeedingToSend; 00676 00679 #ifdef DEBUG_USE_UART 00680 #ifdef DEBUG_DUMP_RF_STATE 00681 00684 void DebugDumpRfState() { 00685 if (rfState != rfStateOld || llState != llStateOld) { 00686 putchar((rfState & RF_STATE_RUNNING) ? 'R' : 'S'); 00687 putchar('-'); 00688 putchar((rfState & RF_STATE_MASTER_RIVAL_TEST) ? 'R' : '-'); 00689 putchar((rfState & RF_STATE_LONG_SEARCH_FOR_MASTER) ? 'L' : '-'); 00690 putchar((rfState & RF_STATE_TRAFFIC_GROUP) ? 'T' : '-'); 00691 putchar((rfState & RF_STATE_IN_SYNC) ? 'Y' : '-'); 00692 putchar((rfState & RF_STATE_ALONE) ? 'A' : '-'); 00693 putchar((rfState & RF_STATE_MASTER) ? 'M' : 'S'); 00694 DEBUG_SPACE; 00695 rfStateOld= rfState; 00696 putchar((llState & LL_STATE_RUNNING) ? 'R' : '-'); 00697 putchar((llState & LL_STATE_PACKET_JUST_RECEIVED) ? 'J' : '-'); 00698 putchar((llState & LL_STATE_PACKET_JUST_SENT) ? 'J' : '-'); 00699 putchar((llState & LL_STATE_NEW_DATA) ? 'N' : '-'); 00700 putchar((llState & LL_STATE_RECEIVE_BUFFER_LOCKED) ? 'L' : '-'); 00701 putchar((llState & LL_STATE_NEED_TO_SEND) ? 'S' : '-'); 00702 putchar((llState & LL_STATE_CTRL_MESSAGE_INSERTED) ? 'C' : '-'); 00703 putchar((llState & LL_STATE_SEND_SUCCESS) ? 'S' : '-'); 00704 DEBUG_NEWLINE; 00705 llStateOld= llState; 00706 } 00707 } 00708 #endif 00709 #endif 00710 00715 #ifdef DEBUG_USE_UART 00716 void DebugDumpReceivedAclPacket() { 00717 int i, dataEnd; 00718 00719 ACLLockReceiveBuffer(); 00720 00721 // dump last byte of sender address 00722 putchar('A'); 00723 putchar(':'); 00724 sendByteToUart(LL_header_received[3+7]); 00725 DEBUG_SPACE; 00726 // dump packet content 00727 for (i=0; i < LL_payload_received_length;) { 00728 sendByteToUart(LL_payload_received[i++]); 00729 sendByteToUart(LL_payload_received[i++]); 00730 dataEnd= i + LL_payload_received[i++]; 00731 putchar(':'); 00732 while (i < dataEnd && i < LL_payload_received_length) 00733 sendByteToUart(LL_payload_received[i++]); 00734 DEBUG_SPACE; 00735 } 00736 DEBUG_NEWLINE; 00737 } 00738 00739 #endif 00740 00745 void putchar(char c) { 00746 #ifdef DEBUG_USE_UART 00747 #ifdef DEBUG_USE_UART1_FOR_DUMPING 00748 UART1_WAIT_AND_SEND(c); 00749 #else 00750 UART0_WAIT_AND_SEND(c); 00751 #endif 00752 #endif 00753 } 00754 00758 void DebugBuildTestPacketACL() { 00759 /* 00760 byte i; 00761 // this is only testcode 00762 00763 ACLAddNewType(ACL_TYPE_ACM_H,ACL_TYPE_ACM_L); 00764 ACLAddNewType(115,216); //helo 00765 00766 ACLAddData(255); 00767 ACLAddData(255); 00768 ACLAddData(255); 00769 ACLAddData(255); 00770 00771 ACLAddData(255); 00772 ACLAddData(255); 00773 ACLAddData(255); 00774 ACLAddData(255); 00775 00776 00777 //ACLAddNewType(100,100); 00778 //for (i=0;i<32;i++) 00779 // ACLAddData(i); 00780 */ 00781 } 00782 00783 00784 #ifdef DEBUG_VERIFY_SCRAMBLING 00785 bool DebugScrambleAndDescrambleTest() { 00786 byte b, j; 00787 byte scrReg; 00788 00789 // test scrambling and descrambling for some numbers 00790 j=0; 00791 YLED= LED_ON; 00792 do { 00793 b= j; 00794 YLED= ~YLED; 00795 00796 // scramble b 00797 scrReg= rfScramblerReg; 00798 b= RFScramble(j); 00799 00800 // descramble b 00801 rfScramblerReg= scrReg; 00802 b= RFDescramble(b); 00803 00804 if (b != j) { 00805 YLED= LED_OFF; 00806 return FALSE; 00807 } 00808 00809 rfScramblerReg= scrReg; 00810 } while (++j); 00811 YLED= LED_OFF; 00812 return TRUE; 00813 } 00814 #endif 00815 00816 /* 00818 void waitUsingTimer2(TIMER_DATA *timeOutPeriodData) { 00819 TIMER2_RUN(FALSE); 00820 INT_SETFLAG(INUM_TIMER2, INT_CLR); 00821 halConfigTimerFast23(TIMER2|TIMER23_NO_INT_TIMER, timeOutPeriodData); 00822 TIMER2_RUN(TRUE); 00823 00824 while (1) { 00825 if (INT_GETFLAG(INUM_TIMER2)) { 00826 INT_SETFLAG(INUM_TIMER2, INT_CLR); 00827 break; 00828 } 00829 } 00830 } 00831 */ 00832 00833 #ifdef DEBUG_USE_UART 00834 #ifdef DEBUG_DUMP_RF_STATISTICS 00835 00839 void DebugResetStats() { 00840 statsRfSlotsInSync= 0; 00841 00842 statsRfSlotsNeedToSend= 0; 00843 statsRfArbitrationWon= 0; 00844 00845 statsRfDataPacketsReceivedNotNeedingToSend= 0; 00846 } 00847 00851 void DebugDumpStats() { 00852 //sendUlongToUart(statsRfSlotsInSync); 00853 //putchar(':'); 00854 sendUlongToUart(statsRfSlotsNeedToSend); 00855 putchar(':'); 00856 sendUlongToUart(statsRfArbitrationWon); 00857 putchar(':'); 00858 sendUlongToUart(statsRfDataPacketsReceivedNotNeedingToSend); 00859 DEBUG_NEWLINE; 00860 } 00861 #endif 00862 #endif 00863 00864 00865 00871 00872 void ParticleInit() { 00873 #ifdef DEBUG_USE_UART 00874 byte i; 00875 #endif 00876 00877 // Initialize peripherals 00878 //WDT_ENABLE(FALSE); 00879 // for BIZARRE reasons, the watchdog timer is only disabled when the 00880 // WDT register is set directly and TWICE (???). found this out by trial and error. 00881 WDT= 0x13; 00882 WDT= 0x13; 00883 00884 // Set optimum settings for speed and low power consumption 00885 MEM_NO_WAIT_STATES(); 00886 FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS); 00887 00888 // setup leds 00889 RLED_OE(TRUE); 00890 YLED_OE(TRUE); 00891 GLED_OE(TRUE); 00892 BLED_OE(TRUE); 00893 RLED= YLED= GLED= BLED= LED_OFF; 00894 00895 TIMER0_RUN(FALSE); 00896 00897 // generate a truly random seed for the random number generator 00898 // rf must not be used when this happens 00899 RFInitRandom(); 00900 00901 #ifdef DEBUG_VERIFY_SCRAMBLING 00902 // make sure scrambling and descrambling macros work correctly 00903 if (!DebugScrambleAndDescrambleTest()) { 00904 RLED= LED_ON; 00905 while (1); 00906 } 00907 #endif 00908 00909 #ifdef DEBUG_USE_UART 00910 #ifdef DEBUG_DUMP_RSSI_SAMPLES 00911 for (i=0; i<3*8; i++) { 00912 rssiSamples[i]= 0; 00913 } 00914 #endif 00915 #endif 00916 00917 #ifdef DEBUG_USE_UART 00918 // setup UART for serial debug messages 00919 #ifdef DEBUG_USE_UART1_FOR_DUMPING 00920 UART1_SETUP(57600, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_TX_ONLY | UART_POLLED); 00921 #else 00922 UART0_SETUP(57600, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_TX_ONLY | UART_POLLED); 00923 #endif 00924 00925 DEBUG_CLEAR_SCREEN; 00926 sendStringToUart("Welcome to the CC1010 Particle Sensor Node!\r\nOn this console you will see debug information. Have Fun!\r\n\r\nSync method: master slave\r\n\r\nRandom numbers:\r\n"); 00927 00928 for (i=0; i<10; i++) { 00929 sendByteToUart(RF_NEW_RANDOM_BYTE()); 00930 DEBUG_SPACE; 00931 } 00932 DEBUG_NEWLINE; 00933 #endif 00934 00935 // let the 32 kHz clock run continously 00936 X32_INPUT_SOURCE(X32_USING_CRYSTAL); 00937 X32_ENABLE(TRUE); 00938 } 00939 00940 byte AppSelfTest(char* result) { 00941 int i; 00942 00943 for (i=0; i<10; i++) { 00944 result[i]= RF_NEW_RANDOM_BYTE(); 00945 } 00946 00947 return TRUE; 00948 } 00950 00953 00955 void RFInit() { 00956 ulong tmp; 00957 00958 // Calibration (now done in rfStart) 00959 // halRFCalib(&RF_SETTINGS, &RF_CALDATA); 00960 00961 // calculate timer periods for timer 2 (timeouts) 00962 halCalcTimerPeriod(RF_SLOT_LENGTH_IN_US, CC1010EB_CLKFREQ, &periodDataSlot); 00963 halCalcTimerPeriod(RF_MAX_SYNC_JITTER, CC1010EB_CLKFREQ, &periodDataSyncByteReceiveTimeout); 00964 00965 // Set up timer 0 as the slot timer 00966 TIMER0_RUN(FALSE); 00967 tmp= halConfigTimer01(TIMER0|TIMER01_INT_TIMER, RF_SLOT_LENGTH_IN_US, CC1010EB_CLKFREQ, &moduloTimer0); 00968 #ifdef DEBUG_USE_UART 00969 sendStringToUart("\r\nTimer start val:\r\n"); 00970 sendUshortToUart(moduloTimer0); 00971 DEBUG_NEWLINE; 00972 #endif 00973 00974 // Turn on interrupts for timer0, turn off for timer 2 00975 INT_GLOBAL_ENABLE(INT_ON); 00976 INT_ENABLE(INUM_TIMER0, TRUE); 00977 INT_ENABLE(INUM_TIMER2, FALSE); 00978 PA_POW= 0; 00979 } 00980 00982 void RFStart() { 00983 if ((rfState & RF_STATE_RUNNING) == 0) { 00984 // set current field strength from data structure 00985 rfFieldStrength= RF_SETTINGS.pa_pow; 00986 00987 // Calibration 00988 halWait(200, CC1010EB_CLKFREQ); 00989 halRFCalib(&RF_SETTINGS, &RF_CALDATA); 00990 00991 // the function halRFSetRxTxOff() has to be called at least once, because later, we use 00992 // the simpler functions RFstartRx and RFstartTx instead, that don't set the values needed. 00993 halRFSetRxTxOff(RF_RX, &RF_SETTINGS, &RF_CALDATA); 00994 halWait(200, CC1010EB_CLKFREQ); 00995 halRFSetRxTxOff(RF_TX, &RF_SETTINGS, &RF_CALDATA); 00996 halWait(200, CC1010EB_CLKFREQ); 00997 halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 00998 halWait(200, CC1010EB_CLKFREQ); 00999 01000 // turn on ADC for RSSI reading (needs 200ms, because it waits until ADC has stabilized) 01001 halRFReadRSSIlevel(RSSI_MODE_INIT); 01002 ADC_POWER(FALSE); 01003 } 01004 PA_POW= 0; 01005 01006 01007 // set initial rf state 01008 rfState= RF_STATE_RUNNING | RF_STATE_LONG_SEARCH_FOR_MASTER; // RF_STATE_MASTER | RF_STATE_ALONE; // 01009 #ifndef RF_USE_TRAFFIC_INDICATION 01010 // if traffic indication is not used, the flag has to be set permanently, so that 01011 // all packets are sent and received immediately 01012 rfState|= RF_STATE_TRAFFIC_GROUP; 01013 #endif 01014 01015 #ifdef RF_USE_ALONE_DETECTION 01016 rfSlotsWithoutSignalReceived= 0; 01017 #endif 01018 01019 01020 #ifdef DEBUG_USE_UART 01021 #ifdef DEBUG_DUMP_RF_STATE 01022 DebugDumpRfState(); 01023 #endif 01024 #endif 01025 01026 // start timer0 01027 INT_SETFLAG(INUM_TIMER0, INT_CLR); 01028 TIMER0_RUN(TRUE); 01029 } 01030 01032 void RFStop() { 01033 // clear flag RF_STATE_RUNNING and all other flags 01034 rfState= 0; 01035 // stop timer0 01036 TIMER0_RUN(FALSE); 01037 // stop sending 01038 RF_SET_MODE_SLEEP; 01039 // turn off rf power 01040 //PA_POW= 0; 01041 } 01042 01047 void RFSetModeTransmit() { 01048 code byte *rfSettingsPtr= (code byte *)&RF_SETTINGS; 01049 unsigned short i; 01050 01051 PA_POW= 0; 01052 01053 // turn on transceiver for tx-ing 01054 PLL=rfSettingsPtr[12]; // pll_tx 01055 RFMAIN=0xF0; 01056 TEST5=0x10 | RF_CALDATA.vco_ao_tx; 01057 TEST6=0x3B; 01058 CURRENT=rfSettingsPtr[14]; 01059 01060 // Wait 200 usec before monitoring LOCK, slowly raise rf power to reduce spurious emission. 01061 // the data sheet says 200 usec for each step is optimal, but this is too long for our application 01062 // we use 50 us for each step. this should mitigate spurious emission. 01063 for(i=LOCK_MONITOR_DELAY; i>0; i--); 01064 01065 /* for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01066 if (rfFieldStrength >= 0x01) 01067 PA_POW= 0x01; 01068 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01069 if (rfFieldStrength >= 0x1E) 01070 PA_POW= 0x1E; 01071 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01072 if (rfFieldStrength >= 0x8F) 01073 PA_POW= 0x8F; 01074 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01075 PA_POW= rfFieldStrength; */ 01076 01077 // Wait for lock (much less than normal version!) 01078 for(i=LOCK_TIMEOUT; !(LOCK&0x01) && (i>0); i--); 01079 PA_POW= rfFieldStrength; 01080 } 01081 01086 void RFSetModeReceive() { 01087 code byte *rfSettingsPtr= (code byte *)&RF_SETTINGS; 01088 unsigned short i; 01089 01090 // turn on transceiver for rx-ing 01091 PLL=rfSettingsPtr[11]; // pll_rx 01092 RFMAIN=0x30; 01093 TEST5=0x10 | RF_CALDATA.vco_ao_rx; 01094 TEST6=0x20 | RF_CALDATA.chp_co_rx; 01095 CURRENT=rfSettingsPtr[13]; 01096 01097 // Wait 200 usec before monitoring LOCK 01098 // notice: had to change the value of constant LOCK_MONITOR_DELAY, because 01099 // it produced a wait time of 418 us -> sdcc seems to have less efficient 01100 // integer algorithms. perhaps keil sees that LOCK_MONITOR_DELAY is < 256 01101 // and temporary uses i as a byte variable (?), which is much faster 01102 for(i=LOCK_MONITOR_DELAY; i>0; i--); 01103 01104 // Wait for lock (much less than normal version!) 01105 for(i=LOCK_TIMEOUT; !(LOCK&0x01) && (i>0); i--); 01106 } 01107 01109 void RFInitRandom() { 01110 byte a; 01111 01112 randomNumberReg= 0; 01113 do { 01114 halRandomNumberGen(&a, 1); 01115 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01116 randomNumberReg= (ulong)a << 24; 01117 do { 01118 halRandomNumberGen(&a, 1); 01119 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01120 randomNumberReg|= (ulong)a << 16; 01121 do { 01122 halRandomNumberGen(&a, 1); 01123 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01124 randomNumberReg|= (ulong)a << 8; 01125 do { 01126 halRandomNumberGen(&a, 1); 01127 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01128 randomNumberReg|= (ulong)a; 01129 01130 for (a=0; a<128; a++) 01131 RF_NEW_RANDOM_BYTE(); 01132 } 01133 01162 unsigned short RFReceivePacket(xdata TIMER_DATA *timeOutPeriodData, bool onlyDataPackets) { 01163 bool syncByteReceived, aclMatch; 01164 byte j, tHi, tLo, a, b, length, sync; 01165 unsigned short crc; 01166 01167 // Turn on RF for RX, do this early, because it needs 200us to power up 01168 RFSetModeReceive(); 01169 //halRFSetRxTxOff(RF_RX, &RF_SETTINGS, &RF_CALDATA); 01170 //BLED= ~BLED; 01171 RFCON|=0x01; // Ensure that bytemode is selected 01172 RF_SET_PREAMBLE_COUNT(RF_PREAMBLE_LENGTH_RX); 01173 RF_SET_SYNC_BYTE(RF_SUITABLE_SYNC_BYTE); 01174 MODEM1=(MODEM1&0x03)|0x24; // Make sure avg filter is free-running + 22 baud settling time 01175 INT_ENABLE(INUM_RF, INT_OFF); 01176 INT_SETFLAG(INUM_RF, INT_CLR); 01177 01178 if (onlyDataPackets) { 01179 /* tHi= TH0; 01180 tLo= TL0; 01181 sendByteToUart(tHi); 01182 sendByteToUart(tLo); 01183 DEBUG_NEWLINE; 01184 return 0; */ 01185 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_DATA_PACKET_START_HI, RF_RENDEZVOUS_DATA_PACKET_START_LO); 01186 } 01187 BLED= LED_ON; // start receiving 01188 RF_START_RX(); 01189 01190 // setup timer 2 as a no-interrupt timer for timeout of the master search process 01191 halConfigTimerFast23(TIMER2|TIMER23_NO_INT_TIMER, timeOutPeriodData); 01192 //halConfigTimer23(TIMER2|TIMER23_NO_INT_TIMER, 5000, CC1010EB_CLKFREQ); 01193 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01194 TIMER2_RUN(TRUE); 01195 //BLED= ~BLED; 01196 01197 // receive sync byte 01198 syncByteReceived= FALSE; 01199 while (1) { 01200 // Check if timer2 is finished so that we time out 01201 // alternatively, if interrupt for timer0 is enabled, also check if enough time is left 01202 // before the next slot 01203 if (INT_GETFLAG(INUM_TIMER2) || (ET0 == INT_ON && !RF_SLOT_500_US_LEFT)) { 01204 // Clear interrupt and timeout 01205 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01206 //RLED= ~RLED; 01207 break; 01208 } 01209 // Check if sync byte received 01210 if (INT_GETFLAG(INUM_RF)) { 01211 INT_SETFLAG(INUM_RF, INT_CLR); // Clear the flag 01212 syncByteReceived= TRUE; 01213 break; 01214 } 01215 } 01216 //BLED= ~BLED; 01217 //BLED= ~BLED; 01218 01219 length= 0; 01220 if (syncByteReceived) { 01221 // turn on blue led when sync byte is received 01222 //BLED= ~BLED; 01223 //YLED= ~YLED; 01224 01225 // Lock average filter 01226 RF_LOCK_AVERAGE_FILTER(TRUE); 01227 01228 // get scrambler init 01229 //RF_SPI_RECEIVE_BYTE(rfScramblerReg); 01230 01231 // receveive scrambled FF to train local descrambling register 01232 RF_SPI_RECEIVE_BYTE(a); 01233 a= RFDescramble(a); 01234 01235 // get rf options byte 01236 RF_SPI_RECEIVE_BYTE(a); 01237 a= RFDescramble(a); 01238 01239 // it's a sync packet if the lsb of the rf options byte is 1 01240 sync= a & 1; 01241 01242 if (sync) { 01243 // receive timestamp 01244 RF_SPI_RECEIVE_BYTE(tHi); 01245 tHi= RFDescramble(tHi); 01246 RF_SPI_RECEIVE_BYTE(tLo); 01247 tLo= RFDescramble(tLo); 01248 // receive the xor protection byte 01249 RF_SPI_RECEIVE_BYTE(j); 01250 j= RFDescramble(j); 01251 01252 // if xor bit is correct 01253 if (j == tHi ^ tLo) { 01254 TIMER0_RUN(FALSE); 01255 a= TH0; 01256 b= TL0; 01257 TH0= tHi; 01258 TL0= tLo; 01259 TIMER0_RUN(TRUE); 01260 01261 //GLED= ~GLED; 01262 // turn rf off 01263 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01264 length= 1; 01265 01266 #ifdef DEBUG_DUMP_SYNC_VALUES 01267 #ifdef DEBUG_USE_UART 01268 // dump received tHi and tLo 01269 sendByteToUart(tHi); 01270 sendByteToUart(tLo); 01271 DEBUG_SPACE; 01272 01273 // dump local tHi and tLo before setting 01274 sendByteToUart(a); 01275 sendByteToUart(b); 01276 DEBUG_SPACE; 01277 01278 // dump difference between reported and set timer values 01279 sendSignedByteToUart((int)tHi-(int)a,3); 01280 sendSignedByteToUart((int)tLo-(int)b,3); 01281 DEBUG_NEWLINE; 01282 #endif 01283 #endif 01284 } 01285 } else { 01286 // get physical payload length 01287 RF_SPI_RECEIVE_BYTE(length); 01288 length= RFDescramble(length); 01289 01290 // read acl filter types 01291 RF_SPI_RECEIVE_BYTE(a); 01292 a= RFDescramble(a); 01293 01294 RF_SPI_RECEIVE_BYTE(b); 01295 b= RFDescramble(b); 01296 01297 // now we expect 4 "don't-care bytes" in order to have enough time to check local subscriptions 01298 if (ACL_subscribe_all) { 01299 // don't test anything 01300 RF_SPI_RECEIVE_BYTE(a); 01301 //BLED= ~BLED; 01302 //BLED= ~BLED; 01303 RF_SPI_RECEIVE_BYTE(a); 01304 //BLED= ~BLED; 01305 //BLED= ~BLED; 01306 RF_SPI_RECEIVE_BYTE(a); 01307 //BLED= ~BLED; 01308 //BLED= ~BLED; 01309 RF_SPI_RECEIVE_BYTE(a); 01310 //BLED= ~BLED; 01311 //BLED= ~BLED; 01312 } else { 01313 aclMatch= FALSE; 01314 // check subscriptions 0-3 01315 for (i=0; i < ACL_SUBSCRIPTIONLIST_LENGTH; i+= ACL_SUBSCRIPTIONLIST_LENGTH / 4) { 01316 // receive next 0 01317 while(!(EXIF & 0x10)); EXIF&=~0x10; 01318 for (j=i;j < i + ACL_SUBSCRIPTIONLIST_LENGTH / 4;j++) { 01319 if ((ACL_subscriptions[j][0] == a) && (ACL_subscriptions[j][1] == b)) 01320 aclMatch= TRUE; 01321 //BLED= ~BLED; 01322 //BLED= ~BLED; 01323 } 01324 } 01325 if (!aclMatch) { 01326 RF_SET_MODE_SLEEP; 01327 return 0; 01328 } 01329 } 01330 01331 // receive the incoming packet into the receivebuffer 01332 01333 // receive ll header 01334 for (j=0; j<12; j++) { 01335 RF_SPI_RECEIVE_BYTE(b); 01336 LL_header_receivebuffer[j]= RFDescramble(b); 01337 } 01338 01339 // receive ll packet payload 01340 length= length-LL_HEADER_SIZE-LL_TAIL_SIZE; 01341 if (length > 64) { 01342 // length is not in allowed range 01343 RF_SET_MODE_SLEEP; 01344 return 0; 01345 } 01346 for (j=0; j<length; j++) { 01347 RF_SPI_RECEIVE_BYTE(b); 01348 LL_payload_receivebuffer[j]= RFDescramble(b); 01349 } 01350 01351 // receive ll tail 01352 for (j=0; j<LL_TAIL_SIZE; j++) { 01353 RF_SPI_RECEIVE_BYTE(b); 01354 LL_tail_receivebuffer[j]= RFDescramble(b); 01355 } 01356 // turn rf off 01357 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01358 01359 // check the crc of the packet 01360 crc= LLCalcCRC16(LL_header_receivebuffer, LL_payload_receivebuffer, length); 01361 if ((crc >> 8) == LL_tail_receivebuffer[0] && (crc & 0xFF) == LL_tail_receivebuffer[1]) { 01362 // crc was correct, so copy received packet to the "received" buffer 01363 for (i=0; i<LL_HEADER_SIZE; i++) { 01364 LL_header_received[i]= LL_header_receivebuffer[i]; 01365 } 01366 for (i=0; i<length; i++) { 01367 LL_payload_received[i]= LL_payload_receivebuffer[i]; 01368 } 01369 for (i=0; i<LL_TAIL_SIZE; i++) { 01370 LL_tail_received[i]= LL_tail_receivebuffer[i]; 01371 } 01372 } else { 01373 // crc check failed 01374 /* if (onlyDataPackets) { 01375 putchar('c'); 01376 } */ 01377 RF_SET_MODE_SLEEP; 01378 return 0; 01379 } 01380 } 01381 } /* else { 01382 if (onlyDataPackets) { 01383 putchar('_'); 01384 } 01385 } */ 01386 01387 // turn transceiver off 01388 RFCON&=~0x01; // Ensure that bitmode is selected 01389 01390 // don't write back the local scrambler register to the global variable: 01391 // each node should have it's own scrReg sequence 01392 // rfScramblerReg= scrReg; 01393 01394 // stop timer 2 and start timer 3 if not already done 01395 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01396 TIMER2_RUN(FALSE); 01397 01398 BLED= LED_OFF; // end RFReceivePacket 01399 /* if (onlyDataPackets) { 01400 putchar('L'); 01401 sendByteToUart(length); 01402 DEBUG_SPACE; 01403 } */ 01404 if (sync && length == 1) { 01405 return 2; 01406 } else if (!sync && length > 0) { 01407 return 1 + 256 * length; 01408 } else { 01409 return 0; 01410 } 01411 } 01412 01413 01421 byte RFArbitrationBit(bool sendOrListen, byte t0hi, byte t0lo) { 01422 byte i, sample1, paPow; 01423 01424 if (sendOrListen) { 01425 // bit is 1, so send a short signal (2 bit) 01426 // turn on transceiver for tx-ing 01427 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01428 BLED= ~BLED; 01429 // increase transmission power for the small burst 01430 paPow= PA_POW; 01431 PA_POW= 0xF0; 01432 RFSetModeTransmit(); 01433 RFBUF= 0xFF; 01434 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01435 BLED= ~BLED; 01436 // write rf buffer 01437 RFBUF= sample1= BIN(10101010); 01438 01439 // wait for next rendezvous point 01440 WAIT_FOR_TIMER0_EXACT(t0hi, t0lo); 01441 BLED= ~BLED; 01442 RF_START_TX(); 01443 for (i=0; i<30; i++) { 01444 sample1= ((sample1 << 1) | (sample1 >> 7)); // rotate left (not through carry, because it's faster!) 01445 while(!RF_READY_TO_SEND()); 01446 RFBUF= sample1; 01447 } 01448 while(!RF_READY_TO_SEND()); 01449 BLED= ~BLED; 01450 BLED= ~BLED; // mark the 1 with an extra peak 01451 BLED= ~BLED; 01452 PA_POW= paPow; 01453 01454 // we sent a 1, so we must send the highest number possible, because a low number could 01455 // indicate that we lost the arbitration. 01456 return 255; 01457 } else { 01458 // bit is 0, so listen to the channel 01459 // turn on transceiver for rx-ing 01460 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01461 BLED= ~BLED; 01462 // turn on continuous sampling 01463 RFSetModeReceive(); // param true means fast 01464 RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF); 01465 RF_START_RX(); 01466 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01467 BLED= ~BLED; 01468 01469 // wait for next rendezvous point 01470 WAIT_FOR_TIMER0_EXACT(t0hi, t0lo-5); 01471 // the next two lines must (for strange) reasons be there 01472 // because otherwise you would get the result of the last 01473 // sample instead of the current result 01474 BLED= ~BLED; 01475 ADC_SAMPLE_SINGLE(); 01476 ADC_GET_SAMPLE_8BIT(); 01477 ADC_SAMPLE_SINGLE(); 01478 sample1= ADC_GET_SAMPLE_8BIT(); 01479 01480 BLED= ~BLED; // mark finish of sync slice 01481 // if the medium is not clear 01482 return sample1; 01483 } 01484 } 01485 01493 byte RFArbitrationBitDebug(bool sendOrListen, byte t0hi, byte t0lo) { 01494 byte sample1; 01495 01496 if (sendOrListen) { 01497 // bit is 1, so send a short signal (2 bit) 01498 // turn on transceiver for tx-ing 01499 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01500 BLED= ~BLED; 01501 RFSetModeTransmit(); 01502 RFBUF= 0xFF; 01503 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01504 BLED= ~BLED; 01505 // write rf buffer 01506 RFBUF= sample1= BIN(10101010); 01507 01508 t0hi= TH0; 01509 t0lo= TL0; 01510 } else { 01511 // bit is 0, so listen to the channel 01512 // turn on transceiver for rx-ing 01513 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01514 BLED= ~BLED; 01515 // turn on continuous sampling 01516 RFSetModeReceive(); // param true means fast 01517 RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF); 01518 RF_START_RX(); 01519 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01520 BLED= ~BLED; 01521 01522 t0hi= TH0; 01523 t0lo= TL0; 01524 } 01525 sendByteToUart(t0hi); 01526 putchar(':'); 01527 sendByteToUart(t0lo); 01528 DEBUG_NEWLINE; 01529 return 0; // return 0, the strongest signal possible 01530 } 01531 01532 #ifdef DEBUG_ARBITRATION_TEST_COUNT 01533 byte arbitrationByteGlob= 0; 01534 #endif 01535 01542 bool RFArbitration(bool tryToObtainMediaAccess) { 01543 bool success= TRUE; 01544 byte arbitrationByte, j, sample1, syncTimeH, syncTimeL; 01545 01546 // get timer0 time when entering the function, should be the same on both nodes 01547 //syncTimeH= TH0; 01548 //syncTimeL= TL0; 01549 BLED= LED_ON; // start RFArbitration 01550 01551 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01552 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01553 ADC_POWER(TRUE); // turn on adc 01554 01555 // do CAN-like arbitration for media access 01556 // see param documentation for explanation 01557 #ifndef DEBUG_ARBITRATION_TEST_ACTIVE 01558 if (tryToObtainMediaAccess) { 01559 arbitrationByte= RF_NEW_RANDOM_BYTE(); 01560 // if the node competes for the medium, he must send at least one 1, 01561 // so 0 as arbitration byte is not allowed 01562 if (!arbitrationByte) 01563 arbitrationByte= 1; 01564 } else { 01565 arbitrationByte= 0; 01566 } 01567 #endif 01568 01569 // for debugging, one of the following three assignments can be chosen for arbitrationByte 01570 #ifdef DEBUG_ARBITRATION_TEST_PATTERN 01571 arbitrationByte= BIN(10101010); // for debugging 01572 #endif 01573 #ifdef DEBUG_ARBITRATION_TEST_COUNT 01574 arbitrationByte= arbitrationByteGlob++; // for debugging 01575 #endif 01576 #ifdef DEBUG_ARBITRATION_TEST_0 01577 arbitrationByte= 0; // for debugging 01578 #endif 01579 01580 /*syncTimeH= TH0; 01581 syncTimeL= TL0; 01582 sendByteToUart(syncTimeH); 01583 putchar(':'); 01584 sendByteToUart(syncTimeL); 01585 DEBUG_NEWLINE; 01586 RF_SET_MODE_SLEEP; 01587 return 0;*/ 01588 01589 // synchronize. hint: the value of this sync time has been measured with the 01590 // commented out block above 01591 syncTimeH= RF_RENDEZVOUS_ARBITRATION_START_HI; // first period should be shorter 01592 syncTimeL= RF_RENDEZVOUS_ARBITRATION_START_LO; 01593 01594 RFCON&=~0x01; // Ensure that bitmode is selected 01595 for (j=0; j<8; j++) { 01596 01597 sample1= RFArbitrationBit(bit_test(arbitrationByte, j), syncTimeH, syncTimeL); 01598 #ifdef DEBUG_USE_UART 01599 #ifdef DEBUG_DUMP_RSSI_SAMPLES 01600 rssiSamples[j]= sample1; 01601 //rssiSamples[8+j]= sample2; 01602 //rssiSamples[3*j+2]= sample3; 01603 #endif 01604 #endif 01605 // if the medium is not clear 01606 if (sample1 < RF_ARBITRATION_RSSI_THRESHOLD) { 01607 success= FALSE; 01608 // we're out, so break 01609 #ifndef DEBUG_ARBITRATION_TEST_ACTIVE 01610 break; // for testing and calibration of the arbitration threshold, this line has to be commented out 01611 #endif 01612 } 01613 01614 // this adds an equivalent of approx. 2.5 * 206 us to the variables for the next rendezvous point 01615 syncTimeH= ((syncTimeH << 8 | syncTimeL) + RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS) >> 8; 01616 syncTimeL+= RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS; 01617 } 01618 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01619 ADC_POWER(FALSE); 01620 //BLED= LED_OFF; 01621 if (success) 01622 GLED= ~GLED; 01623 else 01624 RLED= ~RLED; 01625 #ifdef DEBUG_USE_UART 01626 #ifdef DEBUG_DUMP_RSSI_SAMPLES 01627 if (success) { 01628 arbitrationGood++; 01629 } else { 01630 arbitrationBad++; 01631 } 01632 sample1= 0; 01633 for (j=0; j<8; j++) { 01634 // if (rssiSamples[j] > 0xAF) { 01635 // continue; 01636 // } 01637 sendByteToUart(j); 01638 DEBUG_SPACE; 01639 //putchar((bit_test(arbitrationByte, j) ? '1' : '0')); 01640 //DEBUG_SPACE; 01641 sendByteToUart(rssiSamples[j]); 01642 //sendByteToUart(rssiSamples[8+j]); 01643 //DEBUG_SPACE; 01644 /* if ((rssiSamples[j] < RF_ARBITRATION_RSSI_THRESHOLD)) { 01645 putchar('1'); 01646 //sample1|= 1<<j; 01647 } else { 01648 putchar('0'); 01649 } */ 01650 //putchar((bit_test(arbitrationByte,j)) ? '1' : '0'); 01651 DEBUG_NEWLINE; 01652 } 01653 //sendByteToUart(sample1); 01654 //DEBUG_SPACE; 01655 01656 // this dumps the value of successful and not successful arbitration 01657 //sendUlongToUart(arbitrationGood); 01658 //DEBUG_SPACE; 01659 //sendUlongToUart(arbitrationBad); 01660 DEBUG_NEWLINE; 01661 #endif 01662 #endif 01663 BLED= LED_OFF; // end RFArbitration 01664 return success; 01665 } 01666 01667 01677 #ifdef RF_USE_TRAFFIC_INDICATION 01678 void RFTrafficIndication() { 01679 bool result,tmp; 01680 01681 BLED= LED_ON; // start 01682 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01683 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01684 ADC_POWER(TRUE); // turn on adc 01685 01686 if (llState & LL_STATE_NEED_TO_SEND) { 01687 RFArbitrationBit(TRUE, RF_RENDEZVOUS_TRAFFIC_INDICATION_HI, RF_RENDEZVOUS_TRAFFIC_INDICATION_LO); 01688 result= TRUE; 01689 } else { 01690 // let's be pessimistic and compare the rssi value to the more conservative 01691 // alone test value. there is a trade off between unnecessary traffic groups 01692 // and lost packets. we choose reliable reception at the cost of some 01693 // unnecessary traffic groups. 01694 tmp= RFArbitrationBit(FALSE, RF_RENDEZVOUS_TRAFFIC_INDICATION_HI, RF_RENDEZVOUS_TRAFFIC_INDICATION_LO); 01695 result= tmp < RF_ALONE_TEST_RSSI_THRESHOLD; 01696 } 01697 01698 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01699 ADC_POWER(FALSE); 01700 BLED= LED_OFF; 01701 01702 //putchar((result) ? 'T' : 't'); 01703 //sendByteToUart(tmp); 01704 if (result) 01705 rfState|= RF_STATE_TRAFFIC_GROUP; 01706 else 01707 rfState&= ~RF_STATE_TRAFFIC_GROUP; 01708 } 01709 #endif 01710 01719 #ifdef RF_USE_ALONE_DETECTION 01720 bool RFAloneTest() { 01721 bool result; 01722 byte tmp; 01723 01724 BLED= LED_ON; // start 01725 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01726 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01727 ADC_POWER(TRUE); // turn on adc 01728 RFCON&=~0x01; // Ensure that bitmode is selected 01729 01730 if (rfState & RF_STATE_MASTER) { 01731 // as a master we try to receive a signal 01732 // let's be pessimistic and compare the rssi value to a high 01733 // alone test value. there is a trade off between 01734 tmp= RFArbitrationBit(FALSE, RF_RENDEZVOUS_ALONE_TEST_HI, RF_RENDEZVOUS_ALONE_TEST_LO); 01735 result= tmp < RF_ALONE_TEST_RSSI_THRESHOLD; 01736 //putchar('r'); 01737 //sendByteToUart(tmp); 01738 //putchar((result) ? '+' : '-'); 01739 //DEBUG_SPACE; 01740 } else { 01741 // as a slave, we send a signal 01742 RFArbitrationBit(TRUE, RF_RENDEZVOUS_ALONE_TEST_HI, RF_RENDEZVOUS_ALONE_TEST_LO); 01743 result= TRUE; 01744 //putchar('s'); 01745 } 01746 01747 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01748 ADC_POWER(FALSE); 01749 BLED= LED_OFF; 01750 return result; 01751 } 01752 #endif 01753 01760 byte RFAloneRandomNumberOfSeconds() { 01761 // returns a number betwenn 10 and 25 (including both) 01762 return 10 + (RF_NEW_RANDOM_BYTE() >> 4); 01763 } 01764 01772 unsigned short RFAloneRandomNumberOfSlots() { 01773 // returns a number betwenn 10 and 26 (including both) 01774 return 333 + ((RF_NEW_RANDOM_BYTE() << 8)+ RF_NEW_RANDOM_BYTE()) % 534; 01775 } 01776 01785 void RFLowPowerSleep(byte sleepTime) { 01786 byte i; 01787 01788 sendByteToUart(sleepTime); 01789 DEBUG_NEWLINE; 01790 01791 TIMER0_RUN(FALSE); 01792 RLED= GLED= YLED= LED_OFF; 01793 01794 // config timer 2 for a period of 1 second in X32 clock mode 01795 // this will result in calling the dummy isr ISR 01796 // set timer 2 to high prio so that it can interrupt the ISR of timer0 where we are currently in. 01797 INT_PRIORITY(INUM_TIMER2, INT_HIGH); 01798 halConfigTimer23(TIMER2 | TIMER23_INT_TIMER, 1000000, CC1010EB_LOW_CLKFREQ); 01799 01800 MAIN_CLOCK_SET_SOURCE(CLOCK_X32); 01801 XOSC_ENABLE(FALSE); 01802 01803 // stay in 32kHz clock mode for for sleepTime seconds 01804 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01805 TIMER2_RUN(TRUE); 01806 for (i=0; i<sleepTime; i++) { 01807 RLED= ~RLED; 01808 // this inner loop needs 1 s to execute 01809 ENTER_IDLE_MODE(); 01810 } 01811 TIMER2_RUN(FALSE); 01812 01813 // Enable high speed XOSC, switch clock source, then disable 32kHz XOSC 01814 XOSC_ENABLE(TRUE); 01815 MAIN_CLOCK_SET_SOURCE(CLOCK_XOSC); 01816 01817 INT_ENABLE(INUM_TIMER2, INT_OFF); 01818 INT_PRIORITY(INUM_TIMER2, INT_LOW); 01819 TIMER0_RUN(TRUE); 01820 01821 RLED= LED_OFF; 01822 } 01823 01829 byte RFScramble(byte b) { 01830 byte i, scrReg; 01831 01832 scrReg= rfScramblerReg; 01833 RF_SCRAMBLE(b, scrReg, i); 01834 rfScramblerReg= scrReg; 01835 01836 return b; 01837 } 01838 01844 byte RFDescramble(byte b) { 01845 byte i, scrReg; 01846 01847 scrReg= rfScramblerReg; 01848 RF_DESCRAMBLE(b, scrReg, i); 01849 rfScramblerReg= scrReg; 01850 return b; 01851 } 01852 01853 /* 01854 sends a data or a sync packet. 01855 if param sync is true, a sync packet is sent. 01856 if it is false, a normal packet will be sent. in that case, the 01857 packet content is taken out of LL_header_sendbuffer and LL_payload_sendbuffer. 01858 the length of the payload to send is taken out of LL_payload_sendbuffer_length. 01859 @param sync true if sync packet should be sent, false (default) otherwise 01860 @return bool false if data packet and transmission during backoff so that packet could not be sent, false otherwise 01861 */ 01862 bool RFSendPacket(bool sync) { 01863 byte numPreambles, j, b, currentTHi, currentTLo; 01864 //unsigned short tEndCurrent; // the start time for a sync packet 01865 01866 // wait one ms to give the receivers time to prepare 01867 halWait(1, CC1010EB_CLKFREQ); 01868 numPreambles= RF_PREAMBLE_LENGTH_TX; 01869 01870 // Turn on RF for TX, send packet 01871 RFSetModeTransmit(); 01872 //halRFSetRxTxOff(RF_TX, &RF_SETTINGS, &RF_CALDATA); 01873 01874 // Set the first byte to transmit & turn on TX 01875 RFCON|=0x01; // Ensure that bytemode is selected 01876 RF_SEND_BYTE(RF_PREAMBLE_BYTE); 01877 01878 if (sync) { 01879 /* j= TH0; 01880 b= TL0; 01881 sendByteToUart(j); 01882 sendByteToUart(b); 01883 DEBUG_NEWLINE; 01884 RF_SET_MODE_SLEEP; 01885 return 1; */ 01886 01887 // wait for the sync time start rendezvous point. the values for RF_SYNC_START_TIMER0_* 01888 // have been calibrated with the commented out code above 01889 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_SYNC_START_HI, RF_RENDEZVOUS_SYNC_START_LO); 01890 } else { 01891 /* j= TH0; 01892 b= TL0; 01893 sendByteToUart(j); 01894 sendByteToUart(b); 01895 DEBUG_NEWLINE; 01896 RF_SET_MODE_SLEEP; 01897 return 0; */ 01898 01899 // synchronize 01900 // the value of this sync time has been measured with the commented out block above 01901 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_DATA_PACKET_START_HI, RF_RENDEZVOUS_DATA_PACKET_START_LO); 01902 } 01903 BLED= LED_ON; // start sending 01904 01905 // turn on transceiver 01906 RF_START_TX(); 01907 //YLED= ~YLED; 01908 //BLED= ~BLED; 01909 01910 if (sync) { 01911 // get current timer value of timer0 01912 currentTHi= TH0; 01913 currentTLo= TL0; 01914 currentTHi= ((currentTHi | currentTLo) + RF_SYNC_ADDITIONAL_OFFSET) >> 8; 01915 currentTLo= currentTLo + RF_SYNC_ADDITIONAL_OFFSET; 01916 j= currentTHi ^ currentTLo; 01917 } 01918 01919 // Send remaining preambles 01920 while (--numPreambles) { 01921 RF_SPI_SEND_BYTE(RF_PREAMBLE_BYTE); 01922 } 01923 01924 // Send start of packet byte 01925 RF_SPI_SEND_BYTE(RF_SUITABLE_SYNC_BYTE); 01926 01927 // Send scrambler init 01928 //scrReg= rfScramblerReg; 01929 //RF_SPI_SEND_BYTE(scrReg); 01930 01931 // send FF byte to train the receiver's descrambler 01932 b= RFScramble(0xFF); 01933 RF_SPI_SEND_BYTE(b); 01934 01935 //YLED= ~YLED; 01936 //BLED= ~BLED; 01937 if (sync) { 01938 //BLED= ~BLED; 01939 // send rf options. 1 means beacon. 01940 b= RFScramble(1); 01941 RF_SPI_SEND_BYTE(b); 01942 01943 // send timestamp 01944 // use redundant protection byte j (= tEndAvgLo xor tEndAvgHi) to detect transmission bit errors 01945 b= RFScramble(currentTHi); 01946 RF_SPI_SEND_BYTE(b); 01947 b= RFScramble(currentTLo); 01948 RF_SPI_SEND_BYTE(b); 01949 b= RFScramble(j); 01950 RF_SPI_SEND_BYTE(b); 01951 01952 //YLED= ~YLED; 01953 //BLED= ~BLED; 01954 //tEndCurrent= (TH0<<8) | TL0; 01955 01956 #ifdef DEBUG_USE_UART 01957 #ifdef DEBUG_DUMP_SYNC_VALUES 01958 j= TH0; 01959 b= TL0; 01960 sendByteToUart(currentTHi); 01961 sendByteToUart(currentTLo); 01962 DEBUG_SPACE; 01963 sendByteToUart(j); 01964 sendByteToUart(b); 01965 DEBUG_NEWLINE; 01966 01967 #endif 01968 #endif 01969 } else { 01970 // send rf options. 0 means regular packet 01971 b= RFScramble(0); 01972 RF_SPI_SEND_BYTE(b); 01973 01974 if (LL_payload_sendbuffer_length > 64) 01975 LL_payload_sendbuffer_length= 64; 01976 01977 // Send physical payload length 01978 b= RFScramble(LL_payload_sendbuffer_length + LL_HEADER_SIZE + LL_TAIL_SIZE); 01979 RF_SPI_SEND_BYTE(b); 01980 01981 b= RFScramble(LL_payload_sendbuffer[0]); 01982 RF_SPI_SEND_BYTE(b); 01983 b= RFScramble(LL_payload_sendbuffer[1]); 01984 RF_SPI_SEND_BYTE(b); 01985 01986 // send 4 foo bytes to give receiver time to check subscriptions 01987 // use RF_PREAMBLE_BYTE because it's dc balanced 01988 RF_SPI_SEND_BYTE(BIN(11001010)); 01989 //BLED= ~BLED; 01990 //BLED= ~BLED; 01991 RF_SPI_SEND_BYTE(BIN(11001010)); 01992 //BLED= ~BLED; 01993 //BLED= ~BLED; 01994 RF_SPI_SEND_BYTE(BIN(11001010)); 01995 //BLED= ~BLED; 01996 //BLED= ~BLED; 01997 RF_SPI_SEND_BYTE(BIN(11001010)); 01998 //BLED= ~BLED; 01999 //BLED= ~BLED; 02000 02001 // send ll header 02002 for (j=0; j<12; j++) { 02003 b= RFScramble(LL_header_sendbuffer[j]); 02004 RF_SPI_SEND_BYTE(b); 02005 } 02006 02007 // send ll packet payload 02008 for (j=0; j<LL_payload_sendbuffer_length; j++) { 02009 b= RFScramble(LL_payload_sendbuffer[j]); 02010 RF_SPI_SEND_BYTE(b); 02011 } 02012 02013 // send ll tail 02014 for (j=0; j<LL_TAIL_SIZE; j++) { 02015 b= RFScramble(LL_tail_sendbuffer[j]); 02016 RF_SPI_SEND_BYTE(b); 02017 } 02018 } 02019 02020 RF_SPI_SEND_BYTE(0); 02021 RF_SPI_SEND_BYTE(0); 02022 //while (!RF_READY_TO_SEND()); 02023 //YLED= ~YLED; 02024 //BLED= ~BLED; 02025 02026 // turn transceiver off 02027 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 02028 RFCON&=~0x01; // Ensure that bitmode is selected 02029 02030 // do exponential smoothing on the timer value when finished with sending 02031 // add an additional offset, because detection and timer setting on the slave 02032 // is slightly later than measurement on the master after after transmitting. 02033 /* if (sync) { 02034 //tEndAvg= tEndCurrent + RF_SYNC_ADDITIONAL_OFFSET; 02035 //tEndAvg= (((ulong)tEndCurrent + (ulong)RF_SYNC_ADDITIONAL_OFFSET) + 9*(ulong)tEndAvg) / 10; 02036 02037 // dump the sync values just sent 02038 #ifdef DEBUG_DUMP_SYNC_VALUES 02039 #ifdef DEBUG_USE_UART 02040 // dump received tHi and tLo 02041 sendByteToUart(tEndCurrent >> 8); 02042 sendByteToUart(tEndCurrent & 0xFF); 02043 DEBUG_SPACE; 02044 sendByteToUart(tEndAvg >> 8); 02045 sendByteToUart(tEndAvg & 0xFF); 02046 DEBUG_NEWLINE; 02047 #endif 02048 #endif 02049 02050 // when dividing by 8 don't simply cut the last 3 bits (like a cast would do) 02051 // to improve accuracy, add 1 if last bit that is shifted out is 1 02052 // this is the binary equivalent to decimal rounding (down if next decimal place is <=4, up if next decimal place is >4) 02053 rfAverageTEndBuffer*= 7; 02054 rfAverageTEndBuffer/= 8; 02055 rfAverageTEndBuffer+= tEndCurrent + RF_SYNC_ADDITIONAL_OFFSET; 02056 j= (rfAverageTEndBuffer & 0x4); 02057 tEndAvg= rfAverageTEndBuffer / 8; 02058 if (j) 02059 tEndAvg++; 02060 } */ 02061 02062 BLED= LED_OFF; // end RFSendPacket 02063 return TRUE; 02064 } 02065 02073 bool RFLongSearchForMaster(unsigned short numTries) { 02074 bool success= FALSE; 02075 02076 // let timer 0 (the slot timer) run, but turn off the interrupt 02077 INT_ENABLE(INUM_TIMER0, FALSE); 02078 YLED= LED_ON; 02079 while (numTries-- > 0) { 02080 YLED= ~YLED; 02081 //sendUlongToUart(numTries); 02082 //DEBUG_SPACE; 02083 // if it is a sync packet 02084 if (RFReceivePacket(&periodDataSlot, FALSE) == 2) { 02085 success= TRUE; 02086 break; 02087 } 02088 } 02089 02090 // turn on the interrupt for timer 0 again 02091 INT_ENABLE(INUM_TIMER0, TRUE); 02092 // clear the interrupt flag, because it is most probably set 02093 INT_SETFLAG(INUM_TIMER0, INT_CLR); 02094 02095 YLED= LED_OFF; 02096 return success; 02097 } 02098 02104 bool RFMasterRivalTest() { 02105 bool success= FALSE; 02106 02107 YLED= LED_ON; 02108 // the RFReceivePacket() function automagically returns if only 200us are left in 02109 // current timer0 slot. 02110 success= (RFReceivePacket(&periodDataSlot, FALSE) == 2); 02111 YLED= LED_OFF; 02112 02113 return success; 02114 } 02115 02117 // end of RF Layer Functions 02118 02119 02125 byte LLGetIDFromHardware() { 02126 for (i=0;i<=7;i++) { 02127 LL_header_sendbuffer[3+0]= llEpromAddress.a1; 02128 LL_header_sendbuffer[3+1]= llEpromAddress.a2; 02129 LL_header_sendbuffer[3+2]= llEpromAddress.a3; 02130 LL_header_sendbuffer[3+3]= llEpromAddress.a4; 02131 LL_header_sendbuffer[3+4]= llEpromAddress.a5; 02132 LL_header_sendbuffer[3+5]= llEpromAddress.a6; 02133 LL_header_sendbuffer[3+6]= llEpromAddress.a7; 02134 LL_header_sendbuffer[3+7]= RF_NEW_RANDOM_BYTE(); 02135 } 02136 02137 return 1; //both were invalid: there's no valid id 02138 } 02139 02143 void LLInit() { 02144 LL_sequence_no=0; 02145 02146 LL_header_sendbuffer[0]= LL_PROTOCOL_VERSION; 02147 02148 LL_payload_send_length=0; // set used ll payload to zero 02149 LL_payload_received_length= 0; 02150 llState= LL_STATE_RUNNING; 02151 LL_sequence_no= 0; 02152 02153 LLGetIDFromHardware(); //take id from internal or external eeprom and set LL_header_sendbuffer 02154 02155 #ifdef DEBUG_USE_UART 02156 sendStringToUart("Particle ID:\r\n"); 02157 sendByteToUart(LL_header_sendbuffer[3+0]); 02158 putchar('.'); 02159 sendByteToUart(LL_header_sendbuffer[3+1]); 02160 putchar('.'); 02161 sendByteToUart(LL_header_sendbuffer[3+2]); 02162 putchar('.'); 02163 sendByteToUart(LL_header_sendbuffer[3+3]); 02164 putchar('.'); 02165 sendByteToUart(LL_header_sendbuffer[3+4]); 02166 putchar('.'); 02167 sendByteToUart(LL_header_sendbuffer[3+5]); 02168 putchar('.'); 02169 sendByteToUart(LL_header_sendbuffer[3+6]); 02170 putchar('.'); 02171 sendByteToUart(LL_header_sendbuffer[3+7]); 02172 DEBUG_NEWLINE; 02173 #endif 02174 02175 RFInit(); 02176 } 02177 02178 02182 void LLStart() { 02183 RFStart(); 02184 } 02185 02186 02190 void LLStop() { 02191 RFStop(); // handles the states 02192 } 02193 02199 void LLSlotEnd() { 02200 02201 } 02202 02203 02207 void LLAbortSending() { 02208 if (LLSendingBusy()) { 02209 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 02210 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; 02211 } else { 02212 llState&= ~LL_STATE_SEND_SUCCESS; 02213 } 02214 // clear some llState flags 02215 llState&= ~(LL_STATE_SEND_SUCCESS|LL_STATE_NEED_TO_SEND); 02216 } 02217 } 02218 02222 void LLSetSendingSuccess() { 02223 //update the states 02224 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 02225 // no update of success flag 02226 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; // clear the control management bit 02227 } else { 02228 llState|= LL_STATE_SEND_SUCCESS; // successful sent 02229 } 02230 llState&= ~LL_STATE_NEED_TO_SEND; // busy is over 02231 LL_payload_send_length=0; // set used payload to zero 02232 } 02233 02237 byte LLSendingBusy() { 02238 return ((llState & LL_STATE_NEED_TO_SEND) != 0); 02239 } 02240 02245 byte LLGetSendSuccess() { 02246 return ((llState & LL_STATE_SEND_SUCCESS) != 0); 02247 } 02248 02252 byte LLIsActive() { 02253 return ((llState & LL_STATE_RUNNING) != 0); 02254 } 02255 02259 void LLSetDataToOld() { 02260 llState&= ~LL_STATE_NEW_DATA; 02261 } 02262 02267 void LLLockReceiveBuffer() { 02268 llState|= LL_STATE_RECEIVE_BUFFER_LOCKED; 02269 LLSetDataToOld(); 02270 } 02271 02275 byte LLReceiveBufferLocked() { 02276 return ((llState & LL_STATE_RECEIVE_BUFFER_LOCKED) != 0); 02277 } 02278 02282 void LLReleaseReceiveBuffer() { 02283 llState&= ~LL_STATE_RECEIVE_BUFFER_LOCKED; 02284 LLSetDataToOld(); 02285 } 02286 02290 void LLSetDataToNew() { 02291 llState|= LL_STATE_NEW_DATA; 02292 } 02293 02294 02299 byte LLDataIsNew() { 02300 return ((llState & LL_STATE_NEW_DATA) != 0); 02301 } 02302 02306 byte LLGetFieldStrength() { 02307 return rfFieldStrength; 02308 } 02309 02313 void LLSetFieldStrength(byte value) { 02314 rfFieldStrength= value; 02315 } 02316 02317 02325 unsigned short LLCalcCRC16(byte *header_data, byte *payload_data, byte payload_size) { 02326 //calcs a crc on LL_header, ACL_data, LL_tail 02327 byte hb,lb,i,tmp; 02328 lb=0; 02329 hb=0; 02330 02331 for(i=0;i<LL_HEADER_SIZE;i++) { 02332 tmp = lb; 02333 lb = hb; 02334 hb = tmp; 02335 lb ^= header_data[i]; 02336 lb ^= lb >> 4; 02337 hb ^= lb << 4; 02338 hb ^= lb >> 3; 02339 lb ^= (lb << 4) << 1; 02340 } 02341 02342 //len=header_data[1]-LL_HEADER_SIZE-LL_TAIL_SIZE; 02343 for(i=0;i<payload_size;i++) { 02344 tmp = lb; 02345 lb = hb; 02346 hb = tmp; 02347 lb ^= payload_data[i]; 02348 lb ^= lb >> 4; 02349 hb ^= lb << 4; 02350 hb ^= lb >> 3; 02351 lb ^= (lb << 4) << 1; 02352 } 02353 02354 return hb*256+lb; 02355 } 02356 02357 02362 byte LLSendPacket(byte slot_limit) { 02363 unsigned short crc; 02364 02365 if (slot_limit==0) slot_limit=1; // zero makes no sense 02366 02367 // if rf is off, stop it 02368 if (!(rfState & RF_STATE_RUNNING)) return 0; 02369 // if ll is still trying to send the last packet, ignore 02370 if (llState & LL_STATE_NEED_TO_SEND) return 0; 02371 02372 // complete the LLPacket 02373 //DebugGiveOut(LL_payload_send_length); 02374 02375 // copy "send" variables to "sendbuffer" variables 02376 if (LL_payload_send_length > 64) 02377 LL_payload_send_length= 64; 02378 LL_payload_sendbuffer_length= LL_payload_send_length; 02379 for (i=0; i<LL_payload_send_length; i++) 02380 LL_payload_sendbuffer[i]= LL_payload_send[i]; 02381 02382 LL_header_sendbuffer[1]= LL_payload_sendbuffer_length; 02383 LL_header_sendbuffer[2]= rfFieldStrength; 02384 LL_header_sendbuffer[11]= LL_sequence_no; 02385 02386 crc= LLCalcCRC16(LL_header_sendbuffer,LL_payload_sendbuffer, LL_payload_sendbuffer_length); 02387 02388 LL_tail_sendbuffer[0]= (crc>>8); //CRCh (implicit typecast; ´takes low byte from long 02389 LL_tail_sendbuffer[1]= crc & 0xFF; //CRCl 02390 02391 LL_slots_left= slot_limit; 02392 LL_sequence_no++; 02393 02394 llState|= LL_STATE_NEED_TO_SEND; 02395 02396 LL_payload_send_length= 0; // set the used payload to 0, so that new data can be written 02397 02398 return 1; 02399 } 02400 02401 02405 byte LLGetRemainingPayloadSpace() { 02406 return (LL_PAYLOAD_SIZE-LL_payload_send_length); 02407 } 02408 02409 02413 byte LLSentPacketInThisSlot() { 02414 return ((llState & LL_STATE_PACKET_JUST_SENT) != 0); 02415 } 02416 02418 // end of LL Layer Functions 02419 02422 02423 void ACLStartUp() { 02424 xdata byte result[10]; 02425 byte selftest_result,i; 02426 02427 selftest_result= AppSelfTest(result); //runs the selftest 02428 02429 GLED= LED_OFF; 02430 RLED= LED_OFF; 02431 halWait(100, CC1010EB_CLKFREQ); // for stabilizing of power etc. 02432 02433 ACLInit(); // sets the pins in pic init correct (looks at selftest_active 02434 // GLED= LED_ON; 02435 // while(1); 02436 02437 #ifdef DEBUG_DUMP_RF_STATISTICS 02438 // reset the statistics 02439 DebugResetStats(); 02440 #endif 02441 02442 if(selftest_result) { 02443 //send result ten times to get it 02444 for(i=0;i<10;i++) { 02445 //send packet 02446 ACLAddNewType(ACL_TYPE_CST_HI,ACL_TYPE_CST_LO); //MST Self test 02447 ACLAddData(result[0]); 02448 ACLAddData(result[1]); 02449 ACLAddData(result[2]); 02450 ACLAddData(result[3]); 02451 ACLAddData(result[4]); 02452 ACLAddData(result[5]); 02453 ACLAddData(result[9]); 02454 02455 ACLSendPacket(30); 02456 while(ACLSendingBusy()); 02457 } 02458 } 02459 } 02460 02461 02463 void ACLInit() { 02464 ACLFlushSubscriptions(); 02465 ACL_send_buffer_locked=false; 02466 ACL_write_position=0; 02467 ACL_subscribe_all=false; 02468 ACL_ACM_answers=true; 02469 ACL_ignore_control_messages=false; 02470 ACL_pass_control_messages=false; 02471 02472 LL_payload_send_length=0; 02473 ACL_write_position=0; 02474 02475 //APPSetLEDBehaviour(LEDS_NORMAL); //setdefault 02476 02477 LLInit(); 02478 ACLStart(); 02479 } 02480 02481 02488 byte ACLSubscribe(byte LL_type_h, byte LL_type_l) { 02489 byte i; 02490 02491 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { // first test if already there 02492 if ((ACL_subscriptions[i][0]==LL_type_h) && (ACL_subscriptions[i][1]==LL_type_l)) return 1; 02493 } 02494 02495 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02496 if ((ACL_subscriptions[i][0]==0) && (ACL_subscriptions[i][1]==0)) { 02497 ACL_subscriptions[i][0]=LL_type_h; 02498 ACL_subscriptions[i][1]=LL_type_l; 02499 return 1; // successful subscribed 02500 } 02501 } 02502 return 0; // subsriptions failed 02503 } 02504 02511 byte ACLUnsubscribe(byte LL_type_h, byte LL_type_l) { 02512 byte i; 02513 02514 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { // if type is there, delete it 02515 if ((ACL_subscriptions[i][0]==LL_type_h) && (ACL_subscriptions[i][1]==LL_type_l)) { 02516 ACL_subscriptions[i][0]=0; 02517 ACL_subscriptions[i][1]=0; 02518 return 1; // successful unsubscribed 02519 } 02520 } 02521 return 0; // not found 02522 } 02523 02524 02526 void ACLFlushSubscriptions() { 02527 byte i; 02528 02529 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02530 ACL_subscriptions[i][0]=0; 02531 ACL_subscriptions[i][1]=0; 02532 } 02533 02534 ACL_subscribe_all=false; 02535 ACLSubscribeDefault(); 02536 return; 02537 } 02538 02540 void ACLSubscribeAll() { 02541 ACL_subscribe_all=true; 02542 } 02543 02545 void ACLAnswerOnACM() { 02546 ACL_ACM_answers=true; 02547 } 02548 02550 void ACLNoAnswerOnACM() { 02551 ACL_ACM_answers=false; 02552 } 02553 02555 byte ACLSubscribeDefault() { 02556 byte result; 02557 result=ACLSubscribe(ACL_TYPE_ACM_H,ACL_TYPE_ACM_L); //165 14 is CMA (Control and Management virtual Artifact) 02558 return result; 02559 } 02560 02567 byte ACLVerifySubscription(byte type_h,byte type_l) { 02568 byte i; 02569 02570 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02571 if ((ACL_subscriptions[i][0]==type_h) && (ACL_subscriptions[i][1]==type_l)) return 1; 02572 } 02573 return 0; //subscription not found 02574 } 02575 02576 02581 byte ACLProcessControlMessages() { 02582 02583 if ((LL_payload_receivebuffer[0]==ACL_TYPE_ACM_H) && (LL_payload_receivebuffer[1]==ACL_TYPE_ACM_L)) { 02584 // there was a control msg 02585 02586 // check a control msg, e.g. "1,1" 02587 //remote shutdown 02588 if ((LL_payload_receivebuffer[3]==172) && (LL_payload_receivebuffer[4]==224)) { 02589 if (ACLMatchesMyIP(LL_payload_receivebuffer,6)) { 02590 return 4; 02591 } 02592 } 02593 02594 // check syncrate setting 02595 if ((LL_payload_receivebuffer[3]==198) && (LL_payload_receivebuffer[4]==208)) { 02596 //RFSetSyncRate(LL_payload_receivebuffer[6]); 02597 } 02598 02599 // check initial listen slots setting 02600 if ((LL_payload_receivebuffer[3]==159) && (LL_payload_receivebuffer[4]==192)) { 02601 //RFSetInitialListenSlots(LL_payload_receivebuffer[6]); 02602 } 02603 02604 // check shutdown (dirty hack; actually need ip check, different shutdown modes 02605 if ((LL_payload_receivebuffer[3]==204) && (LL_payload_receivebuffer[4]==232)) { 02606 RFStop(); 02607 } 02608 02609 // check modeswitch 02610 if ((LL_payload_receivebuffer[3]==1) && (LL_payload_receivebuffer[4]==1)) { 02611 } 02612 02613 //check over the air programming 02614 if ((LL_payload_receivebuffer[3]==198) && (LL_payload_receivebuffer[4]==88)) { 02615 //OtapReceive(); // call over the airprogramming stuff 02616 } 02617 02618 //check set ID 02619 if ((LL_payload_receivebuffer[3]==ACL_TYPE_CID_HI) && (LL_payload_receivebuffer[4]==ACL_TYPE_CID_LO) /* && selftest_active==true */) { 02620 //OtapSetID(); 02621 } 02622 02623 // no answering allowed; all messages below here are not allowed, if acl_acm_answer is false 02624 if (ACL_ACM_answers==false) return 0; 02625 02626 //check Helo 02627 if ((LL_payload_receivebuffer[3]==115) && (LL_payload_receivebuffer[4]==216)) { 02628 if (ACLMatchesMyIP(LL_payload_receivebuffer,6)) { // does sent IP match my IP?? 02629 if (LLSendingBusy()) ACLAbortSending(); // interrupt any waiting stuff 02630 02631 // now set the whole packet by hand: 02632 LL_payload_send[0]=ACL_TYPE_ACM_H; 02633 LL_payload_send[1]=ACL_TYPE_ACM_L; 02634 LL_payload_send[2]=0; 02635 02636 LL_payload_send[3]=134; 02637 LL_payload_send[4]=32; 02638 02639 LL_payload_send[5]=8; 02640 LL_payload_send[6]=LL_header_receivebuffer[3]; // put in origin adress from HELO 02641 LL_payload_send[7]=LL_header_receivebuffer[4]; 02642 LL_payload_send[8]=LL_header_receivebuffer[5]; 02643 LL_payload_send[9]=LL_header_receivebuffer[6]; 02644 LL_payload_send[10]=LL_header_receivebuffer[7]; 02645 LL_payload_send[11]=LL_header_receivebuffer[8]; 02646 LL_payload_send[12]=LL_header_receivebuffer[9]; 02647 LL_payload_send[13]=LL_header_receivebuffer[10]; 02648 LL_payload_send_length=14; // give the correct len 02649 02650 if (LLSendPacket(ACL_CONTROL_MESSAGES_TIMEOUT)) { // NEVER USE ACL FUNCTION here!!! 02651 llState|= LL_STATE_CTRL_MESSAGE_INSERTED; // this means that this msg if special concerning the update of the states 02652 } 02653 } 02654 } 02655 // this was helo 02656 return 1; // there was a control management msg 02657 } 02658 return 0; // there was no control management msg 02659 } 02660 02662 void ACLSetFieldStrength(byte power) { 02663 LLSetFieldStrength(power); 02664 } 02665 02667 byte ACLSendingBusy() { 02668 return LLSendingBusy(); 02669 } 02670 02676 byte ACLGetSendSuccess() { 02677 return LLGetSendSuccess(); 02678 } 02679 02686 byte ACLMatchesMyIP(char *buffer,byte start) { 02687 byte i; 02688 bool res; 02689 02690 // check if was my address // ll_header_send [3..10] hold the myIP 02691 res= TRUE; 02692 for (i=0;i<8;i++) { 02693 if (buffer[start+i] != LL_header_sendbuffer[3+i]) { 02694 res= FALSE; 02695 break; 02696 } 02697 } 02698 if (res) return 1; 02699 02700 // check if was broadcast adress 02701 res= TRUE; 02702 for (i=0;i<8;i++) { 02703 if (buffer[start+i]!=255) { 02704 res= FALSE; 02705 break; 02706 } 02707 } 02708 if (res) return 1; 02709 02710 return 0; 02711 } 02712 02713 02719 byte ACLSendPacket(byte slot_timeout) { 02720 return LLSendPacket(slot_timeout); 02721 } 02722 02724 void ACLAbortSending() { 02725 ACL_send_buffer_locked=false; 02726 ACL_write_position=0; 02727 LLAbortSending(); 02728 } 02729 02731 byte ACLClearSendData() { 02732 byte i; 02733 if (LLSendingBusy()) return 0; // dont clear if busy sending 02734 if (ACL_send_buffer_locked==true) return 0; // dont clear if reservation is active 02735 02736 for(i=0;i<LL_PAYLOAD_SIZE;i++) 02737 LL_payload_send[i]=0; 02738 LL_payload_send_length=0; 02739 ACL_write_position=0; 02740 02741 return 1; 02742 } 02743 02744 02749 bool ACLAddNewType(byte type_h, byte type_l) { 02750 //if (LLSendingBusy()) return 2; // not needed any more, we have a sendbuffer 02751 if ((LL_PAYLOAD_SIZE-LL_payload_send_length)<3) { 02752 return 1; // not enough space 02753 } else { 02754 //place the new type 02755 LL_payload_send[LL_payload_send_length]=type_h; 02756 LL_payload_send[LL_payload_send_length+1]=type_l; 02757 LL_payload_send[LL_payload_send_length+2]=0; //data length is zero 02758 ACL_write_position=LL_payload_send_length+2; 02759 LL_payload_send_length+=3; 02760 return 0; 02761 } 02762 } 02763 02769 byte ACLAddData(byte newByte) { 02770 //if (LLSendingBusy()) return 2; 02771 if ((LL_PAYLOAD_SIZE-LL_payload_send_length)<1) { 02772 return 1; // not enough space 02773 } else { 02774 LL_payload_send[ACL_write_position]++; //increase length byte after type bytes 02775 LL_payload_send[LL_payload_send_length]= newByte; //place the data 02776 LL_payload_send_length+=1; 02777 return 0; 02778 } 02779 } 02780 02785 byte ACLGetRemainingPayloadSpace() { 02786 return LLGetRemainingPayloadSpace(); 02787 } 02788 02793 byte ACLGetReceivedPayloadLength() { 02794 return LL_header_received[1]; 02795 } 02796 02801 signed char ACLGetReceivedDataLength(byte type_h, byte type_l) { 02802 byte i; 02803 02804 for (i=0; i<=LL_header_received[1]-2; i++) { 02805 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02806 return (LL_payload_received[i+2]); //return valid length 02807 } 02808 } 02809 return -1; // type not found 02810 } 02811 02812 02816 char* ACLGetReceivedData(byte type_h, byte type_l) { 02817 byte i; 02818 02819 for (i=0;i<=LL_header_received[1]-2;i++) { 02820 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02821 if (LL_payload_received[i+2]!=0) { 02822 return (&(LL_payload_received[i+3])); // if len is not zero: return valid pointer to data begin 02823 } 02824 } 02825 } 02826 return NULL; 02827 } 02828 02829 02834 char ACLGetReceivedByte(byte type_h, byte type_l, byte position) { 02835 byte i; 02836 02837 for (i=0;i<=LL_header_received[1]-2;i++) { 02838 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02839 if (LL_payload_received[i+2]!=0) { 02840 return (LL_payload_received[i+3+position]); // if len is not zero: return valid pointer to data begin 02841 } 02842 02843 } 02844 } 02845 02846 return 0; 02847 //attention!! if type wasn't found: return is undetermined!! 02848 } 02849 02855 byte ACLFoundReceivedType(byte type_h, byte type_l) { 02856 return (ACLGetReceivedDataLength(type_h,type_l) != -1); //not found 02857 } 02858 02866 void ACLSetControlMessagesBehaviour(boolean ignore, boolean pass) { 02867 ACL_ignore_control_messages= ignore; //all control messages are ignored 02868 ACL_pass_control_messages= pass; // all control messages are as well copied to receivebuffer for application 02869 } 02870 02871 02875 byte ACLSentPacketInThisSlot() { 02876 return (LLSentPacketInThisSlot()); 02877 } 02878 02880 void ACLStart() { 02881 LLStart(); 02882 } 02883 02885 void ACLStop() { 02886 LLStop(); 02887 } 02888 02890 void ACLLockReceiveBuffer() { 02891 LLLockReceiveBuffer(); 02892 } 02893 02897 byte ACLReceiveBufferLocked() { 02898 return LLReceiveBufferLocked(); 02899 } 02900 02902 void ACLReleaseReceiveBuffer() { 02903 LLReleaseReceiveBuffer(); 02904 } 02905 02907 void ACLSetDataToOld() { 02908 LLSetDataToOld(); 02909 } 02910 02912 void ACLSetDataToNew() { 02913 LLSetDataToNew(); 02914 } 02915 02919 byte ACLDataIsNew() { 02920 return LLDataIsNew(); 02921 } 02922 02926 byte ACLDataReceivedInThisSlot() { 02927 return LLSentPacketInThisSlot(); 02928 } 02929 02930 02932 byte ACLDataIsNewNow() { 02933 return ACLDataReceivedInThisSlot(); 02934 } 02935 02936 02940 byte ACLAdressedDataIsNew() { 02941 if (!ACLDataIsNew()) return 0; 02942 02943 if (ACLFoundReceivedType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO)) { 02944 if (ACLMatchesMyIP( ACLGetReceivedData( ACL_TYPE_CAD_HI, ACL_TYPE_CAD_LO), 0 )) 02945 return 1; 02946 } 02947 return 0; 02948 } 02949 02953 byte ACLAdressedDataIsNewNow() { 02954 if (!ACLDataIsNewNow()) return 0; 02955 02956 if (ACLFoundReceivedType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO)) { 02957 if (ACLMatchesMyIP( ACLGetReceivedData( ACL_TYPE_CAD_HI, ACL_TYPE_CAD_LO), 0 )) 02958 return 1; 02959 } 02960 return 0; 02961 } 02962 02963 02969 byte ACLSendPacketAdressed(nodeAddrType *address, byte timeout) { 02970 02971 if (ACLGetRemainingPayloadSpace()<11) 02972 return 0; //not enough space 02973 02974 ACLAddNewType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO); 02975 ACLAddData(address->a1); 02976 ACLAddData(address->a2); 02977 ACLAddData(address->a3); 02978 ACLAddData(address->a4); 02979 ACLAddData(address->a5); 02980 ACLAddData(address->a6); 02981 ACLAddData(address->a7); 02982 ACLAddData(address->a8); 02983 02984 if (ACLSendPacket(timeout)) return 1; 02985 return 0; 02986 } 02988 // end of ACL Layer Functions 02989 02990 02995 void isr_timer0() interrupt INUM_TIMER0 { 02996 byte length; //, time1H, time1L, time2H, time2L; 02997 bool arbitrationResult, recResult; 02998 unsigned short receiveResult; 02999 03000 ISR_TIMER0_ADJUST(moduloTimer0); 03001 INT_SETFLAG(INUM_TIMER0, INT_CLR); 03002 // BLED= LED_ON; // start isr 03003 // BLED= LED_OFF; // start isr 03004 // BLED= LED_ON; // start isr 03005 // BLED= LED_OFF; // start isr 03006 /* time1H= TH0; 03007 time1L= TL0; */ 03008 03009 llState&= ~(LL_STATE_PACKET_JUST_RECEIVED | LL_STATE_PACKET_JUST_SENT); 03010 03011 if (!(rfState & RF_STATE_RUNNING)) { 03012 TIMER0_RUN(FALSE); 03013 return; 03014 } 03015 03016 // this is a slot-counter. every RF_SYNC_MAX_SLOT_COUNT slots, the node sends a sync packet 03017 // (if he is a master) or expects a sync packet to synchronize to. 03018 rfSlotCount++; 03019 rfSlotsWithoutSignalReceived++; 03020 //sendByteToUart(rfSlotCount); 03021 //DEBUG_NEWLINE; 03022 03023 if (rfSlotCount >= RF_SYNC_MAX_SLOT_COUNT) { 03024 // we are in a sync slot. so let's send a sync packet or receive one 03025 //YLED= ~YLED; 03026 //BLED= ~BLED; 03027 if (!(rfState & RF_STATE_ALONE)) { 03028 if (rfState & RF_STATE_MASTER) { 03029 // we are master, so send sync 03030 RFSendPacket(TRUE); 03031 #ifdef RF_USE_ALONE_DETECTION 03032 // try to receive an alone test signal, to know if other nodes are there 03033 if (RFAloneTest()) 03034 rfSlotsWithoutSignalReceived= 0; 03035 #endif 03036 #ifdef RF_USE_TRAFFIC_INDICATION 03037 RFTrafficIndication(); 03038 #endif 03039 if (++rfSyncCount == RF_MASTER_RIVAL_TEST_SYNC_COUNT) { 03040 // many beacon slots have passed, so let's check if we find another master 03041 rfSyncCount= 0; 03042 #ifndef DEBUG_ARBITRATION_TEST_ACTIVE 03043 rfState|= RF_STATE_MASTER_RIVAL_TEST; 03044 if (RFMasterRivalTest()) { 03045 // clear rival test and master flags 03046 rfState&= ~(RF_STATE_MASTER_RIVAL_TEST|RF_STATE_MASTER); 03047 rfFailedSyncCount= 0; 03048 rfSlotsWithoutSignalReceived= 0; 03049 } 03050 #endif 03051 } else { 03052 rfState&= ~RF_STATE_MASTER_RIVAL_TEST; 03053 } 03054 } else { 03055 // we are a slave, so try to receive a sync packet 03056 recResult= RFReceivePacket(&periodDataSyncByteReceiveTimeout, FALSE); 03057 if (recResult == 2) { 03058 #ifdef RF_USE_ALONE_DETECTION 03059 RFAloneTest(); // sends the alone test signal to tell the master that we are there 03060 #endif 03061 #ifdef RF_USE_TRAFFIC_INDICATION 03062 RFTrafficIndication(); 03063 #endif 03064 rfFailedSyncCount= 0; 03065 rfState|= RF_STATE_IN_SYNC; 03066 putchar('R'); 03067 DEBUG_NEWLINE; 03068 YLED= ~YLED; 03069 } else { 03070 rfState&= ~RF_STATE_IN_SYNC; 03071 // if sync is lost for RF_MAX_FAILED_SYNC_COUNT times, we try to become master 03072 if (++rfFailedSyncCount >= RF_MAX_FAILED_SYNC_COUNT) { 03073 rfState|= RF_STATE_LONG_SEARCH_FOR_MASTER; 03074 rfFailedSyncCount= 0; 03075 } 03076 YLED= LED_OFF; 03077 } 03078 } 03079 } 03080 rfSlotCount= 0; 03081 } else { 03082 // we are not in a sync slot (i.e. a regular slot for data transmission) 03083 // first, let's handle the alone stuff 03084 if (rfState & RF_STATE_ALONE) { 03085 #ifdef RF_USE_ALONE_DETECTION 03086 #ifdef RF_ALONE_DEEP_SLEEP 03087 // the node thinks it is alone, and deep sleeping is activated, 03088 // so we put it to sleep for 10-26 seconds. 03089 RFLowPowerSleep(RFAloneRandomNumberOfSeconds()); 03090 rfState&= ~RF_STATE_ALONE; 03091 if (RFLongSearchForMaster(RF_MASTER_SEARCH_SLOTS_AFTER_ALONE)) { 03092 rfState&= ~RF_STATE_MASTER; // clear the master flag, become a slave 03093 } else { 03094 rfState|= RF_STATE_MASTER; // set the master flag, become a master 03095 } 03096 // start the failed sync count and slot count from 0 03097 rfFailedSyncCount= rfSlotCount= rfSlotsWithoutSignalReceived= 0; 03098 #else 03099 // the node thinks it is alone, only transmission supression is activated 03100 if (( (rfSlotsInAloneModeLeft & 0xFF) % 32 ) == 0) 03101 RLED= ~RLED; 03102 //sendUshortToUart(rfSlotsInAloneModeLeft); 03103 //sendByteToUart(rfSlotCount); 03104 //DEBUG_SPACE; 03105 if (rfSlotsInAloneModeLeft-- == 0) { 03106 rfState&= ~RF_STATE_ALONE; 03107 if (RFLongSearchForMaster(RF_MASTER_SEARCH_SLOTS_AFTER_ALONE)) { 03108 rfState&= ~RF_STATE_MASTER; // clear the master flag, become a slave 03109 } else { 03110 rfState|= RF_STATE_MASTER; // set the master flag, become a master 03111 } 03112 // start the failed sync count and slot count from 0 03113 rfFailedSyncCount= rfSlotCount= rfSlotsWithoutSignalReceived= 0; 03114 } 03115 #endif 03116 #endif 03117 } else if (rfState & RF_STATE_LONG_SEARCH_FOR_MASTER) { 03118 // node must do á long search for master. use random length because if a master gives 03119 // up and all slaves find themselves in this state, they shouldn't all at once try 03120 // to become master 03121 if (RFLongSearchForMaster(RF_MASTER_SEARCH_SLOTS_STARTUP + RF_NEW_RANDOM_BYTE())) { 03122 rfState&= ~RF_STATE_MASTER; // clear the master flag, become a slave 03123 #ifdef RF_USE_ALONE_DETECTION 03124 RFAloneTest(); // send the alone test signal 03125 #endif 03126 } else { 03127 rfState|= RF_STATE_MASTER; // set the master flag, become a master 03128 } 03129 rfState&= ~RF_STATE_LONG_SEARCH_FOR_MASTER; // clear the long search flag 03130 // start the failed sync count and slot count from 0 03131 rfFailedSyncCount= rfSlotCount= 0; 03132 } else if (rfState & RF_STATE_MASTER_RIVAL_TEST) { 03133 // do a short search for master just within the protocol 03134 if (RFMasterRivalTest()) { 03135 rfState&= ~RF_STATE_MASTER; 03136 rfState&= ~RF_STATE_MASTER_RIVAL_TEST; 03137 rfFailedSyncCount= rfSlotCount= 0; 03138 } 03139 } else if (rfState & (RF_STATE_IN_SYNC | RF_STATE_MASTER)) { 03140 // if we are in sync or a master and not alone we go on here. 03141 // 03142 // we do an arbitration for rf access. if we want to send a packet 03143 // we draw a random arbitration byte and compete for the medium. if we don't have 03144 // anything to send, choose 0 as arbitration byte so that arbitration can tell us 03145 // if the medium is busy or not. 03146 // 03147 // if node is alone or not in a traffic group, don't do arbitration pretend the 03148 // medium is free. 03149 arbitrationResult= ((rfState & RF_STATE_ALONE) || !(rfState & RF_STATE_TRAFFIC_GROUP)) ? 03150 TRUE : RFArbitration(llState & LL_STATE_NEED_TO_SEND); 03151 //putchar((arbitrationResult) ? 'y' : 'n'); 03152 if (llState & LL_STATE_NEED_TO_SEND) 03153 statsRfSlotsNeedToSend++; 03154 if (arbitrationResult && (llState & LL_STATE_NEED_TO_SEND)) { 03155 statsRfArbitrationWon++; 03156 // if we are alone, we just pretend to send the packet and set the flags 03157 // if we are not in a traffic group we don't send anything and don't update the flags 03158 if ((rfState & RF_STATE_TRAFFIC_GROUP) && !(rfState & RF_STATE_ALONE)) 03159 RFSendPacket(FALSE); 03160 if (rfState & RF_STATE_TRAFFIC_GROUP) { 03161 llState&= ~(LL_STATE_NEED_TO_SEND); 03162 llState|= LL_STATE_SEND_SUCCESS | LL_STATE_PACKET_JUST_SENT; 03163 } 03164 } else { 03165 // only receive a packet if we neither detected a node that wants to send (arbitrationResult == false) 03166 // nor the receive buffer is locked 03167 if ( !(arbitrationResult || (llState & LL_STATE_RECEIVE_BUFFER_LOCKED)) ) { 03168 // for return type of RFReceivePacket() see documentation of that function 03169 // if we are an alone master, we don't need to receive a packet 03170 receiveResult= (rfState & RF_STATE_ALONE) ? 0 : RFReceivePacket(&periodDataSyncByteReceiveTimeout, TRUE); 03171 //sendByteToUart(receiveResult >> 8); 03172 //sendByteToUart(receiveResult); 03173 //DEBUG_SPACE; 03174 if ((receiveResult & 0xFF) == 1) { 03175 length= receiveResult >> 8; 03176 /* #ifdef DEBUG_USE_UART 03177 sendByteToUart(length); 03178 DEBUG_NEWLINE; 03179 #endif */ 03180 LL_payload_received_length= length; 03181 statsRfDataPacketsReceivedNotNeedingToSend++; 03182 llState|= LL_STATE_NEW_DATA | LL_STATE_PACKET_JUST_RECEIVED; 03183 rfSlotsWithoutSignalReceived= 0; 03184 } 03185 } 03186 } 03187 statsRfSlotsInSync++; 03188 } else { 03189 // if we are not in sync, only try to receive a packet because sending could damage 03190 // other packets or disturb an arbitration procedure. 03191 if ( !(llState & LL_STATE_RECEIVE_BUFFER_LOCKED) ) { 03192 // for return type of RFReceivePacket() see documentation of that function 03193 // if we are an alone master, we don't need to receive a packet 03194 receiveResult= (rfState & RF_STATE_ALONE) ? 0 : RFReceivePacket(&periodDataSyncByteReceiveTimeout, TRUE); 03195 //sendByteToUart(receiveResult >> 8); 03196 //sendByteToUart(receiveResult); 03197 //DEBUG_SPACE; 03198 if ((receiveResult & 0xFF) == 1) { 03199 length= receiveResult >> 8; 03200 /* #ifdef DEBUG_USE_UART 03201 sendByteToUart(length); 03202 DEBUG_NEWLINE; 03203 #endif */ 03204 LL_payload_received_length= length; 03205 llState|= LL_STATE_NEW_DATA | LL_STATE_PACKET_JUST_RECEIVED; 03206 rfSlotsWithoutSignalReceived= 0; 03207 } 03208 } 03209 } 03210 } // end of if that determines if current slot is a sync slot 03211 03212 // check if packet has been sent or sending slot timeout occurred and set the right llState. 03213 // this used to be done in LLSlotEnd(), but has moved here for efficiency. 03214 if ((llState & LL_STATE_NEED_TO_SEND)) { 03215 if (--LL_slots_left == 0) { 03216 03217 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 03218 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; // in case it was switched on, switch it off and do NOT update the success state 03219 } else { 03220 llState&= ~LL_STATE_SEND_SUCCESS; // busy is over 03221 } 03222 03223 llState&= ~LL_STATE_NEED_TO_SEND; 03224 LL_payload_sendbuffer_length= 0; // set used payload to zero 03225 } 03226 } 03227 03228 #ifdef RF_USE_ALONE_DETECTION 03229 if (rfSlotsWithoutSignalReceived > RF_ALONE_TEST_FAILED_THRESHOLD && !(rfState & RF_STATE_ALONE)) { 03230 rfState|= RF_STATE_ALONE; 03231 #ifndef RF_ALONE_DEEP_SLEEP 03232 rfSlotsInAloneModeLeft= RFAloneRandomNumberOfSlots(); 03233 #endif 03234 } 03235 #endif 03236 03237 #ifdef DEBUG_USE_UART 03238 #ifdef DEBUG_DUMP_RF_STATISTICS 03239 DebugDumpStats(); 03240 #endif 03241 #ifdef DEBUG_DUMP_RF_STATE 03242 DebugDumpRfState(); 03243 #endif 03244 #endif 03245 // BLED= LED_ON; // end isr 03246 // BLED= LED_OFF; // end isr 03247 // BLED= LED_ON; // end isr 03248 // BLED= LED_OFF; // end isr 03249 } //end isr_timer0() 03250 03251 03252 03257 void isr_timer2() interrupt INUM_TIMER2 { 03258 INT_SETFLAG(INUM_TIMER2, INT_CLR); 03259 } 03260 03261 03262 /***************************************************************************** 03263 MAIN PROGRAM 03264 *****************************************************************************/ 03269 void main(void) { 03270 byte i, j=0; 03271 03272 // sets up the board 03273 ParticleInit(); 03274 03275 // initializes and starts all communications layers 03276 ACLStartUp(); 03277 ACLSubscribeAll(); 03278 03279 i= 1; 03280 while (TRUE) { 03281 i++; 03282 03283 /* if (LLSentPacketInThisSlot()) { 03284 putchar('>'); 03285 sendByteToUart(90-LL_slots_left); 03286 DEBUG_NEWLINE; 03287 } 03288 03289 // compose packet content 03290 if (!ACLSendingBusy() && (i % 128 == 0)) { 03291 //putchar('x'); 03292 //DEBUG_NEWLINE; 03293 ACLAddNewType(ACL_TYPE_ASM_HI, ACL_TYPE_ASM_LO); 03294 ACLAddData(j++); 03295 ACLSendPacket(90); 03296 } 03297 03298 if (!ACLGetSendSuccess()) { 03299 putchar('F'); 03300 DEBUG_NEWLINE; 03301 // reset the flag 03302 llState|= LL_STATE_SEND_SUCCESS; 03303 } */ 03304 03305 //sendByteToUart(LL_payload_send_length); 03306 //DEBUG_SPACE; 03307 03308 if (ACLDataIsNew()) { 03309 // lock buffer 03310 ACLLockReceiveBuffer(); 03311 03312 #ifdef DEBUG_USE_UART 03313 //putchar((ACLDataIsNew()) ? 'y' : 'n'); 03314 //putchar((llState & LL_STATE_NEW_DATA) ? 'y' : 'n'); 03315 //DEBUG_SPACE; 03316 DebugDumpReceivedAclPacket(); 03317 //DebugDumpRfState(); 03318 #endif 03319 03320 // release lock 03321 ACLReleaseReceiveBuffer(); 03322 } 03323 RF_LOW_POWER_SLEEP_UNTIL_NEXT_SLOT(); 03324 03325 // dump statistics every 256th time 03326 // if (i++ == 0) { 03327 // DebugDumpStats(); 03328 // } 03329 } 03330 } // end of main()

Generated on Fri Aug 27 10:04:04 2004 for Cheap Sensor Network by doxygen 1.3.8