示例#1
0
/**
 * @brief  Returns the LDCR wake up reload timer, according to the formula: Twu=(PRESCALER +1)*(COUNTER+1)*Tck, where Tck = 28.818 us.
 * @param  pfWakeUpReloadMsec pointer to the variable in which the wake-up reload time expressed in milliseconds has to be stored.
 *         This parameter must be a float*.
 * @param  pcCounter pointer to the variable in which the timer counter has to be stored.
 *         This parameter must be an uint8_t*.
 * @param  pcPrescaler pointer to the variable in which the timer prescaler has to be stored.
 *         This parameter must be an uint8_t*.
 * @retval None.
 */
void SpiritTimerGetWakeUpTimerReload(float* pfWakeUpReloadMsec, uint8_t* pcCounter , uint8_t* pcPrescaler)
{
  uint8_t tempRegValue[2];
  /* uint32_t xtal=SpiritRadioGetXtalFrequency(); */
  float rco_freq;
  
  rco_freq=(float)SpiritTimerGetRcoFrequency();
  
  /* Reads the reload Wake_Up timer registers value */
  g_xStatus = SpiritSpiReadRegisters(TIMERS1_LDC_RELOAD_PRESCALER_BASE, 2, tempRegValue);

  /* Returns values */
  (*pcPrescaler)=tempRegValue[0];
  (*pcCounter)=tempRegValue[1];
  *pfWakeUpReloadMsec = (float)((((*pcPrescaler)+1)*((*pcCounter)+1)*(1000.0f/rco_freq)));    /* #1035-D */

}
示例#2
0
/**
 * @brief  Computes the values of the wakeup timer counter and prescaler from the user time expressed in millisecond.
 *         The prescaler and the counter values are computed maintaining the prescaler value as
 *         small as possible in order to obtain the best resolution, and in the meantime minimizing the error.
 * @param  fDesiredMsec desired wakeup timeout in millisecs.
 *         This parameter must be a float. Since the counter and prescaler are 8 bit registers the maximum
 *         reachable value is maxTime = fTclk x 256 x 256.
 * @param  pcCounter pointer to the variable in which the value for the wakeup timer counter has to be stored.
 *         This parameter must be a uint8_t*.
 * @param  pcPrescaler pointer to the variable in which the value for the wakeup timer prescaler has to be stored.
 *         This parameter must be an uint8_t*.
 * @retval None
 */
void SpiritTimerComputeWakeUpValues(float fDesiredMsec , uint8_t* pcCounter , uint8_t* pcPrescaler)
{
  float rco_freq,err;
  uint32_t n;
  
  rco_freq=((float)SpiritTimerGetRcoFrequency())/1000;
  
  /* N cycles in the time base of the timer: 
     - clock of the timer is RCO frequency
     - divide times 1000 more because we have an input in ms (variable rco_freq is already this frequency divided by 1000)
  */
  n=(uint32_t)(fDesiredMsec*rco_freq);
    
  /* check if it is possible to reach that target with prescaler and counter of spirit1 */
  if(n/0xFF>0xFD)
  {
    /* if not return the maximum possible value */
    (*pcCounter) = 0xFF;
    (*pcPrescaler) = 0xFF;
    return;
  }
  
  /* prescaler is really 2 as min value */
  (*pcPrescaler)=(n/0xFF)+2;
  (*pcCounter) = n / (*pcPrescaler);
  
  /* check if the error is minimum */
  err=S_ABS((float)(*pcCounter)*(*pcPrescaler)/rco_freq-fDesiredMsec);
  
  if((*pcCounter)<=254)
  {
    if(S_ABS((float)((*pcCounter)+1)*(*pcPrescaler)/rco_freq-fDesiredMsec)<err)
      (*pcCounter)=(*pcCounter)+1;
  }
    
  /* decrement prescaler and counter according to the logic of this timer in spirit1 */
  (*pcPrescaler)--;
  if((*pcCounter)>1)
    (*pcCounter)--;
  else
    (*pcCounter)=1;
}
/**
* @brief  System main function.
* @param  None
* @retval None
*/
void main (void)
{
 SDK_SYSTEM_CONFIG();
  
#ifdef USE_VCOM
#ifdef STM8L
  SdkEvalComInit(115200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Rx | USART_Mode_Tx));
#elif SDK
  /* VC config */
  SdkEvalVCInit();
  while(bDeviceState != CONFIGURED);
#endif
#endif

  /* Spirit ON */
  SpiritEnterShutdown();
  SpiritExitShutdown();
  SpiritManagementWaExtraCurrent();
  
