⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iai_ezmac_plus_internal.c

📁 Integration的EZMac Plus
💻 C
📖 第 1 页 / 共 5 页
字号:
			{
				if( (EZ_reg.bits.PFWCRB.ACKRQ) && (!(EZ_reg.bits.MCRB.NWAD)))
					{
					// We need to receive an ACK
					//disable all interrupts
					_Turn_Off_Timer1();
					disable_interrupts(INT_EXT);
					#ifdef SM_REAL_FUNCTION
						EZ_commands.arr[Config_command]=0x8000 | FREQ_Band | XTAL_COMP;  //switch off TX latch, and RX FIFO
						EZRadio_Write(EZ_commands.arr[Config_command]);
						EZ_commands.arr[Power_command] &= 0xff0f;	// switch of the radio
						EZRadio_Write(EZ_commands.arr[Power_command]);
						EZRadio_Statusread ();                     //reset all non latched IT in Trixie
						EZMac_AckReceive();
					#endif
					}
					else
					{
					 EZMac_Idle();
					}
			}
   break;
   }
}

//===============================================================================================
// start new CRC calculation
void Crc_Init()
{
   // EZ_crc have to be initialized before the first CRC calculation
   EZ_crccnt = 0;
}

//===============================================================================================
#pragma separate
void Crc_On_Fly(int8 Data)
{
#ifdef HITECH_COMPILER
   static int8 bank2 bit_counter;
#endif
#ifdef CCS_C_COMPILER
   int8 bit_counter;
#endif

   #ifdef FIX_MEM_ADDRESS
    #pragma byte Data = 0x63
    #pragma byte bit_counter = 0x65;
   #endif

   if (EZ_crccnt<2)                        // first two bytes
   {
      if (EZ_crccnt==0)
      {
         EZ_crc.bytes[1].adat = Data; // first byte: CrcH
         EZ_crccnt++;
      }
      else
      { // crccnt ==1
         EZ_crc.bytes[0].adat = Data;   // second byte: CrcL
         EZ_crccnt++;
      }
   }
   else // bytes 3, 4...
   {
      for(bit_counter=8; bit_counter > 0; bit_counter--)
      {
         if(!EZ_crc.bytes[1].testreg.bit7)
            {
            EZ_crc.adat <<= 1;
            if(Data&0x80)
               EZ_crc.adat |= 0x0001;
            Data <<= 1;
            }
         else
            {
            EZ_crc.adat <<= 1;
            if(Data&0x80)
               EZ_crc.adat |= 0x0001;
            Data <<= 1;

            EZ_crc.adat ^= CRC_16_PATTERN;
            }
      }
   }
}

//===============================================================================================
#pragma separate
#ifdef HITECH_COMPILER
 #pragma interrupt_level 1
#endif
void _Set_Timer1(int16 Limit)
{
   #ifdef FIX_MEM_ADDRESS
    #pragma locate Limit = 0x68
   #endif
   _Turn_Off_Timer1();
   //set up Timer1 to wake up the state machine for various events
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);      //internal oscillator (Fosc/4), prescaler = 8
   set_timer1(Limit);                             //setup the timer interval
   //enable the interrupt
   clear_interrupt(INT_TIMER1);
   enable_interrupts(INT_TIMER1);
}

//===============================================================================================
#ifdef CCS_C_COMPILER
 #pragma inline
#endif
void _Turn_Off_Timer1(void)
{  //turn of the Timer1
   setup_timer_1(T1_DISABLED);
   disable_interrupts(INT_TIMER1);
}

//===============================================================================================
#pragma separate
#ifdef HITECH_COMPILER
 #pragma interrupt_level 1
#endif
char EZRadio_CMD_Write (int8 CMD_index)
{
#ifdef HITECH_COMPILER
   static int16 bank2 EZcmd;
#endif
#ifdef CCS_C_COMPILER
   int16 EZcmd;
#endif

   EZcmd=EZ_commands.arr[CMD_index];
   //send a command for Trixie
   SEL_PIN = 0;                     //SPI sel pin = 0
   EZ_Spi_Write(EZcmd>>8);      //write high byte
   EZ_Spi_Write(EZcmd);         //write low byte
   SEL_PIN = 1;                     //set SPI sel pin
}

