Exemplo n.º 1
0
PUBLIC void vJenie_CbInit(bool_t bWarmStart)
{

    vUtils_Init();

    if(bWarmStart==FALSE)
    {
        (void)u32AHI_Init();
        sHomeData.bStackReady=FALSE;
        /* Initialise buttons, LEDs and program variables */
        vInitEndpoint();
        /* Set DIO for buttons and LEDs */
        vLedControl(LED1, FALSE);
        vLedControl(LED2, FALSE);
        vLedInitRfd();
        vButtonInitRfd();

        #ifdef NO_SLEEP
            vAHI_WakeTimerEnable(E_AHI_WAKE_TIMER_1, TRUE);
        #endif

        /* Set SW1(dio9) to input */
        vAHI_DioSetDirection(E_AHI_DIO9_INT, 0);
        /* set interrupt for DIO9 to occur on button release - rising edge */
        vAHI_DioInterruptEdge(E_AHI_DIO9_INT, 0);
        /* enable interrupt for DIO9 */
        vAHI_DioInterruptEnable(E_AHI_DIO9_INT, 0);

        /* Set SW2(dio10) to input */
        vAHI_DioSetDirection(E_AHI_DIO10_INT, 0);
        /* set interrupt for DIO9 to occur on button release - rising edge */
        vAHI_DioInterruptEdge(E_AHI_DIO10_INT, 0);
        /* enable interrupt for DIO9 */
        vAHI_DioInterruptEnable(E_AHI_DIO10_INT, 0);

        /* Set up peripheral hardware */
        vALSreset();
        vHTSreset();

        /* Start ALS now: it automatically keeps re-sampling after this */
        vALSstartReadChannel(0);

        sHomeData.eAppState = E_STATE_REGISTER;
        switch(eJenie_Start(E_JENIE_END_DEVICE))        /* Start network as end device */
        {
        case E_JENIE_SUCCESS:
            #ifdef DEBUG
                vUtils_Debug("Jenie Started");
            #endif
            #ifdef HIGH_POWER
                /* Set high power mode */
                eJenie_RadioPower(18, TRUE);
            #endif
            break;

        case E_JENIE_ERR_UNKNOWN:
        case E_JENIE_ERR_INVLD_PARAM:
        case E_JENIE_ERR_STACK_RSRC:
        case E_JENIE_ERR_STACK_BUSY:

        default:
            /* Do something on failure?? */
            break;
        }
    }else{

        /* Set up peripheral hardware */
        vALSreset();
        vHTSreset();

        /* Start ALS now: it automatically keeps re-sampling after this */
        vALSstartReadChannel(0);

        switch(eJenie_Start(E_JENIE_END_DEVICE))        /* Start network as end device */
        {
        case E_JENIE_SUCCESS:
            #ifdef HIGH_POWER
                /* Set high power mode */
                eJenie_RadioPower(18, TRUE);
            #endif
            break;

        case E_JENIE_ERR_UNKNOWN:
        case E_JENIE_ERR_INVLD_PARAM:
        case E_JENIE_ERR_STACK_RSRC:
        case E_JENIE_ERR_STACK_BUSY:

        default:
            /* Do something on failure?? */
            break;
        }

    }
    /* set watchdog to long timeout - override setting in JenNet startup */
    #ifdef WATCHDOG_ENABLED
       vAHI_WatchdogStart(254);
    #endif

}
Exemplo n.º 2
0
static void
main_loop(void)
{
  int r;
  clock_time_t time_to_etimer;
  rtimer_clock_t ticks_to_rtimer;

  while(1) {
    do {
      /* Reset watchdog. */
      watchdog_periodic();
      r = process_run();
    } while(r > 0);
    /*
     * Idle processing.
     */
    watchdog_stop();

#if DCOSYNCH_CONF_ENABLED
    /* Calibrate the DCO every DCOSYNCH_PERIOD
     * if we have more than 500uSec until next rtimer
     * PS: Calibration disables interrupts and blocks for 200uSec.
     *  */
    if(clock_seconds() - last_dco_calibration_time > DCOSYNCH_PERIOD) {
      if(rtimer_arch_time_to_rtimer() > RTIMER_SECOND / 2000) {
        /* PRINTF("ContikiMain: Calibrating the DCO\n"); */
        eAHI_AttemptCalibration();
        /* Patch to allow CpuDoze after calibration */
        vREG_PhyWrite(REG_PHY_IS, REG_PHY_INT_VCO_CAL_MASK);
        last_dco_calibration_time = clock_seconds();
      }
    }
#endif /* DCOSYNCH_CONF_ENABLED */

    /* flush standard output before sleeping */
    uart_driver_flush(E_AHI_UART_0, TRUE, FALSE);

    /* calculate the time to the next etimer and rtimer */
    time_to_etimer = clock_arch_time_to_etimer();
    ticks_to_rtimer = rtimer_arch_time_to_rtimer();

#if JN516X_SLEEP_ENABLED
    /* we can sleep only up to the next rtimer/etimer */
    rtimer_clock_t max_sleep_time = ticks_to_rtimer;
    if(max_sleep_time >= JN516X_MIN_SLEEP_TIME) {
      /* also take into account etimers */
      uint64_t ticks_to_etimer = ((uint64_t)time_to_etimer * RTIMER_SECOND) / CLOCK_SECOND;
      max_sleep_time = MIN(ticks_to_etimer, ticks_to_rtimer);
    }

    if(max_sleep_time >= JN516X_MIN_SLEEP_TIME) {
      max_sleep_time -= JN516X_SLEEP_GUARD_TIME;
      /* bound the sleep time to 1 second */
      max_sleep_time = MIN(max_sleep_time, JN516X_MAX_SLEEP_TIME);

#if !RTIMER_USE_32KHZ
      /* convert to 32.768 kHz oscillator ticks */
      max_sleep_time = (uint64_t)max_sleep_time * JN516X_XOSC_SECOND / RTIMER_SECOND;
#endif
      vAHI_WakeTimerEnable(WAKEUP_TIMER, TRUE);
      /* sync with the tick timer */
      WAIT_FOR_EDGE(sleep_start);
      sleep_start_ticks = u32AHI_TickTimerRead();

      vAHI_WakeTimerStartLarge(WAKEUP_TIMER, max_sleep_time);
      ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_EXTRA_LPM);
      vAHI_Sleep(E_AHI_SLEEP_OSCON_RAMON);
    } else {
#else
    {
#endif /* JN516X_SLEEP_ENABLED */
      clock_arch_schedule_interrupt(time_to_etimer, ticks_to_rtimer);
      ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
      vAHI_CpuDoze();
      watchdog_start();
      ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
    }
  }
}
/*---------------------------------------------------------------------------*/
void
AppColdStart(void)
{
  /* After reset or sleep with memory off */
  main();
}
/*---------------------------------------------------------------------------*/
void
AppWarmStart(void)
{
  /* Wakeup after sleep with memory on.
   * Need to initialize devices but not the application state.
   * Note: the actual time this function is called is
   * ~8 ticks (32kHz timer) later than the scheduled sleep end time.
   */
  uint32_t sleep_ticks;
  uint64_t sleep_end;
  rtimer_clock_t sleep_ticks_rtimer;

  clock_arch_calibrate();
  leds_init();
  uart0_init(UART_BAUD_RATE); /* Must come before first PRINTF */
  NETSTACK_RADIO.init();
  watchdog_init();
  watchdog_stop();

  WAIT_FOR_EDGE(sleep_end);
  sleep_ticks = (uint32_t)(sleep_start - sleep_end) + 1;

#if RTIMER_USE_32KHZ
  sleep_ticks_rtimer = sleep_ticks;
#else
  {
    static uint32_t remainder;
    uint64_t t = (uint64_t)sleep_ticks * RTIMER_SECOND + remainder;
    sleep_ticks_rtimer = (uint32_t)(t / JN516X_XOSC_SECOND);
    remainder = t - sleep_ticks_rtimer * JN516X_XOSC_SECOND;
  }
#endif

  /* reinitialize rtimers */
  rtimer_arch_reinit(sleep_start_ticks, sleep_ticks_rtimer);

  ENERGEST_SWITCH(ENERGEST_TYPE_EXTRA_LPM, ENERGEST_TYPE_CPU);

  watchdog_start();

  /* reinitialize clock */
  clock_arch_init(1);
  /* schedule etimer interrupt */
  clock_arch_schedule_interrupt(clock_arch_time_to_etimer(), rtimer_arch_time_to_rtimer());

#if DCOSYNCH_CONF_ENABLED
  /* The radio is recalibrated on wakeup */
  last_dco_calibration_time = clock_seconds();
#endif

  main_loop();
}