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 00117 //#define DEBUG_ARBITRATION_TEST_0 00118 //#define DEBUG_ARBITRATION_TEST_PATTERN 00119 //#define DEBUG_ARBITRATION_TEST_COUNT 00120 00125 #ifdef DEBUG_ARBITRATION_TEST_PATTERN 00126 #define DEBUG_ARBITRATION_TEST_ACTIVE 00127 #endif 00128 #ifdef DEBUG_ARBITRATION_TEST_COUNT 00129 #define DEBUG_ARBITRATION_TEST_ACTIVE 00130 #endif 00131 #ifdef DEBUG_ARBITRATION_TEST_0 00132 #define DEBUG_ARBITRATION_TEST_ACTIVE 00133 #endif 00134 00136 00139 // the possible states of the rf layer 00145 #define RF_STATE_MASTER BIN(00000001) 00146 00151 #define RF_STATE_ALONE BIN(00000010) 00152 00157 #define RF_STATE_IN_SYNC BIN(00000100) 00158 00164 #define RF_STATE_TRAFFIC_GROUP BIN(00001000) 00165 00170 #define RF_STATE_LONG_SEARCH_FOR_MASTER BIN(00010000) 00171 00178 #define RF_STATE_MASTER_RIVAL_TEST BIN(00100000) 00179 00181 #define RF_STATE_RUNNING BIN(10000000) 00182 00202 data volatile byte rfState= 0; 00203 00204 #ifdef DEBUG_USE_UART 00205 #ifdef DEBUG_DUMP_RF_STATE 00206 data volatile byte rfStateOld= 0xFF; 00207 #endif 00208 #endif 00209 00214 #define RF_SYNC_MAX_SLOT_COUNT 10 00215 00220 //#define RF_USE_ALONE_DETECTION 00221 00226 #define RF_ALONE_TEST_FAILED_THRESHOLD 167 00227 00239 #define RF_ALONE_DEEP_SLEEP 00240 00242 #define RF_PREAMBLE_LENGTH_TX 20 00243 00245 #define RF_PREAMBLE_LENGTH_RX 20 00246 00254 #define RF_MAX_SYNC_JITTER 5769ul 00255 00260 #define RF_SLOT_LENGTH_IN_US 36865ul 00261 00267 #define RF_MASTER_SEARCH_SLOTS_STARTUP 50 00268 00272 #define RF_MASTER_SEARCH_SLOTS_AFTER_ALONE 167 00273 00281 #define RF_MAX_FAILED_SYNC_COUNT 6 00282 00287 #define RF_MASTER_RIVAL_TEST_SYNC_COUNT 30 00288 00297 #define RF_SYNC_ADDITIONAL_OFFSET 0x89B3 00298 00312 #define RF_ARBITRATION_RSSI_THRESHOLD 0x64 00313 00321 #define RF_ALONE_TEST_RSSI_THRESHOLD 0xC0 00322 00328 #define RF_RENDEZVOUS_SYNC_START_HI 0x7C 00329 #define RF_RENDEZVOUS_SYNC_START_LO 0xC8 00330 00337 #define RF_RENDEZVOUS_DATA_PACKET_START_HI 0x93 00338 #define RF_RENDEZVOUS_DATA_PACKET_START_LO 0x12 00339 00345 #define RF_RENDEZVOUS_ARBITRATION_START_HI 0x71 00346 #define RF_RENDEZVOUS_ARBITRATION_START_LO 0x88 00347 00357 #define RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS 0x390 00358 00359 00360 00364 #define RF_BUFFER_SIZE 64 00365 00372 //#define RF_USE_TRAFFIC_INDICATION 00373 00378 #define RF_RENDEZVOUS_TRAFFIC_INDICATION_HI 0x92 00379 #define RF_RENDEZVOUS_TRAFFIC_INDICATION_LO 0x60 00380 00385 #define RF_RENDEZVOUS_ALONE_TEST_HI 0x8C 00386 #define RF_RENDEZVOUS_ALONE_TEST_LO 0xBA 00387 00389 static byte rfSlotCount= 0; 00390 00392 static byte rfSyncCount= 0; 00393 00395 static byte rfFailedSyncCount= 0; 00396 00401 data byte rfScramblerReg= BIN(01101100); 00402 00404 data unsigned long randomNumberReg; 00405 00411 unsigned short rfSlotsWithoutSignalReceived= 0; 00412 00418 data unsigned short tEndAvg= 0; 00420 //data unsigned long rfAverageTEndBuffer; 00421 00423 xdata TIMER_DATA periodDataSlot; 00424 xdata TIMER_DATA periodDataSyncByteReceiveTimeout; 00425 00427 xdata signed short moduloTimer0; 00428 00429 #ifdef DEBUG_USE_UART 00430 #ifdef DEBUG_DUMP_RSSI_SAMPLES 00431 00432 xdata byte rssiSamples[16]; 00434 unsigned short arbitrationGood= 0; 00436 unsigned short arbitrationBad= 0; 00437 #endif 00438 #endif 00439 00453 RF_RXTXPAIR_SETTINGS code RF_SETTINGS = { 00454 0xA0, 0x2F, 0x52, // Modem 0, 1 and 2 00455 0x76, 0x00, 0x00, // Freq A 00456 0x58, 0x7A, 0x8D, // Freq B 00457 0x01, 0xAB, // FSEP 1 and 0 00458 0x40, // PLL_RX 00459 0x30, // PLL_TX 00460 0x6C, // CURRENT_RX 00461 0xF3, // CURRENT_TX 00462 0x32, // FREND 00463 0x0F, // PA_POW 00464 0x00, // MATCH 00465 0x00, // PRESCALER 00466 }; 00468 xdata RF_RXTXPAIR_CALDATA RF_CALDATA; 00469 00480 xdata byte rfFieldStrength; 00481 00482 #ifdef RF_USE_ALONE_DETECTION 00483 #ifndef RF_ALONE_DEEP_SLEEP 00484 00490 xdata unsigned short rfSlotsInAloneModeLeft; 00491 #endif 00492 #endif 00493 00494 00495 00498 00499 #define LL_PROTOCOL_VERSION 4 00500 #define LL_HEADER_SIZE 12 00501 #define LL_PAYLOAD_SIZE 64 00502 #define LL_TAIL_SIZE 2 00503 #define LL_FRAME_MAX_SIZE (LL_HEADER_SIZE+LL_PAYLOAD_SIZE+LL_TAIL_SIZE) 00504 00505 #define LL_CRC16_POLY 0x1021 00506 00508 #define LL_CRC16_INIT 0xFFFF 00509 00511 #define LL_CRC_OK 0 00512 00514 xdata byte LL_sequence_no; 00515 00516 byte LL_slots_left; 00517 00519 #define LL_STATE_SEND_SUCCESS BIN(00000001) 00520 00521 #define LL_STATE_CTRL_MESSAGE_INSERTED BIN(00000010) 00522 00526 #define LL_STATE_NEED_TO_SEND BIN(00000100) 00527 00528 #define LL_STATE_RECEIVE_BUFFER_LOCKED BIN(00001000) 00529 00530 #define LL_STATE_NEW_DATA BIN(00010000) 00531 00532 #define LL_STATE_PACKET_JUST_SENT BIN(00100000) 00533 00534 #define LL_STATE_PACKET_JUST_RECEIVED BIN(01000000) 00535 00536 #define LL_STATE_RUNNING BIN(10000000) 00537 00557 data volatile byte llState= 0; 00558 data static byte llStateOld= 0xFF; 00559 00560 code nodeAddrType llEpromAddress= {10, 1, 0, 1, 10, 1, 2, 0}; 00562 00565 00567 #define ACL_SUBSCRIPTIONLIST_LENGTH 8 00568 00570 #define ACL_CONTROL_MESSAGES_TIMEOUT 30 00571 00573 #define ACL_TYPE_ACM_H ACL_TYPE_ACM_HI 00574 00575 #define ACL_TYPE_ACM_L ACL_TYPE_ACM_LO 00576 00577 00578 xdata byte ACL_subscriptions[ACL_SUBSCRIPTIONLIST_LENGTH][2]; 00579 xdata boolean ACL_send_buffer_locked; 00580 xdata boolean ACL_subscribe_all; 00581 xdata boolean ACL_ACM_answers; // answer on ACM messages like helo and so on 00582 xdata byte ACL_write_position; 00583 00584 xdata boolean ACL_ignore_control_messages; 00585 xdata boolean ACL_pass_control_messages; 00587 00593 00594 00595 xdata byte LL_header_received[LL_HEADER_SIZE]; 00597 xdata byte LL_payload_received[LL_PAYLOAD_SIZE]; 00599 xdata byte LL_payload_received_length; 00601 xdata byte LL_tail_received[LL_TAIL_SIZE]; 00603 00610 00611 00612 volatile xdata byte LL_header_receivebuffer[LL_HEADER_SIZE]; 00614 volatile xdata byte LL_payload_receivebuffer[LL_PAYLOAD_SIZE]; 00616 volatile xdata byte LL_payload_receivebuffer_length; 00618 volatile xdata byte LL_tail_receivebuffer[LL_TAIL_SIZE]; 00620 00627 00628 00629 xdata byte LL_payload_send[LL_PAYLOAD_SIZE]; 00631 xdata byte LL_payload_send_length; 00633 00640 00641 00642 xdata byte LL_header_sendbuffer[LL_HEADER_SIZE]; 00644 xdata byte LL_payload_sendbuffer[LL_PAYLOAD_SIZE]; 00646 xdata byte LL_payload_sendbuffer_length; 00648 xdata byte LL_tail_sendbuffer[LL_TAIL_SIZE]; 00650 00653 00654 xdata unsigned long statsRfSlotsInSync; 00655 00657 xdata unsigned long statsRfSlotsNeedToSend; 00659 xdata unsigned long statsRfArbitrationWon; 00660 00661 /* /// those of the statsRfSlotsNeedToSend where arbitration was lost 00662 xdata unsigned long statsRfArbitrationLost; 00664 xdata unsigned long statsRfDataPacketsReceivedNeedingToSend; 00665 00667 xdata unsigned long statsRfArbitrationSingalDetected; 00669 xdata unsigned long statsRfArbitrationNoSingalDetected; */ 00670 00672 xdata unsigned long statsRfDataPacketsReceivedNotNeedingToSend; 00674 00677 #ifdef DEBUG_USE_UART 00678 #ifdef DEBUG_DUMP_RF_STATE 00679 00682 void DebugDumpRfState() { 00683 if (rfState != rfStateOld || llState != llStateOld) { 00684 putchar((rfState & RF_STATE_RUNNING) ? 'R' : 'S'); 00685 putchar('-'); 00686 putchar((rfState & RF_STATE_MASTER_RIVAL_TEST) ? 'R' : '-'); 00687 putchar((rfState & RF_STATE_LONG_SEARCH_FOR_MASTER) ? 'L' : '-'); 00688 putchar((rfState & RF_STATE_TRAFFIC_GROUP) ? 'T' : '-'); 00689 putchar((rfState & RF_STATE_IN_SYNC) ? 'Y' : '-'); 00690 putchar((rfState & RF_STATE_ALONE) ? 'A' : '-'); 00691 putchar((rfState & RF_STATE_MASTER) ? 'M' : 'S'); 00692 DEBUG_SPACE; 00693 rfStateOld= rfState; 00694 putchar((llState & LL_STATE_RUNNING) ? 'R' : '-'); 00695 putchar((llState & LL_STATE_PACKET_JUST_RECEIVED) ? 'J' : '-'); 00696 putchar((llState & LL_STATE_PACKET_JUST_SENT) ? 'J' : '-'); 00697 putchar((llState & LL_STATE_NEW_DATA) ? 'N' : '-'); 00698 putchar((llState & LL_STATE_RECEIVE_BUFFER_LOCKED) ? 'L' : '-'); 00699 putchar((llState & LL_STATE_NEED_TO_SEND) ? 'S' : '-'); 00700 putchar((llState & LL_STATE_CTRL_MESSAGE_INSERTED) ? 'C' : '-'); 00701 putchar((llState & LL_STATE_SEND_SUCCESS) ? 'S' : '-'); 00702 DEBUG_NEWLINE; 00703 llStateOld= llState; 00704 } 00705 } 00706 #endif 00707 #endif 00708 00713 #ifdef DEBUG_USE_UART 00714 void DebugDumpReceivedAclPacket() { 00715 int i, dataEnd; 00716 00717 ACLLockReceiveBuffer(); 00718 00719 // dump last byte of sender address 00720 putchar('A'); 00721 putchar(':'); 00722 sendByteToUart(LL_header_received[3+7]); 00723 DEBUG_SPACE; 00724 // dump packet content 00725 for (i=0; i < LL_payload_received_length;) { 00726 sendByteToUart(LL_payload_received[i++]); 00727 sendByteToUart(LL_payload_received[i++]); 00728 dataEnd= i + LL_payload_received[i++]; 00729 putchar(':'); 00730 while (i < dataEnd && i < LL_payload_received_length) 00731 sendByteToUart(LL_payload_received[i++]); 00732 DEBUG_SPACE; 00733 } 00734 DEBUG_NEWLINE; 00735 } 00736 00737 #endif 00738 00743 void putchar(char c) { 00744 #ifdef DEBUG_USE_UART 00745 #ifdef DEBUG_USE_UART1_FOR_DUMPING 00746 UART1_WAIT_AND_SEND(c); 00747 #else 00748 UART0_WAIT_AND_SEND(c); 00749 #endif 00750 #endif 00751 } 00752 00756 void DebugBuildTestPacketACL() { 00757 /* 00758 byte i; 00759 // this is only testcode 00760 00761 ACLAddNewType(ACL_TYPE_ACM_H,ACL_TYPE_ACM_L); 00762 ACLAddNewType(115,216); //helo 00763 00764 ACLAddData(255); 00765 ACLAddData(255); 00766 ACLAddData(255); 00767 ACLAddData(255); 00768 00769 ACLAddData(255); 00770 ACLAddData(255); 00771 ACLAddData(255); 00772 ACLAddData(255); 00773 00774 00775 //ACLAddNewType(100,100); 00776 //for (i=0;i<32;i++) 00777 // ACLAddData(i); 00778 */ 00779 } 00780 00781 00782 #ifdef DEBUG_VERIFY_SCRAMBLING 00783 bool DebugScrambleAndDescrambleTest() { 00784 byte b, j; 00785 byte scrReg; 00786 00787 // test scrambling and descrambling for some numbers 00788 j=0; 00789 //YLED= LED_ON; 00790 do { 00791 b= j; 00792 //YLED= ~YLED; 00793 00794 // scramble b 00795 scrReg= rfScramblerReg; 00796 b= RFScramble(j); 00797 00798 // descramble b 00799 rfScramblerReg= scrReg; 00800 b= RFDescramble(b); 00801 00802 if (b != j) { 00803 //YLED= LED_OFF; 00804 return FALSE; 00805 } 00806 00807 rfScramblerReg= scrReg; 00808 } while (++j); 00809 //YLED= LED_OFF; 00810 return TRUE; 00811 } 00812 #endif 00813 00814 /* 00816 void waitUsingTimer2(TIMER_DATA *timeOutPeriodData) { 00817 TIMER2_RUN(FALSE); 00818 INT_SETFLAG(INUM_TIMER2, INT_CLR); 00819 halConfigTimerFast23(TIMER2|TIMER23_NO_INT_TIMER, timeOutPeriodData); 00820 TIMER2_RUN(TRUE); 00821 00822 while (1) { 00823 if (INT_GETFLAG(INUM_TIMER2)) { 00824 INT_SETFLAG(INUM_TIMER2, INT_CLR); 00825 break; 00826 } 00827 } 00828 } 00829 */ 00830 00831 #ifdef DEBUG_USE_UART 00832 #ifdef DEBUG_DUMP_RF_STATISTICS 00833 00837 void DebugResetStats() { 00838 statsRfSlotsInSync= 0; 00839 00840 statsRfSlotsNeedToSend= 0; 00841 statsRfArbitrationWon= 0; 00842 00843 statsRfDataPacketsReceivedNotNeedingToSend= 0; 00844 } 00845 00849 void DebugDumpStats() { 00850 //sendUlongToUart(statsRfSlotsInSync); 00851 //putchar(':'); 00852 sendUlongToUart(statsRfSlotsNeedToSend); 00853 putchar(':'); 00854 sendUlongToUart(statsRfArbitrationWon); 00855 putchar(':'); 00856 sendUlongToUart(statsRfDataPacketsReceivedNotNeedingToSend); 00857 DEBUG_NEWLINE; 00858 } 00859 #endif 00860 #endif 00861 00862 00863 00869 00870 void ParticleInit() { 00871 #ifdef DEBUG_USE_UART 00872 byte i; 00873 #endif 00874 00875 // Initialize peripherals 00876 //WDT_ENABLE(FALSE); 00877 // for BIZARRE reasons, the watchdog timer is only disabled when the 00878 // WDT register is set directly and TWICE (???). found this out by trial and error. 00879 WDT= 0x13; 00880 WDT= 0x13; 00881 00882 // Set optimum settings for speed and low power consumption 00883 MEM_NO_WAIT_STATES(); 00884 FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS); 00885 00886 // setup leds 00887 RLED_OE(TRUE); 00888 YLED_OE(TRUE); 00889 GLED_OE(TRUE); 00890 BLED_OE(TRUE); 00891 RLED= YLED= GLED= BLED= LED_OFF; 00892 00893 TIMER0_RUN(FALSE); 00894 00895 // generate a truly random seed for the random number generator 00896 // rf must not be used when this happens 00897 RFInitRandom(); 00898 00899 #ifdef DEBUG_VERIFY_SCRAMBLING 00900 // make sure scrambling and descrambling macros work correctly 00901 if (!DebugScrambleAndDescrambleTest()) { 00902 RLED= LED_ON; 00903 while (1); 00904 } 00905 #endif 00906 00907 #ifdef DEBUG_USE_UART 00908 #ifdef DEBUG_DUMP_RSSI_SAMPLES 00909 for (i=0; i<3*8; i++) { 00910 rssiSamples[i]= 0; 00911 } 00912 #endif 00913 #endif 00914 00915 #ifdef DEBUG_USE_UART 00916 // setup UART for serial debug messages 00917 #ifdef DEBUG_USE_UART1_FOR_DUMPING 00918 UART1_SETUP(57600, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_TX_ONLY | UART_POLLED); 00919 #else 00920 UART0_SETUP(57600, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_TX_ONLY | UART_POLLED); 00921 #endif 00922 00923 DEBUG_CLEAR_SCREEN; 00924 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"); 00925 00926 for (i=0; i<10; i++) { 00927 sendByteToUart(RF_NEW_RANDOM_BYTE()); 00928 DEBUG_SPACE; 00929 } 00930 DEBUG_NEWLINE; 00931 #endif 00932 00933 // let the 32 kHz clock run continously 00934 X32_INPUT_SOURCE(X32_USING_CRYSTAL); 00935 X32_ENABLE(TRUE); 00936 } 00937 00938 byte AppSelfTest(char* result) { 00939 int i; 00940 00941 for (i=0; i<10; i++) { 00942 result[i]= RF_NEW_RANDOM_BYTE(); 00943 } 00944 00945 return TRUE; 00946 } 00948 00951 00953 void RFInit() { 00954 ulong tmp; 00955 00956 // Calibration (now done in rfStart) 00957 // halRFCalib(&RF_SETTINGS, &RF_CALDATA); 00958 00959 // calculate timer periods for timer 2 (timeouts) 00960 halCalcTimerPeriod(RF_SLOT_LENGTH_IN_US, CC1010EB_CLKFREQ, &periodDataSlot); 00961 halCalcTimerPeriod(RF_MAX_SYNC_JITTER, CC1010EB_CLKFREQ, &periodDataSyncByteReceiveTimeout); 00962 00963 // Set up timer 0 as the slot timer 00964 TIMER0_RUN(FALSE); 00965 tmp= halConfigTimer01(TIMER0|TIMER01_INT_TIMER, RF_SLOT_LENGTH_IN_US, CC1010EB_CLKFREQ, &moduloTimer0); 00966 #ifdef DEBUG_USE_UART 00967 sendStringToUart("\r\nTimer start val:\r\n"); 00968 sendUshortToUart(moduloTimer0); 00969 DEBUG_NEWLINE; 00970 #endif 00971 00972 // Turn on interrupts for timer0, turn off for timer 2 00973 INT_GLOBAL_ENABLE(INT_ON); 00974 INT_ENABLE(INUM_TIMER0, INT_ON); 00975 INT_ENABLE(INUM_TIMER2, INT_OFF); 00976 PA_POW= 0; 00977 00978 // set current field strength from data structure 00979 rfFieldStrength= RF_SETTINGS.pa_pow; 00980 00981 // Calibration 00982 halWait(200, CC1010EB_CLKFREQ); 00983 halRFCalib(&RF_SETTINGS, &RF_CALDATA); 00984 00985 // the function halRFSetRxTxOff() has to be called at least once, because later, we use 00986 // the simpler functions RFstartRx and RFstartTx instead, that don't set the values needed. 00987 halRFSetRxTxOff(RF_RX, &RF_SETTINGS, &RF_CALDATA); 00988 halWait(200, CC1010EB_CLKFREQ); 00989 halRFSetRxTxOff(RF_TX, &RF_SETTINGS, &RF_CALDATA); 00990 halWait(200, CC1010EB_CLKFREQ); 00991 halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 00992 halWait(200, CC1010EB_CLKFREQ); 00993 00994 // turn on ADC for RSSI reading (needs 200ms, because it waits until ADC has stabilized) 00995 halRFReadRSSIlevel(RSSI_MODE_INIT); 00996 ADC_POWER(FALSE); 00997 00998 } 00999 01001 void RFStart() { 01002 PA_POW= 0; 01003 01004 // set initial rf state 01005 rfState= RF_STATE_RUNNING | RF_STATE_LONG_SEARCH_FOR_MASTER; // RF_STATE_MASTER | RF_STATE_ALONE; // 01006 #ifndef RF_USE_TRAFFIC_INDICATION 01007 // if traffic indication is not used, the flag has to be set permanently, so that 01008 // all packets are sent and received immediately 01009 rfState|= RF_STATE_TRAFFIC_GROUP; 01010 #endif 01011 01012 #ifdef RF_USE_ALONE_DETECTION 01013 rfSlotsWithoutSignalReceived= 0; 01014 #endif 01015 01016 01017 #ifdef DEBUG_USE_UART 01018 #ifdef DEBUG_DUMP_RF_STATE 01019 DebugDumpRfState(); 01020 #endif 01021 #endif 01022 01023 // start timer0 01024 INT_SETFLAG(INUM_TIMER0, INT_CLR); 01025 TIMER0_RUN(TRUE); 01026 } 01027 01029 void RFStop() { 01030 // clear flag RF_STATE_RUNNING and all other flags 01031 rfState= 0; 01032 // stop timer0 01033 TIMER0_RUN(FALSE); 01034 // stop sending 01035 RF_SET_MODE_SLEEP; 01036 // turn off rf power 01037 //PA_POW= 0; 01038 } 01039 01044 void RFSetModeTransmit() { 01045 code byte *rfSettingsPtr= (code byte *)&RF_SETTINGS; 01046 unsigned short i; 01047 01048 PA_POW= 0; 01049 01050 // turn on transceiver for tx-ing 01051 PLL=rfSettingsPtr[12]; // pll_tx 01052 RFMAIN=0xF0; 01053 TEST5=0x10 | RF_CALDATA.vco_ao_tx; 01054 TEST6=0x3B; 01055 CURRENT=rfSettingsPtr[14]; 01056 01057 // Wait 200 usec before monitoring LOCK, slowly raise rf power to reduce spurious emission. 01058 // the data sheet says 200 usec for each step is optimal, but this is too long for our application 01059 // we use 50 us for each step. this should mitigate spurious emission. 01060 for(i=LOCK_MONITOR_DELAY; i>0; i--); 01061 01062 /* for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01063 if (rfFieldStrength >= 0x01) 01064 PA_POW= 0x01; 01065 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01066 if (rfFieldStrength >= 0x1E) 01067 PA_POW= 0x1E; 01068 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01069 if (rfFieldStrength >= 0x8F) 01070 PA_POW= 0x8F; 01071 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01072 PA_POW= rfFieldStrength; */ 01073 01074 // Wait for lock (much less than normal version!) 01075 for(i=LOCK_TIMEOUT; !(LOCK&0x01) && (i>0); i--); 01076 PA_POW= rfFieldStrength; 01077 } 01078 01083 void RFSetModeReceive() { 01084 code byte *rfSettingsPtr= (code byte *)&RF_SETTINGS; 01085 unsigned short i; 01086 01087 // turn on transceiver for rx-ing 01088 PLL=rfSettingsPtr[11]; // pll_rx 01089 RFMAIN=0x30; 01090 TEST5=0x10 | RF_CALDATA.vco_ao_rx; 01091 TEST6=0x20 | RF_CALDATA.chp_co_rx; 01092 CURRENT=rfSettingsPtr[13]; 01093 01094 // Wait 200 usec before monitoring LOCK 01095 // notice: had to change the value of constant LOCK_MONITOR_DELAY, because 01096 // it produced a wait time of 418 us -> sdcc seems to have less efficient 01097 // integer algorithms. perhaps keil sees that LOCK_MONITOR_DELAY is < 256 01098 // and temporary uses i as a byte variable (?), which is much faster 01099 for(i=LOCK_MONITOR_DELAY; i>0; i--); 01100 01101 // Wait for lock (much less than normal version!) 01102 for(i=LOCK_TIMEOUT; !(LOCK&0x01) && (i>0); i--); 01103 } 01104 01106 void RFInitRandom() { 01107 byte a; 01108 01109 randomNumberReg= 0; 01110 do { 01111 halRandomNumberGen(&a, 1); 01112 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01113 randomNumberReg= (ulong)a << 24; 01114 do { 01115 halRandomNumberGen(&a, 1); 01116 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01117 randomNumberReg|= (ulong)a << 16; 01118 do { 01119 halRandomNumberGen(&a, 1); 01120 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01121 randomNumberReg|= (ulong)a << 8; 01122 do { 01123 halRandomNumberGen(&a, 1); 01124 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01125 randomNumberReg|= (ulong)a; 01126 01127 for (a=0; a<128; a++) 01128 RF_NEW_RANDOM_BYTE(); 01129 } 01130 01136 byte RFDescramble(byte b) { 01137 byte i, scrReg; 01138 01139 scrReg= rfScramblerReg; 01140 RF_DESCRAMBLE(b, scrReg, i); 01141 rfScramblerReg= scrReg; 01142 return b; 01143 } 01144 01173 unsigned short RFReceivePacket(xdata TIMER_DATA *timeOutPeriodData, bool onlyDataPackets) { 01174 bool syncByteReceived, aclMatch; 01175 byte j, tHi, tLo, a, b, length, sync; 01176 unsigned short crc; 01177 01178 // Turn on RF for RX, do this early, because it needs 200us to power up 01179 RFSetModeReceive(); 01180 //halRFSetRxTxOff(RF_RX, &RF_SETTINGS, &RF_CALDATA); 01181 //BLED= ~BLED; 01182 RFCON|=0x01; // Ensure that bytemode is selected 01183 RF_SET_PREAMBLE_COUNT(RF_PREAMBLE_LENGTH_RX); 01184 RF_SET_SYNC_BYTE(RF_SUITABLE_SYNC_BYTE); 01185 MODEM1=(MODEM1&0x03)|0x24; // Make sure avg filter is free-running + 22 baud settling time 01186 INT_ENABLE(INUM_RF, INT_OFF); 01187 INT_SETFLAG(INUM_RF, INT_CLR); 01188 01189 if (onlyDataPackets) { 01190 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_DATA_PACKET_START_HI, RF_RENDEZVOUS_DATA_PACKET_START_LO); 01191 } 01192 BLED= LED_ON; // start receiving 01193 RF_START_RX(); 01194 01195 // setup timer 2 as a no-interrupt timer for timeout of the master search process 01196 halConfigTimerFast23(TIMER2|TIMER23_NO_INT_TIMER, timeOutPeriodData); 01197 //halConfigTimer23(TIMER2|TIMER23_NO_INT_TIMER, 5000, CC1010EB_CLKFREQ); 01198 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01199 TIMER2_RUN(TRUE); 01200 //BLED= ~BLED; 01201 01202 // receive sync byte 01203 syncByteReceived= FALSE; 01204 while (1) { 01205 // Check if timer2 is finished so that we time out 01206 // alternatively, if interrupt for timer0 is enabled, also check if enough time is left 01207 // before the next slot 01208 if (INT_GETFLAG(INUM_TIMER2) || (ET0 == INT_ON && !RF_SLOT_500_US_LEFT)) { 01209 // Clear interrupt and timeout 01210 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01211 //RLED= ~RLED; 01212 break; 01213 } 01214 // Check if sync byte received 01215 if (INT_GETFLAG(INUM_RF)) { 01216 INT_SETFLAG(INUM_RF, INT_CLR); // Clear the flag 01217 syncByteReceived= TRUE; 01218 break; 01219 } 01220 } 01221 //BLED= ~BLED; 01222 //BLED= ~BLED; 01223 01224 length= 0; 01225 if (syncByteReceived) { 01226 // turn on blue led when sync byte is received 01227 //BLED= ~BLED; 01228 //YLED= ~YLED; 01229 01230 // Lock average filter 01231 RF_LOCK_AVERAGE_FILTER(TRUE); 01232 01233 // get scrambler init 01234 //RF_SPI_RECEIVE_BYTE(rfScramblerReg); 01235 01236 // receveive scrambled FF to train local descrambling register 01237 RF_SPI_RECEIVE_BYTE(a); 01238 a= RFDescramble(a); 01239 01240 // get rf options byte 01241 RF_SPI_RECEIVE_BYTE(a); 01242 a= RFDescramble(a); 01243 01244 // it's a sync packet if the lsb of the rf options byte is 1 01245 sync= a & 1; 01246 01247 if (sync) { 01248 // receive timestamp 01249 RF_SPI_RECEIVE_BYTE(tHi); 01250 tHi= RFDescramble(tHi); 01251 RF_SPI_RECEIVE_BYTE(tLo); 01252 tLo= RFDescramble(tLo); 01253 // receive the xor protection byte 01254 RF_SPI_RECEIVE_BYTE(j); 01255 j= RFDescramble(j); 01256 01257 // if xor bit is correct 01258 if (j == tHi ^ tLo) { 01259 TIMER0_RUN(FALSE); 01260 a= TH0; 01261 b= TL0; 01262 TH0= tHi; 01263 TL0= tLo; 01264 TIMER0_RUN(TRUE); 01265 01266 //GLED= ~GLED; 01267 // turn rf off 01268 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01269 length= 1; 01270 01271 #ifdef DEBUG_DUMP_SYNC_VALUES 01272 #ifdef DEBUG_USE_UART 01273 // dump received tHi and tLo 01274 sendByteToUart(tHi); 01275 sendByteToUart(tLo); 01276 DEBUG_SPACE; 01277 01278 // dump local tHi and tLo before setting 01279 sendByteToUart(a); 01280 sendByteToUart(b); 01281 DEBUG_SPACE; 01282 01283 // dump difference between reported and set timer values 01284 sendSignedByteToUart((int)tHi-(int)a,3); 01285 sendSignedByteToUart((int)tLo-(int)b,3); 01286 DEBUG_NEWLINE; 01287 #endif 01288 #endif 01289 } 01290 } else { 01291 // get physical payload length 01292 RF_SPI_RECEIVE_BYTE(length); 01293 length= RFDescramble(length); 01294 01295 // read acl filter types 01296 RF_SPI_RECEIVE_BYTE(a); 01297 a= RFDescramble(a); 01298 01299 RF_SPI_RECEIVE_BYTE(b); 01300 b= RFDescramble(b); 01301 01302 // now we expect 4 "don't-care bytes" in order to have enough time to check local subscriptions 01303 if (ACL_subscribe_all) { 01304 // don't test anything 01305 RF_SPI_RECEIVE_BYTE(a); 01306 //BLED= ~BLED; 01307 //BLED= ~BLED; 01308 RF_SPI_RECEIVE_BYTE(a); 01309 //BLED= ~BLED; 01310 //BLED= ~BLED; 01311 RF_SPI_RECEIVE_BYTE(a); 01312 //BLED= ~BLED; 01313 //BLED= ~BLED; 01314 RF_SPI_RECEIVE_BYTE(a); 01315 //BLED= ~BLED; 01316 //BLED= ~BLED; 01317 } else { 01318 aclMatch= FALSE; 01319 // check subscriptions 0-3 01320 for (i=0; i < ACL_SUBSCRIPTIONLIST_LENGTH; i+= ACL_SUBSCRIPTIONLIST_LENGTH / 4) { 01321 // receive next 0 01322 while(!(EXIF & 0x10)); EXIF&=~0x10; 01323 for (j=i;j < i + ACL_SUBSCRIPTIONLIST_LENGTH / 4;j++) { 01324 if ((ACL_subscriptions[j][0] == a) && (ACL_subscriptions[j][1] == b)) 01325 aclMatch= TRUE; 01326 //BLED= ~BLED; 01327 //BLED= ~BLED; 01328 } 01329 } 01330 if (!aclMatch) { 01331 RF_SET_MODE_SLEEP; 01332 return 0; 01333 } 01334 } 01335 01336 // receive the incoming packet into the receivebuffer 01337 01338 // receive ll header 01339 for (j=0; j<12; j++) { 01340 RF_SPI_RECEIVE_BYTE(b); 01341 LL_header_receivebuffer[j]= RFDescramble(b); 01342 } 01343 01344 // receive ll packet payload 01345 length= length-LL_HEADER_SIZE-LL_TAIL_SIZE; 01346 if (length > 64) { 01347 // length is not in allowed range 01348 RF_SET_MODE_SLEEP; 01349 return 0; 01350 } 01351 for (j=0; j<length; j++) { 01352 RF_SPI_RECEIVE_BYTE(b); 01353 LL_payload_receivebuffer[j]= RFDescramble(b); 01354 } 01355 01356 // receive ll tail 01357 for (j=0; j<LL_TAIL_SIZE; j++) { 01358 RF_SPI_RECEIVE_BYTE(b); 01359 LL_tail_receivebuffer[j]= RFDescramble(b); 01360 } 01361 // turn rf off 01362 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01363 01364 // check the crc of the packet 01365 crc= LLCalcCRC16(LL_header_receivebuffer, LL_payload_receivebuffer, length); 01366 if ((crc >> 8) == LL_tail_receivebuffer[0] && (crc & 0xFF) == LL_tail_receivebuffer[1]) { 01367 // crc was correct, so copy received packet to the "received" buffer 01368 for (i=0; i<LL_HEADER_SIZE; i++) { 01369 LL_header_received[i]= LL_header_receivebuffer[i]; 01370 } 01371 for (i=0; i<length; i++) { 01372 LL_payload_received[i]= LL_payload_receivebuffer[i]; 01373 } 01374 for (i=0; i<LL_TAIL_SIZE; i++) { 01375 LL_tail_received[i]= LL_tail_receivebuffer[i]; 01376 } 01377 } else { 01378 // crc check failed 01379 /* if (onlyDataPackets) { 01380 putchar('c'); 01381 } */ 01382 RF_SET_MODE_SLEEP; 01383 return 0; 01384 } 01385 } 01386 } /* else { 01387 if (onlyDataPackets) { 01388 putchar('_'); 01389 } 01390 } */ 01391 01392 // turn transceiver off 01393 RFCON&=~0x01; // Ensure that bitmode is selected 01394 01395 // don't write back the local scrambler register to the global variable: 01396 // each node should have it's own scrReg sequence 01397 // rfScramblerReg= scrReg; 01398 01399 // stop timer 2 and start timer 3 if not already done 01400 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01401 TIMER2_RUN(FALSE); 01402 01403 BLED= LED_OFF; // end RFReceivePacket 01404 /* if (onlyDataPackets) { 01405 putchar('L'); 01406 sendByteToUart(length); 01407 DEBUG_SPACE; 01408 } */ 01409 if (sync && length == 1) { 01410 return 2; 01411 } else if (!sync && length > 0) { 01412 return 1 + 256 * length; 01413 } else { 01414 return 0; 01415 } 01416 } 01417 01418 01426 byte RFArbitrationBit(bool sendOrListen, byte t0hi, byte t0lo) { 01427 byte i, sample1, paPow; 01428 01429 if (sendOrListen) { 01430 // bit is 1, so send a short signal (2 bit) 01431 // turn on transceiver for tx-ing 01432 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01433 BLED= ~BLED; 01434 // increase transmission power for the small burst 01435 paPow= PA_POW; 01436 PA_POW= 0xF0; 01437 RFSetModeTransmit(); 01438 RFBUF= 0xFF; 01439 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01440 BLED= ~BLED; 01441 // write rf buffer 01442 RFBUF= sample1= BIN(10101010); 01443 01444 // wait for next rendezvous point 01445 WAIT_FOR_TIMER0_EXACT(t0hi, t0lo); 01446 BLED= ~BLED; 01447 RF_START_TX(); 01448 for (i=0; i<30; i++) { 01449 sample1= ((sample1 << 1) | (sample1 >> 7)); // rotate left (not through carry, because it's faster!) 01450 while(!RF_READY_TO_SEND()); 01451 RFBUF= sample1; 01452 } 01453 while(!RF_READY_TO_SEND()); 01454 BLED= ~BLED; 01455 BLED= ~BLED; // mark the 1 with an extra peak 01456 BLED= ~BLED; 01457 PA_POW= paPow; 01458 01459 // we sent a 1, so we must send the highest number possible, because a low number could 01460 // indicate that we lost the arbitration. 01461 return 255; 01462 } else { 01463 // bit is 0, so listen to the channel 01464 // turn on transceiver for rx-ing 01465 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01466 BLED= ~BLED; 01467 // turn on continuous sampling 01468 RFSetModeReceive(); // param true means fast 01469 RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF); 01470 RF_START_RX(); 01471 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01472 BLED= ~BLED; 01473 01474 // wait for next rendezvous point 01475 WAIT_FOR_TIMER0_EXACT(t0hi, t0lo-5); 01476 // the next two lines must (for strange) reasons be there 01477 // because otherwise you would get the result of the last 01478 // sample instead of the current result 01479 BLED= ~BLED; 01480 ADC_SAMPLE_SINGLE(); 01481 ADC_GET_SAMPLE_8BIT(); 01482 ADC_SAMPLE_SINGLE(); 01483 sample1= ADC_GET_SAMPLE_8BIT(); 01484 01485 BLED= ~BLED; // mark finish of sync slice 01486 // if the medium is not clear 01487 return sample1; 01488 } 01489 } 01490 01498 byte RFArbitrationBitDebug(bool sendOrListen, byte t0hi, byte t0lo) { 01499 byte sample1; 01500 01501 if (sendOrListen) { 01502 // bit is 1, so send a short signal (2 bit) 01503 // turn on transceiver for tx-ing 01504 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01505 BLED= ~BLED; 01506 RFSetModeTransmit(); 01507 RFBUF= 0xFF; 01508 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01509 BLED= ~BLED; 01510 // write rf buffer 01511 RFBUF= sample1= BIN(10101010); 01512 01513 t0hi= TH0; 01514 t0lo= TL0; 01515 } else { 01516 // bit is 0, so listen to the channel 01517 // turn on transceiver for rx-ing 01518 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01519 BLED= ~BLED; 01520 // turn on continuous sampling 01521 RFSetModeReceive(); // param true means fast 01522 RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF); 01523 RF_START_RX(); 01524 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01525 BLED= ~BLED; 01526 01527 t0hi= TH0; 01528 t0lo= TL0; 01529 } 01530 sendByteToUart(t0hi); 01531 putchar(':'); 01532 sendByteToUart(t0lo); 01533 DEBUG_NEWLINE; 01534 return 0; // return 0, the strongest signal possible 01535 } 01536 01537 #ifdef DEBUG_ARBITRATION_TEST_COUNT 01538 byte arbitrationByteGlob= 0; 01539 #endif 01540 01547 bool RFArbitration(bool tryToObtainMediaAccess) { 01548 bool success= TRUE; 01549 byte arbitrationByte, j, sample1, syncTimeH, syncTimeL; 01550 01551 // get timer0 time when entering the function, should be the same on both nodes 01552 //syncTimeH= TH0; 01553 //syncTimeL= TL0; 01554 BLED= LED_ON; // start RFArbitration 01555 01556 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01557 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01558 ADC_POWER(TRUE); // turn on adc 01559 01560 // do CAN-like arbitration for media access 01561 // see param documentation for explanation 01562 #ifndef DEBUG_ARBITRATION_TEST_ACTIVE 01563 if (tryToObtainMediaAccess) { 01564 arbitrationByte= RF_NEW_RANDOM_BYTE(); 01565 // if the node competes for the medium, he must send at least one 1, 01566 // so 0 as arbitration byte is not allowed 01567 if (!arbitrationByte) 01568 arbitrationByte= 1; 01569 } else { 01570 arbitrationByte= 0; 01571 } 01572 #endif 01573 01574 // for debugging, one of the following three assignments can be chosen for arbitrationByte 01575 #ifdef DEBUG_ARBITRATION_TEST_PATTERN 01576 arbitrationByte= BIN(10101010); // for debugging 01577 #endif 01578 #ifdef DEBUG_ARBITRATION_TEST_COUNT 01579 arbitrationByte= arbitrationByteGlob++; // for debugging 01580 #endif 01581 #ifdef DEBUG_ARBITRATION_TEST_0 01582 arbitrationByte= 0; // for debugging 01583 #endif 01584 01585 /* syncTimeH= TH0; 01586 syncTimeL= TL0; 01587 sendByteToUart(syncTimeH); 01588 putchar(':'); 01589 sendByteToUart(syncTimeL); 01590 DEBUG_NEWLINE; 01591 RF_SET_MODE_SLEEP; 01592 return 0; */ 01593 01594 // synchronize. hint: the value of this sync time has been measured with the 01595 // commented out block above 01596 syncTimeH= RF_RENDEZVOUS_ARBITRATION_START_HI; // first period should be shorter 01597 syncTimeL= RF_RENDEZVOUS_ARBITRATION_START_LO; 01598 01599 RFCON&=~0x01; // Ensure that bitmode is selected 01600 for (j=0; j<8; j++) { 01601 01602 sample1= RFArbitrationBit(bit_test(arbitrationByte, j), syncTimeH, syncTimeL); 01603 #ifdef DEBUG_USE_UART 01604 #ifdef DEBUG_DUMP_RSSI_SAMPLES 01605 rssiSamples[j]= sample1; 01606 //rssiSamples[8+j]= sample2; 01607 //rssiSamples[3*j+2]= sample3; 01608 #endif 01609 #endif 01610 // if the medium is not clear 01611 if (sample1 < RF_ARBITRATION_RSSI_THRESHOLD) { 01612 success= FALSE; 01613 // we're out, so break 01614 #ifndef DEBUG_ARBITRATION_TEST_ACTIVE 01615 break; // for testing and calibration of the arbitration threshold, this line has to be commented out 01616 #endif 01617 } 01618 01619 // this adds an equivalent of approx. 2.5 * 206 us to the variables for the next rendezvous point 01620 syncTimeH= ((syncTimeH << 8 | syncTimeL) + RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS) >> 8; 01621 syncTimeL+= RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS; 01622 } 01623 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01624 ADC_POWER(FALSE); 01625 return sample1; 01626 //BLED= LED_OFF; 01627 if (success) 01628 GLED= ~GLED; 01629 else 01630 RLED= ~RLED; 01631 #ifdef DEBUG_USE_UART 01632 #ifdef DEBUG_DUMP_RSSI_SAMPLES 01633 if (success) { 01634 arbitrationGood++; 01635 } else { 01636 arbitrationBad++; 01637 } 01638 sample1= 0; 01639 for (j=0; j<8; j++) { 01640 // if (rssiSamples[j] > 0xAF) { 01641 // continue; 01642 // } 01643 sendByteToUart(j); 01644 DEBUG_SPACE; 01645 //putchar((bit_test(arbitrationByte, j) ? '1' : '0')); 01646 //DEBUG_SPACE; 01647 sendByteToUart(rssiSamples[j]); 01648 //sendByteToUart(rssiSamples[8+j]); 01649 //DEBUG_SPACE; 01650 /* if ((rssiSamples[j] < RF_ARBITRATION_RSSI_THRESHOLD)) { 01651 putchar('1'); 01652 //sample1|= 1<<j; 01653 } else { 01654 putchar('0'); 01655 } */ 01656 //putchar((bit_test(arbitrationByte,j)) ? '1' : '0'); 01657 DEBUG_NEWLINE; 01658 } 01659 //sendByteToUart(sample1); 01660 //DEBUG_SPACE; 01661 01662 // this dumps the value of successful and not successful arbitration 01663 //sendUlongToUart(arbitrationGood); 01664 //DEBUG_SPACE; 01665 //sendUlongToUart(arbitrationBad); 01666 DEBUG_NEWLINE; 01667 #endif 01668 #endif 01669 BLED= LED_OFF; // end RFArbitration 01670 return success; 01671 } 01672 01673 01683 #ifdef RF_USE_TRAFFIC_INDICATION 01684 void RFTrafficIndication() { 01685 bool result,tmp; 01686 01687 BLED= LED_ON; // start 01688 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01689 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01690 ADC_POWER(TRUE); // turn on adc 01691 01692 if (llState & LL_STATE_NEED_TO_SEND) { 01693 RFArbitrationBit(TRUE, RF_RENDEZVOUS_TRAFFIC_INDICATION_HI, RF_RENDEZVOUS_TRAFFIC_INDICATION_LO); 01694 result= TRUE; 01695 } else { 01696 // let's be pessimistic and compare the rssi value to the more conservative 01697 // alone test value. there is a trade off between unnecessary traffic groups 01698 // and lost packets. we choose reliable reception at the cost of some 01699 // unnecessary traffic groups. 01700 tmp= RFArbitrationBit(FALSE, RF_RENDEZVOUS_TRAFFIC_INDICATION_HI, RF_RENDEZVOUS_TRAFFIC_INDICATION_LO); 01701 result= tmp < RF_ALONE_TEST_RSSI_THRESHOLD; 01702 } 01703 01704 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01705 ADC_POWER(FALSE); 01706 BLED= LED_OFF; 01707 01708 //putchar((result) ? 'T' : 't'); 01709 //sendByteToUart(tmp); 01710 if (result) 01711 rfState|= RF_STATE_TRAFFIC_GROUP; 01712 else 01713 rfState&= ~RF_STATE_TRAFFIC_GROUP; 01714 } 01715 #endif 01716 01725 #ifdef RF_USE_ALONE_DETECTION 01726 bool RFAloneTest() { 01727 bool result; 01728 byte tmp; 01729 01730 BLED= LED_ON; // start 01731 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01732 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01733 ADC_POWER(TRUE); // turn on adc 01734 RFCON&=~0x01; // Ensure that bitmode is selected 01735 01736 if (rfState & RF_STATE_MASTER) { 01737 // as a master we try to receive a signal 01738 // let's be pessimistic and compare the rssi value to a high 01739 // alone test value. there is a trade off between 01740 tmp= RFArbitrationBit(FALSE, RF_RENDEZVOUS_ALONE_TEST_HI, RF_RENDEZVOUS_ALONE_TEST_LO); 01741 result= tmp < RF_ALONE_TEST_RSSI_THRESHOLD; 01742 //putchar('r'); 01743 //sendByteToUart(tmp); 01744 //putchar((result) ? '+' : '-'); 01745 //DEBUG_SPACE; 01746 } else { 01747 // as a slave, we send a signal 01748 RFArbitrationBit(TRUE, RF_RENDEZVOUS_ALONE_TEST_HI, RF_RENDEZVOUS_ALONE_TEST_LO); 01749 result= TRUE; 01750 //putchar('s'); 01751 } 01752 01753 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01754 ADC_POWER(FALSE); 01755 BLED= LED_OFF; 01756 return result; 01757 } 01758 #endif 01759 01766 byte RFAloneRandomNumberOfSeconds() { 01767 // returns a number betwenn 10 and 25 (including both) 01768 return 10 + (RF_NEW_RANDOM_BYTE() >> 4); 01769 } 01770 01778 unsigned short RFAloneRandomNumberOfSlots() { 01779 // returns a number betwenn 10 and 26 (including both) 01780 return 333 + ((RF_NEW_RANDOM_BYTE() << 8)+ RF_NEW_RANDOM_BYTE()) % 534; 01781 } 01782 01791 void RFLowPowerSleep(byte sleepTime) { 01792 byte i; 01793 01794 sendByteToUart(sleepTime); 01795 DEBUG_NEWLINE; 01796 01797 TIMER0_RUN(FALSE); 01798 RLED= GLED= LED_OFF; 01799 YLED= LED_OFF; 01800 01801 // config timer 2 for a period of 1 second in X32 clock mode 01802 // this will result in calling the dummy isr ISR 01803 // set timer 2 to high prio so that it can interrupt the ISR of timer0 where we are currently in. 01804 INT_PRIORITY(INUM_TIMER2, INT_HIGH); 01805 halConfigTimer23(TIMER2 | TIMER23_INT_TIMER, 1000000, CC1010EB_LOW_CLKFREQ); 01806 01807 MAIN_CLOCK_SET_SOURCE(CLOCK_X32); 01808 XOSC_ENABLE(FALSE); 01809 01810 // stay in 32kHz clock mode for for sleepTime seconds 01811 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01812 TIMER2_RUN(TRUE); 01813 for (i=0; i<sleepTime; i++) { 01814 RLED= ~RLED; 01815 // this inner loop needs 1 s to execute 01816 ENTER_IDLE_MODE(); 01817 } 01818 TIMER2_RUN(FALSE); 01819 01820 // Enable high speed XOSC, switch clock source, then disable 32kHz XOSC 01821 XOSC_ENABLE(TRUE); 01822 MAIN_CLOCK_SET_SOURCE(CLOCK_XOSC); 01823 01824 INT_ENABLE(INUM_TIMER2, INT_OFF); 01825 INT_PRIORITY(INUM_TIMER2, INT_LOW); 01826 TIMER0_RUN(TRUE); 01827 01828 RLED= LED_OFF; 01829 YLED= LED_ON; 01830 } 01831 01835 void RFLowPowerSleepTillKeyPressed() { 01836 TIMER0_RUN(FALSE); 01837 RLED= GLED= LED_OFF; 01838 YLED= LED_OFF; 01839 01840 INT_EXTERNAL0_TRIGGER_ON_EDGE(); 01841 INT_ENABLE(INUM_EXTERNAL0, TRUE); 01842 01843 MAIN_CLOCK_SET_SOURCE(CLOCK_X32); 01844 XOSC_ENABLE(FALSE); 01845 01846 // stay in 32kHz clock mode for for sleepTime seconds 01847 INT_SETFLAG(INUM_EXTERNAL0, INT_CLR); 01848 ENTER_IDLE_MODE(); 01849 01850 // Enable high speed XOSC, switch clock source, then disable 32kHz XOSC 01851 XOSC_ENABLE(TRUE); 01852 MAIN_CLOCK_SET_SOURCE(CLOCK_XOSC); 01853 01854 INT_ENABLE(INUM_EXTERNAL0, FALSE); 01855 TIMER0_RUN(TRUE); 01856 01857 RLED= LED_OFF; 01858 YLED= LED_ON; 01859 01860 } 01861 01862 01868 byte RFScramble(byte b) { 01869 byte i, scrReg; 01870 01871 scrReg= rfScramblerReg; 01872 RF_SCRAMBLE(b, scrReg, i); 01873 rfScramblerReg= scrReg; 01874 01875 return b; 01876 } 01877 01888 bool RFSendPacket(bool sync) { 01889 byte numPreambles, j, b, currentTHi, currentTLo; 01890 //unsigned short tEndCurrent; // the start time for a sync packet 01891 01892 // wait one ms to give the receivers time to prepare 01893 halWait(1, CC1010EB_CLKFREQ); 01894 numPreambles= RF_PREAMBLE_LENGTH_TX; 01895 01896 // Turn on RF for TX, send packet 01897 RFSetModeTransmit(); 01898 //halRFSetRxTxOff(RF_TX, &RF_SETTINGS, &RF_CALDATA); 01899 01900 // Set the first byte to transmit & turn on TX 01901 RFCON|=0x01; // Ensure that bytemode is selected 01902 RF_SEND_BYTE(RF_PREAMBLE_BYTE); 01903 01904 if (sync) { 01905 /* j= TH0; 01906 b= TL0; 01907 sendByteToUart(j); 01908 sendByteToUart(b); 01909 DEBUG_NEWLINE; 01910 RF_SET_MODE_SLEEP; 01911 return 1; */ 01912 01913 // wait for the sync time start rendezvous point. the values for RF_SYNC_START_TIMER0_* 01914 // have been calibrated with the commented out code above 01915 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_SYNC_START_HI, RF_RENDEZVOUS_SYNC_START_LO); 01916 } else { 01917 /*j= TH0; 01918 b= TL0; 01919 sendByteToUart(j); 01920 sendByteToUart(b); 01921 DEBUG_NEWLINE; 01922 RF_SET_MODE_SLEEP; 01923 return 0;*/ 01924 01925 // synchronize 01926 // the value of this sync time has been measured with the commented out block above 01927 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_DATA_PACKET_START_HI, RF_RENDEZVOUS_DATA_PACKET_START_LO); 01928 } 01929 BLED= LED_ON; // start sending 01930 01931 // turn on transceiver 01932 RF_START_TX(); 01933 //YLED= ~YLED; 01934 //BLED= ~BLED; 01935 01936 if (sync) { 01937 // get current timer value of timer0 01938 currentTHi= TH0; 01939 currentTLo= TL0; 01940 currentTHi= ((currentTHi | currentTLo) + RF_SYNC_ADDITIONAL_OFFSET) >> 8; 01941 currentTLo= currentTLo + RF_SYNC_ADDITIONAL_OFFSET; 01942 j= currentTHi ^ currentTLo; 01943 } 01944 01945 // Send remaining preambles 01946 while (--numPreambles) { 01947 RF_SPI_SEND_BYTE(RF_PREAMBLE_BYTE); 01948 } 01949 01950 // Send start of packet byte 01951 RF_SPI_SEND_BYTE(RF_SUITABLE_SYNC_BYTE); 01952 01953 // Send scrambler init 01954 //scrReg= rfScramblerReg; 01955 //RF_SPI_SEND_BYTE(scrReg); 01956 01957 // send FF byte to train the receiver's descrambler 01958 b= RFScramble(0xFF); 01959 RF_SPI_SEND_BYTE(b); 01960 01961 //YLED= ~YLED; 01962 //BLED= ~BLED; 01963 if (sync) { 01964 //BLED= ~BLED; 01965 // send rf options. 1 means beacon. 01966 b= RFScramble(1); 01967 RF_SPI_SEND_BYTE(b); 01968 01969 // send timestamp 01970 // use redundant protection byte j (= tEndAvgLo xor tEndAvgHi) to detect transmission bit errors 01971 b= RFScramble(currentTHi); 01972 RF_SPI_SEND_BYTE(b); 01973 b= RFScramble(currentTLo); 01974 RF_SPI_SEND_BYTE(b); 01975 b= RFScramble(j); 01976 RF_SPI_SEND_BYTE(b); 01977 01978 //YLED= ~YLED; 01979 //BLED= ~BLED; 01980 //tEndCurrent= (TH0<<8) | TL0; 01981 01982 #ifdef DEBUG_USE_UART 01983 #ifdef DEBUG_DUMP_SYNC_VALUES 01984 j= TH0; 01985 b= TL0; 01986 sendByteToUart(currentTHi); 01987 sendByteToUart(currentTLo); 01988 DEBUG_SPACE; 01989 sendByteToUart(j); 01990 sendByteToUart(b); 01991 DEBUG_NEWLINE; 01992 01993 #endif 01994 #endif 01995 } else { 01996 // send rf options. 0 means regular packet 01997 b= RFScramble(0); 01998 RF_SPI_SEND_BYTE(b); 01999 02000 if (LL_payload_sendbuffer_length > 64) 02001 LL_payload_sendbuffer_length= 64; 02002 02003 // Send physical payload length 02004 b= RFScramble(LL_payload_sendbuffer_length + LL_HEADER_SIZE + LL_TAIL_SIZE); 02005 RF_SPI_SEND_BYTE(b); 02006 02007 b= RFScramble(LL_payload_sendbuffer[0]); 02008 RF_SPI_SEND_BYTE(b); 02009 b= RFScramble(LL_payload_sendbuffer[1]); 02010 RF_SPI_SEND_BYTE(b); 02011 02012 // send 4 foo bytes to give receiver time to check subscriptions 02013 // use RF_PREAMBLE_BYTE because it's dc balanced 02014 RF_SPI_SEND_BYTE(BIN(11001010)); 02015 //BLED= ~BLED; 02016 //BLED= ~BLED; 02017 RF_SPI_SEND_BYTE(BIN(11001010)); 02018 //BLED= ~BLED; 02019 //BLED= ~BLED; 02020 RF_SPI_SEND_BYTE(BIN(11001010)); 02021 //BLED= ~BLED; 02022 //BLED= ~BLED; 02023 RF_SPI_SEND_BYTE(BIN(11001010)); 02024 //BLED= ~BLED; 02025 //BLED= ~BLED; 02026 02027 // send ll header 02028 for (j=0; j<12; j++) { 02029 b= RFScramble(LL_header_sendbuffer[j]); 02030 RF_SPI_SEND_BYTE(b); 02031 } 02032 02033 // send ll packet payload 02034 for (j=0; j<LL_payload_sendbuffer_length; j++) { 02035 b= RFScramble(LL_payload_sendbuffer[j]); 02036 RF_SPI_SEND_BYTE(b); 02037 } 02038 02039 // send ll tail 02040 for (j=0; j<LL_TAIL_SIZE; j++) { 02041 b= RFScramble(LL_tail_sendbuffer[j]); 02042 RF_SPI_SEND_BYTE(b); 02043 } 02044 } 02045 02046 RF_SPI_SEND_BYTE(0); 02047 RF_SPI_SEND_BYTE(0); 02048 //while (!RF_READY_TO_SEND()); 02049 //YLED= ~YLED; 02050 //BLED= ~BLED; 02051 02052 // turn transceiver off 02053 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 02054 RFCON&=~0x01; // Ensure that bitmode is selected 02055 02056 // do exponential smoothing on the timer value when finished with sending 02057 // add an additional offset, because detection and timer setting on the slave 02058 // is slightly later than measurement on the master after after transmitting. 02059 /* if (sync) { 02060 //tEndAvg= tEndCurrent + RF_SYNC_ADDITIONAL_OFFSET; 02061 //tEndAvg= (((ulong)tEndCurrent + (ulong)RF_SYNC_ADDITIONAL_OFFSET) + 9*(ulong)tEndAvg) / 10; 02062 02063 // dump the sync values just sent 02064 #ifdef DEBUG_DUMP_SYNC_VALUES 02065 #ifdef DEBUG_USE_UART 02066 // dump received tHi and tLo 02067 sendByteToUart(tEndCurrent >> 8); 02068 sendByteToUart(tEndCurrent & 0xFF); 02069 DEBUG_SPACE; 02070 sendByteToUart(tEndAvg >> 8); 02071 sendByteToUart(tEndAvg & 0xFF); 02072 DEBUG_NEWLINE; 02073 #endif 02074 #endif 02075 02076 // when dividing by 8 don't simply cut the last 3 bits (like a cast would do) 02077 // to improve accuracy, add 1 if last bit that is shifted out is 1 02078 // this is the binary equivalent to decimal rounding (down if next decimal place is <=4, up if next decimal place is >4) 02079 rfAverageTEndBuffer*= 7; 02080 rfAverageTEndBuffer/= 8; 02081 rfAverageTEndBuffer+= tEndCurrent + RF_SYNC_ADDITIONAL_OFFSET; 02082 j= (rfAverageTEndBuffer & 0x4); 02083 tEndAvg= rfAverageTEndBuffer / 8; 02084 if (j) 02085 tEndAvg++; 02086 } */ 02087 02088 BLED= LED_OFF; // end RFSendPacket 02089 return TRUE; 02090 } 02091 02099 bool RFLongSearchForMaster(unsigned short numTries) { 02100 bool success= FALSE; 02101 02102 // let timer 0 (the slot timer) run, but turn off the interrupt 02103 INT_ENABLE(INUM_TIMER0, INT_OFF); 02104 //YLED= LED_ON; 02105 while (numTries-- > 0) { 02106 //YLED= ~YLED; 02107 //sendUlongToUart(numTries); 02108 //DEBUG_SPACE; 02109 // if it is a sync packet 02110 if (RFReceivePacket(&periodDataSlot, FALSE) == 2) { 02111 success= TRUE; 02112 break; 02113 } 02114 } 02115 02116 // turn on the interrupt for timer 0 again 02117 INT_ENABLE(INUM_TIMER0, INT_ON); 02118 // clear the interrupt flag, because it is most probably set 02119 INT_SETFLAG(INUM_TIMER0, INT_CLR); 02120 02121 //YLED= LED_OFF; 02122 return success; 02123 } 02124 02130 bool RFMasterRivalTest() { 02131 bool success= FALSE; 02132 02133 //YLED= LED_ON; 02134 // the RFReceivePacket() function automagically returns if only 200us are left in 02135 // current timer0 slot. 02136 success= (RFReceivePacket(&periodDataSlot, FALSE) == 2); 02137 //YLED= LED_OFF; 02138 02139 return success; 02140 } 02141 02143 // end of RF Layer Functions 02144 02145 02151 byte LLGetIDFromHardware() { 02152 for (i=0;i<=7;i++) { 02153 LL_header_sendbuffer[3+0]= llEpromAddress.a1; 02154 LL_header_sendbuffer[3+1]= llEpromAddress.a2; 02155 LL_header_sendbuffer[3+2]= llEpromAddress.a3; 02156 LL_header_sendbuffer[3+3]= llEpromAddress.a4; 02157 LL_header_sendbuffer[3+4]= llEpromAddress.a5; 02158 LL_header_sendbuffer[3+5]= llEpromAddress.a6; 02159 LL_header_sendbuffer[3+6]= llEpromAddress.a7; 02160 LL_header_sendbuffer[3+7]= RF_NEW_RANDOM_BYTE(); 02161 } 02162 02163 return 1; //both were invalid: there's no valid id 02164 } 02165 02169 void LLInit() { 02170 LL_sequence_no=0; 02171 02172 LL_header_sendbuffer[0]= LL_PROTOCOL_VERSION; 02173 02174 LL_payload_send_length=0; // set used ll payload to zero 02175 LL_payload_received_length= 0; 02176 llState= LL_STATE_RUNNING; 02177 LL_sequence_no= 0; 02178 02179 LLGetIDFromHardware(); //take id from internal or external eeprom and set LL_header_sendbuffer 02180 02181 #ifdef DEBUG_USE_UART 02182 sendStringToUart("Particle ID:\r\n"); 02183 sendByteToUart(LL_header_sendbuffer[3+0]); 02184 putchar('.'); 02185 sendByteToUart(LL_header_sendbuffer[3+1]); 02186 putchar('.'); 02187 sendByteToUart(LL_header_sendbuffer[3+2]); 02188 putchar('.'); 02189 sendByteToUart(LL_header_sendbuffer[3+3]); 02190 putchar('.'); 02191 sendByteToUart(LL_header_sendbuffer[3+4]); 02192 putchar('.'); 02193 sendByteToUart(LL_header_sendbuffer[3+5]); 02194 putchar('.'); 02195 sendByteToUart(LL_header_sendbuffer[3+6]); 02196 putchar('.'); 02197 sendByteToUart(LL_header_sendbuffer[3+7]); 02198 DEBUG_NEWLINE; 02199 #endif 02200 02201 RFInit(); 02202 } 02203 02204 02208 void LLStart() { 02209 RFStart(); 02210 } 02211 02212 02216 void LLStop() { 02217 RFStop(); // handles the states 02218 } 02219 02225 void LLSlotEnd() { 02226 02227 } 02228 02229 02233 void LLAbortSending() { 02234 if (LLSendingBusy()) { 02235 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 02236 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; 02237 } else { 02238 llState&= ~LL_STATE_SEND_SUCCESS; 02239 } 02240 // clear some llState flags 02241 llState&= ~(LL_STATE_SEND_SUCCESS|LL_STATE_NEED_TO_SEND); 02242 } 02243 } 02244 02248 void LLSetSendingSuccess() { 02249 //update the states 02250 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 02251 // no update of success flag 02252 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; // clear the control management bit 02253 } else { 02254 llState|= LL_STATE_SEND_SUCCESS; // successful sent 02255 } 02256 llState&= ~LL_STATE_NEED_TO_SEND; // busy is over 02257 LL_payload_send_length=0; // set used payload to zero 02258 } 02259 02263 byte LLSendingBusy() { 02264 return ((llState & LL_STATE_NEED_TO_SEND) != 0); 02265 } 02266 02271 byte LLGetSendSuccess() { 02272 return ((llState & LL_STATE_SEND_SUCCESS) != 0); 02273 } 02274 02278 byte LLIsActive() { 02279 return ((llState & LL_STATE_RUNNING) != 0); 02280 } 02281 02285 void LLSetDataToOld() { 02286 llState&= ~LL_STATE_NEW_DATA; 02287 } 02288 02293 void LLLockReceiveBuffer() { 02294 llState|= LL_STATE_RECEIVE_BUFFER_LOCKED; 02295 LLSetDataToOld(); 02296 } 02297 02301 byte LLReceiveBufferLocked() { 02302 return ((llState & LL_STATE_RECEIVE_BUFFER_LOCKED) != 0); 02303 } 02304 02308 void LLReleaseReceiveBuffer() { 02309 llState&= ~LL_STATE_RECEIVE_BUFFER_LOCKED; 02310 LLSetDataToOld(); 02311 } 02312 02316 void LLSetDataToNew() { 02317 llState|= LL_STATE_NEW_DATA; 02318 } 02319 02320 02325 byte LLDataIsNew() { 02326 return ((llState & LL_STATE_NEW_DATA) != 0); 02327 } 02328 02332 byte LLGetFieldStrength() { 02333 return rfFieldStrength; 02334 } 02335 02339 void LLSetFieldStrength(byte value) { 02340 rfFieldStrength= value; 02341 } 02342 02343 02351 unsigned short LLCalcCRC16(byte *header_data, byte *payload_data, byte payload_size) { 02352 //calcs a crc on LL_header, ACL_data, LL_tail 02353 byte hb,lb,i,tmp; 02354 lb=0; 02355 hb=0; 02356 02357 for(i=0;i<LL_HEADER_SIZE;i++) { 02358 tmp = lb; 02359 lb = hb; 02360 hb = tmp; 02361 lb ^= header_data[i]; 02362 lb ^= lb >> 4; 02363 hb ^= lb << 4; 02364 hb ^= lb >> 3; 02365 lb ^= (lb << 4) << 1; 02366 } 02367 02368 //len=header_data[1]-LL_HEADER_SIZE-LL_TAIL_SIZE; 02369 for(i=0;i<payload_size;i++) { 02370 tmp = lb; 02371 lb = hb; 02372 hb = tmp; 02373 lb ^= payload_data[i]; 02374 lb ^= lb >> 4; 02375 hb ^= lb << 4; 02376 hb ^= lb >> 3; 02377 lb ^= (lb << 4) << 1; 02378 } 02379 02380 return hb*256+lb; 02381 } 02382 02383 02388 byte LLSendPacket(byte slot_limit) { 02389 unsigned short crc; 02390 02391 if (slot_limit==0) slot_limit=1; // zero makes no sense 02392 02393 // if rf is off, stop it 02394 if (!(rfState & RF_STATE_RUNNING)) return 0; 02395 // if ll is still trying to send the last packet, ignore 02396 if (llState & LL_STATE_NEED_TO_SEND) return 0; 02397 02398 // complete the LLPacket 02399 //DebugGiveOut(LL_payload_send_length); 02400 02401 // copy "send" variables to "sendbuffer" variables 02402 if (LL_payload_send_length > 64) 02403 LL_payload_send_length= 64; 02404 LL_payload_sendbuffer_length= LL_payload_send_length; 02405 for (i=0; i<LL_payload_send_length; i++) 02406 LL_payload_sendbuffer[i]= LL_payload_send[i]; 02407 02408 LL_header_sendbuffer[1]= LL_payload_sendbuffer_length; 02409 LL_header_sendbuffer[2]= rfFieldStrength; 02410 LL_header_sendbuffer[11]= LL_sequence_no; 02411 02412 crc= LLCalcCRC16(LL_header_sendbuffer,LL_payload_sendbuffer, LL_payload_sendbuffer_length); 02413 02414 LL_tail_sendbuffer[0]= (crc>>8); //CRCh (implicit typecast; ´takes low byte from long 02415 LL_tail_sendbuffer[1]= crc & 0xFF; //CRCl 02416 02417 LL_slots_left= slot_limit; 02418 LL_sequence_no++; 02419 02420 llState|= LL_STATE_NEED_TO_SEND; 02421 02422 LL_payload_send_length= 0; // set the used payload to 0, so that new data can be written 02423 02424 return 1; 02425 } 02426 02427 02431 byte LLGetRemainingPayloadSpace() { 02432 return (LL_PAYLOAD_SIZE-LL_payload_send_length); 02433 } 02434 02435 02439 byte LLSentPacketInThisSlot() { 02440 return ((llState & LL_STATE_PACKET_JUST_SENT) != 0); 02441 } 02442 02444 // end of LL Layer Functions 02445 02448 02449 void ACLStartUp() { 02450 xdata byte result[10]; 02451 byte selftest_result,i; 02452 02453 selftest_result= AppSelfTest(result); //runs the selftest 02454 02455 GLED= LED_OFF; 02456 RLED= LED_OFF; 02457 halWait(100, CC1010EB_CLKFREQ); // for stabilizing of power etc. 02458 02459 ACLInit(); // sets the pins in pic init correct (looks at selftest_active 02460 // GLED= LED_ON; 02461 // while(1); 02462 02463 #ifdef DEBUG_DUMP_RF_STATISTICS 02464 // reset the statistics 02465 DebugResetStats(); 02466 #endif 02467 02468 if(selftest_result) { 02469 //send result ten times to get it 02470 for(i=0;i<10;i++) { 02471 //send packet 02472 ACLAddNewType(ACL_TYPE_CST_HI,ACL_TYPE_CST_LO); //MST Self test 02473 ACLAddData(result[0]); 02474 ACLAddData(result[1]); 02475 ACLAddData(result[2]); 02476 ACLAddData(result[3]); 02477 ACLAddData(result[4]); 02478 ACLAddData(result[5]); 02479 ACLAddData(result[9]); 02480 02481 ACLSendPacket(10); 02482 while(ACLSendingBusy()); 02483 } 02484 } 02485 } 02486 02487 02489 void ACLInit() { 02490 ACLFlushSubscriptions(); 02491 ACL_send_buffer_locked=false; 02492 ACL_write_position=0; 02493 ACL_subscribe_all=false; 02494 ACL_ACM_answers=true; 02495 ACL_ignore_control_messages=false; 02496 ACL_pass_control_messages=false; 02497 02498 LL_payload_send_length=0; 02499 ACL_write_position=0; 02500 02501 //APPSetLEDBehaviour(LEDS_NORMAL); //setdefault 02502 02503 LLInit(); 02504 ACLStart(); 02505 } 02506 02507 02514 byte ACLSubscribe(byte LL_type_h, byte LL_type_l) { 02515 byte i; 02516 02517 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { // first test if already there 02518 if ((ACL_subscriptions[i][0]==LL_type_h) && (ACL_subscriptions[i][1]==LL_type_l)) return 1; 02519 } 02520 02521 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02522 if ((ACL_subscriptions[i][0]==0) && (ACL_subscriptions[i][1]==0)) { 02523 ACL_subscriptions[i][0]=LL_type_h; 02524 ACL_subscriptions[i][1]=LL_type_l; 02525 return 1; // successful subscribed 02526 } 02527 } 02528 return 0; // subsriptions failed 02529 } 02530 02537 byte ACLUnsubscribe(byte LL_type_h, byte LL_type_l) { 02538 byte i; 02539 02540 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { // if type is there, delete it 02541 if ((ACL_subscriptions[i][0]==LL_type_h) && (ACL_subscriptions[i][1]==LL_type_l)) { 02542 ACL_subscriptions[i][0]=0; 02543 ACL_subscriptions[i][1]=0; 02544 return 1; // successful unsubscribed 02545 } 02546 } 02547 return 0; // not found 02548 } 02549 02550 02552 void ACLFlushSubscriptions() { 02553 byte i; 02554 02555 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02556 ACL_subscriptions[i][0]=0; 02557 ACL_subscriptions[i][1]=0; 02558 } 02559 02560 ACL_subscribe_all=false; 02561 ACLSubscribeDefault(); 02562 return; 02563 } 02564 02566 void ACLSubscribeAll() { 02567 ACL_subscribe_all=true; 02568 } 02569 02571 void ACLAnswerOnACM() { 02572 ACL_ACM_answers=true; 02573 } 02574 02576 void ACLNoAnswerOnACM() { 02577 ACL_ACM_answers=false; 02578 } 02579 02581 byte ACLSubscribeDefault() { 02582 byte result; 02583 result=ACLSubscribe(ACL_TYPE_ACM_H,ACL_TYPE_ACM_L); //165 14 is CMA (Control and Management virtual Artifact) 02584 return result; 02585 } 02586 02593 byte ACLVerifySubscription(byte type_h,byte type_l) { 02594 byte i; 02595 02596 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02597 if ((ACL_subscriptions[i][0]==type_h) && (ACL_subscriptions[i][1]==type_l)) return 1; 02598 } 02599 return 0; //subscription not found 02600 } 02601 02602 02607 byte ACLProcessControlMessages() { 02608 02609 if ((LL_payload_receivebuffer[0]==ACL_TYPE_ACM_H) && (LL_payload_receivebuffer[1]==ACL_TYPE_ACM_L)) { 02610 // there was a control msg 02611 02612 // check a control msg, e.g. "1,1" 02613 //remote shutdown 02614 if ((LL_payload_receivebuffer[3]==172) && (LL_payload_receivebuffer[4]==224)) { 02615 if (ACLMatchesMyIP(LL_payload_receivebuffer,6)) { 02616 return 4; 02617 } 02618 } 02619 02620 // check syncrate setting 02621 if ((LL_payload_receivebuffer[3]==198) && (LL_payload_receivebuffer[4]==208)) { 02622 //RFSetSyncRate(LL_payload_receivebuffer[6]); 02623 } 02624 02625 // check initial listen slots setting 02626 if ((LL_payload_receivebuffer[3]==159) && (LL_payload_receivebuffer[4]==192)) { 02627 //RFSetInitialListenSlots(LL_payload_receivebuffer[6]); 02628 } 02629 02630 // check shutdown (dirty hack; actually need ip check, different shutdown modes 02631 if ((LL_payload_receivebuffer[3]==204) && (LL_payload_receivebuffer[4]==232)) { 02632 RFStop(); 02633 } 02634 02635 // check modeswitch 02636 if ((LL_payload_receivebuffer[3]==1) && (LL_payload_receivebuffer[4]==1)) { 02637 } 02638 02639 //check over the air programming 02640 if ((LL_payload_receivebuffer[3]==198) && (LL_payload_receivebuffer[4]==88)) { 02641 //OtapReceive(); // call over the airprogramming stuff 02642 } 02643 02644 //check set ID 02645 if ((LL_payload_receivebuffer[3]==ACL_TYPE_CID_HI) && (LL_payload_receivebuffer[4]==ACL_TYPE_CID_LO) /* && selftest_active==true */) { 02646 //OtapSetID(); 02647 } 02648 02649 // no answering allowed; all messages below here are not allowed, if acl_acm_answer is false 02650 if (ACL_ACM_answers==false) return 0; 02651 02652 //check Helo 02653 if ((LL_payload_receivebuffer[3]==115) && (LL_payload_receivebuffer[4]==216)) { 02654 if (ACLMatchesMyIP(LL_payload_receivebuffer,6)) { // does sent IP match my IP?? 02655 if (LLSendingBusy()) ACLAbortSending(); // interrupt any waiting stuff 02656 02657 // now set the whole packet by hand: 02658 LL_payload_send[0]=ACL_TYPE_ACM_H; 02659 LL_payload_send[1]=ACL_TYPE_ACM_L; 02660 LL_payload_send[2]=0; 02661 02662 LL_payload_send[3]=134; 02663 LL_payload_send[4]=32; 02664 02665 LL_payload_send[5]=8; 02666 LL_payload_send[6]=LL_header_receivebuffer[3]; // put in origin adress from HELO 02667 LL_payload_send[7]=LL_header_receivebuffer[4]; 02668 LL_payload_send[8]=LL_header_receivebuffer[5]; 02669 LL_payload_send[9]=LL_header_receivebuffer[6]; 02670 LL_payload_send[10]=LL_header_receivebuffer[7]; 02671 LL_payload_send[11]=LL_header_receivebuffer[8]; 02672 LL_payload_send[12]=LL_header_receivebuffer[9]; 02673 LL_payload_send[13]=LL_header_receivebuffer[10]; 02674 LL_payload_send_length=14; // give the correct len 02675 02676 if (LLSendPacket(ACL_CONTROL_MESSAGES_TIMEOUT)) { // NEVER USE ACL FUNCTION here!!! 02677 llState|= LL_STATE_CTRL_MESSAGE_INSERTED; // this means that this msg if special concerning the update of the states 02678 } 02679 } 02680 } 02681 // this was helo 02682 return 1; // there was a control management msg 02683 } 02684 return 0; // there was no control management msg 02685 } 02686 02688 void ACLSetFieldStrength(byte power) { 02689 LLSetFieldStrength(power); 02690 } 02691 02693 byte ACLSendingBusy() { 02694 return LLSendingBusy(); 02695 } 02696 02702 byte ACLGetSendSuccess() { 02703 return LLGetSendSuccess(); 02704 } 02705 02712 byte ACLMatchesMyIP(char *buffer,byte start) { 02713 byte i; 02714 bool res; 02715 02716 // check if was my address // ll_header_send [3..10] hold the myIP 02717 res= TRUE; 02718 for (i=0;i<8;i++) { 02719 if (buffer[start+i] != LL_header_sendbuffer[3+i]) { 02720 res= FALSE; 02721 break; 02722 } 02723 } 02724 if (res) return 1; 02725 02726 // check if was broadcast adress 02727 res= TRUE; 02728 for (i=0;i<8;i++) { 02729 if (buffer[start+i]!=255) { 02730 res= FALSE; 02731 break; 02732 } 02733 } 02734 if (res) return 1; 02735 02736 return 0; 02737 } 02738 02739 02745 byte ACLSendPacket(byte slot_timeout) { 02746 return LLSendPacket(slot_timeout); 02747 } 02748 02750 void ACLAbortSending() { 02751 ACL_send_buffer_locked=false; 02752 ACL_write_position=0; 02753 LLAbortSending(); 02754 } 02755 02757 byte ACLClearSendData() { 02758 byte i; 02759 if (LLSendingBusy()) return 0; // dont clear if busy sending 02760 if (ACL_send_buffer_locked==true) return 0; // dont clear if reservation is active 02761 02762 for(i=0;i<LL_PAYLOAD_SIZE;i++) 02763 LL_payload_send[i]=0; 02764 LL_payload_send_length=0; 02765 ACL_write_position=0; 02766 02767 return 1; 02768 } 02769 02770 02775 bool ACLAddNewType(byte type_h, byte type_l) { 02776 //if (LLSendingBusy()) return 2; // not needed any more, we have a sendbuffer 02777 if ((LL_PAYLOAD_SIZE-LL_payload_send_length)<3) { 02778 return 1; // not enough space 02779 } else { 02780 //place the new type 02781 LL_payload_send[LL_payload_send_length]=type_h; 02782 LL_payload_send[LL_payload_send_length+1]=type_l; 02783 LL_payload_send[LL_payload_send_length+2]=0; //data length is zero 02784 ACL_write_position=LL_payload_send_length+2; 02785 LL_payload_send_length+=3; 02786 return 0; 02787 } 02788 } 02789 02795 byte ACLAddData(byte newByte) { 02796 //if (LLSendingBusy()) return 2; 02797 if ((LL_PAYLOAD_SIZE-LL_payload_send_length)<1) { 02798 return 1; // not enough space 02799 } else { 02800 LL_payload_send[ACL_write_position]++; //increase length byte after type bytes 02801 LL_payload_send[LL_payload_send_length]= newByte; //place the data 02802 LL_payload_send_length+=1; 02803 return 0; 02804 } 02805 } 02806 02811 byte ACLGetRemainingPayloadSpace() { 02812 return LLGetRemainingPayloadSpace(); 02813 } 02814 02819 byte ACLGetReceivedPayloadLength() { 02820 return LL_header_received[1]; 02821 } 02822 02827 signed char ACLGetReceivedDataLength(byte type_h, byte type_l) { 02828 byte i; 02829 02830 for (i=0; i<=LL_header_received[1]-2; i++) { 02831 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02832 return (LL_payload_received[i+2]); //return valid length 02833 } 02834 } 02835 return -1; // type not found 02836 } 02837 02838 02842 char* ACLGetReceivedData(byte type_h, byte type_l) { 02843 byte i; 02844 02845 for (i=0;i<=LL_header_received[1]-2;i++) { 02846 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02847 if (LL_payload_received[i+2]!=0) { 02848 return (&(LL_payload_received[i+3])); // if len is not zero: return valid pointer to data begin 02849 } 02850 } 02851 } 02852 return NULL; 02853 } 02854 02855 02860 char ACLGetReceivedByte(byte type_h, byte type_l, byte position) { 02861 byte i; 02862 02863 for (i=0;i<=LL_header_received[1]-2;i++) { 02864 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02865 if (LL_payload_received[i+2]!=0) { 02866 return (LL_payload_received[i+3+position]); // if len is not zero: return valid pointer to data begin 02867 } 02868 02869 } 02870 } 02871 02872 return 0; 02873 //attention!! if type wasn't found: return is undetermined!! 02874 } 02875 02881 byte ACLFoundReceivedType(byte type_h, byte type_l) { 02882 return (ACLGetReceivedDataLength(type_h,type_l) != -1); //not found 02883 } 02884 02892 void ACLSetControlMessagesBehaviour(boolean ignore, boolean pass) { 02893 ACL_ignore_control_messages= ignore; //all control messages are ignored 02894 ACL_pass_control_messages= pass; // all control messages are as well copied to receivebuffer for application 02895 } 02896 02897 02901 byte ACLSentPacketInThisSlot() { 02902 return (LLSentPacketInThisSlot()); 02903 } 02904 02906 void ACLStart() { 02907 LLStart(); 02908 } 02909 02911 void ACLStop() { 02912 LLStop(); 02913 } 02914 02916 void ACLLockReceiveBuffer() { 02917 LLLockReceiveBuffer(); 02918 } 02919 02923 byte ACLReceiveBufferLocked() { 02924 return LLReceiveBufferLocked(); 02925 } 02926 02928 void ACLReleaseReceiveBuffer() { 02929 LLReleaseReceiveBuffer(); 02930 } 02931 02933 void ACLSetDataToOld() { 02934 LLSetDataToOld(); 02935 } 02936 02938 void ACLSetDataToNew() { 02939 LLSetDataToNew(); 02940 } 02941 02945 byte ACLDataIsNew() { 02946 return LLDataIsNew(); 02947 } 02948 02952 byte ACLDataReceivedInThisSlot() { 02953 return LLSentPacketInThisSlot(); 02954 } 02955 02956 02958 byte ACLDataIsNewNow() { 02959 return ACLDataReceivedInThisSlot(); 02960 } 02961 02962 02966 byte ACLAdressedDataIsNew() { 02967 if (!ACLDataIsNew()) return 0; 02968 02969 if (ACLFoundReceivedType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO)) { 02970 if (ACLMatchesMyIP( ACLGetReceivedData( ACL_TYPE_CAD_HI, ACL_TYPE_CAD_LO), 0 )) 02971 return 1; 02972 } 02973 return 0; 02974 } 02975 02979 byte ACLAdressedDataIsNewNow() { 02980 if (!ACLDataIsNewNow()) return 0; 02981 02982 if (ACLFoundReceivedType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO)) { 02983 if (ACLMatchesMyIP( ACLGetReceivedData( ACL_TYPE_CAD_HI, ACL_TYPE_CAD_LO), 0 )) 02984 return 1; 02985 } 02986 return 0; 02987 } 02988 02989 02995 byte ACLSendPacketAdressed(nodeAddrType *address, byte timeout) { 02996 02997 if (ACLGetRemainingPayloadSpace()<11) 02998 return 0; //not enough space 02999 03000 ACLAddNewType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO); 03001 ACLAddData(address->a1); 03002 ACLAddData(address->a2); 03003 ACLAddData(address->a3); 03004 ACLAddData(address->a4); 03005 ACLAddData(address->a5); 03006 ACLAddData(address->a6); 03007 ACLAddData(address->a7); 03008 ACLAddData(address->a8); 03009 03010 if (ACLSendPacket(timeout)) return 1; 03011 return 0; 03012 } 03014 // end of ACL Layer Functions 03015 03016 03021 void isr_timer0() interrupt INUM_TIMER0 { 03022 byte length; //, time1H, time1L, time2H, time2L; 03023 bool arbitrationResult, recResult; 03024 unsigned short receiveResult; 03025 03026 ISR_TIMER0_ADJUST(moduloTimer0); 03027 INT_SETFLAG(INUM_TIMER0, INT_CLR); 03028 // BLED= LED_ON; // start isr 03029 // BLED= LED_OFF; // start isr 03030 // BLED= LED_ON; // start isr 03031 // BLED= LED_OFF; // start isr 03032 /* time1H= TH0; 03033 time1L= TL0; */ 03034 03035 llState&= ~(LL_STATE_PACKET_JUST_RECEIVED | LL_STATE_PACKET_JUST_SENT); 03036 03037 if (!(rfState & RF_STATE_RUNNING)) { 03038 TIMER0_RUN(FALSE); 03039 return; 03040 } 03041 03042 // this is a slot-counter. every RF_SYNC_MAX_SLOT_COUNT slots, the node sends a sync packet 03043 // (if he is a master) or expects a sync packet to synchronize to. 03044 rfSlotCount++; 03045 rfSlotsWithoutSignalReceived++; 03046 //sendByteToUart(rfSlotCount); 03047 //DEBUG_NEWLINE; 03048 03049 if (rfSlotCount >= RF_SYNC_MAX_SLOT_COUNT) { 03050 // we are in a sync slot. so let's send a sync packet or receive one 03051 //YLED= ~YLED; 03052 //BLED= ~BLED; 03053 if (!(rfState & RF_STATE_ALONE)) { 03054 if (rfState & RF_STATE_MASTER) { 03055 // we are master, so send sync 03056 RFSendPacket(TRUE); 03057 #ifdef RF_USE_ALONE_DETECTION 03058 // try to receive an alone test signal, to know if other nodes are there 03059 if (RFAloneTest()) 03060 rfSlotsWithoutSignalReceived= 0; 03061 #endif 03062 #ifdef RF_USE_TRAFFIC_INDICATION 03063 RFTrafficIndication(); 03064 #endif 03065 if (++rfSyncCount == RF_MASTER_RIVAL_TEST_SYNC_COUNT) { 03066 // many beacon slots have passed, so let's check if we find another master 03067 rfSyncCount= 0; 03068 #ifndef DEBUG_ARBITRATION_TEST_ACTIVE 03069 rfState|= RF_STATE_MASTER_RIVAL_TEST; 03070 if (RFMasterRivalTest()) { 03071 // clear rival test and master flags 03072 rfState&= ~(RF_STATE_MASTER_RIVAL_TEST|RF_STATE_MASTER); 03073 rfFailedSyncCount= 0; 03074 rfSlotsWithoutSignalReceived= 0; 03075 } 03076 #endif 03077 } else { 03078 rfState&= ~RF_STATE_MASTER_RIVAL_TEST; 03079 } 03080 } else { 03081 // we are a slave, so try to receive a sync packet 03082 recResult= RFReceivePacket(&periodDataSyncByteReceiveTimeout, FALSE); 03083 if (recResult == 2) { 03084 #ifdef RF_USE_ALONE_DETECTION 03085 RFAloneTest(); // sends the alone test signal to tell the master that we are there 03086 #endif 03087 #ifdef RF_USE_TRAFFIC_INDICATION 03088 RFTrafficIndication(); 03089 #endif 03090 rfFailedSyncCount= 0; 03091 rfState|= RF_STATE_IN_SYNC; 03092 putchar('R'); 03093 DEBUG_NEWLINE; 03094 //YLED= ~YLED; 03095 } else { 03096 rfState&= ~RF_STATE_IN_SYNC; 03097 // if sync is lost for RF_MAX_FAILED_SYNC_COUNT times, we try to become master 03098 if (++rfFailedSyncCount >= RF_MAX_FAILED_SYNC_COUNT) { 03099 rfState|= RF_STATE_LONG_SEARCH_FOR_MASTER; 03100 rfFailedSyncCount= 0; 03101 } 03102 //YLED= LED_OFF; 03103 } 03104 } 03105 } 03106 rfSlotCount= 0; 03107 } else { 03108 // we are not in a sync slot (i.e. a regular slot for data transmission) 03109 // first, let's handle the alone stuff 03110 if (rfState & RF_STATE_ALONE) { 03111 #ifdef RF_USE_ALONE_DETECTION 03112 #ifdef RF_ALONE_DEEP_SLEEP 03113 // the node thinks it is alone, and deep sleeping is activated, 03114 // so we put it to sleep for 10-26 seconds. 03115 RFLowPowerSleep(RFAloneRandomNumberOfSeconds()); 03116 rfState&= ~RF_STATE_ALONE; 03117 if (RFLongSearchForMaster(RF_MASTER_SEARCH_SLOTS_AFTER_ALONE)) { 03118 rfState&= ~RF_STATE_MASTER; // clear the master flag, become a slave 03119 } else { 03120 rfState|= RF_STATE_MASTER; // set the master flag, become a master 03121 } 03122 // start the failed sync count and slot count from 0 03123 rfFailedSyncCount= rfSlotCount= rfSlotsWithoutSignalReceived= 0; 03124 #else 03125 // the node thinks it is alone, only transmission supression is activated 03126 if (( (rfSlotsInAloneModeLeft & 0xFF) % 32 ) == 0) 03127 RLED= ~RLED; 03128 //sendUshortToUart(rfSlotsInAloneModeLeft); 03129 //sendByteToUart(rfSlotCount); 03130 //DEBUG_SPACE; 03131 if (rfSlotsInAloneModeLeft-- == 0) { 03132 rfState&= ~RF_STATE_ALONE; 03133 if (RFLongSearchForMaster(RF_MASTER_SEARCH_SLOTS_AFTER_ALONE)) { 03134 rfState&= ~RF_STATE_MASTER; // clear the master flag, become a slave 03135 } else { 03136 rfState|= RF_STATE_MASTER; // set the master flag, become a master 03137 } 03138 // start the failed sync count and slot count from 0 03139 rfFailedSyncCount= rfSlotCount= rfSlotsWithoutSignalReceived= 0; 03140 } 03141 #endif 03142 #endif 03143 } else if (rfState & RF_STATE_LONG_SEARCH_FOR_MASTER) { 03144 // node must do á long search for master. use random length because if a master gives 03145 // up and all slaves find themselves in this state, they shouldn't all at once try 03146 // to become master 03147 if (RFLongSearchForMaster(10)) { 03148 rfState&= ~RF_STATE_MASTER; // clear the master flag, become a slave 03149 #ifdef RF_USE_ALONE_DETECTION 03150 RFAloneTest(); // send the alone test signal 03151 #endif 03152 } else { 03153 //rfState&= ~RF_STATE_MASTER; //just for testing 03154 rfState|= RF_STATE_MASTER; // set the master flag, become a master 03155 } 03156 rfState&= ~RF_STATE_LONG_SEARCH_FOR_MASTER; // clear the long search flag 03157 // start the failed sync count and slot count from 0 03158 rfFailedSyncCount= rfSlotCount= 0; 03159 } else if (rfState & RF_STATE_MASTER_RIVAL_TEST) { 03160 // do a short search for master just within the protocol 03161 if (RFMasterRivalTest()) { 03162 rfState&= ~RF_STATE_MASTER; 03163 rfState&= ~RF_STATE_MASTER_RIVAL_TEST; 03164 rfFailedSyncCount= rfSlotCount= 0; 03165 } 03166 } else if (rfState & (RF_STATE_IN_SYNC | RF_STATE_MASTER)) { 03167 // if we are in sync or a master and not alone we go on here. 03168 // 03169 // we do an arbitration for rf access. if we want to send a packet 03170 // we draw a random arbitration byte and compete for the medium. if we don't have 03171 // anything to send, choose 0 as arbitration byte so that arbitration can tell us 03172 // if the medium is busy or not. 03173 // 03174 // if node is alone or not in a traffic group, don't do arbitration pretend the 03175 // medium is free. 03176 arbitrationResult= ((rfState & RF_STATE_ALONE) || !(rfState & RF_STATE_TRAFFIC_GROUP)) ? 03177 TRUE : RFArbitration(llState & LL_STATE_NEED_TO_SEND); 03178 //putchar((arbitrationResult) ? 'y' : 'n'); 03179 if (llState & LL_STATE_NEED_TO_SEND) 03180 statsRfSlotsNeedToSend++; 03181 if (arbitrationResult && (llState & LL_STATE_NEED_TO_SEND)) { 03182 statsRfArbitrationWon++; 03183 // if we are alone, we just pretend to send the packet and set the flags 03184 // if we are not in a traffic group we don't send anything and don't update the flags 03185 if ((rfState & RF_STATE_TRAFFIC_GROUP) && !(rfState & RF_STATE_ALONE)) 03186 RFSendPacket(FALSE); 03187 if (rfState & RF_STATE_TRAFFIC_GROUP) { 03188 llState&= ~(LL_STATE_NEED_TO_SEND); 03189 llState|= LL_STATE_SEND_SUCCESS | LL_STATE_PACKET_JUST_SENT; 03190 } 03191 putchar((llState & LL_STATE_NEED_TO_SEND) ? 'n' : 'y'); 03192 } else { 03193 // only receive a packet if we neither detected a node that wants to send (arbitrationResult == false) 03194 // nor the receive buffer is locked 03195 if ( !(arbitrationResult || (llState & LL_STATE_RECEIVE_BUFFER_LOCKED)) ) { 03196 // for return type of RFReceivePacket() see documentation of that function 03197 // if we are an alone master, we don't need to receive a packet 03198 receiveResult= (rfState & RF_STATE_ALONE) ? 0 : RFReceivePacket(&periodDataSyncByteReceiveTimeout, TRUE); 03199 //sendByteToUart(receiveResult >> 8); 03200 //sendByteToUart(receiveResult); 03201 //DEBUG_SPACE; 03202 if ((receiveResult & 0xFF) == 1) { 03203 length= receiveResult >> 8; 03204 /* #ifdef DEBUG_USE_UART 03205 sendByteToUart(length); 03206 DEBUG_NEWLINE; 03207 #endif */ 03208 LL_payload_received_length= length; 03209 statsRfDataPacketsReceivedNotNeedingToSend++; 03210 llState|= LL_STATE_NEW_DATA | LL_STATE_PACKET_JUST_RECEIVED; 03211 rfSlotsWithoutSignalReceived= 0; 03212 } 03213 } 03214 } 03215 statsRfSlotsInSync++; 03216 } else { 03217 // if we are not in sync, only try to receive a packet because sending could damage 03218 // other packets or disturb an arbitration procedure. 03219 if ( !(llState & LL_STATE_RECEIVE_BUFFER_LOCKED) ) { 03220 // for return type of RFReceivePacket() see documentation of that function 03221 // if we are an alone master, we don't need to receive a packet 03222 receiveResult= (rfState & RF_STATE_ALONE) ? 0 : RFReceivePacket(&periodDataSyncByteReceiveTimeout, TRUE); 03223 //sendByteToUart(receiveResult >> 8); 03224 //sendByteToUart(receiveResult); 03225 //DEBUG_SPACE; 03226 if ((receiveResult & 0xFF) == 1) { 03227 length= receiveResult >> 8; 03228 /* #ifdef DEBUG_USE_UART 03229 sendByteToUart(length); 03230 DEBUG_NEWLINE; 03231 #endif */ 03232 LL_payload_received_length= length; 03233 llState|= LL_STATE_NEW_DATA | LL_STATE_PACKET_JUST_RECEIVED; 03234 rfSlotsWithoutSignalReceived= 0; 03235 } 03236 } 03237 } 03238 } // end of if that determines if current slot is a sync slot 03239 03240 putchar((arbitrationResult) ? 'w' : 'l'); 03241 DEBUG_NEWLINE; 03242 03243 // check if packet has been sent or sending slot timeout occurred and set the right llState. 03244 // this used to be done in LLSlotEnd(), but has moved here for efficiency. 03245 if ((llState & LL_STATE_NEED_TO_SEND)) { 03246 if (--LL_slots_left == 0) { 03247 03248 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 03249 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; // in case it was switched on, switch it off and do NOT update the success state 03250 } else { 03251 llState&= ~LL_STATE_SEND_SUCCESS; // busy is over 03252 } 03253 03254 llState&= ~LL_STATE_NEED_TO_SEND; 03255 LL_payload_sendbuffer_length= 0; // set used payload to zero 03256 } 03257 } 03258 03259 #ifdef RF_USE_ALONE_DETECTION 03260 if (rfSlotsWithoutSignalReceived > RF_ALONE_TEST_FAILED_THRESHOLD && !(rfState & RF_STATE_ALONE)) { 03261 rfState|= RF_STATE_ALONE; 03262 #ifndef RF_ALONE_DEEP_SLEEP 03263 rfSlotsInAloneModeLeft= RFAloneRandomNumberOfSlots(); 03264 #endif 03265 } 03266 #endif 03267 03268 #ifdef DEBUG_USE_UART 03269 #ifdef DEBUG_DUMP_RF_STATISTICS 03270 DebugDumpStats(); 03271 #endif 03272 #ifdef DEBUG_DUMP_RF_STATE 03273 DebugDumpRfState(); 03274 #endif 03275 #endif 03276 // BLED= LED_ON; // end isr 03277 // BLED= LED_OFF; // end isr 03278 // BLED= LED_ON; // end isr 03279 // BLED= LED_OFF; // end isr 03280 } //end isr_timer0() 03281 03282 03287 void isr_timer2() interrupt INUM_TIMER2 { 03288 INT_SETFLAG(INUM_TIMER2, INT_CLR); 03289 } 03290 03295 void isr_externalInterrupt() interrupt INUM_EXTERNAL0 { 03296 INT_SETFLAG(INUM_EXTERNAL0, INT_CLR); 03297 } 03298 03299 03300 /***************************************************************************** 03301 MAIN PROGRAM 03302 *****************************************************************************/ 03307 void main(void) { 03308 data unsigned long count=0; 03309 03310 // sets up the board 03311 ParticleInit(); 03312 03313 // initializes and starts all communications layers 03314 ACLStartUp(); 03315 //ACLSubscribeAll(); 03316 03317 while (TRUE) { 03318 if (!(rfState & RF_STATE_MASTER)) { 03319 rfState|= RF_STATE_IN_SYNC; 03320 03321 // compose packet content 03322 count++; 03323 ACLAddNewType(ACL_TYPE_ASM_HI, ACL_TYPE_ASM_LO); 03324 ACLAddData(count >> 24); 03325 ACLAddData(count >> 16); 03326 ACLAddData(count >> 8); 03327 ACLAddData(count & 0xFF); 03328 03329 // wait till packet can be sent 03330 ACLSendPacket(1); 03331 03332 // wait till packet has been sent 03333 while (ACLSendingBusy()) 03334 RF_LOW_POWER_SLEEP_UNTIL_NEXT_SLOT(); 03335 03336 RLED= GLED= LED_OFF; 03337 /* if (ACLGetSendSuccess()) 03338 putchar('y'); 03339 else 03340 putchar('n'); */ 03341 } 03342 03343 LLStop(); 03344 //RFLowPowerSleepTillKeyPressed(); 03345 RFLowPowerSleep(5); 03346 // start sync search 03347 LLStart(); 03348 RF_LOW_POWER_SLEEP_UNTIL_NEXT_SLOT(); 03349 } 03350 } // end of main()

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