stackDistSync.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 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_BEACON BIN(00010000) 00171 00178 #define RF_STATE_SHORT_SEARCH_FOR_BEACON 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 15 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 00255 #define RF_MAX_SYNC_JITTER 4000ul 00256 00261 #define RF_SLOT_LENGTH_IN_US 36865ul 00262 00268 #define RF_BEACON_SEARCH_SLOTS_STARTUP 50 00269 00273 #define RF_MASTER_SEARCH_SLOTS_AFTER_ALONE 167 00274 00279 #define RF_MAX_FAILED_SYNC_COUNT 10 00280 00293 #define RF_SYNC_ADDITIONAL_OFFSET 0xA3BD 00294 00308 #define RF_ARBITRATION_RSSI_THRESHOLD 0x78 00309 00318 #define RF_ALONE_TEST_RSSI_THRESHOLD 0xC0 00319 00327 #define RF_RENDEZVOUS_PACKET_START_HI 0x96 00328 #define RF_RENDEZVOUS_PACKET_START_LO 0x87 00329 00336 #define RF_RENDEZVOUS_ARBITRATION_START_HI 0x72 00337 #define RF_RENDEZVOUS_ARBITRATION_START_LO 0x20 00338 00348 #define RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS 0x390 00349 00353 #define RF_BUFFER_SIZE 64 00354 00361 //#define RF_USE_TRAFFIC_INDICATION 00362 00367 #define RF_RENDEZVOUS_TRAFFIC_INDICATION_HI 0xAE 00368 #define RF_RENDEZVOUS_TRAFFIC_INDICATION_LO 0x64 00369 00374 #define RF_RENDEZVOUS_ALONE_TEST_HI 0xAA 00375 #define RF_RENDEZVOUS_ALONE_TEST_LO 0x70 00376 00378 static byte rfSlotCount= 0; 00379 00381 static byte rfSlotsWithoutSignalReceived= 0; 00382 00387 data byte rfScramblerReg= BIN(01101100); 00388 00390 data unsigned long randomNumberReg; 00391 00397 data unsigned short tEndAvg= 0; 00399 //data unsigned long rfAverageTEndBuffer; 00400 00402 xdata TIMER_DATA periodDataSlot; 00403 xdata TIMER_DATA periodDataSyncByteReceiveTimeout; 00404 00406 xdata signed short moduloTimer0; 00407 00408 #ifdef DEBUG_USE_UART 00409 #ifdef DEBUG_DUMP_RSSI_SAMPLES 00410 00411 xdata byte rssiSamples[16]; 00413 unsigned short arbitrationGood= 0; 00415 unsigned short arbitrationBad= 0; 00416 #endif 00417 #endif 00418 00432 RF_RXTXPAIR_SETTINGS code RF_SETTINGS = { 00433 0xA0, 0x2F, 0x52, // Modem 0, 1 and 2 00434 0x76, 0x00, 0x00, // Freq A 00435 0x58, 0x7A, 0x8D, // Freq B 00436 0x01, 0xAB, // FSEP 1 and 0 00437 0x40, // PLL_RX 00438 0x30, // PLL_TX 00439 0x6C, // CURRENT_RX 00440 0xF3, // CURRENT_TX 00441 0x32, // FREND 00442 0xA0, // PA_POW 00443 0x00, // MATCH 00444 0x00, // PRESCALER 00445 }; 00447 xdata RF_RXTXPAIR_CALDATA RF_CALDATA; 00448 00459 xdata byte rfFieldStrength; 00460 00461 #ifdef RF_USE_ALONE_DETECTION 00462 #ifndef RF_ALONE_DEEP_SLEEP 00463 00469 xdata unsigned short rfSlotsInAloneModeLeft; 00470 #endif 00471 #endif 00472 00477 xdata unsigned short rfMaxTimerValue; 00479 00480 00483 00484 #define LL_PROTOCOL_VERSION 4 00485 #define LL_HEADER_SIZE 12 00486 #define LL_PAYLOAD_SIZE 64 00487 #define LL_TAIL_SIZE 2 00488 #define LL_FRAME_MAX_SIZE (LL_HEADER_SIZE+LL_PAYLOAD_SIZE+LL_TAIL_SIZE) 00489 00490 #define LL_CRC16_POLY 0x1021 00491 00493 #define LL_CRC16_INIT 0xFFFF 00494 00496 #define LL_CRC_OK 0 00497 00499 xdata byte LL_sequence_no; 00500 00501 byte LL_slots_left; 00502 00504 #define LL_STATE_SEND_SUCCESS BIN(00000001) 00505 00506 #define LL_STATE_CTRL_MESSAGE_INSERTED BIN(00000010) 00507 00511 #define LL_STATE_NEED_TO_SEND BIN(00000100) 00512 00513 #define LL_STATE_RECEIVE_BUFFER_LOCKED BIN(00001000) 00514 00515 #define LL_STATE_NEW_DATA BIN(00010000) 00516 00517 #define LL_STATE_PACKET_JUST_SENT BIN(00100000) 00518 00519 #define LL_STATE_PACKET_JUST_RECEIVED BIN(01000000) 00520 00521 #define LL_STATE_RUNNING BIN(10000000) 00522 00542 data volatile byte llState= 0; 00543 data static byte llStateOld= 0xFF; 00544 00545 code nodeAddrType llEpromAddress= {10, 1, 0, 1, 10, 1, 2, 0}; 00547 00550 00552 #define ACL_SUBSCRIPTIONLIST_LENGTH 8 00553 00555 #define ACL_CONTROL_MESSAGES_TIMEOUT 30 00556 00558 #define ACL_TYPE_ACM_H ACL_TYPE_ACM_HI 00559 00560 #define ACL_TYPE_ACM_L ACL_TYPE_ACM_LO 00561 00562 00563 xdata byte ACL_subscriptions[ACL_SUBSCRIPTIONLIST_LENGTH][2]; 00564 xdata boolean ACL_send_buffer_locked; 00565 xdata boolean ACL_subscribe_all; 00566 xdata boolean ACL_ACM_answers; // answer on ACM messages like helo and so on 00567 xdata byte ACL_write_position; 00568 00569 xdata boolean ACL_ignore_control_messages; 00570 xdata boolean ACL_pass_control_messages; 00572 00578 00579 00580 xdata byte LL_header_received[LL_HEADER_SIZE]; 00582 xdata byte LL_payload_received[LL_PAYLOAD_SIZE]; 00584 xdata byte LL_payload_received_length; 00586 xdata byte LL_tail_received[LL_TAIL_SIZE]; 00588 00595 00596 00597 volatile xdata byte LL_header_receivebuffer[LL_HEADER_SIZE]; 00599 volatile xdata byte LL_payload_receivebuffer[LL_PAYLOAD_SIZE]; 00601 volatile xdata byte LL_payload_receivebuffer_length; 00603 volatile xdata byte LL_tail_receivebuffer[LL_TAIL_SIZE]; 00605 00612 00613 00614 xdata byte LL_payload_send[LL_PAYLOAD_SIZE]; 00616 xdata byte LL_payload_send_length; 00618 00625 00626 00627 xdata byte LL_header_sendbuffer[LL_HEADER_SIZE]; 00629 xdata byte LL_payload_sendbuffer[LL_PAYLOAD_SIZE]; 00631 xdata byte LL_payload_sendbuffer_length; 00633 xdata byte LL_tail_sendbuffer[LL_TAIL_SIZE]; 00635 00638 00639 xdata unsigned long statsRfSlotsInSync; 00640 00642 xdata unsigned long statsRfSlotsNeedToSend; 00644 xdata unsigned long statsRfArbitrationWon; 00645 00646 /* /// those of the statsRfSlotsNeedToSend where arbitration was lost 00647 xdata unsigned long statsRfArbitrationLost; 00649 xdata unsigned long statsRfDataPacketsReceivedNeedingToSend; 00650 00652 xdata unsigned long statsRfArbitrationSingalDetected; 00654 xdata unsigned long statsRfArbitrationNoSingalDetected; */ 00655 00657 xdata unsigned long statsRfDataPacketsReceivedNotNeedingToSend; 00659 00662 #ifdef DEBUG_USE_UART 00663 #ifdef DEBUG_DUMP_RF_STATE 00664 00667 void DebugDumpRfState() { 00668 if (rfState != rfStateOld || llState != llStateOld) { 00669 putchar((rfState & RF_STATE_RUNNING) ? 'R' : 'S'); 00670 putchar('-'); 00671 putchar((rfState & RF_STATE_SHORT_SEARCH_FOR_BEACON) ? 'R' : '-'); 00672 putchar((rfState & RF_STATE_LONG_SEARCH_FOR_BEACON) ? 'L' : '-'); 00673 putchar((rfState & RF_STATE_TRAFFIC_GROUP) ? 'T' : '-'); 00674 putchar((rfState & RF_STATE_IN_SYNC) ? 'Y' : '-'); 00675 putchar((rfState & RF_STATE_ALONE) ? 'A' : '-'); 00676 putchar((rfState & RF_STATE_MASTER) ? 'M' : 'S'); 00677 DEBUG_SPACE; 00678 rfStateOld= rfState; 00679 putchar((llState & LL_STATE_RUNNING) ? 'R' : '-'); 00680 putchar((llState & LL_STATE_PACKET_JUST_RECEIVED) ? 'J' : '-'); 00681 putchar((llState & LL_STATE_PACKET_JUST_SENT) ? 'J' : '-'); 00682 putchar((llState & LL_STATE_NEW_DATA) ? 'N' : '-'); 00683 putchar((llState & LL_STATE_RECEIVE_BUFFER_LOCKED) ? 'L' : '-'); 00684 putchar((llState & LL_STATE_NEED_TO_SEND) ? 'S' : '-'); 00685 putchar((llState & LL_STATE_CTRL_MESSAGE_INSERTED) ? 'C' : '-'); 00686 putchar((llState & LL_STATE_SEND_SUCCESS) ? 'S' : '-'); 00687 DEBUG_NEWLINE; 00688 llStateOld= llState; 00689 } 00690 } 00691 #endif 00692 #endif 00693 00698 #ifdef DEBUG_USE_UART 00699 void DebugDumpReceivedAclPacket() { 00700 int i, dataEnd; 00701 00702 ACLLockReceiveBuffer(); 00703 00704 // dump last byte of sender address 00705 putchar('A'); 00706 putchar(':'); 00707 sendByteToUart(LL_header_received[3+7]); 00708 DEBUG_SPACE; 00709 // dump packet content 00710 for (i=0; i < LL_payload_received_length;) { 00711 sendByteToUart(LL_payload_received[i++]); 00712 sendByteToUart(LL_payload_received[i++]); 00713 dataEnd= i + LL_payload_received[i++]; 00714 putchar(':'); 00715 while (i < dataEnd && i < LL_payload_received_length) 00716 sendByteToUart(LL_payload_received[i++]); 00717 DEBUG_SPACE; 00718 } 00719 DEBUG_NEWLINE; 00720 } 00721 00722 #endif 00723 00728 void putchar(char c) { 00729 #ifdef DEBUG_USE_UART 00730 #ifdef DEBUG_USE_UART1_FOR_DUMPING 00731 UART1_WAIT_AND_SEND(c); 00732 #else 00733 UART0_WAIT_AND_SEND(c); 00734 #endif 00735 #endif 00736 } 00737 00741 void DebugBuildTestPacketACL() { 00742 /* 00743 byte i; 00744 // this is only testcode 00745 00746 ACLAddNewType(ACL_TYPE_ACM_H,ACL_TYPE_ACM_L); 00747 ACLAddNewType(115,216); //helo 00748 00749 ACLAddData(255); 00750 ACLAddData(255); 00751 ACLAddData(255); 00752 ACLAddData(255); 00753 00754 ACLAddData(255); 00755 ACLAddData(255); 00756 ACLAddData(255); 00757 ACLAddData(255); 00758 00759 00760 //ACLAddNewType(100,100); 00761 //for (i=0;i<32;i++) 00762 // ACLAddData(i); 00763 */ 00764 } 00765 00766 00767 #ifdef DEBUG_VERIFY_SCRAMBLING 00768 bool DebugScrambleAndDescrambleTest() { 00769 byte b, j; 00770 byte scrReg; 00771 00772 // test scrambling and descrambling for some numbers 00773 j=0; 00774 YLED= LED_ON; 00775 do { 00776 b= j; 00777 YLED= ~YLED; 00778 00779 // scramble b 00780 scrReg= rfScramblerReg; 00781 b= RFScramble(j); 00782 00783 // descramble b 00784 rfScramblerReg= scrReg; 00785 b= RFDescramble(b); 00786 00787 if (b != j) { 00788 YLED= LED_OFF; 00789 return FALSE; 00790 } 00791 00792 rfScramblerReg= scrReg; 00793 } while (++j); 00794 YLED= LED_OFF; 00795 return TRUE; 00796 } 00797 #endif 00798 00799 #ifdef DEBUG_USE_UART 00800 #ifdef DEBUG_DUMP_RF_STATISTICS 00801 00804 void DebugResetStats() { 00805 statsRfSlotsInSync= 0; 00806 00807 statsRfSlotsNeedToSend= 0; 00808 statsRfArbitrationWon= 0; 00809 00810 statsRfDataPacketsReceivedNotNeedingToSend= 0; 00811 } 00812 00816 void DebugDumpStats() { 00817 //sendUlongToUart(statsRfSlotsInSync); 00818 //putchar(':'); 00819 sendUlongToUart(statsRfSlotsNeedToSend); 00820 putchar(':'); 00821 sendUlongToUart(statsRfArbitrationWon); 00822 putchar(':'); 00823 sendUlongToUart(statsRfDataPacketsReceivedNotNeedingToSend); 00824 DEBUG_NEWLINE; 00825 } 00826 #endif 00827 #endif 00828 00829 00830 00836 00837 void ParticleInit() { 00838 #ifdef DEBUG_USE_UART 00839 byte i; 00840 #endif 00841 00842 // Initialize peripherals 00843 //WDT_ENABLE(FALSE); 00844 // for BIZARRE reasons, the watchdog timer is only disabled when the 00845 // WDT register is set directly and TWICE (???). found this out by trial and error. 00846 WDT= 0x13; 00847 WDT= 0x13; 00848 00849 // Set optimum settings for speed and low power consumption 00850 MEM_NO_WAIT_STATES(); 00851 FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS); 00852 00853 // setup leds 00854 RLED_OE(TRUE); 00855 YLED_OE(TRUE); 00856 GLED_OE(TRUE); 00857 BLED_OE(TRUE); 00858 RLED= YLED= GLED= BLED= LED_OFF; 00859 00860 TIMER0_RUN(FALSE); 00861 00862 // generate a truly random seed for the random number generator 00863 // rf must not be used when this happens 00864 RFInitRandom(); 00865 00866 #ifdef DEBUG_VERIFY_SCRAMBLING 00867 // make sure scrambling and descrambling macros work correctly 00868 if (!DebugScrambleAndDescrambleTest()) { 00869 RLED= LED_ON; 00870 while (1); 00871 } 00872 #endif 00873 00874 #ifdef DEBUG_USE_UART 00875 #ifdef DEBUG_DUMP_RSSI_SAMPLES 00876 for (i=0; i<3*8; i++) { 00877 rssiSamples[i]= 0; 00878 } 00879 #endif 00880 #endif 00881 00882 #ifdef DEBUG_USE_UART 00883 // setup UART for serial debug messages 00884 #ifdef DEBUG_USE_UART1_FOR_DUMPING 00885 UART1_SETUP(57600, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_TX_ONLY | UART_POLLED); 00886 #else 00887 UART0_SETUP(57600, CC1010EB_CLKFREQ, UART_NO_PARITY | UART_TX_ONLY | UART_POLLED); 00888 #endif 00889 00890 DEBUG_CLEAR_SCREEN; 00891 sendStringToUart("Welcome to the CC1010 Particle Sensor Node!\r\nOn this console you will see debug information. Have Fun!\r\n\r\nSync method: Distributed\r\n\r\nRandom numbers:\r\n"); 00892 00893 for (i=0; i<10; i++) { 00894 sendByteToUart(RF_NEW_RANDOM_BYTE()); 00895 DEBUG_SPACE; 00896 } 00897 DEBUG_NEWLINE; 00898 #endif 00899 00900 // let the 32 kHz clock run continously 00901 X32_INPUT_SOURCE(X32_USING_CRYSTAL); 00902 X32_ENABLE(TRUE); 00903 } 00904 00905 byte AppSelfTest(char* result) { 00906 int i; 00907 00908 for (i=0; i<10; i++) { 00909 result[i]= RF_NEW_RANDOM_BYTE(); 00910 } 00911 00912 return TRUE; 00913 } 00915 00918 00920 void RFInit() { 00921 ulong tmp; 00922 00923 // Calibration (now done in rfStart) 00924 // halRFCalib(&RF_SETTINGS, &RF_CALDATA); 00925 00926 // calculate timer periods for timer 2 (timeouts) 00927 halCalcTimerPeriod(RF_SLOT_LENGTH_IN_US, CC1010EB_CLKFREQ, &periodDataSlot); 00928 halCalcTimerPeriod(RF_MAX_SYNC_JITTER, CC1010EB_CLKFREQ, &periodDataSyncByteReceiveTimeout); 00929 00930 // Set up timer 0 as the slot timer 00931 TIMER0_RUN(FALSE); 00932 tmp= halConfigTimer01(TIMER0|TIMER01_INT_TIMER, RF_SLOT_LENGTH_IN_US, CC1010EB_CLKFREQ, &moduloTimer0); 00933 #ifdef DEBUG_USE_UART 00934 sendStringToUart("\r\nTimer start val:\r\n"); 00935 sendUshortToUart(moduloTimer0); 00936 DEBUG_NEWLINE; 00937 #endif 00938 // Turn on interrupts for timer0, turn off for timer 2 00939 INT_GLOBAL_ENABLE(INT_ON); 00940 INT_ENABLE(INUM_TIMER0, TRUE); 00941 INT_ENABLE(INUM_TIMER2, FALSE); 00942 PA_POW= 0; 00943 } 00944 00946 void RFStart() { 00947 if ((rfState & RF_STATE_RUNNING) == 0) { 00948 // set current field strength from data structure 00949 rfFieldStrength= RF_SETTINGS.pa_pow; 00950 00951 // Calibration 00952 halWait(200, CC1010EB_CLKFREQ); 00953 halRFCalib(&RF_SETTINGS, &RF_CALDATA); 00954 00955 // the function halRFSetRxTxOff() has to be called at least once, because later, we use 00956 // the simpler functions RFstartRx and RFstartTx instead, that don't set the values needed. 00957 halRFSetRxTxOff(RF_RX, &RF_SETTINGS, &RF_CALDATA); 00958 halWait(200, CC1010EB_CLKFREQ); 00959 halRFSetRxTxOff(RF_TX, &RF_SETTINGS, &RF_CALDATA); 00960 halWait(200, CC1010EB_CLKFREQ); 00961 halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 00962 halWait(200, CC1010EB_CLKFREQ); 00963 00964 // turn on ADC for RSSI reading (needs 200ms, because it waits until ADC has stabilized) 00965 halRFReadRSSIlevel(RSSI_MODE_INIT); 00966 ADC_POWER(FALSE); 00967 } 00968 PA_POW= 0; 00969 00970 // set initial rf state 00971 rfState= RF_STATE_RUNNING | RF_STATE_LONG_SEARCH_FOR_BEACON; // RF_STATE_MASTER | RF_STATE_ALONE; // 00972 #ifndef RF_USE_TRAFFIC_INDICATION 00973 // if traffic indication is not used, the flag has to be set permanently, so that 00974 // all packets are sent and received immediately 00975 rfState|= RF_STATE_TRAFFIC_GROUP; 00976 #endif 00977 00978 #ifdef DEBUG_USE_UART 00979 #ifdef DEBUG_DUMP_RF_STATE 00980 DebugDumpRfState(); 00981 #endif 00982 #endif 00983 00984 // reset slot counters 00985 rfSlotCount= rfSlotsWithoutSignalReceived= 0; 00986 00987 rfMaxTimerValue= 0; 00988 00989 // start timer0 00990 INT_SETFLAG(INUM_TIMER0, INT_CLR); 00991 TIMER0_RUN(TRUE); 00992 } 00993 00995 void RFStop() { 00996 // clear flag RF_STATE_RUNNING and all other flags 00997 rfState= 0; 00998 // stop timer0 00999 TIMER0_RUN(FALSE); 01000 // stop sending 01001 RF_SET_MODE_SLEEP; 01002 // turn off rf power 01003 //PA_POW= 0; 01004 } 01005 01010 void RFSetModeTransmit() { 01011 code byte *rfSettingsPtr= (code byte *)&RF_SETTINGS; 01012 unsigned short i; 01013 01014 PA_POW= 0; 01015 01016 // turn on transceiver for tx-ing 01017 PLL=rfSettingsPtr[12]; // pll_tx 01018 RFMAIN=0xF0; 01019 TEST5=0x10 | RF_CALDATA.vco_ao_tx; 01020 TEST6=0x3B; 01021 CURRENT=rfSettingsPtr[14]; 01022 01023 // Wait 200 usec before monitoring LOCK, slowly raise rf power to reduce spurious emission. 01024 // the data sheet says 200 usec for each step is optimal, but this is too long for our application 01025 // we use 50 us for each step. this should mitigate spurious emission. 01026 for(i=LOCK_MONITOR_DELAY; i>0; i--); 01027 01028 /* for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01029 if (rfFieldStrength >= 0x01) 01030 PA_POW= 0x01; 01031 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01032 if (rfFieldStrength >= 0x1E) 01033 PA_POW= 0x1E; 01034 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01035 if (rfFieldStrength >= 0x8F) 01036 PA_POW= 0x8F; 01037 for(i=LOCK_MONITOR_DELAY / 4; i>0; i--); 01038 PA_POW= rfFieldStrength; */ 01039 01040 // Wait for lock (much less than normal version!) 01041 for(i=LOCK_TIMEOUT; !(LOCK&0x01) && (i>0); i--); 01042 PA_POW= rfFieldStrength; 01043 } 01044 01049 void RFSetModeReceive() { 01050 code byte *rfSettingsPtr= (code byte *)&RF_SETTINGS; 01051 unsigned short i; 01052 01053 // turn on transceiver for rx-ing 01054 PLL=rfSettingsPtr[11]; // pll_rx 01055 RFMAIN=0x30; 01056 TEST5=0x10 | RF_CALDATA.vco_ao_rx; 01057 TEST6=0x20 | RF_CALDATA.chp_co_rx; 01058 CURRENT=rfSettingsPtr[13]; 01059 01060 // Wait 200 usec before monitoring LOCK 01061 // notice: had to change the value of constant LOCK_MONITOR_DELAY, because 01062 // it produced a wait time of 418 us -> sdcc seems to have less efficient 01063 // integer algorithms. perhaps keil sees that LOCK_MONITOR_DELAY is < 256 01064 // and temporary uses i as a byte variable (?), which is much faster 01065 for(i=LOCK_MONITOR_DELAY; i>0; i--); 01066 01067 // Wait for lock (much less than normal version!) 01068 for(i=LOCK_TIMEOUT; !(LOCK&0x01) && (i>0); i--); 01069 } 01070 01072 void RFInitRandom() { 01073 byte a; 01074 01075 randomNumberReg= 0; 01076 do { 01077 halRandomNumberGen(&a, 1); 01078 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01079 randomNumberReg= (ulong)a << 24; 01080 do { 01081 halRandomNumberGen(&a, 1); 01082 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01083 randomNumberReg|= (ulong)a << 16; 01084 do { 01085 halRandomNumberGen(&a, 1); 01086 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01087 randomNumberReg|= (ulong)a << 8; 01088 do { 01089 halRandomNumberGen(&a, 1); 01090 } while (bit_ones(a) < 2 || bit_ones(a) > 6); 01091 randomNumberReg|= (ulong)a; 01092 01093 for (a=0; a<128; a++) 01094 RF_NEW_RANDOM_BYTE(); 01095 } 01096 01126 unsigned short RFReceivePacket(xdata TIMER_DATA *timeOutPeriodData, bool waitForPacketsRendevouz, bool ignoreDataPackets) { 01127 bool syncByteReceived, aclMatch; 01128 byte j, tHi, tLo, a, b, length, sync; 01129 unsigned short crc; 01130 01131 // Turn on RF for RX, do this early, because it needs 200us to power up 01132 RFSetModeReceive(); 01133 //halRFSetRxTxOff(RF_RX, &RF_SETTINGS, &RF_CALDATA); 01134 //BLED= ~BLED; 01135 RFCON|=0x01; // Ensure that bytemode is selected 01136 RF_SET_PREAMBLE_COUNT(RF_PREAMBLE_LENGTH_RX); 01137 RF_SET_SYNC_BYTE(RF_SUITABLE_SYNC_BYTE); 01138 MODEM1=(MODEM1&0x03)|0x24; // Make sure avg filter is free-running + 22 baud settling time 01139 INT_ENABLE(INUM_RF, INT_OFF); 01140 INT_SETFLAG(INUM_RF, INT_CLR); 01141 01142 if (waitForPacketsRendevouz) { 01143 /* j= TH0; 01144 b= TL0; 01145 sendByteToUart(j); 01146 sendByteToUart(b); 01147 DEBUG_NEWLINE; 01148 RF_SET_MODE_SLEEP; 01149 return 0; */ 01150 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_PACKET_START_HI, RF_RENDEZVOUS_PACKET_START_LO); 01151 } 01152 BLED= LED_ON; // start receiving 01153 RF_START_RX(); 01154 01155 // setup timer 2 as a non-interrupt timer for timeout of sync byte 01156 halConfigTimerFast23(TIMER2|TIMER23_NO_INT_TIMER, timeOutPeriodData); 01157 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01158 TIMER2_RUN(TRUE); 01159 //BLED= ~BLED; 01160 01161 // receive sync byte 01162 syncByteReceived= FALSE; 01163 while (1) { 01164 // Check if timer2 is finished so that we time out 01165 // alternatively, if interrupt for timer0 is enabled, also check if enough time is left 01166 // before the next slot 01167 if (INT_GETFLAG(INUM_TIMER2) || (ET0 == INT_ON && !RF_SLOT_500_US_LEFT)) { 01168 // Clear interrupt and timeout 01169 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01170 //RLED= ~RLED; 01171 break; 01172 } 01173 // Check if sync byte received 01174 if (INT_GETFLAG(INUM_RF)) { 01175 INT_SETFLAG(INUM_RF, INT_CLR); // Clear the flag 01176 syncByteReceived= TRUE; 01177 break; 01178 } 01179 } 01180 //BLED= ~BLED; 01181 //BLED= ~BLED; 01182 01183 length= 0; 01184 if (syncByteReceived) { 01185 // turn on blue led when sync byte is received 01186 //BLED= ~BLED; 01187 //YLED= ~YLED; 01188 01189 // Lock average filter 01190 RF_LOCK_AVERAGE_FILTER(TRUE); 01191 01192 // receveive scrambled FF to train local descrambling register 01193 RF_SPI_RECEIVE_BYTE(a); 01194 a= RFDescramble(a); 01195 01196 // get rf options byte 01197 RF_SPI_RECEIVE_BYTE(a); 01198 a= RFDescramble(a); 01199 01200 // it's a beacon if the lsb of the rf options byte is 1 01201 sync= a & 1; 01202 01203 if (sync) { 01204 // receive timestamp 01205 RF_SPI_RECEIVE_BYTE(tHi); 01206 tHi= RFDescramble(tHi); 01207 RF_SPI_RECEIVE_BYTE(tLo); 01208 tLo= RFDescramble(tLo); 01209 // receive the xor protection byte 01210 RF_SPI_RECEIVE_BYTE(j); 01211 j= RFDescramble(j); 01212 01213 // if xor bit is correct 01214 if (j == tHi ^ tLo) { 01215 // wait for next byte time to prevent sync jitter induced by unsteady execution time 01216 // of descrambling the last byte 01217 //while(!RF_BYTE_RECEIVED()); 01218 TIMER0_RUN(FALSE); 01219 a= TH0; // for testing of clock drift, comment out this and following 3 lines 01220 b= TL0; 01221 TH0= tHi; 01222 TL0= tLo; 01223 TIMER0_RUN(TRUE); 01224 01225 //GLED= ~GLED; 01226 // turn rf off 01227 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01228 length= 1; 01229 01230 #ifdef DEBUG_DUMP_SYNC_VALUES 01231 #ifdef DEBUG_USE_UART 01232 // dump received tHi and tLo 01233 sendByteToUart(tHi); 01234 sendByteToUart(tLo); 01235 DEBUG_SPACE; 01236 01237 // dump local tHi and tLo before setting 01238 sendByteToUart(a); 01239 sendByteToUart(b); 01240 DEBUG_SPACE; 01241 01242 // dump difference between reported and set timer values 01243 sendSignedByteToUart((int)tHi-(int)a,3); 01244 sendSignedByteToUart((int)tLo-(int)b,3); 01245 DEBUG_NEWLINE; 01246 #endif 01247 #endif 01248 // putchar('R'); 01249 // DEBUG_NEWLINE; 01250 } 01251 } else { 01252 // stop if no data packets are needed 01253 if (ignoreDataPackets) { 01254 RF_SET_MODE_SLEEP; 01255 BLED= LED_OFF; 01256 return 0; 01257 } 01258 01259 // get physical payload length 01260 RF_SPI_RECEIVE_BYTE(length); 01261 length= RFDescramble(length); 01262 01263 // read acl filter types 01264 RF_SPI_RECEIVE_BYTE(a); 01265 a= RFDescramble(a); 01266 01267 RF_SPI_RECEIVE_BYTE(b); 01268 b= RFDescramble(b); 01269 01270 // now we expect 4 "don't-care bytes" in order to have enough time to check local subscriptions 01271 if (ACL_subscribe_all) { 01272 // don't test anything 01273 RF_SPI_RECEIVE_BYTE(a); 01274 //BLED= ~BLED; 01275 //BLED= ~BLED; 01276 RF_SPI_RECEIVE_BYTE(a); 01277 //BLED= ~BLED; 01278 //BLED= ~BLED; 01279 RF_SPI_RECEIVE_BYTE(a); 01280 //BLED= ~BLED; 01281 //BLED= ~BLED; 01282 RF_SPI_RECEIVE_BYTE(a); 01283 //BLED= ~BLED; 01284 //BLED= ~BLED; 01285 } else { 01286 aclMatch= FALSE; 01287 // check subscriptions 0-3 01288 for (i=0; i < ACL_SUBSCRIPTIONLIST_LENGTH; i+= ACL_SUBSCRIPTIONLIST_LENGTH / 4) { 01289 // receive next 0 01290 while(!(EXIF & 0x10)); EXIF&=~0x10; 01291 for (j=i;j < i + ACL_SUBSCRIPTIONLIST_LENGTH / 4;j++) { 01292 if ((ACL_subscriptions[j][0] == a) && (ACL_subscriptions[j][1] == b)) 01293 aclMatch= TRUE; 01294 //BLED= ~BLED; 01295 //BLED= ~BLED; 01296 } 01297 } 01298 if (!aclMatch) { 01299 RF_SET_MODE_SLEEP; 01300 return 0; 01301 } 01302 } 01303 01304 // receive the incoming packet into the receivebuffer 01305 01306 // receive ll header 01307 for (j=0; j<12; j++) { 01308 RF_SPI_RECEIVE_BYTE(b); 01309 LL_header_receivebuffer[j]= RFDescramble(b); 01310 } 01311 01312 // receive ll packet payload 01313 length= length-LL_HEADER_SIZE-LL_TAIL_SIZE; 01314 if (length > 64) { 01315 // length is not in allowed range 01316 RF_SET_MODE_SLEEP; 01317 return 0; 01318 } 01319 for (j=0; j<length; j++) { 01320 RF_SPI_RECEIVE_BYTE(b); 01321 LL_payload_receivebuffer[j]= RFDescramble(b); 01322 } 01323 01324 // receive ll tail 01325 for (j=0; j<LL_TAIL_SIZE; j++) { 01326 RF_SPI_RECEIVE_BYTE(b); 01327 LL_tail_receivebuffer[j]= RFDescramble(b); 01328 } 01329 // turn rf off 01330 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01331 01332 // check the crc of the packet 01333 crc= LLCalcCRC16(LL_header_receivebuffer, LL_payload_receivebuffer, length); 01334 if ((crc >> 8) == LL_tail_receivebuffer[0] && (crc & 0xFF) == LL_tail_receivebuffer[1]) { 01335 // crc was correct, so copy received packet to the "received" buffer 01336 for (i=0; i<LL_HEADER_SIZE; i++) { 01337 LL_header_received[i]= LL_header_receivebuffer[i]; 01338 } 01339 for (i=0; i<length; i++) { 01340 LL_payload_received[i]= LL_payload_receivebuffer[i]; 01341 } 01342 for (i=0; i<LL_TAIL_SIZE; i++) { 01343 LL_tail_received[i]= LL_tail_receivebuffer[i]; 01344 } 01345 } else { 01346 // crc check failed 01347 /* if (onlyDataPackets) { 01348 putchar('c'); 01349 } */ 01350 RF_SET_MODE_SLEEP; 01351 return 0; 01352 } 01353 } 01354 } 01355 01356 // turn transceiver off 01357 RFCON&=~0x01; // Ensure that bitmode is selected 01358 01359 // don't write back the local scrambler register to the global variable: 01360 // each node should have it's own scrReg sequence 01361 // rfScramblerReg= scrReg; 01362 01363 // stop timer 2 and start timer 3 if not already done 01364 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01365 TIMER2_RUN(FALSE); 01366 01367 BLED= LED_OFF; // end RFReceivePacket 01368 /* if (onlyDataPackets) { 01369 putchar('L'); 01370 sendByteToUart(length); 01371 DEBUG_SPACE; 01372 } */ 01373 if (sync && length == 1) { 01374 return 2; 01375 } else if (!sync && length > 0) { 01376 return 1 + 256 * length; 01377 } else { 01378 return 0; 01379 } 01380 } 01381 01389 byte RFArbitrationBit(bool sendOrListen, byte syncTimeH, byte syncTimeL) { 01390 byte i, sample1, paPow; 01391 01392 if (sendOrListen) { 01393 // bit is 1, so send a short signal (2 bit) 01394 // turn on transceiver for tx-ing 01395 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01396 BLED= ~BLED; 01397 // increase transmission power for the small burst 01398 paPow= PA_POW; 01399 PA_POW= 0xF0; 01400 RFSetModeTransmit(); 01401 RFCON&=~0x01; // Ensure that bitmode is selected 01402 //RFBUF= 0xFF; 01403 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01404 BLED= ~BLED; 01405 // write rf buffer 01406 RFBUF= sample1= BIN(10101010); 01407 01408 // wait for next rendezvous point 01409 WAIT_FOR_TIMER0_EXACT(syncTimeH, syncTimeL); 01410 BLED= ~BLED; 01411 RF_START_TX(); 01412 for (i=0; i<30; i++) { 01413 sample1= ((sample1 << 1) | (sample1 >> 7)); // rotate left (not through carry, because it's faster!) 01414 while(!RF_READY_TO_SEND()); 01415 RFBUF= sample1; 01416 } 01417 while(!RF_READY_TO_SEND()); 01418 BLED= ~BLED; 01419 BLED= ~BLED; // mark the 1 with an extra peak 01420 BLED= ~BLED; 01421 PA_POW= paPow; 01422 01423 // we sent a 1, so we must send the highest number possible, because a low number could 01424 // indicate that we lost the arbitration. 01425 return 255; 01426 } else { 01427 // bit is 0, so listen to the channel 01428 // turn on transceiver for rx-ing 01429 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01430 BLED= ~BLED; 01431 // turn on continuous sampling 01432 RFSetModeReceive(); // param true means fast 01433 RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF); 01434 RF_START_RX(); 01435 RFCON&=~0x01; // Ensure that bitmode is selected 01436 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01437 BLED= ~BLED; 01438 01439 // wait for next rendezvous point 01440 WAIT_FOR_TIMER0_EXACT(syncTimeH, syncTimeL-5); 01441 // the next two lines must (for strange) reasons be there 01442 // because otherwise you would get the result of the last 01443 // sample instead of the current result 01444 BLED= ~BLED; 01445 ADC_SAMPLE_SINGLE(); 01446 ADC_GET_SAMPLE_8BIT(); 01447 ADC_SAMPLE_SINGLE(); 01448 sample1= ADC_GET_SAMPLE_8BIT(); 01449 01450 BLED= ~BLED; // mark finish of sync slice 01451 // if the medium is not clear 01452 return sample1; 01453 } 01454 } 01455 01463 byte RFArbitrationBitDebug(bool sendOrListen, byte syncTimeH, byte syncTimeL) { 01464 byte sample1; 01465 01466 if (sendOrListen) { 01467 // bit is 1, so send a short signal (2 bit) 01468 // turn on transceiver for tx-ing 01469 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01470 BLED= ~BLED; 01471 RFSetModeTransmit(); 01472 RFBUF= 0xFF; 01473 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01474 BLED= ~BLED; 01475 // write rf buffer 01476 RFBUF= sample1= BIN(10101010); 01477 01478 syncTimeH= TH0; 01479 syncTimeL= TL0; 01480 } else { 01481 // bit is 0, so listen to the channel 01482 // turn on transceiver for rx-ing 01483 BLED= ~BLED; // mark powering up the transceiver with an extra peak 01484 BLED= ~BLED; 01485 // turn on continuous sampling 01486 RFSetModeReceive(); // param true means fast 01487 RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF); 01488 RF_START_RX(); 01489 BLED= ~BLED; // mark finish of transceiver power-up and waiting for rendezvous point 01490 BLED= ~BLED; 01491 01492 syncTimeH= TH0; 01493 syncTimeL= TL0; 01494 } 01495 if (rfMaxTimerValue < 256*syncTimeH + syncTimeL) 01496 rfMaxTimerValue= 256*syncTimeH + syncTimeL; 01497 sendUshortToUart(rfMaxTimerValue); 01498 DEBUG_NEWLINE; 01499 RF_SET_MODE_SLEEP; 01500 return 0; 01501 } 01502 01503 #ifdef DEBUG_ARBITRATION_TEST_COUNT 01504 byte arbitrationByteGlob= 0; 01505 #endif 01506 01522 bool RFArbitration(bool tryToObtainMediaAccess, bool syncPrio) { 01523 bool success= TRUE; 01524 byte arbitrationByte, j, sample1, syncTimeH, syncTimeL; 01525 01526 BLED= LED_ON; // start RFArbitration 01527 01528 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01529 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01530 ADC_POWER(TRUE); // turn on adc 01531 01532 // sync prio without trying to obtain media access is not reasonable 01533 tryToObtainMediaAccess|= syncPrio; 01534 01535 // do CAN-like arbitration for media access 01536 // see param documentation for explanation 01537 #ifndef DEBUG_ARBITRATION_TEST_ACTIVE 01538 if (tryToObtainMediaAccess) { 01539 arbitrationByte= RF_NEW_RANDOM_BYTE(); 01540 // if the node competes for the medium, he must send at least one 1, 01541 // so 0 as arbitration byte is not allowed 01542 if (!arbitrationByte) 01543 arbitrationByte= 1; 01544 } else { 01545 arbitrationByte= 0; 01546 } 01547 #endif 01548 01549 // for debugging, one of the following three assignments can be chosen for arbitrationByte 01550 #ifdef DEBUG_ARBITRATION_TEST_PATTERN 01551 arbitrationByte= BIN(10101010); // for debugging 01552 #endif 01553 #ifdef DEBUG_ARBITRATION_TEST_COUNT 01554 arbitrationByte= arbitrationByteGlob++; // for debugging 01555 #endif 01556 #ifdef DEBUG_ARBITRATION_TEST_0 01557 arbitrationByte= 0; // for debugging 01558 #endif 01559 01560 // take the 1st sample >100us later 01561 syncTimeH= RF_RENDEZVOUS_ARBITRATION_START_HI; 01562 syncTimeL= RF_RENDEZVOUS_ARBITRATION_START_LO; 01563 sample1= RFArbitrationBit(syncPrio, syncTimeH, syncTimeL); 01564 //RFArbitrationBitDebug(syncPrio); 01565 #ifdef DEBUG_USE_UART 01566 #ifdef DEBUG_DUMP_RSSI_SAMPLES 01567 rssiSamples[j]= sample1; 01568 #endif 01569 #endif 01570 if (!syncPrio) { 01571 if (sample1 < RF_ARBITRATION_RSSI_THRESHOLD) { 01572 // if we don't compete for sending a beacon, we give up, if we hear a signal 01573 // in this slot because another node wants to send a sync signal. 01574 RF_SET_MODE_SLEEP; 01575 ADC_POWER(FALSE); 01576 BLED= LED_OFF; 01577 return FALSE; 01578 } 01579 01580 // at this point we bail out if the stack is in an idle group and no sync has to be sent 01581 if (!(rfState & RF_STATE_TRAFFIC_GROUP)) { 01582 RF_SET_MODE_SLEEP; 01583 ADC_POWER(FALSE); 01584 BLED= LED_OFF; 01585 01586 // return true if a signal was detected, false otherwise 01587 return (sample1 >= RF_ARBITRATION_RSSI_THRESHOLD); 01588 } 01589 } 01590 01591 01592 // this adds an equivalent of approx. 2.5 * 206 us to the variables for the next rendezvous point 01593 syncTimeH= ((syncTimeH << 8 | syncTimeL) + RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS) >> 8; 01594 syncTimeL+= RF_ARBITRATION_TIMER_TICS_OTHER_PERIODS; 01595 01596 for (j=0; j<8; j++) { 01597 01598 sample1= RFArbitrationBit(bit_test(arbitrationByte, j), syncTimeH, syncTimeL); 01599 #ifdef DEBUG_USE_UART 01600 #ifdef DEBUG_DUMP_RSSI_SAMPLES 01601 rssiSamples[j]= sample1; 01602 //rssiSamples[8+j]= sample2; 01603 //rssiSamples[3*j+2]= sample3; 01604 #endif 01605 #endif 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 01676 #ifdef RF_USE_TRAFFIC_INDICATION 01677 void RFTrafficIndication() { 01678 bool result,tmp; 01679 01680 BLED= LED_ON; // start 01681 halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_INTERNAL_1_25, CC1010EB_CLKFREQ, 0); 01682 ADC_SELECT_INPUT(ADC_INPUT_AD2); // select RSSI input pin 01683 ADC_POWER(TRUE); // turn on adc 01684 01685 if (llState & LL_STATE_NEED_TO_SEND) { 01686 RFArbitrationBit(TRUE, RF_RENDEZVOUS_TRAFFIC_INDICATION_HI, RF_RENDEZVOUS_TRAFFIC_INDICATION_LO); 01687 result= TRUE; 01688 } else { 01689 // let's be pessimistic and compare the rssi value to the more conservative 01690 // alone test value. there is a trade off between unnecessary traffic groups 01691 // and lost packets. we choose reliable reception at the cost of some 01692 // unnecessary traffic groups. 01693 tmp= RFArbitrationBit(FALSE, RF_RENDEZVOUS_TRAFFIC_INDICATION_HI, RF_RENDEZVOUS_TRAFFIC_INDICATION_LO); 01694 result= tmp < RF_ALONE_TEST_RSSI_THRESHOLD; 01695 } 01696 01697 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01698 ADC_POWER(FALSE); 01699 BLED= LED_OFF; 01700 01701 //putchar((result) ? 'T' : 't'); 01702 //sendByteToUart(tmp); 01703 if (result) 01704 rfState|= RF_STATE_TRAFFIC_GROUP; 01705 else 01706 rfState&= ~RF_STATE_TRAFFIC_GROUP; 01707 } 01708 #endif 01709 01718 #ifdef RF_USE_ALONE_DETECTION 01719 bool RFAloneTest(bool send) { 01720 bool result; 01721 byte tmp; 01722 01723 BLED= LED_ON; // start 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 01729 if (send) { 01730 RFArbitrationBit(TRUE, RF_RENDEZVOUS_ALONE_TEST_HI, RF_RENDEZVOUS_ALONE_TEST_LO/*, FALSE*/); 01731 result= TRUE; 01732 //putchar('s'); 01733 } else { 01734 // let's be pessimistic and compare the rssi value to a high 01735 // alone test value. there is a trade off between 01736 tmp= RFArbitrationBit(FALSE, RF_RENDEZVOUS_ALONE_TEST_HI, RF_RENDEZVOUS_ALONE_TEST_LO/*, FALSE*/); 01737 result= tmp < RF_ALONE_TEST_RSSI_THRESHOLD; 01738 //putchar('r'); 01739 //sendByteToUart(tmp); 01740 //putchar((result) ? '+' : '-'); 01741 //DEBUG_SPACE; 01742 } 01743 01744 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 01745 ADC_POWER(FALSE); 01746 BLED= LED_OFF; 01747 return result; 01748 } 01749 #endif 01750 01757 byte RFAloneRandomNumberOfSeconds() { 01758 // returns a number betwenn 10 and 25 (including both) 01759 return 8 + (RF_NEW_RANDOM_BYTE() >> 2); 01760 } 01761 01769 unsigned short RFAloneRandomNumberOfSlots() { 01770 // returns a number betwenn 10 and 26 (including both) 01771 return 267 + ((256 * RF_NEW_RANDOM_BYTE() + RF_NEW_RANDOM_BYTE()) % 2133); 01772 } 01773 01782 void RFLowPowerSleep(byte sleepTime) { 01783 byte i; 01784 01785 sendByteToUart(sleepTime); 01786 DEBUG_NEWLINE; 01787 01788 TIMER0_RUN(FALSE); 01789 RLED= GLED= YLED= LED_OFF; 01790 01791 // config timer 2 for a period of 1 second in X32 clock mode 01792 // this will result in calling the dummy isr ISR 01793 // set timer 2 to high prio so that it can interrupt the ISR of timer0 where we are currently in. 01794 INT_PRIORITY(INUM_TIMER2, INT_HIGH); 01795 halConfigTimer23(TIMER2 | TIMER23_INT_TIMER, 1000000, CC1010EB_LOW_CLKFREQ); 01796 01797 MAIN_CLOCK_SET_SOURCE(CLOCK_X32); 01798 XOSC_ENABLE(FALSE); 01799 01800 // stay in 32kHz clock mode for for sleepTime seconds 01801 INT_SETFLAG(INUM_TIMER2, INT_CLR); 01802 TIMER2_RUN(TRUE); 01803 for (i=0; i<sleepTime; i++) { 01804 RLED= ~RLED; 01805 // this inner loop needs 1 s to execute 01806 ENTER_IDLE_MODE(); 01807 } 01808 TIMER2_RUN(FALSE); 01809 01810 // Enable high speed XOSC, switch clock source, then disable 32kHz XOSC 01811 XOSC_ENABLE(TRUE); 01812 MAIN_CLOCK_SET_SOURCE(CLOCK_XOSC); 01813 01814 INT_ENABLE(INUM_TIMER2, INT_OFF); 01815 INT_PRIORITY(INUM_TIMER2, INT_LOW); 01816 TIMER0_RUN(TRUE); 01817 01818 RLED= LED_OFF; 01819 } 01820 01836 bool RFShallISendSync(byte slotCount) { 01837 unsigned short i, j; 01838 01839 i= RF_NEW_RANDOM_BYTE(); 01840 j= RF_NEW_RANDOM_BYTE(); 01841 switch (slotCount) { 01842 case 10: 01843 return ( (i == 0) && ((j & 3) == 0) ); 01844 case 11: 01845 return (i == 0); 01846 case 12: 01847 return ((i & 63) == 0); 01848 case 13: 01849 return ((i & 15) == 0); 01850 case 14: 01851 return ((i & 3) == 0); 01852 case 15: 01853 return TRUE; 01854 default: 01855 return FALSE; 01856 } 01857 } 01858 01864 byte RFScramble(byte b) { 01865 byte i, scrReg; 01866 01867 scrReg= rfScramblerReg; 01868 RF_SCRAMBLE(b, scrReg, i); 01869 rfScramblerReg= scrReg; 01870 01871 return b; 01872 } 01873 01879 byte RFDescramble(byte b) { 01880 byte i, scrReg; 01881 01882 scrReg= rfScramblerReg; 01883 RF_DESCRAMBLE(b, scrReg, i); 01884 rfScramblerReg= scrReg; 01885 return b; 01886 } 01887 01898 bool RFSendPacket(bool sync) { 01899 byte numPreambles, j, b, currentTHi, currentTLo; 01900 //unsigned short tEndCurrent; // the start time for a beacon 01901 01902 // wait one ms to give the receivers time to prepare 01903 numPreambles= RF_PREAMBLE_LENGTH_TX; 01904 01905 // Turn on RF for TX, send packet 01906 RFSetModeTransmit(); 01907 //halRFSetRxTxOff(RF_TX, &RF_SETTINGS, &RF_CALDATA); 01908 01909 // Set the first byte to transmit & turn on TX 01910 RFCON|=0x01; // Ensure that bytemode is selected 01911 RF_SEND_BYTE(RF_PREAMBLE_BYTE); 01912 01913 /*j= TH0; 01914 b= TL0; 01915 sendByteToUart(j); 01916 sendByteToUart(b); 01917 DEBUG_NEWLINE; 01918 RF_SET_MODE_SLEEP; 01919 return 0;*/ 01920 01921 // synchronize 01922 // the value of this sync time has been measured with the commented out block above 01923 // in this version of the stack, both data and beacons are sent after arbitration, so 01924 // we always wait for (the later) RF_RENDEZVOUS_DATA_PACKET_START 01925 WAIT_FOR_TIMER0_EXACT(RF_RENDEZVOUS_PACKET_START_HI, RF_RENDEZVOUS_PACKET_START_LO); 01926 01927 BLED= LED_ON; // start sending 01928 01929 // turn on transceiver 01930 RF_START_TX(); 01931 //YLED= ~YLED; 01932 //BLED= ~BLED; 01933 01934 if (sync) { 01935 // get current timer value of timer0 01936 currentTHi= TH0; 01937 currentTLo= TL0; 01938 currentTHi= ((currentTHi | currentTLo) + RF_SYNC_ADDITIONAL_OFFSET) >> 8; 01939 currentTLo= currentTLo + RF_SYNC_ADDITIONAL_OFFSET; 01940 j= currentTHi ^ currentTLo; 01941 } 01942 01943 // Send remaining preambles 01944 while (--numPreambles) { 01945 RF_SPI_SEND_BYTE(RF_PREAMBLE_BYTE); 01946 } 01947 01948 // Send start of packet byte 01949 RF_SPI_SEND_BYTE(RF_SUITABLE_SYNC_BYTE); 01950 01951 // send FF byte to train the receiver's descrambler 01952 b= RFScramble(0xFF); 01953 RF_SPI_SEND_BYTE(b); 01954 01955 //YLED= ~YLED; 01956 //BLED= ~BLED; 01957 if (sync) { 01958 //BLED= ~BLED; 01959 // send rf options. 1 means beacon. 01960 b= RFScramble(1); 01961 RF_SPI_SEND_BYTE(b); 01962 01963 // send timestamp 01964 // use redundant protection byte j (= tEndAvgLo xor tEndAvgHi) to detect transmission bit errors 01965 b= RFScramble(currentTHi); 01966 RF_SPI_SEND_BYTE(b); 01967 b= RFScramble(currentTLo); 01968 RF_SPI_SEND_BYTE(b); 01969 b= RFScramble(j); 01970 RF_SPI_SEND_BYTE(b); 01971 01972 //YLED= ~YLED; 01973 //BLED= ~BLED; 01974 //tEndCurrent= (TH0<<8) | TL0; 01975 01976 #ifdef DEBUG_USE_UART 01977 #ifdef DEBUG_DUMP_SYNC_VALUES 01978 j= TH0; 01979 b= TL0; 01980 sendByteToUart(currentTHi); 01981 sendByteToUart(currentTLo); 01982 DEBUG_SPACE; 01983 sendByteToUart(j); 01984 sendByteToUart(b); 01985 DEBUG_NEWLINE; 01986 01987 #endif 01988 #endif 01989 } else { 01990 // send rf options. 0 means regular packet 01991 b= RFScramble(0); 01992 RF_SPI_SEND_BYTE(b); 01993 01994 if (LL_payload_sendbuffer_length > 64) 01995 LL_payload_sendbuffer_length= 64; 01996 01997 // Send physical payload length 01998 b= RFScramble(LL_payload_sendbuffer_length + LL_HEADER_SIZE + LL_TAIL_SIZE); 01999 RF_SPI_SEND_BYTE(b); 02000 02001 b= RFScramble(LL_payload_sendbuffer[0]); 02002 RF_SPI_SEND_BYTE(b); 02003 b= RFScramble(LL_payload_sendbuffer[1]); 02004 RF_SPI_SEND_BYTE(b); 02005 02006 // send 4 foo bytes to give receiver time to check subscriptions 02007 // use RF_PREAMBLE_BYTE because it's dc balanced 02008 RF_SPI_SEND_BYTE(BIN(11001010)); 02009 //BLED= ~BLED; 02010 //BLED= ~BLED; 02011 RF_SPI_SEND_BYTE(BIN(11001010)); 02012 //BLED= ~BLED; 02013 //BLED= ~BLED; 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 02021 // send ll header 02022 for (j=0; j<12; j++) { 02023 b= RFScramble(LL_header_sendbuffer[j]); 02024 RF_SPI_SEND_BYTE(b); 02025 } 02026 02027 // send ll packet payload 02028 for (j=0; j<LL_payload_sendbuffer_length; j++) { 02029 b= RFScramble(LL_payload_sendbuffer[j]); 02030 RF_SPI_SEND_BYTE(b); 02031 } 02032 02033 // send ll tail 02034 for (j=0; j<LL_TAIL_SIZE; j++) { 02035 b= RFScramble(LL_tail_sendbuffer[j]); 02036 RF_SPI_SEND_BYTE(b); 02037 } 02038 } 02039 02040 RF_SPI_SEND_BYTE(0); 02041 RF_SPI_SEND_BYTE(0); 02042 //while (!RF_READY_TO_SEND()); 02043 //YLED= ~YLED; 02044 //BLED= ~BLED; 02045 02046 // turn transceiver off 02047 RF_SET_MODE_SLEEP; // shorter for halRFSetRxTxOff(RF_OFF, &RF_SETTINGS, &RF_CALDATA); 02048 RFCON&=~0x01; // Ensure that bitmode is selected 02049 02050 // do exponential smoothing on the timer value when finished with sending 02051 // add an additional offset, because detection and timer setting on the slave 02052 // is slightly later than measurement on the master after after transmitting. 02053 /* if (sync) { 02054 //tEndAvg= tEndCurrent + RF_SYNC_ADDITIONAL_OFFSET; 02055 //tEndAvg= (((ulong)tEndCurrent + (ulong)RF_SYNC_ADDITIONAL_OFFSET) + 9*(ulong)tEndAvg) / 10; 02056 02057 // dump the sync values just sent 02058 #ifdef DEBUG_DUMP_SYNC_VALUES 02059 #ifdef DEBUG_USE_UART 02060 // dump received tHi and tLo 02061 sendByteToUart(tEndCurrent >> 8); 02062 sendByteToUart(tEndCurrent & 0xFF); 02063 DEBUG_SPACE; 02064 sendByteToUart(tEndAvg >> 8); 02065 sendByteToUart(tEndAvg & 0xFF); 02066 DEBUG_NEWLINE; 02067 #endif 02068 #endif 02069 02070 // when dividing by 8 don't simply cut the last 3 bits (like a cast would do) 02071 // to improve accuracy, add 1 if last bit that is shifted out is 1 02072 // this is the binary equivalent to decimal rounding (down if next decimal place is <=4, up if next decimal place is >4) 02073 rfAverageTEndBuffer*= 7; 02074 rfAverageTEndBuffer/= 8; 02075 rfAverageTEndBuffer+= tEndCurrent + RF_SYNC_ADDITIONAL_OFFSET; 02076 j= (rfAverageTEndBuffer & 0x4); 02077 tEndAvg= rfAverageTEndBuffer / 8; 02078 if (j) 02079 tEndAvg++; 02080 } */ 02081 02082 BLED= LED_OFF; // end RFSendPacket 02083 return TRUE; 02084 } 02085 02093 bool RFLongSearchForBeacon(unsigned short numTries) { 02094 bool success= FALSE; 02095 02096 // let timer 0 (the slot timer) run, but turn off the interrupt 02097 INT_ENABLE(INUM_TIMER0, FALSE); 02098 YLED= LED_ON; 02099 while (numTries-- > 0) { 02100 YLED= ~YLED; 02101 //sendUlongToUart(numTries); 02102 //DEBUG_SPACE; 02103 // if it is a beacon 02104 if (RFReceivePacket(&periodDataSlot, FALSE, TRUE) == 2) { 02105 success= TRUE; 02106 break; 02107 } 02108 } 02109 02110 // turn on the interrupt for timer 0 again 02111 INT_ENABLE(INUM_TIMER0, TRUE); 02112 // clear the interrupt flag, because it is most probably set 02113 INT_SETFLAG(INUM_TIMER0, INT_CLR); 02114 02115 YLED= LED_OFF; 02116 return success; 02117 } 02118 02124 bool RFShortSearchForBeacon() { 02125 bool success= FALSE; 02126 02127 YLED= LED_ON; 02128 // the RFReceivePacket() function automagically returns if only 200us are left in 02129 // current timer0 slot. 02130 success= (RFReceivePacket(&periodDataSlot, FALSE, TRUE) == 2); 02131 YLED= LED_OFF; 02132 02133 return success; 02134 } 02135 02137 // end of RF Layer Functions 02138 02139 02145 byte LLGetIDFromHardware() { 02146 for (i=0;i<=7;i++) { 02147 LL_header_sendbuffer[3+0]= llEpromAddress.a1; 02148 LL_header_sendbuffer[3+1]= llEpromAddress.a2; 02149 LL_header_sendbuffer[3+2]= llEpromAddress.a3; 02150 LL_header_sendbuffer[3+3]= llEpromAddress.a4; 02151 LL_header_sendbuffer[3+4]= llEpromAddress.a5; 02152 LL_header_sendbuffer[3+5]= llEpromAddress.a6; 02153 LL_header_sendbuffer[3+6]= llEpromAddress.a7; 02154 LL_header_sendbuffer[3+7]= RF_NEW_RANDOM_BYTE(); 02155 } 02156 02157 return 1; //both were invalid: there's no valid id 02158 } 02159 02163 void LLInit() { 02164 LL_sequence_no=0; 02165 02166 LL_header_sendbuffer[0]= LL_PROTOCOL_VERSION; 02167 02168 LL_payload_send_length=0; // set used ll payload to zero 02169 LL_payload_received_length= 0; 02170 llState= LL_STATE_RUNNING; 02171 LL_sequence_no= 0; 02172 02173 LLGetIDFromHardware(); //take id from internal or external eeprom and set LL_header_sendbuffer 02174 02175 #ifdef DEBUG_USE_UART 02176 sendStringToUart("Particle ID:\r\n"); 02177 sendByteToUart(LL_header_sendbuffer[3+0]); 02178 putchar('.'); 02179 sendByteToUart(LL_header_sendbuffer[3+1]); 02180 putchar('.'); 02181 sendByteToUart(LL_header_sendbuffer[3+2]); 02182 putchar('.'); 02183 sendByteToUart(LL_header_sendbuffer[3+3]); 02184 putchar('.'); 02185 sendByteToUart(LL_header_sendbuffer[3+4]); 02186 putchar('.'); 02187 sendByteToUart(LL_header_sendbuffer[3+5]); 02188 putchar('.'); 02189 sendByteToUart(LL_header_sendbuffer[3+6]); 02190 putchar('.'); 02191 sendByteToUart(LL_header_sendbuffer[3+7]); 02192 DEBUG_NEWLINE; 02193 #endif 02194 02195 RFInit(); 02196 } 02197 02198 02202 void LLStart() { 02203 RFStart(); 02204 } 02205 02206 02210 void LLStop() { 02211 RFStop(); // handles the states 02212 } 02213 02219 void LLSlotEnd() { 02220 02221 } 02222 02223 02227 void LLAbortSending() { 02228 if (LLSendingBusy()) { 02229 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 02230 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; 02231 } else { 02232 llState&= ~LL_STATE_SEND_SUCCESS; 02233 } 02234 // clear some llState flags 02235 llState&= ~(LL_STATE_SEND_SUCCESS|LL_STATE_NEED_TO_SEND); 02236 } 02237 } 02238 02242 void LLSetSendingSuccess() { 02243 //update the states 02244 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 02245 // no update of success flag 02246 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; // clear the control management bit 02247 } else { 02248 llState|= LL_STATE_SEND_SUCCESS; // successful sent 02249 } 02250 llState&= ~LL_STATE_NEED_TO_SEND; // busy is over 02251 LL_payload_send_length=0; // set used payload to zero 02252 } 02253 02257 byte LLSendingBusy() { 02258 return ((llState & LL_STATE_NEED_TO_SEND) != 0); 02259 } 02260 02265 byte LLGetSendSuccess() { 02266 return ((llState & LL_STATE_SEND_SUCCESS) != 0); 02267 } 02268 02272 byte LLIsActive() { 02273 return ((llState & LL_STATE_RUNNING) != 0); 02274 } 02275 02279 void LLSetDataToOld() { 02280 llState&= ~LL_STATE_NEW_DATA; 02281 } 02282 02287 void LLLockReceiveBuffer() { 02288 llState|= LL_STATE_RECEIVE_BUFFER_LOCKED; 02289 LLSetDataToOld(); 02290 } 02291 02295 byte LLReceiveBufferLocked() { 02296 return ((llState & LL_STATE_RECEIVE_BUFFER_LOCKED) != 0); 02297 } 02298 02302 void LLReleaseReceiveBuffer() { 02303 llState&= ~LL_STATE_RECEIVE_BUFFER_LOCKED; 02304 LLSetDataToOld(); 02305 } 02306 02310 void LLSetDataToNew() { 02311 llState|= LL_STATE_NEW_DATA; 02312 } 02313 02314 02319 byte LLDataIsNew() { 02320 return ((llState & LL_STATE_NEW_DATA) != 0); 02321 } 02322 02326 byte LLGetFieldStrength() { 02327 return rfFieldStrength; 02328 } 02329 02333 void LLSetFieldStrength(byte value) { 02334 rfFieldStrength= value; 02335 } 02336 02337 02345 unsigned short LLCalcCRC16(byte *header_data, byte *payload_data, byte payload_size) { 02346 //calcs a crc on LL_header, ACL_data, LL_tail 02347 byte hb,lb,i,tmp; 02348 lb=0; 02349 hb=0; 02350 02351 for(i=0;i<LL_HEADER_SIZE;i++) { 02352 tmp = lb; 02353 lb = hb; 02354 hb = tmp; 02355 lb ^= header_data[i]; 02356 lb ^= lb >> 4; 02357 hb ^= lb << 4; 02358 hb ^= lb >> 3; 02359 lb ^= (lb << 4) << 1; 02360 } 02361 02362 //len=header_data[1]-LL_HEADER_SIZE-LL_TAIL_SIZE; 02363 for(i=0;i<payload_size;i++) { 02364 tmp = lb; 02365 lb = hb; 02366 hb = tmp; 02367 lb ^= payload_data[i]; 02368 lb ^= lb >> 4; 02369 hb ^= lb << 4; 02370 hb ^= lb >> 3; 02371 lb ^= (lb << 4) << 1; 02372 } 02373 02374 return hb*256+lb; 02375 } 02376 02377 02382 byte LLSendPacket(byte slot_limit) { 02383 unsigned short crc; 02384 02385 if (slot_limit==0) slot_limit=1; // zero makes no sense 02386 02387 // if rf is off, stop it 02388 if (!(rfState & RF_STATE_RUNNING)) return 0; 02389 // if ll is still trying to send the last packet, ignore 02390 if (llState & LL_STATE_NEED_TO_SEND) return 0; 02391 02392 // complete the LLPacket 02393 //DebugGiveOut(LL_payload_send_length); 02394 02395 // copy "send" variables to "sendbuffer" variables 02396 if (LL_payload_send_length > 64) 02397 LL_payload_send_length= 64; 02398 LL_payload_sendbuffer_length= LL_payload_send_length; 02399 for (i=0; i<LL_payload_send_length; i++) 02400 LL_payload_sendbuffer[i]= LL_payload_send[i]; 02401 02402 LL_header_sendbuffer[1]= LL_payload_sendbuffer_length; 02403 LL_header_sendbuffer[2]= rfFieldStrength; 02404 LL_header_sendbuffer[11]= LL_sequence_no; 02405 02406 crc= LLCalcCRC16(LL_header_sendbuffer,LL_payload_sendbuffer, LL_payload_sendbuffer_length); 02407 02408 LL_tail_sendbuffer[0]= (crc>>8); //CRCh (implicit typecast; ´takes low byte from long 02409 LL_tail_sendbuffer[1]= crc & 0xFF; //CRCl 02410 02411 LL_slots_left=slot_limit; 02412 LL_sequence_no++; 02413 02414 LL_payload_send_length= 0; // set the used payload to 0, so that new data can be written 02415 02416 llState|= LL_STATE_NEED_TO_SEND; 02417 02418 return 1; 02419 } 02420 02421 02425 byte LLGetRemainingPayloadSpace() { 02426 return (LL_PAYLOAD_SIZE-LL_payload_send_length); 02427 } 02428 02429 02433 byte LLSentPacketInThisSlot() { 02434 return ((llState & LL_STATE_PACKET_JUST_SENT) != 0); 02435 } 02436 02438 // end of LL Layer Functions 02439 02442 02443 void ACLStartUp() { 02444 xdata byte result[10]; 02445 byte selftest_result,i; 02446 02447 selftest_result= AppSelfTest(result); //runs the selftest 02448 02449 GLED= LED_OFF; 02450 RLED= LED_OFF; 02451 halWait(100, CC1010EB_CLKFREQ); // for stabilizing of power etc. 02452 02453 ACLInit(); // sets the pins in pic init correct (looks at selftest_active 02454 // GLED= LED_ON; 02455 // while(1); 02456 02457 #ifdef DEBUG_DUMP_RF_STATISTICS 02458 // reset the statistics 02459 DebugResetStats(); 02460 #endif 02461 02462 if(selftest_result) { 02463 //send result ten times to get it 02464 for(i=0;i<10;i++) { 02465 //send packet 02466 ACLAddNewType(ACL_TYPE_CST_HI,ACL_TYPE_CST_LO); //MST Self test 02467 ACLAddData(result[0]); 02468 ACLAddData(result[1]); 02469 ACLAddData(result[2]); 02470 ACLAddData(result[3]); 02471 ACLAddData(result[4]); 02472 ACLAddData(result[5]); 02473 ACLAddData(result[9]); 02474 02475 ACLSendPacket(30); 02476 while(ACLSendingBusy()) {RLED= LED_ON;RLED= LED_OFF;} 02477 } 02478 } 02479 } 02480 02481 02483 void ACLInit() { 02484 ACLFlushSubscriptions(); 02485 ACL_send_buffer_locked=false; 02486 ACL_write_position=0; 02487 ACL_subscribe_all=false; 02488 ACL_ACM_answers=true; 02489 ACL_ignore_control_messages=false; 02490 ACL_pass_control_messages=false; 02491 02492 LL_payload_send_length=0; 02493 ACL_write_position=0; 02494 02495 //APPSetLEDBehaviour(LEDS_NORMAL); //setdefault 02496 02497 LLInit(); 02498 ACLStart(); 02499 } 02500 02501 02508 byte ACLSubscribe(byte LL_type_h, byte LL_type_l) { 02509 byte i; 02510 02511 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { // first test if already there 02512 if ((ACL_subscriptions[i][0]==LL_type_h) && (ACL_subscriptions[i][1]==LL_type_l)) return 1; 02513 } 02514 02515 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02516 if ((ACL_subscriptions[i][0]==0) && (ACL_subscriptions[i][1]==0)) { 02517 ACL_subscriptions[i][0]=LL_type_h; 02518 ACL_subscriptions[i][1]=LL_type_l; 02519 return 1; // successful subscribed 02520 } 02521 } 02522 return 0; // subsriptions failed 02523 } 02524 02531 byte ACLUnsubscribe(byte LL_type_h, byte LL_type_l) { 02532 byte i; 02533 02534 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { // if type is there, delete it 02535 if ((ACL_subscriptions[i][0]==LL_type_h) && (ACL_subscriptions[i][1]==LL_type_l)) { 02536 ACL_subscriptions[i][0]=0; 02537 ACL_subscriptions[i][1]=0; 02538 return 1; // successful unsubscribed 02539 } 02540 } 02541 return 0; // not found 02542 } 02543 02544 02546 void ACLFlushSubscriptions() { 02547 byte i; 02548 02549 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02550 ACL_subscriptions[i][0]=0; 02551 ACL_subscriptions[i][1]=0; 02552 } 02553 02554 ACL_subscribe_all=false; 02555 ACLSubscribeDefault(); 02556 return; 02557 } 02558 02560 void ACLSubscribeAll() { 02561 ACL_subscribe_all=true; 02562 } 02563 02565 void ACLAnswerOnACM() { 02566 ACL_ACM_answers=true; 02567 } 02568 02570 void ACLNoAnswerOnACM() { 02571 ACL_ACM_answers=false; 02572 } 02573 02575 byte ACLSubscribeDefault() { 02576 byte result; 02577 result=ACLSubscribe(ACL_TYPE_ACM_H,ACL_TYPE_ACM_L); //165 14 is CMA (Control and Management virtual Artifact) 02578 return result; 02579 } 02580 02587 byte ACLVerifySubscription(byte type_h,byte type_l) { 02588 byte i; 02589 02590 for (i=0;i<ACL_SUBSCRIPTIONLIST_LENGTH;i++) { 02591 if ((ACL_subscriptions[i][0]==type_h) && (ACL_subscriptions[i][1]==type_l)) return 1; 02592 } 02593 return 0; //subscription not found 02594 } 02595 02596 02601 byte ACLProcessControlMessages() { 02602 02603 if ((LL_payload_receivebuffer[0]==ACL_TYPE_ACM_H) && (LL_payload_receivebuffer[1]==ACL_TYPE_ACM_L)) { 02604 // there was a control msg 02605 02606 // check a control msg, e.g. "1,1" 02607 //remote shutdown 02608 if ((LL_payload_receivebuffer[3]==172) && (LL_payload_receivebuffer[4]==224)) { 02609 if (ACLMatchesMyIP(LL_payload_receivebuffer,6)) { 02610 return 4; 02611 } 02612 } 02613 02614 // check syncrate setting 02615 if ((LL_payload_receivebuffer[3]==198) && (LL_payload_receivebuffer[4]==208)) { 02616 //RFSetSyncRate(LL_payload_receivebuffer[6]); 02617 } 02618 02619 // check initial listen slots setting 02620 if ((LL_payload_receivebuffer[3]==159) && (LL_payload_receivebuffer[4]==192)) { 02621 //RFSetInitialListenSlots(LL_payload_receivebuffer[6]); 02622 } 02623 02624 // check shutdown (dirty hack; actually need ip check, different shutdown modes 02625 if ((LL_payload_receivebuffer[3]==204) && (LL_payload_receivebuffer[4]==232)) { 02626 RFStop(); 02627 } 02628 02629 // check modeswitch 02630 if ((LL_payload_receivebuffer[3]==1) && (LL_payload_receivebuffer[4]==1)) { 02631 } 02632 02633 //check over the air programming 02634 if ((LL_payload_receivebuffer[3]==198) && (LL_payload_receivebuffer[4]==88)) { 02635 //OtapReceive(); // call over the airprogramming stuff 02636 } 02637 02638 //check set ID 02639 if ((LL_payload_receivebuffer[3]==ACL_TYPE_CID_HI) && (LL_payload_receivebuffer[4]==ACL_TYPE_CID_LO) /* && selftest_active==true */) { 02640 //OtapSetID(); 02641 } 02642 02643 // no answering allowed; all messages below here are not allowed, if acl_acm_answer is false 02644 if (ACL_ACM_answers==false) return 0; 02645 02646 //check Helo 02647 if ((LL_payload_receivebuffer[3]==115) && (LL_payload_receivebuffer[4]==216)) { 02648 if (ACLMatchesMyIP(LL_payload_receivebuffer,6)) { // does sent IP match my IP?? 02649 if (LLSendingBusy()) ACLAbortSending(); // interrupt any waiting stuff 02650 02651 // now set the whole packet by hand: 02652 LL_payload_send[0]=ACL_TYPE_ACM_H; 02653 LL_payload_send[1]=ACL_TYPE_ACM_L; 02654 LL_payload_send[2]=0; 02655 02656 LL_payload_send[3]=134; 02657 LL_payload_send[4]=32; 02658 02659 LL_payload_send[5]=8; 02660 LL_payload_send[6]=LL_header_receivebuffer[3]; // put in origin adress from HELO 02661 LL_payload_send[7]=LL_header_receivebuffer[4]; 02662 LL_payload_send[8]=LL_header_receivebuffer[5]; 02663 LL_payload_send[9]=LL_header_receivebuffer[6]; 02664 LL_payload_send[10]=LL_header_receivebuffer[7]; 02665 LL_payload_send[11]=LL_header_receivebuffer[8]; 02666 LL_payload_send[12]=LL_header_receivebuffer[9]; 02667 LL_payload_send[13]=LL_header_receivebuffer[10]; 02668 LL_payload_send_length=14; // give the correct len 02669 02670 if (LLSendPacket(ACL_CONTROL_MESSAGES_TIMEOUT)) { // NEVER USE ACL FUNCTION here!!! 02671 llState|= LL_STATE_CTRL_MESSAGE_INSERTED; // this means that this msg if special concerning the update of the states 02672 } 02673 } 02674 } 02675 // this was helo 02676 return 1; // there was a control management msg 02677 } 02678 return 0; // there was no control management msg 02679 } 02680 02682 void ACLSetFieldStrength(byte power) { 02683 LLSetFieldStrength(power); 02684 } 02685 02687 byte ACLSendingBusy() { 02688 return LLSendingBusy(); 02689 } 02690 02696 byte ACLGetSendSuccess() { 02697 return LLGetSendSuccess(); 02698 } 02699 02706 byte ACLMatchesMyIP(char *buffer,byte start) { 02707 byte i; 02708 bool res; 02709 02710 // check if was my address // ll_header_send [3..10] hold the myIP 02711 res= TRUE; 02712 for (i=0;i<8;i++) { 02713 if (buffer[start+i] != LL_header_sendbuffer[3+i]) { 02714 res= FALSE; 02715 break; 02716 } 02717 } 02718 if (res) return 1; 02719 02720 // check if was broadcast adress 02721 res= TRUE; 02722 for (i=0;i<8;i++) { 02723 if (buffer[start+i]!=255) { 02724 res= FALSE; 02725 break; 02726 } 02727 } 02728 if (res) return 1; 02729 02730 return 0; 02731 } 02732 02733 02739 byte ACLSendPacket(byte slot_timeout) { 02740 return LLSendPacket(slot_timeout); 02741 } 02742 02744 void ACLAbortSending() { 02745 ACL_send_buffer_locked=false; 02746 ACL_write_position=0; 02747 LLAbortSending(); 02748 } 02749 02751 byte ACLClearSendData() { 02752 byte i; 02753 if (LLSendingBusy()) return 0; // dont clear if busy sending 02754 if (ACL_send_buffer_locked==true) return 0; // dont clear if reservation is active 02755 02756 for(i=0;i<LL_PAYLOAD_SIZE;i++) 02757 LL_payload_send[i]=0; 02758 LL_payload_send_length=0; 02759 ACL_write_position=0; 02760 02761 return 1; 02762 } 02763 02764 02769 bool ACLAddNewType(byte type_h, byte type_l) { 02770 //if (LLSendingBusy()) return 2; // not needed any more, we have a sendbuffer 02771 if ((LL_PAYLOAD_SIZE-LL_payload_send_length)<3) { 02772 return 1; // not enough space 02773 } else { 02774 //place the new type 02775 LL_payload_send[LL_payload_send_length]=type_h; 02776 LL_payload_send[LL_payload_send_length+1]=type_l; 02777 LL_payload_send[LL_payload_send_length+2]=0; //data length is zero 02778 ACL_write_position=LL_payload_send_length+2; 02779 LL_payload_send_length+=3; 02780 return 0; 02781 } 02782 } 02783 02789 byte ACLAddData(byte newByte) { 02790 //if (LLSendingBusy()) return 2; 02791 if ((LL_PAYLOAD_SIZE-LL_payload_send_length)<1) { 02792 return 1; // not enough space 02793 } else { 02794 LL_payload_send[ACL_write_position]++; //increase length byte after type bytes 02795 LL_payload_send[LL_payload_send_length]= newByte; //place the data 02796 LL_payload_send_length+=1; 02797 return 0; 02798 } 02799 } 02800 02805 byte ACLGetRemainingPayloadSpace() { 02806 return LLGetRemainingPayloadSpace(); 02807 } 02808 02813 byte ACLGetReceivedPayloadLength() { 02814 return LL_header_received[1]; 02815 } 02816 02821 signed char ACLGetReceivedDataLength(byte type_h, byte type_l) { 02822 byte i; 02823 02824 for (i=0; i<=LL_header_received[1]-2; i++) { 02825 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02826 return (LL_payload_received[i+2]); //return valid length 02827 } 02828 } 02829 return -1; // type not found 02830 } 02831 02832 02836 char* ACLGetReceivedData(byte type_h, byte type_l) { 02837 byte i; 02838 02839 for (i=0;i<=LL_header_received[1]-2;i++) { 02840 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02841 if (LL_payload_received[i+2]!=0) { 02842 return (&(LL_payload_received[i+3])); // if len is not zero: return valid pointer to data begin 02843 } 02844 } 02845 } 02846 return NULL; 02847 } 02848 02849 02854 char ACLGetReceivedByte(byte type_h, byte type_l, byte position) { 02855 byte i; 02856 02857 for (i=0;i<=LL_header_received[1]-2;i++) { 02858 if ((LL_payload_received[i]==type_h) && (LL_payload_received[i+1]==type_l)) { 02859 if (LL_payload_received[i+2]!=0) { 02860 return (LL_payload_received[i+3+position]); // if len is not zero: return valid pointer to data begin 02861 } 02862 02863 } 02864 } 02865 02866 return 0; 02867 //attention!! if type wasn't found: return is undetermined!! 02868 } 02869 02875 byte ACLFoundReceivedType(byte type_h, byte type_l) { 02876 return (ACLGetReceivedDataLength(type_h,type_l) != -1); //not found 02877 } 02878 02886 void ACLSetControlMessagesBehaviour(boolean ignore, boolean pass) { 02887 ACL_ignore_control_messages= ignore; //all control messages are ignored 02888 ACL_pass_control_messages= pass; // all control messages are as well copied to receivebuffer for application 02889 } 02890 02891 02895 byte ACLSentPacketInThisSlot() { 02896 return (LLSentPacketInThisSlot()); 02897 } 02898 02900 void ACLStart() { 02901 LLStart(); 02902 } 02903 02905 void ACLStop() { 02906 LLStop(); 02907 } 02908 02910 void ACLLockReceiveBuffer() { 02911 LLLockReceiveBuffer(); 02912 } 02913 02917 byte ACLReceiveBufferLocked() { 02918 return LLReceiveBufferLocked(); 02919 } 02920 02922 void ACLReleaseReceiveBuffer() { 02923 LLReleaseReceiveBuffer(); 02924 } 02925 02927 void ACLSetDataToOld() { 02928 LLSetDataToOld(); 02929 } 02930 02932 void ACLSetDataToNew() { 02933 LLSetDataToNew(); 02934 } 02935 02939 byte ACLDataIsNew() { 02940 return LLDataIsNew(); 02941 } 02942 02946 byte ACLDataReceivedInThisSlot() { 02947 return LLSentPacketInThisSlot(); 02948 } 02949 02950 02952 byte ACLDataIsNewNow() { 02953 return ACLDataReceivedInThisSlot(); 02954 } 02955 02956 02960 byte ACLAdressedDataIsNew() { 02961 if (!ACLDataIsNew()) return 0; 02962 02963 if (ACLFoundReceivedType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO)) { 02964 if (ACLMatchesMyIP( ACLGetReceivedData( ACL_TYPE_CAD_HI, ACL_TYPE_CAD_LO), 0 )) 02965 return 1; 02966 } 02967 return 0; 02968 } 02969 02973 byte ACLAdressedDataIsNewNow() { 02974 if (!ACLDataIsNewNow()) return 0; 02975 02976 if (ACLFoundReceivedType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO)) { 02977 if (ACLMatchesMyIP( ACLGetReceivedData( ACL_TYPE_CAD_HI, ACL_TYPE_CAD_LO), 0 )) 02978 return 1; 02979 } 02980 return 0; 02981 } 02982 02983 02989 byte ACLSendPacketAdressed(nodeAddrType *address, byte timeout) { 02990 02991 if (ACLGetRemainingPayloadSpace()<11) 02992 return 0; //not enough space 02993 02994 ACLAddNewType(ACL_TYPE_CAD_HI,ACL_TYPE_CAD_LO); 02995 ACLAddData(address->a1); 02996 ACLAddData(address->a2); 02997 ACLAddData(address->a3); 02998 ACLAddData(address->a4); 02999 ACLAddData(address->a5); 03000 ACLAddData(address->a6); 03001 ACLAddData(address->a7); 03002 ACLAddData(address->a8); 03003 03004 if (ACLSendPacket(timeout)) return 1; 03005 return 0; 03006 } 03008 // end of ACL Layer Functions 03009 03010 03015 void isr_timer0() interrupt INUM_TIMER0 { 03016 byte length; //, time1H, time1L, time2H, time2L; 03017 bool arbitrationResult, sendSync; 03018 unsigned short receiveResult; 03019 03020 ISR_TIMER0_ADJUST(moduloTimer0); 03021 INT_SETFLAG(INUM_TIMER0, INT_CLR); 03022 // BLED= LED_ON; // start isr 03023 // BLED= LED_OFF; // start isr 03024 // BLED= LED_ON; // start isr 03025 // BLED= LED_OFF; // start isr 03026 /* time1H= TH0; 03027 time1L= TL0; */ 03028 03029 llState&= ~(LL_STATE_PACKET_JUST_RECEIVED | LL_STATE_PACKET_JUST_SENT); 03030 03031 if (!(rfState & RF_STATE_RUNNING)) { 03032 TIMER0_RUN(FALSE); 03033 return; 03034 } 03035 03036 rfSlotCount++; 03037 rfSlotsWithoutSignalReceived++; 03038 03039 // in distributed sync mode we normally have 15 as RF_SYNC_MAX_SLOT_COUNT, this means 03040 // the slot count is 0, 1, 2, ..., 15 03041 if (rfSlotCount > RF_SYNC_MAX_SLOT_COUNT) 03042 rfSlotCount= 0; 03043 03044 rfState|= RF_STATE_MASTER; 03045 sendSync= RFShallISendSync(rfSlotCount); 03046 03047 if (rfState & RF_STATE_LONG_SEARCH_FOR_BEACON) { 03048 // node must do long search for master, use random part because if a master gives 03049 // up and all slaves find themselves in this state, they shouldn't all at once try 03050 // to become master 03051 RFLongSearchForBeacon(RF_BEACON_SEARCH_SLOTS_STARTUP + RF_NEW_RANDOM_BYTE()); 03052 rfState&= ~RF_STATE_LONG_SEARCH_FOR_BEACON; // clear the long search flag 03053 // start the failed sync count and slot count from 0 03054 rfSlotCount= 0; 03055 } else if (rfState & RF_STATE_SHORT_SEARCH_FOR_BEACON) { 03056 if (RFShortSearchForBeacon()) { 03057 rfSlotCount= 0; 03058 } 03059 rfState&= ~RF_STATE_SHORT_SEARCH_FOR_BEACON; 03060 } else { 03061 // if we are not alone we go on here. we do an arbitration of rf access. if we want to send a packet 03062 // we draw a random arbitration byte and compete for the medium. if we don't have 03063 // anything to send, choose 0 as arbitration byte so that arbitration can tell us 03064 // if the medium is busy or not. 03065 if (rfState & RF_STATE_ALONE) { 03066 #ifdef RF_USE_ALONE_DETECTION 03067 // depending on switch RF_ALONE_DEEP_SLEEP we go to deep sleep (application is halted) or just 03068 // suppress receiving and sending of data. 03069 #ifdef RF_ALONE_DEEP_SLEEP 03070 RFLowPowerSleep(RFAloneRandomNumberOfSeconds()); 03071 rfState&= ~RF_STATE_ALONE; 03072 RFLongSearchForBeacon(RF_MASTER_SEARCH_SLOTS_AFTER_ALONE); 03073 // start the failed sync count and slot count from 0 03074 rfSlotsWithoutSignalReceived= rfSlotCount= 0; 03075 #else 03076 // if we are alone, we don't need arbitration - we know we win :) 03077 arbitrationResult= TRUE; 03078 if (( (rfSlotsInAloneModeLeft & 0xFF) % 32 ) == 0) 03079 RLED= ~RLED; 03080 //sendUshortToUart(rfSlotsInAloneModeLeft); 03081 //DEBUG_SPACE; 03082 if (rfSlotsInAloneModeLeft-- == 0) { 03083 rfState&= ~RF_STATE_ALONE; 03084 RFLongSearchForBeacon(RF_MASTER_SEARCH_SLOTS_AFTER_ALONE); 03085 // start the failed sync count and slot count from 0 03086 rfSlotsWithoutSignalReceived= rfSlotCount= 0; 03087 } 03088 #endif 03089 #endif 03090 } else { 03091 // do regular arbitration. only try to obtain media access if a data packet or a beacon needs 03092 // to be send (1st param == true). only apply sync priority if a beacon must be sent. 03093 arbitrationResult= RFArbitration(llState & LL_STATE_NEED_TO_SEND, sendSync); 03094 } 03095 03096 if (llState & LL_STATE_NEED_TO_SEND) 03097 statsRfSlotsNeedToSend++; 03098 if (arbitrationResult && ((llState & LL_STATE_NEED_TO_SEND) || sendSync)) { 03099 statsRfArbitrationWon++; 03100 if (sendSync) { 03101 RFSendPacket(TRUE); 03102 #ifdef RF_USE_ALONE_DETECTION 03103 if (RFAloneTest(FALSE)) { 03104 rfSlotsWithoutSignalReceived= 0; 03105 //putchar('A'); 03106 } 03107 #endif 03108 #ifdef RF_USE_TRAFFIC_INDICATION 03109 RFTrafficIndication(); 03110 #endif 03111 if (!(rfState & RF_STATE_ALONE)) { 03112 YLED= ~YLED; 03113 putchar('S'); 03114 //sendByteToUart(rfSlotCount); 03115 DEBUG_NEWLINE; 03116 } 03117 rfSlotCount= 0; 03118 } else { 03119 // if we are alone, we just pretend to send the packet and set the flags 03120 // if we are not in a traffic group we don't send anything and don't update the flags 03121 if (!(rfState & RF_STATE_ALONE) && (rfState & RF_STATE_TRAFFIC_GROUP)) 03122 RFSendPacket(FALSE); 03123 if ((rfState & RF_STATE_TRAFFIC_GROUP)) { 03124 llState&= ~(LL_STATE_NEED_TO_SEND); 03125 llState|= LL_STATE_SEND_SUCCESS | LL_STATE_PACKET_JUST_SENT; 03126 } 03127 } 03128 } else { 03129 // only receive a packet if we detected a node that wants to send (arbitrationResult == false). 03130 // if the receive buffer is locked, only receive beacons and ignore data packets 03131 if (!arbitrationResult) { 03132 // for return type of RFReceivePacket() see documentation of that function 03133 // if we are an alone master, we don't need to receive a packet 03134 receiveResult= (rfState & RF_STATE_ALONE) ? 0 : RFReceivePacket(&periodDataSyncByteReceiveTimeout, TRUE, llState & LL_STATE_RECEIVE_BUFFER_LOCKED); 03135 //sendByteToUart(receiveResult >> 8); 03136 //sendByteToUart(receiveResult); 03137 //DEBUG_SPACE; 03138 if ((receiveResult & 0xFF) == 1) { 03139 length= receiveResult >> 8; 03140 /* #ifdef DEBUG_USE_UART 03141 sendByteToUart(length); 03142 DEBUG_NEWLINE; 03143 #endif */ 03144 LL_payload_received_length= length; 03145 statsRfDataPacketsReceivedNotNeedingToSend++; 03146 llState|= LL_STATE_NEW_DATA | LL_STATE_PACKET_JUST_RECEIVED; 03147 rfSlotsWithoutSignalReceived= 0; 03148 } else if ((receiveResult & 0xFF) == 2) { 03149 #ifdef RF_USE_ALONE_DETECTION 03150 // send an alone test signal so that the node 03151 RFAloneTest(TRUE); 03152 #endif 03153 #ifdef RF_USE_TRAFFIC_INDICATION 03154 RFTrafficIndication(); 03155 #endif 03156 YLED= ~YLED; 03157 putchar('R'); 03158 //sendByteToUart(rfSlotCount); 03159 DEBUG_NEWLINE; 03160 rfSlotsWithoutSignalReceived= rfSlotCount= 0; 03161 } 03162 } 03163 } 03164 statsRfSlotsInSync++; 03165 } 03166 03167 // check if packet has been sent or sending slot timeout occurred and set the right llState. 03168 // this used to be done in LLSlotEnd(), but has moved here for efficiency. 03169 if (llState & LL_STATE_NEED_TO_SEND) { 03170 if (LL_slots_left-- == 0) { 03171 03172 if (llState & LL_STATE_CTRL_MESSAGE_INSERTED) { 03173 llState&= ~LL_STATE_CTRL_MESSAGE_INSERTED; // in case it was switched on, switch it off and do NOT update the success state 03174 } else { 03175 llState&= ~LL_STATE_SEND_SUCCESS; // busy is over 03176 } 03177 03178 llState&= ~LL_STATE_NEED_TO_SEND; 03179 LL_payload_sendbuffer_length= 0; // set used payload to zero 03180 } 03181 } 03182 03183 // debug code to test sync arbitration in dist sync mode 03184 /* if (sendSync) { 03185 putchar('w'); 03186 if (!arbitrationResult) { 03187 putchar(((receiveResult & 0xFF) == 2) ? 'k' : 'f'); 03188 DEBUG_NEWLINE; 03189 } else { 03190 putchar('S'); 03191 } 03192 DEBUG_NEWLINE; 03193 } */ 03194 03195 #ifdef RF_USE_ALONE_DETECTION 03196 // no packet has been received for too long a time, so put node into alone mode. this is the only place 03197 // in the stack where the alone state is entered. 03198 if (rfSlotsWithoutSignalReceived > RF_ALONE_TEST_FAILED_THRESHOLD && !(rfState & RF_STATE_ALONE)) { 03199 rfState|= RF_STATE_ALONE; 03200 #ifndef RF_ALONE_DEEP_SLEEP 03201 rfSlotsInAloneModeLeft= RFAloneRandomNumberOfSlots(); 03202 #endif 03203 } 03204 #endif 03205 03206 #ifdef DEBUG_USE_UART 03207 #ifdef DEBUG_DUMP_RF_STATISTICS 03208 DebugDumpStats(); 03209 #endif 03210 #ifdef DEBUG_DUMP_RF_STATE 03211 DebugDumpRfState(); 03212 #endif 03213 #endif 03214 03215 //sendByteToUart(rfSlotCount); 03216 //DEBUG_NEWLINE; 03217 // BLED= LED_ON; // end isr 03218 // BLED= LED_OFF; // end isr 03219 // BLED= LED_ON; // end isr 03220 // BLED= LED_OFF; // end isr 03221 } //end isr_timer0() 03222 03223 03228 void isr_timer2() interrupt INUM_TIMER2 { 03229 INT_SETFLAG(INUM_TIMER2, INT_CLR); 03230 } 03231 03232 /***************************************************************************** 03233 MAIN PROGRAM 03234 *****************************************************************************/ 03239 void main(void) { 03240 byte i, j=0; 03241 03242 // sets up the board 03243 ParticleInit(); 03244 03245 /* while (TRUE) { 03246 for (i=0; i<20; i++) { 03247 UART1_WAIT_AND_SEND((RFShallISendSync(i)) ? '1' : '0'); 03248 } 03249 DEBUG_NEWLINE; 03250 } */ 03251 03252 // initializes and starts all communications layers 03253 ACLStartUp(); 03254 ACLSubscribeAll(); 03255 03256 i= 1; 03257 while (TRUE) { 03258 i++; 03259 03260 // compose packet content 03261 if (!ACLSendingBusy() && (i % 128 == 0)) { 03262 //putchar('x'); 03263 //DEBUG_NEWLINE; 03264 ACLAddNewType(ACL_TYPE_ASM_HI, ACL_TYPE_ASM_LO); 03265 ACLAddData(j++); 03266 ACLSendPacket(90); 03267 } 03268 03269 if (LLSentPacketInThisSlot()) { 03270 putchar('>'); 03271 DEBUG_NEWLINE; 03272 } 03273 03274 if (!ACLGetSendSuccess()) { 03275 putchar('F'); 03276 DEBUG_NEWLINE; 03277 // reset the flag 03278 llState|= LL_STATE_SEND_SUCCESS; 03279 } 03280 03281 //sendByteToUart(LL_payload_send_length); 03282 //DEBUG_SPACE; 03283 03284 if (ACLDataIsNew()) { 03285 // lock buffer 03286 ACLLockReceiveBuffer(); 03287 03288 #ifdef DEBUG_USE_UART 03289 //putchar((ACLDataIsNew()) ? 'y' : 'n'); 03290 //putchar((llState & LL_STATE_NEW_DATA) ? 'y' : 'n'); 03291 //DEBUG_SPACE; 03292 DebugDumpReceivedAclPacket(); 03293 //DebugDumpRfState(); 03294 #endif 03295 03296 // release lock 03297 ACLReleaseReceiveBuffer(); 03298 } 03299 RF_LOW_POWER_SLEEP_UNTIL_NEXT_SLOT(); 03300 03301 // dump statistics every 256th time 03302 // if (i++ == 0) { 03303 // DebugDumpStats(); 03304 // } 03305 } 03306 } // end of main()

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