int ZigBee_SetChannel(uint8_t ch) { if (ch > 102) return -1; ZigBee_WriteReg(REG_CC_CTRL_1, 5); //833Mhz + channel number ZigBee_WriteReg(REG_CC_CTRL_0, ch); return 0; }
int ZigBee_ConfigClock() { int ret = 0; //2mA all pins, CLKM disabled ZigBee_WriteReg(REG_TRX_CTRL_0,0x00); _delay_us(2); // Internal crystal disabled, use external freq input ZigBee_WriteReg(REG_XOSC_CTRL,0x40); return ret; }
/// makes one attempt to write a change to rx mode /// return 0 on success, -1 on failure after 1 ms timeout int ZigBee_SetModeRx() { uint8_t zigbee_state = ZigBee_ReadState(); // wait until current state permits a transition while(zigbee_state == STATUS_BUSY_RX || zigbee_state == STATUS_BUSY_TX || zigbee_state == STATUS_STATE_TRANSITION) { DelayMicroseconds(100); // should not be required zigbee_state = ZigBee_ReadState(); } // if current state is already STATUS_RX_ON, return success if(zigbee_state == STATUS_RX_ON) { return(0); // success } // request transition ZigBee_WriteReg(REG_TRX_STATE, CMD_RX_ON); // poll state until transition verified // datasheet reports nominal <170 us transition time // timeout and fail after 1 ms for(int i = 0; i < 100; i++) { // TODO: revisit timeout threshold zigbee_state = ZigBee_ReadState(); if (zigbee_state == STATUS_RX_ON) return(0); // success } // zigbee failed to reach state STATUS_RX_ON return(-1); // failure }
int ZigBee_SetModeRx() { uint8_t zigbee_state = ZigBee_ReadState(); while(zigbee_state == STATUS_BUSY_RX || zigbee_state == STATUS_BUSY_TX || zigbee_state == STATUS_STATE_TRANSITION) { zigbee_state = ZigBee_ReadState(); } ZigBee_WriteReg(REG_TRX_STATE, CMD_RX_ON); uint8_t state = 0; for(int i = 0; i < 100; i++) { state = ZigBee_ReadState(); if (state == STATUS_RX_ON) return(0); } return(-1); }
int ZigBee_ForceTrxOff() { uint8_t zigbee_state = ZigBee_ReadState(); while(zigbee_state == STATUS_BUSY_RX || zigbee_state == STATUS_BUSY_TX || zigbee_state == STATUS_STATE_TRANSITION) { zigbee_state = ZigBee_ReadState(); } //write FORCE_TRX_OFF to TRX_STATE ZigBee_WriteReg(REG_TRX_STATE, CMD_FORCE_TRX_OFF); uint8_t state = 0; for(int i = 0; i < 300; i++) { state = ZigBee_ReadState(); if (state == STATUS_TRX_OFF) return(0); } return(-1); }
uint8_t ZigBee_WriteFrame(const void* data, uint8_t len) { if (len > MAX_ZIGBEE_PACKET_BYTES) return -1; SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); const uint8_t *tx_ptr = data; //clear any remaining bytes while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)) SPI_ReceiveData8(SPI); PullDown(); _delay_us(1); //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); SPI_SendData8(SPI, 0x60); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); SPI_SendData8(SPI, len+2); //two extra bytes for the 16-bit CRC while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); for(int ii = 0; ii < len; ii++) { SPI_SendData8(SPI, tx_ptr[ii]); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); } _delay_us(1); PullUp(); ZigBee_WriteReg(REG_TRX_STATE, 0x02); //alternate method for signaling to send return 0; }
int ZigBee_PllOn() { uint8_t zigbee_state = ZigBee_ReadState(); while(zigbee_state == STATUS_BUSY_RX || zigbee_state == STATUS_BUSY_TX || zigbee_state == STATUS_STATE_TRANSITION) { zigbee_state = ZigBee_ReadState(); } #if defined(DEBUG) printf("zigbee setting PLL ON\r\n"); #endif ZigBee_WriteReg(REG_TRX_STATE, CMD_PLL_ON); uint8_t state = 0; for(int i = 0; i < 100; i++) { state = ZigBee_ReadState(); if (state == STATUS_PLL_ON) return(0); } return(-1); }
void ZigBee_StartEdMeasurement() { ZigBee_WriteReg(REG_PHY_ED_LEVEL, 0xFF); }
int ZigBee_Init() { if ( ZigBee_CheckPartAndVers() == 0 ) { #if defined(DEBUG) printf("zigbee found!\r\n"); #endif } else { #if defined(DEBUG) printf("zigbee NOT found!!\r\n"); #endif return -1; } ZigBee_ConfigClock(); _delay_us(10); if (ZigBee_ForceTrxOff() != 0) return -1; //enable Auto CRC, disable RX buffer empty IRQ ZigBee_WriteReg(REG_TRX_CTRL_1, 0x20); _delay_us(2); ZigBee_WriteReg(REG_TRX_CTRL_2, 0x2C); //modulation: OQPSK-SIN-250 _delay_us(2); ZigBee_WriteReg(REG_PHY_TX_PWR, 0xE1); //output power setting to 10dBm if (ZigBee_SetModeRx() == 0) { #if defined(DEBUG) printf("zigbee initialization complete.\r\n"); #endif } else { #if defined(DEBUG) printf("zigbee initialization FAILED.\r\n"); #endif /* while(1) { uint8_t zigbee_ret = ZigBee_ReadState(); switch(zigbee_ret) { case STATUS_P_ON: mRedTOGGLE; break; case STATUS_BUSY_RX: mWhiteTOGGLE; break; case STATUS_BUSY_TX: mGreenTOGGLE; break; //case STATUS_RX_ON: //mAmberTOGGLE; break; case STATUS_TRX_OFF: mRedTOGGLE; mWhiteTOGGLE; break; case STATUS_PLL_ON: mRedTOGGLE; mGreenTOGGLE; break; case 0xF: mRedTOGGLE; mAmberTOGGLE; break; case 0x11: mRedTOGGLE; mWhiteTOGGLE; mGreenTOGGLE; break; case 0x12: mRedTOGGLE; mWhiteTOGGLE; mAmberTOGGLE; break; case 0x16: mRedTOGGLE; mWhiteTOGGLE; mAmberTOGGLE; mGreenTOGGLE; break; case 0x19: mWhiteTOGGLE; mGreenTOGGLE; break; case 0x1C: mWhiteTOGGLE; mAmberTOGGLE; break; case 0x1D: mWhiteTOGGLE; mGreenTOGGLE; mAmberTOGGLE; break; case 0x1E: mGreenTOGGLE; mAmberTOGGLE; break; case 0x1F: mAmberTOGGLE; break; int irq_ret = ZigBee_ReadIrq(); if((irq_ret & 0x40) != 0) { // Frame Buffer access violation mRedON; mWhiteON; } else if((irq_ret & 0x02) != 0) { // PLL unlock mRedON; mGreenON; } else if((irq_ret & 0x01) != 0) { // PLL lock mGreenON; mWhiteON; } } _delay_ms(500); } */ return -1; } return 0; }
int ZigBee_EnableTrxEndIrq() { ZigBee_WriteReg(REG_IRQ_MASK,0b00001000); return 0; }
int ZigBee_Init() { if ( ZigBee_CheckPartAndVers() == 0 ) { #if defined(DEBUG) printf("zigbee found!\r\n"); #endif } else { #if defined(DEBUG) printf("zigbee NOT found!!\r\n"); #endif return -1; } ZigBee_ConfigClock(); DelayMicroseconds(10); if (ZigBee_ForceTrxOff() != 0) return -1; //enable Auto CRC, disable RX buffer empty IRQ // ZigBee_WriteReg(REG_TRX_CTRL_1, 0x20); // _delay_us(2); //enable Auto CRC, disable RX buffer empty IRQ, Enable Amplifier+LEDs ZigBee_WriteReg(REG_TRX_CTRL_1, 0xE0); DelayMicroseconds(2); // see TRX_CTRL_2 register descriptions in datasheet ZigBee_WriteReg(REG_TRX_CTRL_2, 0b00001100); // modulation: OQPSK-SIN-250, fairly reliable within Modlab // ZigBee_WriteReg(REG_TRX_CTRL_2, 0b00001101); // modulation: OQPSK-SIN-500, fairly reliable within Modlab // ZigBee_WriteReg(REG_TRX_CTRL_2, 0b00101110); // modulation: OQPSK-SIN-1000-SCR-ON, high data loss at any range DelayMicroseconds(2); #ifndef AMPLIFIED_OUTPUT ZigBee_WriteReg(REG_PHY_TX_PWR, 0xE1); //output power setting to 10dBm #else ZigBee_WriteReg(REG_PHY_TX_PWR, 0x84); //output power setting to 5dBm (Amplified) mOrangeOFF; #endif if (ZigBee_SetModeRx() == 0) { #if defined(DEBUG) printf("zigbee initialization complete.\r\n"); #endif } else { #if defined(DEBUG) printf("zigbee initialization FAILED.\r\n"); #endif /* while(1) { uint8_t zigbee_ret = ZigBee_ReadState(); switch(zigbee_ret) { case STATUS_P_ON: mRedTOGGLE; break; case STATUS_BUSY_RX: mWhiteTOGGLE; break; case STATUS_BUSY_TX: mGreenTOGGLE; break; //case STATUS_RX_ON: //mAmberTOGGLE; break; case STATUS_TRX_OFF: mRedTOGGLE; mWhiteTOGGLE; break; case STATUS_PLL_ON: mRedTOGGLE; mGreenTOGGLE; break; case 0xF: mRedTOGGLE; mAmberTOGGLE; break; case 0x11: mRedTOGGLE; mWhiteTOGGLE; mGreenTOGGLE; break; case 0x12: mRedTOGGLE; mWhiteTOGGLE; mAmberTOGGLE; break; case 0x16: mRedTOGGLE; mWhiteTOGGLE; mAmberTOGGLE; mGreenTOGGLE; break; case 0x19: mWhiteTOGGLE; mGreenTOGGLE; break; case 0x1C: mWhiteTOGGLE; mAmberTOGGLE; break; case 0x1D: mWhiteTOGGLE; mGreenTOGGLE; mAmberTOGGLE; break; case 0x1E: mGreenTOGGLE; mAmberTOGGLE; break; case 0x1F: mAmberTOGGLE; break; int irq_ret = ZigBee_ReadIrq(); if((irq_ret & 0x40) != 0) { // Frame Buffer access violation mRedON; mWhiteON; } else if((irq_ret & 0x02) != 0) { // PLL unlock mRedON; mGreenON; } else if((irq_ret & 0x01) != 0) { // PLL lock mGreenON; mWhiteON; } } DelayMilliseconds(500); } */ return -1; } return 0; }