Пример #1
0
/**
* @brief  This function handles External interrupt request (associated with Spirit GPIO 3).
* @param  None
* @retval None
*/
void M2S_GPIO_3_EXTI_IRQ_HANDLER(void)
{
  /* control the EXTI flag */
  if(EXTI_GetFlagStatus(M2S_GPIO_3_EXTI_LINE))
  {
    /* read the IRQ status bytes */
    SpiritIrqGetStatus(&xIrqStatus);
    
    /* set flags according to the raised IRQ*/
    if(xIrqStatus.IRQ_TX_DATA_SENT)
    {
      xTxDoneFlag = SET;
      SdkEvalLedToggle(LED1);
    }
    if(xIrqStatus.IRQ_RX_DATA_READY)
    {
      xRxDoneFlag = SET;
      SdkEvalLedToggle(LED2);
    }
    if(xIrqStatus.IRQ_RX_DATA_DISC)
    {
      SpiritCmdStrobeRx();
      SdkEvalLedToggle(LED3);
    }
    
    /* clear the EXTI flag */
    EXTI_ClearFlag(M2S_GPIO_3_EXTI_LINE);
  }
}
Пример #2
0
void M2S_GPIO_0_EXTI_IRQ_HANDLER(void)
{
 
  /* Check the flag status of EXTI line */
  if(EXTI_GetITStatus(M2S_GPIO_0_EXTI_LINE)){
    
    /* Get the IRQ status */
    SpiritIrqGetStatus(&xIrqStatus);
    
     
    // Check the SPIRIT RX_DATA_DISC IRQ flag 
    if(xIrqStatus.IRQ_RX_DATA_DISC)
    {
      
      SdkEvalLedToggle(LED_YELLOW);  
 
    }
    
    /* Check the SPIRIT RX_DATA_READY IRQ flag */
    if(xIrqStatus.IRQ_RX_DATA_READY)
    {
     
      /* Get the RX FIFO size */
      cRxData=SpiritLinearFifoReadNumElementsRxFifo();
      
      /* Read the RX FIFO */
      SpiritSpiReadLinearFifo(cRxData, vectcRxBuff);
      
      /* Flush the RX FIFO */
      SpiritCmdStrobeFlushRxFifo();


      
#ifdef USE_VCOM
      /* print the received data */
      printf("B data received: [");
      for(uint8_t i=0 ; i<cRxData ; i++)
        printf("%d ", vectcRxBuff[i]);
      printf("]\r\n");
#endif
      
      SdkEvalLedToggle(LED_GREEN);

    }
     
    
     if(xIrqStatus.IRQ_TX_DATA_SENT)
    {
      /* set the tx_done_flag to manage the event in the main() */
      xTxDoneFlag = SET;
      
    } 
    
    /* Clear the EXTI line flag */
    EXTI_ClearITPendingBit(M2S_GPIO_0_EXTI_LINE);
    
  }
}
void M2S_GPIO_3_EXTI_IRQ_HANDLER(void)
#endif
{
  /* Check the flag status of EXTI line */
  if(EXTI_GetITStatus(M2S_GPIO_3_EXTI_LINE))
  {
    SpiritIrqGetStatus(&xIrqStatus);

    /* Check the SPIRIT TX_DATA_SENT IRQ flag */
    if(xIrqStatus.IRQ_TX_DATA_SENT || xIrqStatus.IRQ_MAX_RE_TX_REACH)
    {
      /* set the tx_done_flag to manage the event in the main() */
      xTxDoneFlag = SET;

      if(xIrqStatus.IRQ_TX_DATA_SENT)
        SdkEvalLedToggle(LED1);

      if(xIrqStatus.IRQ_MAX_RE_TX_REACH)
        SdkEvalLedToggle(LED2);
      
#ifdef USE_VCOM
      printf("N.Elem.TX.FIFO: %d\n\r", SpiritLinearFifoReadNumElementsTxFifo());
      printf("SEQ.NUM.: %d\n\r", SpiritPktCommonGetTransmittedSeqNumber());
      printf("NRETX: %d\n\n\r", SpiritPktStackGetNReTx());
#endif
      
#ifdef PIGGYBACKING
      /* Get the RX FIFO size */
      cRxData=SpiritLinearFifoReadNumElementsRxFifo();

      /* Read the RX FIFO */
      SpiritSpiReadLinearFifo(cRxData, vectcRxBuff);

#ifdef USE_VCOM
      /* print the received data */
      printf("A piggybacking data received: [");
      for(uint8_t i=0 ; i<cRxData ; i++)
        printf("%d ", vectcRxBuff[i]);
      printf("]\n\r");
#endif
#endif
    }

    /* Clear the EXTI line flag */
    EXTI_ClearITPendingBit(M2S_GPIO_3_EXTI_LINE);
  }

}
Пример #4
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f10x_xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f10x.c file
  */
  
  /* Identify the board to distinguish the SDK motherboard from the SDK dongle */
  SdkEvalIdentification();
  
  /* Declare and read the DFU flag on E2Prom */
  uint8_t cDfuFlag = SdkDfuReadDfuFlag();

  /* Initialize the Key push-button */
  SdkEvalPushButtonInit(BUTTON_SCM_PS, BUTTON_MODE_GPIO);


  /* Check if the Key push-button on is pressed */
   if (!(SdkEvalPushButtonGetState(BUTTON_SCM_PS) == RESET || cDfuFlag))
  { /* Test if user code is programmed starting from address 0x8003000 */

    if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
    { /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*((__IO uint32_t*) ApplicationAddress));
      
      Jump_To_Application();
    }
  } /*!< Otherwise enters DFU mode to allow user to program his application */

  /* Reset the DFU flag on E2Prom */
  SdkDfuResetDfuFlag();

  /* Enter DFU mode */
  DeviceState = STATE_dfuERROR;
  DeviceStatus[0] = STATUS_ERRFIRMWARE;
  DeviceStatus[4] = DeviceState;

  /* DFU initialization */
  SdkEvalDFUInit();

  /* LED2 initialization */
  SdkEvalLedInit(LED2);

  /* Main loop */
  while (1)
  {
    /* LED2 toggling */
    SdkEvalLedToggle(LED2);
    
    /* pause */
    for(volatile uint32_t i = 0 ; i<0x3FFFFF ; i++);
  }
}
Пример #5
0
/**
* @brief  This function handles External interrupt request (associated with the KEY_BUTTON).
* @param  None
* @retval None
*/
void KEY_BUTTON_EXTI_IRQ_HANDLER(void)
{
  /* Use this ISR to corrupt the data flow in order to stop the test */
  if(EXTI_GetFlagStatus(KEY_BUTTON_EXTI_LINE))
  {
    vectcBallBuff[14]=3;
    SdkEvalLedToggle(LED3);
    EXTI_ClearFlag(KEY_BUTTON_EXTI_LINE);
  }
}
void M2S_GPIO_3_EXTI_IRQ_HANDLER(void)
#endif
{
  /* Check the flag status of EXTI line */
  if(EXTI_GetITStatus(M2S_GPIO_3_EXTI_LINE)){
    
    /* Get the IRQ status */
    SpiritIrqGetStatus(&xIrqStatus);
    
    /* Check the SPIRIT RX_DATA_READY IRQ flag */
    if(xIrqStatus.IRQ_RX_DATA_READY)
    {
      /* Get the RX FIFO size */
      cRxData = SpiritLinearFifoReadNumElementsRxFifo();
      
      /* Read the RX FIFO */
      SpiritSpiReadLinearFifo(cRxData, vectcRxBuff);
      
#ifdef USE_VCOM
      /* print the received data */
      printf("B data received: [");
      for(uint8_t i=0 ; i<cRxData ; i++)
        printf("%d ", vectcRxBuff[i]);
      printf("]\r\n");
#endif
      /* Set the rx data ready flag */
      xRxDataReadyFlag=SET;
      
      SdkEvalLedToggle(LED2);
    }
    if(xIrqStatus.IRQ_RX_DATA_DISC)
    {
      printf("B data discarded\n\r");
      SdkEvalLedToggle(LED3);
    }
    
    /* Clear the EXTI line flag */
    EXTI_ClearITPendingBit(M2S_GPIO_3_EXTI_LINE);
  }
}
Пример #7
0
void M2S_GPIO_0_EXTI_IRQ_HANDLER(void)
{
  if(EXTI_GetFlagStatus(M2S_GPIO_0_EXTI_LINE))
  {
    SpiritIrqGetStatus(&xIrqStatus); // Get the IRQ status
          if(xIrqStatus.IRQ_RX_DATA_DISC)  // Check the SPIRIT1 RX_DATA_DISC IRQ flag 
        {
  //        SdkEvalLedToggle(LED_YELLOW);  // IRQ: Spirit1 RX data discarded (upon filtering)
        }
    // Check the SPIRIT TX_DATA_SENT IRQ flag
    if(xIrqStatus.IRQ_TX_DATA_SENT)
    {
      // Set the xTxDoneFlag to manage processing of this event in the main()  
      xTxDoneFlag = SET;
      SdkEvalLedToggle(LED_GREEN);
    }
    if(xIrqStatus.IRQ_TX_FIFO_ERROR)
    {
      //   
      xTxDoneFlag = SET;
//      SdkEvalLedToggle(LED_GREEN);
    }
    if(xIrqStatus.IRQ_RX_DATA_READY)
    {
      command_size = SpiritLinearFifoReadNumElementsRxFifo();  //get the size of received command
      SpiritSpiReadLinearFifo(command_size, command_buffer);    //read the buffer
      SpiritCmdStrobeFlushRxFifo();     //flush the RX fifo
      SdkEvalLedToggle(LED_YELLOW);
//      SpiritCmdStrobeRx();
      command_pending = 1;
    }
    
    // Clear the EXTI line flag
   
    EXTI_ClearFlag(M2S_GPIO_0_EXTI_LINE);
  }
} // end of M2S_GPIO_0_EXTI_IRQ_HANDLER()
void M2S_GPIO_3_EXTI_IRQ_HANDLER(void)
#endif
{
  /* Check the flag status of EXTI line */
  if(EXTI_GetITStatus(M2S_GPIO_3_EXTI_LINE))
  {    
    /* Check the SPIRIT TX_DATA_SENT IRQ flag */
    if(SpiritIrqCheckFlag(TX_DATA_SENT))
    {
      /* set the tx_done_flag to manage the event in the main() */
      xTxDoneFlag = SET;
      
      /* toggle LED1 */
      SdkEvalLedToggle(LED1);
    }
    
    /* Clear the EXTI line flag */
    EXTI_ClearITPendingBit(M2S_GPIO_3_EXTI_LINE);
  }
  
}
Пример #9
0
/**
 * @brief  System main function.
 * @param  None
 * @retval None
 */
