/**
 * @fn void Query_Current_Status(s_AppRadio * pRadio, PayloadFrame *payload)
 *
 * @brief This function checks if its the time to report the status. If yes, 
 *        It populates the status information in its own node structure.
 *
 * @param pRadio an s_AppRadio structure pointer
 * @param payload a PayloadFrame structure pointer
 */
void Query_Current_Status(s_AppRadio * pRadio, PayloadFrame *payload)
{
  unsigned char i;
  i = NodeIsInMyNet(payload->NodeID);                                           // Check if the node is in my net
  if (i) {                                                                      // Load status bytes into my  node structure
    #ifdef INCLUDE_FRAMECOUNTER_STATUS
      Nodes[i].FrameCounter++;
    #endif
      Nodes[0].LastRSSI = RadioReadRegister(pCurrentFrameworkRadio(pRadio), CCXXX_STAT_RSSI);
      Nodes[i].LastRSSI = RadioLocal.RxFrameStruct.frameHeader[RSSI_RECEIVER];
    #ifdef INCLUDE_LQI_STATUS
      Nodes[i].LastLocalLQI  = RadioReadRegister(pCurrentFrameworkRadio(pRadio), CCXXX_STAT_LQI) & 0x7F;
      Nodes[i].LastRemoteLQI = RadioLocal.RxFrameStruct.frameHeader[LQI_RECEIVER] & 0x7F;
    #endif
    #ifdef INCLUDE_FREQOFF_STATUS
      Nodes[i].LastLocalFoff  = ~(RadioReadRegister(pCurrentFrameworkRadio(pRadio), CCXXX_STAT_FREQEST) - 1);
      Nodes[i].LastRemoteFoff = ~(RadioLocal.RxFrameStruct.frameHeader[FREQ_OFFSET_RECEIVER] - 1);
    #endif
    #ifdef INCLUDE_POWER_STATUS
      Nodes[i].LastLocalPower  = CurrentPower();
      Nodes[i].LastRemotePower = RadioLocal.RxFrameStruct.frameHeader[CURRENT_POWER_INDEX];
    #endif
    #ifdef INCLUDE_CHANNEL_STATUS
      Nodes[i].LastUsedChannel = CurrentChannel();
    #endif
  }
}
Example #2
0
/*******************************************************************
** SetRFMode : Sets the SX1231 operating mode					 **
********************************************************************
** In  : mode													 **
** Out : -														**
*******************************************************************/
void
SetRFMode(uint8_t mode)
{
	if (mode == PreMode)
		return;

	switch (mode) {
	case RF_MODE_TRANSMITTER:
		RadioWriteRegister(REG_OPMODE, (RegistersCfg[REG_OPMODE] & 0xE3) | RF_OPMODE_TRANSMITTER);
		break;
	case RF_MODE_RECEIVER:
		RadioWriteRegister(REG_OPMODE, (RegistersCfg[REG_OPMODE] & 0xE3) | RF_OPMODE_RECEIVER);
		break;
	case RF_MODE_SYNTHESIZER:
		RadioWriteRegister(REG_OPMODE, (RegistersCfg[REG_OPMODE] & 0xE3) | RF_OPMODE_SYNTHESIZER);
		break;
	case RF_MODE_STANDBY:
		RadioWriteRegister(REG_OPMODE, (RegistersCfg[REG_OPMODE] & 0xE3) | RF_OPMODE_STANDBY);
		break;
	case RF_MODE_SLEEP:
		RadioWriteRegister(REG_OPMODE, (RegistersCfg[REG_OPMODE] & 0xE3) | RF_OPMODE_SLEEP);
		break;
	default:
		/* handle bogus mode? */
		return;
	} // ..switch (mode)

#if 0
	/* we are using packet mode: waiting for mode ready is only necessary
	 * when going from to sleep because the FIFO may not be immediately available from previous mode */
	while ((RadioReadRegister(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00)
		; // Wait for ModeReady
#endif

	PreMode = mode;

	if (mode == RF_MODE_TRANSMITTER)
		RF_TRANSMIT_LED = LED_ON;
	else
		RF_TRANSMIT_LED = LED_OFF;

	if (mode == RF_MODE_RECEIVER)
		RF_RECEIVING_LED = LED_ON;
	else
		RF_RECEIVING_LED = LED_OFF;

}
Example #3
0
uint8_t
SendRfFrame(const uint8_t *buffer, uint8_t size, uint8_t Node_adrs, char immediate_rx)
{
	uint8_t ByteCounter;

	/* turn off receiver to prevent reception while filling fifo */
	SetRFMode(RF_MODE_STANDBY);
	while ((RadioReadRegister(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00)
		; // Wait for ModeReady

	RadioWriteRegister(REG_DIOMAPPING1, (RegistersCfg[REG_DIOMAPPING1] & 0x3F) | RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
	RadioWriteRegister(REG_FIFOTHRESH, (RegistersCfg[REG_FIFOTHRESH] & 0x7F) | RF_FIFOTHRESH_TXSTART_FIFONOTEMPTY);

	RFState = RF_STATE_TX_BUSY;

	/* SX1231 FIFO access (write) */
	NSS = 0;
	SpiInOut(REG_FIFO | 0x80);
	SpiInOut(size + 1);
	SpiInOut(Node_adrs);
	for (ByteCounter = 0; ByteCounter < size; ByteCounter++)
	{
		SpiInOut(buffer[ByteCounter]);
	}
	NSS = 1;

	/* no need to wait for transmit mode to be ready since its handled by the radio */
	SetRFMode(RF_MODE_TRANSMITTER);

	/* blocking here until finished transmitting */
	while (IN_RF_DIO0 == 0)
		;

	if (immediate_rx)
	{
		start_rf_rx(0);
	}
	else
	{
		RFState = RF_STATE_STOP;
		//RF_TRANSMIT_LED	= LED_OFF;
		SetRFMode(RF_MODE_STANDBY);
	}

	return RADIO_OK;
} // ...SendRfFrame()
Example #4
0
uint8_t
ReceiveRfFrame(uint8_t *buffer, uint8_t *size, uint16_t rx_timeout)
{
	uint8_t RFFrameSize;
	uint8_t Node_Adrs;
	uint8_t i;

	switch (RFState)
	{
		case RF_STATE_STOP:
			start_rf_rx(rx_timeout);
			return RADIO_RX_RUNNING;
		case RF_STATE_RX_BUSY:
			if (IN_RF_DIO0)
			{
				RFState = RF_STATE_RX_DONE;
			}
			else if (HIRES_COMPARE_B_FLAG)
			{
				RFState = RF_STATE_TIMEOUT;
#ifdef LNA_TEST
			}
			else if (flags.read_lna && IN_RF_DIO4)
			{
				flags.read_lna = FALSE;
				for (i = 0; i < 2300; i++)
				{
					asm("nop");	// about 2mS delay at 20MHz
					asm("nop");
					asm("nop");
					asm("nop");
				}
				i = RadioReadRegister(REG_LNA);
				ltoa((i & 0x38) >> 3, ucStr1, 0);
				UART_send_str(ucStr1, TRUE);
#endif /* LNA_TEST */
			}
			return RADIO_RX_RUNNING;
		case RF_STATE_RX_DONE:
			SetRFMode(RF_MODE_STANDBY);

			/* SX1231 FIFO access (read) */
			NSS = 0;
			SpiInOut(REG_FIFO & 0x7F);
			RFFrameSize = SpiInOut(0);
			Node_Adrs = SpiInOut(0);
			i = Node_Adrs;	// suppress compiler warning
			RFFrameSize--;
			for (i = 0; i < RFFrameSize; i++)
				buffer[i] = SpiInOut(0);
			NSS = 1;

			*size = RFFrameSize;
			RFState = RF_STATE_STOP;
			return RADIO_OK;
		case RF_STATE_ERROR:
			SetRFMode(RF_MODE_STANDBY);

			RFState = RF_STATE_STOP;
			return RADIO_ERROR;
		case RF_STATE_TIMEOUT:
			SetRFMode(RF_MODE_STANDBY);

			RFState = RF_STATE_STOP;
			return RADIO_RX_TIMEOUT;
		default:
			SetRFMode(RF_MODE_STANDBY);
			RFState = RF_STATE_STOP;
			return RADIO_ERROR;
	}
Example #5
0
void
InitRFChip(void)
{
	int i;

	/* possibly writing to registers too early, checkit & write again as necessary */
	do
	{
		RadioWriteRegister(REG_SYNCVALUE1, 0xAA);
	} while (RadioReadRegister(REG_SYNCVALUE1) != 0xAA);

	do
	{
		RadioWriteRegister(REG_SYNCVALUE1, 0x55);
	} while (RadioReadRegister(REG_SYNCVALUE1) != 0x55);


	/////// RC CALIBRATION (Once at POR) ///////
	SetRFMode(RF_MODE_STANDBY);
	while ((RadioReadRegister(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00)
		; // Wait for ModeReady

	RadioWriteRegister(0x57, 0x80);
	RadioWriteRegister(REG_OSC1, RadioReadRegister(REG_OSC1) | RF_OSC1_RCCAL_START);
	while ((RadioReadRegister(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00)
		;
	RadioWriteRegister(REG_OSC1, RadioReadRegister(REG_OSC1) | RF_OSC1_RCCAL_START);
	while ((RadioReadRegister(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00)
		;
	RadioWriteRegister(0x57, 0x00);

	NSS = 0;
	SpiInOut(REG_OPMODE | 0x80);	// send address + r/w bit
	for (i = 1; i <= REG_TEMP2; i++)
	{
		SpiInOut(RegistersCfg[i]);
	}
	NSS = 1;

#ifdef READBACK_VERIFY
	for (i = 1; i <= REG_TEMP2; i++)
	{
		do
		{
			NSS = 0;
			SpiInOut(i & 0x7F);	// send address + r/w bit
			regs[i] = SpiInOut(0);
			NSS = 1;

			switch (i)
			{
				case REG_OSC1:
					regs[i] &= ~RF_OSC1_RCCAL_DONE;	// read-only bit
					break;
				case REG_VERSION:
					regs[i] = DEF_VERSION;	// ignore hw-version
					break;
				case REG_LNA:
					regs[i] &= ~RF_LNA_CURRENTGAIN;	// read-only bits
					break;
				case REG_AFCFEI:
					regs[i] &= ~RF_AFCFEI_AFC_DONE;	// read-only bit
					break;
				case REG_RSSICONFIG:
					regs[i] &= ~RF_RSSI_DONE;	// read-only bit
					break;
				case REG_RSSIVALUE:
					regs[i] = DEF_RSSIVALUE;		// ignore rssi r/o
					break;
				case REG_IRQFLAGS1:
					regs[i] &= ~RF_IRQFLAGS1_MODEREADY;	// read-only bit
					break;
				case REG_IRQFLAGS2:
					regs[i] |= RF_IRQFLAGS2_FIFOOVERRUN;	// write-1->reset bit
					break;
				case REG_OSC2:
					regs[i] = DEF_OSC2;
					break;
				}
			} while (regs[i] != READ_ROM_BYTE(RegistersCfg[i]));
	}
#endif /* ..READBACK_VERIFY */

	SetRFMode(RF_MODE_STANDBY);
}