//===============================================================================================
#pragma separate
void EZ_Reg_Default(void)
{
#ifdef HITECH_COMPILER
   static int8 bank2 i;
#endif
#ifdef CCS_C_COMPILER
   int8 i;
#endif

   EZ_state = EZMac_S_Sleep;                    //first state: Sleep
   EZInternalState = EZ_Sleep;
   for(i=MCR;i<PLEN;i++)
    EZMacReg_Write(i,0);                        //delete all registers, except the read only registers

   /*set the default values of the registers*/
   //AXOT = 1, LBTEN = 1, OutPow = min
   EZ_reg.bytes.TCRB = 0xF8;
   //AXOR = 1
   EZ_reg.bytes.RCRB = 0x80;
   //promiscuous mode
   EZ_reg.bytes.PFCRB = 0x02;
   //different freq in the freq register
   for(i=1;i<9;i++)
      EZ_reg.bytes.FR[i-1] = i;
   EZ_reg.bytes.CIDB = DEFAULT_CID;             //customers can set the default value of this registers
   EZ_reg.bytes.SFIDB = DEFAULT_SFID;           //see IAI_EZMac.h
   EZ_reg.bytes.SFLTNIDL = DEFAULT_NIDL;
   EZ_reg.bytes.SMSKNIDH = DEFAULT_NIDH;
   EZ_reg.bytes.RPLMPL = 0x01;
   SetSpiEn = TRUE;                             //External Interrupt routine should set the SPI select pin
}

//===============================================================================================
#pragma separate
void EZ_Radio_Config(void)
{
   EZ_commands.arr[Config_command]=0x8000 | FREQ_Band | XTAL_COMP;
   EZ_commands.arr[Power_command]=POW_cmd_init_value;             // 
   EZ_commands.arr[Fifo_command]=FIFO_cmd_init_value;             // 
   EZ_commands.arr[Data_Rate_command]=DR_cmd_init_value;          // 
   EZ_commands.arr[RX_Ctrl_command]=RX_cmd_init_value;            // 
   EZ_commands.arr[TX_Ctrl_command]=TX_cmd_init_value;            // 
   EZ_commands.arr[Data_Filter_command]=DF_cmd_init_value;        // 
   EZ_commands.arr[AFC_command]=AFC_cmd_init_value;               // 
   EZ_commands.arr[Freq_command]=0xA000 + ((int16)(FREQ_maxid/2)*FREQ_step + FREQ_start);
   EZ_commands.arr[Wake_command]=WK_cmd_init_value;               // 
   EZ_commands.arr[Low_Duty_command]=LD_cmd_init_value;           // 
   EZ_commands.arr[LBD_command]=LB_cmd_init_value;                // 

   EZRadio_CMD_Write(Power_command);
   EZRadio_CMD_Write(Config_command);
   EZRadio_CMD_Write(Fifo_command);
   EZRadio_CMD_Write(Data_Rate_command);
   EZRadio_CMD_Write(TX_Ctrl_command);
   EZRadio_CMD_Write(RX_Ctrl_command);
   EZRadio_CMD_Write(Data_Filter_command);
   EZRadio_CMD_Write(AFC_command);
   EZRadio_CMD_Write(Freq_command);
   EZRadio_CMD_Write(Wake_command);
   EZRadio_CMD_Write(Low_Duty_command);
   EZRadio_CMD_Write(LBD_command);

   EZRadio_Statusread ();
}

//===============================================================================================
#pragma separate
#ifdef HITECH_COMPILER
 #pragma interrupt_level 1
#endif
void EZMac_AckTransmit (void)
{
	unsigned int del;
	for(del=0;del<100;del++);
   //set the preamble value: number of frequency + 3 (the first 2 copied automatically by Trixie)
   DataByteCounter = EZ_reg.bits.MCRB.NRF + 1;

   //Init the CRC calc. function
   Crc_Init();

   //The frequency is given
  
   //next states will not read data bytes -> External Interrupt routine should set the SPI select pin
   SetSpiEn = TRUE;
   #ifdef LED_DEBUG	
   LED3 = 0;
   #endif

   //listen before talk isn't enable for ack
   #ifdef SM_REAL_FUNCTION                    
   //configure Trixie for transmit
   //switch on TX reg.
   EZ_commands.arr[Config_command]=0x8000 | FREQ_Band | XTAL_COMP | TXREG_USED;
   EZRadio_Write(EZ_commands.arr[Config_command]);
   EZRadio_Statusread ();
   EZ_commands.arr[Power_command] |= 0x0020;                   //switch on the transmitter
   EZRadio_Write(EZ_commands.arr[Power_command]);
   #endif

   EZ_state = EZMac_S_SendingAck;              //set the next state
   EZInternalState = EZ_TxSendPreamble;

    #ifdef SM_SOFTWDT_EN                       //set up the SW Watch dog timer for packet transmitting
    _Set_Timer1(MAX_TX_PACKET_TIMER);
    #endif

    clear_interrupt( INT_EXT );                //clear IT flag
    enable_interrupts( INT_EXT );              //enable ext IT  
}


//===============================================================================================
#pragma separate
#ifdef HITECH_COMPILER
 #pragma interrupt_level 1