#ifdef STM8L
  /* Manually set the XTAL_FREQUENCY */
  SpiritRadioSetXtalFrequency(XTAL_FREQUENCY);    
  /* Initialize the frequency offset variable to compensate XTAL offset */
  xRadioInit.lFrequencyBase = xRadioInit.lFrequencyBase + FREQUENCY_OFFSET;
  /* Initialize the signals to drive the range extender application board */
  RANGE_EXT_INIT(RANGE_TYPE);
#elif SDK
  SpiritManagementIdentificationRFBoard();

  /* if the board has eeprom, we can compensate the offset calling SpiritManagementGetOffset
  (if eeprom is not present this fcn will return 0) */
  xRadioInit.lFrequencyBase = xRadioInit.lFrequencyBase + SpiritManagementGetOffset();
    
  /* Initialize the signals to drive the range extender application board */
  SpiritManagementRangeExtInit();  
#endif

  /* Reset Spirit registers to default */
  SpiritCmdStrobeSres();
  
  SdkEvalM2SGpioInit(M2S_GPIO_3,M2S_MODE_EXTI_IN);

  /* Spirit IRQ config */
  SpiritGpioInit(&xGpioIRQ);
  
  /* Configure some output signals (to be probed with a scope to verify) */
  SpiritGpioInit(&xGpio1Rx);
  SpiritGpioInit(&xGpio0Sleep);
  
  /* Spirit Radio config */
  SpiritRadioInit(&xRadioInit);
  SpiritCalibrationRco(S_ENABLE);

  /* Spirit Packet config */
  SpiritPktBasicInit(&xBasicInit);
  
  /* IRQ registers blanking */
  SpiritIrqClearStatus();
  
#ifdef STM8L
  enableInterrupts();
#elif SDK
  SdkEvalM2SGpioInterruptCmd(M2S_GPIO_3,0x0A,0x0A,ENABLE);
#endif
  
  SpiritQiSetSqiThreshold(SQI_TH_0);
  SpiritQiSqiCheck(S_ENABLE);
  
  /* Spirit IRQs enable */
  SpiritIrqDeInit(&xIrqStatus);
  SpiritIrq(RX_DATA_READY,S_ENABLE);
  SpiritIrq(RX_DATA_DISC,S_ENABLE);
  
  /* Payload length config */
  SpiritPktBasicSetPayloadLength(20);
  
  /* RX timeout config */
  SET_INFINITE_RX_TIMEOUT();
  SpiritTimerSetRxTimeoutStopCondition(SQI_ABOVE_THRESHOLD);
  
  float fWuTimer;
  uint8_t cWuPrescaler,cWuCounter,cWuReloadCounter;
  
  /* Set the wake Up event every WAKEUP_TIMER ms */
  SpiritTimerSetWakeUpTimerMs(WAKEUP_TIMER);
  
  /* Get the counter and the prescaler of the WU timer */
  SpiritTimerGetWakeUpTimer(&fWuTimer,&cWuCounter,&cWuPrescaler);
  
  /*
  Compute the rephasing timer to be reloaded on sync.
  This value will be equal to: WAKE_UP_TIMER - (PREAMB_TIME + SYNC_TIME) - GUARD_TIME,
  where:
  - (PREAMB_TIME + SYNC_TIME) is the time needed to transmit preamble and sync.
  - GUARD_TIME is a security bound to make the Rx awake before the Tx.
  */
  float fReloadMs = WAKEUP_TIMER-1000.0*(float)(SpiritPktBasicGetPreambleLength()+SpiritPktBasicGetSyncLength())*8.0/DATARATE-10.0;
  
  /* In order to have not lack of accuracy it is recommended that the reload timer
  has the same prescaler of the WakeUp timer.
  So compute the counter using the WU prescaler previously computed.
  */
  cWuReloadCounter=(uint8_t)(fReloadMs*(float)SpiritTimerGetRcoFrequency()/1000/(cWuPrescaler+1))-1;
  
  /* Set the Wake up reload timer */
  SpiritTimerSetWakeUpTimerReload(cWuReloadCounter,cWuPrescaler);
  
  /* Enable the auto reload on sync */
  SpiritTimerLdcrAutoReload(S_ENABLE);
  
  /* Enable the LDCR mode */
  SpiritTimerLdcrMode(S_ENABLE);
  
  SpiritCmdStrobeFlushRxFifo();
  
  /* Set Spirit in Rx state */
  SpiritCmdStrobeRx();
  
  /* infinite loop */
  while (1) {
    
    while(!xRxDataReadyFlag);
    xRxDataReadyFlag=RESET;
      
    if(xFirstReception==RESET)
    {
      SpiritTimerSetRxTimeoutMs(80.0);
      
      xFirstReception=SET;
      SdkEvalLedToggle(LED1);
    }
    
    
    
  }
  
}