/*================================================================================================ SiWiCom.c SiWiCom - Simple Wireless Communication copyright 2006 v1.0 (2006-06-22) Name: Joakim Nilsson E-mail: mail@jopin.se Name: Christoffer Martinsson E-mail: cm@cmtec.se ================================================================================================*/ #include "hardware.h" #include #include #include #include #include #include #include #include "spi.h" #include "cc1100.h" #include "rfProtocol.h" #include "pcProtocol.h" #include "usart.h" #define ERR_COUNTER_THR 10 // Counter related declarations static uint8_t errCounter = 0; static uint16_t timeOutCounter = 0; static uint8_t wakeUpCounter = 0; // Wakeup counter static uint8_t wakeUpValue = 0; // Time to "fully" wakeup (sec) static uint8_t wakeUpValueSleep = 5; // Data related declarations static uint8_t inputBuffer[6]; // Buffer for incoming data static uint8_t outputBuffer[3] = {0,0,0}; // Buffer for outgoing data static uint8_t updateBuffer[16][3]; // Program state related declarations enum {IDLE, MASTER, SLAVE, NOICEMAKER}; static uint8_t programMode; static uint8_t remoteMode; static uint8_t programModeTemp; // RF related declarations /* Chipcon */ /* Product = CC1100 */ /* Crystal accuracy = 25 ppm */ /* X-tal frequency = 26 MHz */ /* RF output power = 0 dBm */ /* RX filterbandwidth = 101.562500 kHz */ /* Deviation = 19 kHz */ /* Datarate = 9.992599 kbps */ /* Modulation = (1) 2-GFSK */ /* Manchester enable = (1) Manchester enabled */ /* RF Frequency = 868.299866 MHz */ /* Channel spacing = 199.951172 kHz */ /* Channel number = 0 */ /* Optimization = - */ /* Sync mode = (3) 30/32 sync word bits detected */ /* Format of RX/TX data = (0) Normal mode, use FIFOs for RX and TX */ /* CRC operation = (1) CRC calculation in TX and CRC check in RX enabled */ /* Forward Error Correction = (0) FEC disabled */ /* Packetlength = 6 */ /* Preamble count = (2) 4 bytes */ /* Append status = 1 */ /* Address check = (0) No address check */ uint8_t rfSetupData[] = { 0x0A, // IOCFG2 0x2E, // IOCFG1 0x80, // IOCFG0D 0x07, // FIFOTHR 0xD3, // SYNC1 0x91, // SYNC0 0x06, // PKTLEN 0x04, // PKTCTRL1 0x44, // PKTCTRL0 0x00, // ADDR 0x00, // CHANNR 0x06, // FSCTRL1 0x00, // FSCTRL0 0x21, // FREQ2 0x65, // FREQ1 0x6A, // FREQ0 0xC8, // MDMCFG4 0x93, // MDMCFG3 0x1B, // MDMCFG2 0x22, // MDMCFG1 0xF8, // MDMCFG0 0x34, // DEVIATN 0x07, // MCSM2 0x30, // MCSM1 0x18, // MCSM0 0x16, // FOCCFG 0x6C, // BSCFG 0x43, // AGCCTRL2 0x40, // AGCCTRL1 0x91, // AGCCTRL0 0x87, // WOREVT1 0x6B, // WOREVT0 0xF8, // WORCTRL 0x56, // FREND1 0x10, // FREND0 0xE9, // FSCAL3 0x2A, // FSCAL2 0x00, // FSCAL1 0x1F, // FSCAL0 0x41, // RCCTRL1 0x00, // RCCTRL0 0x59, // FSTEST 0x7F, // PTEST 0x3F, // AGCTST 0x81, // TEST2 0x35, // TEST1 0x09 // TEST0 }; int main (void); /*================================================================================================ Functions ================================================================================================*/ /*================================================================================================ flashLed Description: Flash LED. Input: led (LED2, LED3, LED4, LED5) Output: - ------------------------------------------------------------------------------------------------*/ void flashLed(uint8_t led) { if(LED_PORT & (1< 1sec) TCNT2 = 0; // Reset Timer2 counter register while(ASSR &((1< 1sec) TCNT2 = 0; // Reset Timer2 counter register while(ASSR &((1< 100)) { timeOutCounter = 0; activateRxMode(); } } else if(programMode == SLAVE) { // Init SLAVE if(programModeTemp != programMode) { resetCC1100(); showDeviceSettings(); } activateRxMode(); // Measure temperature and battery level readSensorValue(&outputBuffer[0], &outputBuffer[1], &outputBuffer[2]); rfProtocolSendData(outputBuffer); // Send data to master if(cc1100ReadStatusReg(MARCSTATE) == 0x01) { flashLed(LED3); activateRxMode(); timeOutCounter = 0; while((!(cc1100ReadStatusReg(MARCSTATE) == 0x01)) && (++timeOutCounter < 500)); // Check CRC_OK flag (packet received) if(cc1100ReadStatusReg(PKTSTATUS) & (1<<7)) { // Check if packet is a ACK-packet if(rfProtocolReadPacket(inputBuffer) == RF_COMMAND_ACK_AND_UPDATE) { flashLed(LED2); cc1100WriteCommand(SIDLE); // cc1100 = IDLE cc1100WriteCommand(SPWD); // cc1100 = SLEEP uint8_t storeNewSettings = 0; if((inputBuffer[2]) && (inputBuffer[2] != rfProtocolGetMasterAddr())) { rfProtocolSetMasterAddr(inputBuffer[2]); storeNewSettings = 1; } if((inputBuffer[3]) && (inputBuffer[3] != rfProtocolGetDeviceAddr())) { rfProtocolSetDeviceAddr(inputBuffer[3]); storeNewSettings = 1; } if((inputBuffer[4]) && (inputBuffer[4] != wakeUpValueSleep)) { wakeUpValueSleep = inputBuffer[4]; } if(storeNewSettings) { saveSettingsToEEPROM(); } errCounter = 0; enterSleepMode(wakeUpValueSleep, 32); // Enter sleep mode for 8sec } else { flashLed(LED4); cc1100WriteCommand(SIDLE); // cc1100 = IDLE cc1100WriteCommand(SPWD); // cc1100 = SLEEP if(++errCounter < ERR_COUNTER_THR) { enterSleepMode(1, ((rand() % 31)+1)); // Enter sleep mode (random 0-1sec) } else { errCounter = 0; enterSleepMode(wakeUpValueSleep, 32); // Enter sleep mode for 8sec } } } else { //if(cc1100ReadStatusReg(MARCSTATE) == 0x01) //{ // activateRxMode(); //} flashLed(LED4); cc1100WriteCommand(SIDLE); // cc1100 = IDLE cc1100WriteCommand(SPWD); // cc1100 = SLEEP if(++errCounter < ERR_COUNTER_THR) { enterSleepMode(1, ((rand() % 31)+1)); // Enter sleep mode (random 0-1sec) } else { errCounter = 0; enterSleepMode(wakeUpValueSleep, 32); // Enter sleep mode for 8sec } } } else { cc1100WriteCommand(SFTX); // Flush cc1100 TXFIFO flashLed(LED4); cc1100WriteCommand(SIDLE); // cc1100 = IDLE cc1100WriteCommand(SPWD); // cc1100 = SLEEP if(++errCounter < ERR_COUNTER_THR) { enterSleepMode(1, ((rand() % 31)+1)); // Enter sleep mode (random 0-1sec) } else { errCounter = 0; enterSleepMode(wakeUpValueSleep, 32); // Enter sleep mode for 8sec } } } else if(programMode == NOICEMAKER) { // Init NOICEMAKER if(programModeTemp != programMode) { resetCC1100(); LED_PORT &= ~((1<