#endif
void EZMac_AckReceive (void)
{
   //configure Trixie
   #ifdef SM_REAL_FUNCTION
    EZ_commands.arr[Config_command]=0x8000 | FREQ_Band | XTAL_COMP | FIFO_USED;  //switch on RX FIFO
    EZRadio_Write(EZ_commands.arr[Config_command]);
    EZRadio_Statusread ();
    EZ_commands.arr[Power_command] |= 0x0080;                                    //switch on the receiver
    EZRadio_Write(EZ_commands.arr[Power_command]);
   #endif
	  
   EZ_state = EZMac_S_WaitingForAck;
   AckWaitCounter = 0;
   AckSender = EZ_reg.bytes.DIDB;

   //next states will not read data bytes -> Interrupt routine should set the SPI select pin
   SetSpiEn = TRUE;
   //parameters for channel check
   EZInternalState = EZ_RxDQDCheck;
   Data = 0;
   Data1 = 0;
   _Set_Timer1(FIRST_DQD_TIMER_LIMIT);
}

//===============================================================================================
#pragma separate
#ifdef HITECH_COMPILER
 #pragma interrupt_level 1
#endif
void EZMac_Ackwait (unsigned int cycle_plus)
{
//next states will not read data bytes -> External Interrupt routine should set the SPI select pin
   SetSpiEn = TRUE;
   #ifdef SM_REAL_FUNCTION
   if(EZ_commands.arr[Fifo_command] & 0x0002)
	   {	
	    EZ_commands.arr[Fifo_command] &=0xFFFD;                                        //reset synchron latch
	    EZRadio_CMD_Write(Fifo_command);
	   } 	
   #endif
   disable_interrupts( INT_EXT );

	AckWaitCounter+=cycle_plus;
	if (AckWaitCounter < ACK_WAIT_LIMIT)
	{
		//next states will not read data bytes -> Interrupt routine should set the SPI select pin
		SetSpiEn = TRUE;
	   //Set the next state
		EZInternalState = EZ_RxDQDCheck;
		//set the DQD Check init values
		Data = 0;
		Data1 = 0;
		//Start T1 to first DQD check
		_Set_Timer1(FIRST_DQD_TIMER_LIMIT);
	}
	else
	{
	 //Error timer exepires
	  // switch of the Radio, disable all interrupts
		_GotoTxErrorState();
	}
}


//===============================================================================================
#pragma separate
#ifdef HITECH_COMPILER
 #pragma interrupt_level 1
#endif
void EZMac_ForwardPacket (void)
{
	unsigned int del;
	for(del=0;del<1000;del++);
   //set the preamble value: number of frequency + 3 (the first 2 copied automatically by Trixie)
   DataByteCounter = EZ_reg.bits.MCRB.NRF + 1;

   //Init the CRC calc. function
   Crc_Init();

   //Currently we forward the packet the same frequency we received
  
   //next states will not read data bytes -> External Interrupt routine should set the SPI select pin
   SetSpiEn = TRUE;

   //listen before talk 
    #ifdef SM_REAL_FUNCTION                    //configure Trixie for receiving
	  EZ_commands.arr[Config_command]=0x8000 | FREQ_Band | XTAL_COMP | FIFO_USED;  //switch on RX FIFO
	  EZRadio_Write(EZ_commands.arr[Config_command]);
      EZRadio_Statusread ();
      EZ_commands.arr[Power_command] |= 0x0080;                   //switch on the receiver
      EZRadio_Write(EZ_commands.arr[Power_command]);
    #endif

      EZInternalState = EZ_TxCheckChannel;       //set the next state
      EZ_state = EZMac_S_Forwarding;

      Data = 0;                                  //the parameters for LBT
      Data1 = 0;
	  csmaCycle 	= 0;
	  csmaCounter = CSMA_COUNTER_BASE;

     _Set_Timer1(FIRST_DQD_TIMER_LIMIT);        //set up T1 for DQD check

}


//===============================================================================================
#pragma separate
#ifdef HITECH_COMPILER
 #pragma interrupt_level 1
#endif
bool EZMac_IsRepeatedRx (void)
{
unsigned char i;

	for (i=0;i<REPEATED_TABLE_SIZE;i++)
		if ((repeatedTable[i].SID == EZ_reg.bytes.SIDB)&&(repeatedTable[i].DID == EZ_reg.bytes.DIDB)&& ( (repeatedTable[i].CONTROL & 0xF0) == (ControlField & 0xF0) ) )
			{
				return 1;
			}	

	repeatedTable[rtWritePointer].SID = EZ_reg.bytes.SIDB;
	repeatedTable[rtWritePointer].DID = EZ_reg.bytes.DIDB;
	repeatedTable[rtWritePointer].CONTROL = ControlField;

	rtWritePointer++;
	
	if (rtWritePointer >= REPEATED_TABLE_SIZE)
		rtWritePointer = 0;
		
	return 0;
}


//===============================================================================================
#pragma separate
void EZMac_Repe

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -