/*------------------------------------------------------------------------------
 *
 *    FUNCTION DESCRIPTION:
 *      Initialization of Application parameters. 
 *
 *------------------------------------------------------------------------------
 */
 void vAppInitial(void)
{
// Disable the Matrix and Roff modes on GPIO[3:1] 
  PORT_CTRL &= ~(M_PORT_MATRIX | M_PORT_ROFF | M_PORT_STROBE);
  PORT_CTRL |=  M_PORT_STROBE;
  PORT_CTRL &= (~M_PORT_STROBE);

// Turn LED control off 
  GPIO_LED = 0;
// Set LED intensity .. acceptable values are 0 (off) or 1, 2, and 3 
  vSys_LedIntensity( 0 );

  // Initial debouce time. 
  bIsr_DebounceCount = 0;
  // debounce time definition, the unit is defined by the RTC interrupt time
  //(5ms in the demo).It means that the interval between calls to the
  // vBsr_Service() function is 5*8=40ms.
  // BSR parameters initialization 
  rBsrSetup.bButtonMask = 0x1F;	// GPIO0 only
  rBsrSetup.pbPtsReserveHead = abBsrPtsPlaceHolder;
  rBsrSetup.bPtsSize = 10;
  rBsrSetup.bPushQualThresh = 3;

// Setup the PA.
// fAlpha and fBeta has to be set based on antenna configuration.
// Chose a PA level and nominal cap. Both values come from
// the calculation spreadsheet. 
  rPaSetup.fAlpha      = 0.0;
  rPaSetup.fBeta       = 0.3593;
  rPaSetup.bLevel      = 0;
  rPaSetup.wNominalCap = 10;
  rPaSetup.bMaxDrv     = 0;

 // Setup the ODS 
  rOdsSetup.bModulationType = bRke_OokMod_c; // Use OOK 
//  rOdsSetup.bModulationType = bRke_FskMod_c; // Use FSK 
  rOdsSetup.bClkDiv         = 5;
  rOdsSetup.bEdgeRate       = 0;

// Set group width to 7, which means 8 bits/encoded byte to be transmitted.
// The value must match the output width of the data encoding function
// set by the vStl_EncodeSetup() below! 
  rOdsSetup.bGroupWidth     = 7;
// 24MHz / (bClkDiv+1) / 9.6kbps = 417 
  rOdsSetup.wBitRate        = 1600; // 1,25kbps

// Configure the warm up intervals LC: 8, DIV: 4, PA: 4 
  rOdsSetup.bLcWarmInt  = 8;
  rOdsSetup.bDivWarmInt = 5;
  rOdsSetup.bPaWarmInt  = 4;

// Set external crystal oscillator parameters - 10MHz, 12pF 
// currently XO is not used in this example
  rFCast_XoSetup.fXoFreq = 10000000;//12729633;
  rFCast_XoSetup.bLowCap = 1;

  return;
}
Beispiel #2
0
void  main (void)
{

/*------------------------------------------------------------------------------
 *    SETUP PHASE
 *------------------------------------------------------------------------------
 */
        //Set DMD interrupt to high priority,
        // any other interrupts have to stay low priority
        PDMD=1;
        // Disable the Matrix and Roff modes on GPIO[3:1] 
        PORT_CTRL &= ~(M_PORT_MATRIX | M_PORT_ROFF | M_PORT_STROBE);
        PORT_CTRL |=  M_PORT_STROBE;
        PORT_CTRL &= (~M_PORT_STROBE);
        // Turn LED control off 
        GPIO_LED = 0;
        vSys_Setup( 10 );
        vSys_SetMasterTime(0);
        // Setup the bandgap 
        vSys_BandGapLdo( 1 );

        if ((PROT0_CTRL & M_NVM_BLOWN) > 1) //if part is burned to user or run mode.
        {
                // Check the first power up bit meaning keyfob is powered on by battery insertion
                if ( 0 != (SYSGEN & M_POWER_1ST_TIME) )
                {
                        vSys_FirstPowerUp(); // Function will shutdown.
                }
        }
        // Set LED intensity. Valid values are 0 (off), 1, 2 and 3 
        vSys_LedIntensity( 3 );
        lLEDOnTime=20;
        //Get part ID (4byte factory burned unique serial number)
        lPartID = lSys_GetProdId();
        // Initialize isr call counter 
        bIsr_DebounceCount = 0;

        // BSR parameters initialization 
        rBsrSetup.bButtonMask = bButtonMask_c; 
#ifdef CRYSTAL
        rBsrSetup.bButtonMask &= 0xFE;// clear bit0; GPIO0 will be used by crystal
#endif
        rBsrSetup.pbPtsReserveHead = abBsrPtsPlaceHolder;
        rBsrSetup.bPtsSize = 3;
        rBsrSetup.bPushQualThresh = 3;
        // Setup the BSR 
        vBsr_Setup( &rBsrSetup );

        // Setup the RTC to tick every 5ms and clear it. Keep it disabled. 
        RTC_CTRL = (0x07 << B_RTC_DIV) | M_RTC_CLR;
        // Enable the RTC 
        RTC_CTRL |= M_RTC_ENA;
        // Enable the RTC interrupt and global interrupt enable 
        ERTC = 1;
        EA = 1;

        fDesiredFreqOOK 	 = f_433_RkeFreqOOK_c;
        fDesiredFreqFSK 	 = f_433_RkeFreqFSK_c;
        bFskDev 		 = b_433_RkeFskDev_c;
        // Setup the PA.
        rPaSetup.bLevel      = b_433_PaLevel_c;
        rPaSetup.wNominalCap = b_433_PaNominalCap_c;
        rPaSetup.bMaxDrv     = b_433_PaMaxDrv_c;
        rPaSetup.fAlpha      = 0.0;
        rPaSetup.fBeta       = 0.0;
        vPa_Setup( &rPaSetup );

#ifdef OOK
rOdsSetup.bModulationType = bModOOK_c;
// Setup the STL encoding for Manchester. No user encoding function therefore the pointer is NULL. 
vStl_EncodeSetup( bEncodeManchester_c, NULL );
fDesiredFreq = fDesiredFreqOOK;
bPreamble = bPreambleManch_c;
#else //FSK
rOdsSetup.bModulationType = bModFSK_c;
// Setup the STL encoding for none. No user encoding function therefore the pointer is NULL.
vStl_EncodeSetup( bEnc_NoneNrz_c, NULL );
fDesiredFreq = fDesiredFreqFSK;
bPreamble = bPreambleNrz_c;
#endif
// Setup the ODS 
// Set group width to 7, which means 8 bits/encoded byte to be transmitted.
// The value must match the output width of the data encoding function
// set by vStl_EncodeSetup()! 
rOdsSetup.bGroupWidth     = 7;
rOdsSetup.bClkDiv         = 5;
rOdsSetup.bEdgeRate       = 0;
// Configure the warm up intervals 
rOdsSetup.bLcWarmInt  = 0;
rOdsSetup.bDivWarmInt = 5;
rOdsSetup.bPaWarmInt  = 4;
// 24MHz / (bClkDiv+1) / 9.6kbps = 417 
rOdsSetup.wBitRate        = 417;
vOds_Setup( &rOdsSetup );

// Setup frequency casting .. needed to be called once per boot 
vFCast_Setup();

#ifdef CRYSTAL
//XO Setup
rXoSetup.fXoFreq	= 10000000.0;   // Frequency of the external crystal [Hz]
rXoSetup.bLowCap	= 1;			// Capacitance setup of crystal 0 = above 14pf, 1 = below 14pf
vFCast_XoSetup( &rXoSetup );
#endif

// Measure the battery voltage in mV, only once per boot to save power
// Make loaded measurement .. bBatteryWait_c * 17ms wait after loading
iBatteryMv = iMVdd_Measure( bBatteryWait_c );
if (iBatteryMv < iLowBatMv_c) 
{
        bBatStatus = 0;
}
else
{
        bBatStatus = 1;
}
  
// Setup the DMD temperature sensor for temperature mode 
vDmdTs_RunForTemp( 3 ); // Skip first 3 samples 
// Wait until there is a valid DMD temp sensor sample 
while ( 0 == bDmdTs_GetSamplesTaken() )
{
        //wait
}

 /*------------------------------------------------------------------------------
  *    TRANSMISSION LOOP PHASE
  *------------------------------------------------------------------------------
  */

// Application loop, including push button state analyzation and transmission. 
while(1)
{
// Buttons analyzation 
        vButtonCheck();
        if (bButtonState)
        {
                // Packet transmit repeat counter
	        bRepeatCount = bRepeatCount_c;   
                // Transmit. 
                vRepeatTxLoop();
                //Sync counter increment
        }
        else if( (lSys_GetMasterTime() >> 5) > bMaxWaitForPush_c )
        {
	        if ((PROT0_CTRL & M_NVM_BLOWN) > 1) //if part is burned to user or run mode.
  		{
                        //Disable all interrupts
                        EA = 0;
#ifdef CRYSTAL
                        // Disable XO
			bXO_CTRL = 0 ; 		
                        // Wait 20us for XO to stop
                        vSys_16BitDecLoop( 20 );
#endif
                        // Shutdown
                        vSys_Shutdown();      	
		}
    }
  }
}