void main (void)
{
     
  //*** DiZiC SPIRIT1 Demo
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);   // Use STM32L1xx_flash.icf
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE );
  SdkEvalIdentification();
  SdkStartSysTick();
  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  SdkEvalLedInit(LED1);
  SdkEvalLedInit(LED2);
  SdkEvalM2SGpioInit(M2S_GPIO_SDN,M2S_MODE_GPIO_OUT);
  SpiritSpiInit();
  WUKPIN1_Init();
  /*********************/

  /* Allow access to RTC Domain */
//   PWR_RTCAccessCmd(ENABLE);

  /* Clear WakeUp flag */
//  PWR_ClearFlag(PWR_FLAG_WU);

  /* Check if the StandBy flag is set */
//  if (PWR_GetFlagStatus(PWR_FLAG_SB) != RESET)
//  {
    /* Clear StandBy flag */
//    PWR_ClearFlag(PWR_FLAG_SB);

    /* Wait for RTC APB registers synchronisation */
//    RTC_WaitForSynchro();
    /* No need to configure the RTC as the RTC config(clock source, enable,
       prescaler,...) are kept after wake-up from STANDBY */
 

//  }
  
   
 
    /* RTC Configuration */
  
    /* Reset RTC Domain */
//    RCC_RTCResetCmd(ENABLE);
//    RCC_RTCResetCmd(DISABLE);

    /* Enable the LSE OSC */
//     RCC_LSICmd(ENABLE);
    
    /* Wait till LSE is ready */
//    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
//    {}

    /* Select the RTC Clock Source */
//    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

    /* Enable the RTC Clock */
//    RCC_RTCCLKCmd(ENABLE);


    /* Wait for RTC APB registers synchronisation */
//    RTC_WaitForSynchro();
    
 
   /* RTC domain*/  
//  RTC_WakeUpCmd(DISABLE);
  
//  RTC_ITConfig(RTC_IT_WUT , ENABLE);
//  RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div4); 
//  RTC_SetWakeUpCounter(0xFF00);
  
//  PWR_WakeUpPinCmd(PWR_WakeUpPin_1 , DISABLE);
  
  
  
#ifdef USE_VCOM
  /* VC config */
 // SdkEvalVCInit();
 // while(bDeviceState != CONFIGURED);
#endif

  /* Spirit ON */
  SpiritEnterShutdown();
  SpiritExitShutdown();
  
  
  SpiritManagementIdentificationRFBoard();
  SdkEvalM2SGpioInit(M2S_GPIO_0,M2S_MODE_EXTI_IN);
  
  /* Spirit IRQ config */
  SpiritGpioInit(&xGpioIRQ);
  SdkEvalM2SGpioInterruptCmd(M2S_GPIO_0,0x0F,0x0F,ENABLE);
  
  //*** SdkEvalLedOn(LED1);
  /* Spirit Radio config */
  SpiritRadioInit(&xRadioInit);
  
  /* Spirit Packet config */
  SpiritPktBasicInit(&xBasicInit);
  SpiritPktBasicAddressesInit(&xAddressInit);

  /* Spirit IRQs enable */
  SpiritIrqDeInit(&xIrqStatus);
  SpiritIrq(RX_DATA_DISC,S_ENABLE);
  SpiritIrq(RX_DATA_READY,S_ENABLE);
  SpiritIrq(TX_DATA_SENT , S_ENABLE);

  

  /* payload length config */
  SpiritPktBasicSetPayloadLength(512);

  /* enable SQI check */
  SpiritQiSetSqiThreshold(SQI_TH_0);
  SpiritQiSqiCheck(S_ENABLE);

  /* RX timeout config */
  SpiritTimerSetRxTimeoutMs(200.0);
  SpiritTimerSetRxTimeoutStopCondition(SQI_ABOVE_THRESHOLD);

  /* IRQ registers blanking */
  SpiritIrqClearStatus();

  /* RX command */
  SpiritCmdStrobeRx();
  
  
  
 // PWR_PVDCmd(DISABLE);
 // RCC_MSIRangeConfig(RCC_MSIRange_0);
 // RCC_AdjustMSICalibrationValue(0x00);
 // RCC_MSICmd(DISABLE);
 // RCC_HSICmd(DISABLE);
//  PWR_EnterSTOPMode(PWR_Regulator_LowPower , PWR_STOPEntry_WFI);
//  PWR_UltraLowPowerCmd(ENABLE);
//  PWR_FastWakeUpCmd(ENABLE);  
 
////////////////////////////////////////////////////////////////////////////////
// 	ErrorStatus HSE_Status;
//	RCC_HSEConfig(RCC_HSE_ON);
//	HSE_Status = RCC_WaitForHSEStartUp();
//	FLASH_SetLatency(FLASH_Latency_1);
//	FLASH_PrefetchBufferCmd(ENABLE);
//	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//	RCC_HCLKConfig(RCC_SYSCLK_Div1);
//	RCC_PLLConfig(RCC_PLLSource_HSE, RCC_PLLMul_12, RCC_PLLDiv_3);
//	RCC_PCLK1Config(RCC_HCLK_Div1);
//	RCC_PCLK2Config(RCC_HCLK_Div1);
 ///////////////////////////////////////////////////////////////////////////////// 
  
  
  /* infinite loop */
  while (1){
    
 printf("123");
    SpiritCmdStrobeRx();
        
   
   if(PressButtom)
    {
      PressButtom = FALSE;
      
      NOPdelay(2000);
    
     // fit the TX FIFO 
      SpiritCmdStrobeFlushTxFifo();
      SpiritSpiWriteLinearFifo(500, vectcTxBuff2);
    
     // send the TX command 
     SpiritCmdStrobeTx();
    
    // wait for TX done 
     SdkEvalLedToggle(LED_GREEN);
     while(!xTxDoneFlag);
     SdkEvalLedToggle(LED_GREEN);
     xTxDoneFlag = RESET; 
     
          // fit the TX FIFO 
      SpiritCmdStrobeFlushTxFifo();
      SpiritSpiWriteLinearFifo(500, vectcTxBuff2);
    
     // send the TX command 
     SpiritCmdStrobeTx();
    
    // wait for TX done 
     SdkEvalLedToggle(LED_GREEN);
     while(!xTxDoneFlag);
     SdkEvalLedToggle(LED_GREEN);
     xTxDoneFlag = RESET; 
     
          // fit the TX FIFO 
      SpiritCmdStrobeFlushTxFifo();
      SpiritSpiWriteLinearFifo(500, vectcTxBuff2);
    
     // send the TX command 
     SpiritCmdStrobeTx();
    
    // wait for TX done 
     SdkEvalLedToggle(LED_GREEN);
     while(!xTxDoneFlag);
     SdkEvalLedToggle(LED_GREEN);
     xTxDoneFlag = RESET; 
     
     
    }
   
      
    
  }

}
void M2S_GPIO_3_EXTI_IRQ_HANDLER(void)
#endif
{
  /* Check the flag status of EXTI line */
  if(EXTI_GetITStatus(M2S_GPIO_3_EXTI_LINE)){

	/* Get the IRQ status */
    SpiritIrqGetStatus(&xIrqStatus);

    /* Check the SPIRIT RX_DATA_DISC IRQ flag */
    if(xIrqStatus.IRQ_RX_DATA_DISC)
    {
      SpiritCmdStrobeRx()
      SdkEvalLedToggle(LED1);
    }

    /* Check the SPIRIT TX_DATA_SENT IRQ flag for the acknoledgement */
    if(xIrqStatus.IRQ_TX_DATA_SENT)
    {
//      SdkEvalLedToggle(LED1);

#ifdef PIGGYBACKING
      SpiritSpiWriteLinearFifo(20, vectcTxBuff);
      printf("Loaded piggybacking data: [");

      for(uint8_t i=0 ; i<20 ; i++)
        printf("%d ", vectcTxBuff[i]);
      printf("]\n\r");
#endif

      SpiritCmdStrobeRx();
    }

    /* Check the SPIRIT RX_DATA_READY IRQ flag */
    if(xIrqStatus.IRQ_RX_DATA_READY)
    {
    	/* Get the RX FIFO size */
    	cRxData=SpiritLinearFifoReadNumElementsRxFifo();

    	/* Read the RX FIFO */
    	SpiritSpiReadLinearFifo(cRxData, vectcRxBuff);

    	/* Flush the RX FIFO */
    	SpiritCmdStrobeFlushRxFifo();

      /*  A simple way to control if the received data sequence is correct (in this case LED2 will toggle) */
      {
        SpiritBool correct=S_TRUE;

        for(uint8_t i=0 ; i<cRxData ; i++)
          if(vectcRxBuff[i] != i+1)
            correct=S_FALSE;

        if(correct)
          SdkEvalLedToggle(LED2);
      }
#ifdef USE_VCOM
      /* print the received data */
      printf("B data received: [");

      for(uint8_t i=0 ; i<cRxData ; i++)
        printf("%d ", vectcRxBuff[i]);
      printf("]\r\n");

#endif
    }
    
    /* Clear the EXTI line flag */
    EXTI_ClearITPendingBit(M2S_GPIO_3_EXTI_LINE);
  }

}
/**
* @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);
    }
    
    
    
  }
  
}
void M2S_GPIO_3_EXTI_IRQ_HANDLER(void)
#endif
{
  /* Check the flag status of EXTI line */
  if(EXTI_GetITStatus(M2S_GPIO_3_EXTI_LINE)){
    
    /* Get the IRQ status */
    SpiritIrqGetStatus(&xIrqStatus);
    
    /* Check the SPIRIT RX_DATA_DISC IRQ flag */
    if(xIrqStatus.IRQ_RX_DATA_DISC)
    {
      /* toggle LED1 */
      SdkEvalLedToggle(LED1);
      
      /* RX command - to ensure the device will be ready for the next reception */
      SpiritCmdStrobeRx();
    }
    
    /* Check the SPIRIT RX_DATA_READY IRQ flag */
    if(xIrqStatus.IRQ_RX_DATA_READY)
    {
      /* Get the RX FIFO size */
      cRxData=SpiritLinearFifoReadNumElementsRxFifo();
      
      /* Read the RX FIFO */
      SpiritSpiReadLinearFifo(cRxData, vectcRxBuff);
      
      /* Flush the RX FIFO */
      SpiritCmdStrobeFlushRxFifo();
      
      
      /*  A simple way to check if the received data sequence is correct (in this case LED5 will toggle) */
      {
        SpiritBool xCorrect=S_TRUE;
        
        for(uint8_t i=0 ; i<cRxData ; i++)
          if(vectcRxBuff[i] != i+1)
            xCorrect=S_FALSE;
        
        if(xCorrect) {
          /* toggle LED2 */
          SdkEvalLedToggle(LED2);
//#ifdef USE_VCOM
//          printf("DATA CORRECT\r\n");
//#endif
       }
    }
     /* RX command - to ensure the device will be ready for the next reception */
    SpiritCmdStrobeRx();
 
//#ifdef USE_VCOM
//      /* print the received data */
//      printf("B data received: [");
//      for(uint8_t i=0 ; i<cRxData ; i++)
//        printf("%d ", vectcRxBuff[i]);
//      printf("]\r\n");
//#endif

    }
    
    /* Clear the EXTI line flag */
    EXTI_ClearITPendingBit(M2S_GPIO_3_EXTI_LINE);
    
  }
}
int main(void)
{
    //int ret;
    
    NVIC_SetVectorTable(NVIC_VectTab_FLASH,VECTOR_TABLE_BASE_ADDRESS);
    
    /* Identify the BlueNRG platform */
    SdkEvalIdentification();

    RCC_Configuration();
    /* Basic button init function for handling application jumping */
    Configure_Button();
 
#if 0 /* TBR */
    PWR_PVDCmd(DISABLE);
    
    /* Disable FLASH during Sleep  */
    FLASH_SLEEPPowerDownCmd(ENABLE);
    
    /* Enable Ultra low power mode */
    PWR_UltraLowPowerCmd(ENABLE);
    
    PWR_FastWakeUpCmd(DISABLE);
#endif 
    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    
    Clock_Init();
    
    HCI_Init();
 
    /* Init SPI interface */
    SdkEvalSpiInit(SPI_MODE_EXTI);
    /* Reset BlueNRG SPI interface */
    BlueNRG_RST();
    
    /* Init leds */
    SdkEvalLedInit(LED1);
    SdkEvalLedInit(LED2);

    {
        tHalUint8 bdaddr[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02};

        aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN,
                                        bdaddr);
    }
    
    aci_gatt_init();    
    
    {
        uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
        aci_gap_init(1, &service_handle, &dev_name_char_handle, &appearance_char_handle);        
    }
    
#if 0/* TBR */
    aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED,
                                       OOB_AUTH_DATA_ABSENT,
                                       NULL,
                                       7,
                                       16,
                                       USE_FIXED_PIN_FOR_PAIRING,
                                       123456,
                                       BONDING);
#endif 
    
    //PRINTF("BLE Stack Initialized.\n");
    
#ifdef ST_OTA_BTL
    /* Add OTA bootloader service */
    Add_Btl_Service();
#endif
    
    /* -2 dBm output power */
    aci_hal_set_tx_power_level(1,4);
    
    while(1)
    {
#ifdef ST_OTA_BTL
      static tClockTime startTime = 0;

      if (Clock_Time() - startTime >led_blinking_rate)
      {    
        /* LED D1 is toggling on OTA_Service Manager */
        SdkEvalLedToggle(LED1);     
        startTime = Clock_Time();
      }
#endif /* end ST_OTA_BTL */

        HCI_Process();
        
        if(set_connectable){
            setConnectable();
            set_connectable = 0;
        }
        
      /* Use button to switch to the basic Reset Manager */
      if (GPIO_ReadInputDataBit(ButtonPort,ButtonPin) == RESET)
      {
        /* Add delay to avoid conlict with DFU activation */
        Clock_Wait(2000);
        
        /* Check if an application has been loaded previously through OTA service
           manager */
        if (*((uint32_t*) NEW_APP_MEM_INFO)!= 0) 
          /* Service Manager will jump to the Application previously loaded at
           address  APPLICATION_JUMP_ADDRESS */
          Switch_To_OTA_Service_Manager_Application(APPLICATION_JUMP_ADDRESS);
      }
    }
}