Пример #1
0
void main ()
{
  WDTCTL = WDTPW | WDTHOLD;

#if BSP430_LED - 0
  vBSP430ledInitialize_ni();
  {
    int i;

    vBSP430ledSet(0, 1);
    for (i = 0; i < 20; ++i) {
      vBSP430ledSet(0, -1);
      vBSP430ledSet(1, -1);
      __delay_cycles(BSP430_CLOCK_PUC_MCLK_HZ / 10);
    }
    /* The subsequent vBSP430lpmConfigurePortsForLPM_ni() call should
     * turn off the LED that's still lit. */
  }
#endif

  vBSP430lpmConfigurePortsForLPM_ni();

  __delay_cycles(BSP430_CLOCK_PUC_MCLK_HZ);
  while (1) {
#if (0 <= APP_LPM) && (APP_LPM <= 4)
    BSP430_CORE_LPM_ENTER_NI(lpm_bits[APP_LPM]);
    /* For this application it doesn't matter that GIE is now set */
#endif
  }
}
Пример #2
0
static int
button_isr_ni (const struct sBSP430halISRIndexedChainNode * cb,
               void * context,
               int idx)
{
  hBSP430halPORT hal = (hBSP430halPORT)context;
  volatile sBSP430hplPORTIE * hpl = BSP430_PORT_HAL_GET_HPL_PORTIE(hal);
  sButtonState * sp = (sButtonState *)cb;

  ++sp->count;

  /* Record whether the button is currently pressed based on the edge
   * type we captured (1 means transition to 0 = released, 0 means
   * transition to 1 = pressed).  Configure to detect a state change
   * opposite of the one we just captured, regardless of what that
   * state might be.  This algorithm coupled with interrupts being
   * disabled outside of LPM helps ensure we interleave
   * pressed/released notifications even in the presence of
   * bounces. */
  sp->in_mask = (hpl->ies & sp->bit) ^ sp->bit;
  hpl->ies ^= sp->bit;
  vBSP430ledSet(0, -1);

  /* Wakeup.  Whether this clears #GIE depends on
   * #configBSP430_CORE_LPM_EXIT_CLEAR_GIE */
  return BSP430_HAL_ISR_CALLBACK_EXIT_LPM;
}
Пример #3
0
void main ()
{
  hBSP430halPORT b0hal = hBSP430portLookup(BSP430_PLATFORM_BUTTON0_PORT_PERIPH_HANDLE);
  volatile sBSP430hplPORTIE * b0hpl;
  int b0pin = iBSP430portBitPosition(BSP430_PLATFORM_BUTTON0_PORT_BIT);

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();

  cprintf("\nbutton " __DATE__ " " __TIME__ "\n");

  cprintf("There's supposed to be a button at %s.%u\n",
          xBSP430portName(BSP430_PLATFORM_BUTTON0_PORT_PERIPH_HANDLE),
          b0pin);
  if (! b0hal) {
    cprintf("Whoops, guess it's not really there\n");
    return;
  }
  b0hpl = BSP430_PORT_HAL_GET_HPL_PORTIE(b0hal);
  button_state.button_cb.next_ni = b0hal->pin_cbchain_ni[b0pin];
  b0hal->pin_cbchain_ni[b0pin] = &button_state.button_cb;
  b0hpl->sel &= ~BSP430_PLATFORM_BUTTON0_PORT_BIT;
  b0hpl->dir &= ~BSP430_PLATFORM_BUTTON0_PORT_BIT;
#if (BSP430_PORT_SUPPORTS_REN - 0)
  BSP430_PORT_HAL_SET_REN(b0hal, BSP430_PLATFORM_BUTTON0_PORT_BIT, BSP430_PORT_REN_PULL_UP);
#endif /* BSP430_PORT_SUPPORTS_REN */
  if (b0hpl->in & BSP430_PLATFORM_BUTTON0_PORT_BIT) {
    button_state.in_mask = BSP430_PLATFORM_BUTTON0_PORT_BIT;
    b0hpl->ies |= BSP430_PLATFORM_BUTTON0_PORT_BIT;
  } else {
    button_state.in_mask = 0;
    b0hpl->ies &= ~BSP430_PLATFORM_BUTTON0_PORT_BIT;
  }
  b0hpl->ifg &= ~BSP430_PLATFORM_BUTTON0_PORT_BIT;
  b0hpl->ie |= BSP430_PLATFORM_BUTTON0_PORT_BIT;
  cprintf("Button is configured.  Try pressing it.  No debouncing is done.\n");
#if ! (configBSP430_CORE_LPM_EXIT_CLEAR_GIE - 0)
  cprintf("WARNING: Interrupts remain enabled after wakeup\n");
#endif /* configBSP430_CORE_LPM_EXIT_CLEAR_GIE */

  vBSP430ledSet(0, 1);

  while (1) {
    static const char * state_str[] = { "released", "pressed" };

    cprintf("Count %u, in mask 0x%02x: %s\n", button_state.count, button_state.in_mask, state_str[!button_state.in_mask]);

    /* Note that we've never turned interrupts on, but
     * BSP430_CORE_LPM_ENTER_NI() does so internally so the ISR can be
     * executed.  Whether they are cleared before returning to this
     * loop depends on #configBSP430_CORE_LPM_EXIT_CLEAR_GIE. */
    BSP430_CORE_LPM_ENTER_NI(LPM0_bits);
#if ! (configBSP430_CORE_LPM_EXIT_CLEAR_GIE - 0)
    /* Infrastructure didn't clear this, so we should */
    BSP430_CORE_DISABLE_INTERRUPT();
#endif /* configBSP430_CORE_LPM_EXIT_CLEAR_GIE */
  }
}
Пример #4
0
void main ()
{
  const sColor * cp = colors;
  const sColor * const ecp = colors + sizeof(colors) / sizeof(*colors);

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();
  while (1) {
    if (0 > cp->idx) {
      cprintf("%s is not available\n", cp->name);
    } else {
      vBSP430ledSet(cp->idx, 1);
      cprintf("%s lit; mask 0x%x\n", cp->name, read_leds());
      BSP430_CORE_DELAY_CYCLES(5 * BSP430_CLOCK_NOMINAL_MCLK_HZ);
      vBSP430ledSet(cp->idx, 0);
    }
    if (++cp == ecp) {
      cp = colors;
    }
  }
}
Пример #5
0
static int
hh10d_1Hz_isr_ni (const struct sBSP430halISRIndexedChainNode *cb,
                  void *context,
                  int idx)
{
  volatile struct sHH10D * hh10d = (struct sHH10D *)cb;
  unsigned int capture;
  hBSP430halTIMER timer = (hBSP430halTIMER)context;
  vBSP430ledSet(0, -1);

  /* Record the HH10D counter, schedule the next wakeup, then return
   * waking the MCU and inhibiting further interrupts when active. */
  capture = hh10d->freq_timer->r;
  hh10d->last_period_count = capture - hh10d->last_capture;
  hh10d->last_capture = capture;
  timer->hpl->ccr[idx] += hh10d->sample_duration_utt;
  return LPM4_bits | GIE;
}
Пример #6
0
static int
mux_alarm_callback (sBSP430timerMuxSharedAlarm * shared,
                    sBSP430timerMuxAlarm * alarm)
{
  sExampleMuxAlarm * map = (sExampleMuxAlarm *)alarm;
  int rc;
  int rv = BSP430_HAL_ISR_CALLBACK_EXIT_LPM;
  uBSP430eventAnyType u;

  alarm->setting_tck += map->interval_utt;
  rc = iBSP430timerMuxAlarmAdd_ni(shared, alarm);
  statistics_v.counter[map - mux_alarms] += 1;
  statistics_v.mux_intr_ct += 1;
  if (map->tag) {
    u.p = map;
    xBSP430eventRecordEvent_ni(map->tag, !!rc, &u);
  } else if (map->flag) {
    vBSP430eventFlagsSet_ni(map->flag);
  } else {
    vBSP430ledSet(BSP430_LED_RED, -1);
    rv = 0;
  }
  return rv;
}
Пример #7
0
void main ()
{
#if (BSP430_CONSOLE - 0)
  const char * help;
  unsigned long smclk_Hz;
  unsigned long aclk_Hz;
#endif /* BSP430_CONSOLE */

  /* First thing you do in main is configure the platform. */
  vBSP430platformInitialize_ni();

  /* If we support a console, dump out a bunch of configuration
   * information. */
#if (BSP430_CONSOLE - 0)
  (void)iBSP430consoleInitialize();
  cputtext("\nclocks " __DATE__ " " __TIME__ "\n");
  cputtext("\nBSP430_PLATFORM_BOOT_CONFIGURE_LFXT1: ");
  cputu(BSP430_PLATFORM_BOOT_CONFIGURE_LFXT1, 10);
  cputtext("\nBSP430_CLOCK_LFXT1_STABILIZATION_DELAY_CYCLES: ");
  cputul(BSP430_CLOCK_LFXT1_STABILIZATION_DELAY_CYCLES, 10);
  cputtext("\nBSP430_PLATFORM_BOOT_LFXT1_DELAY_SEC: ");
  cputu(BSP430_PLATFORM_BOOT_LFXT1_DELAY_SEC, 10);
  cputtext("\nBSP430_PLATFORM_BOOT_CONFIGURE_CLOCKS: ");
  cputu(BSP430_PLATFORM_BOOT_CONFIGURE_CLOCKS, 10);
#if defined(__MSP430_HAS_BC2__)
#if (configBSP430_BC2_TRIM_TO_MCLK - 0)
  cputtext("\nconfigBSP430_BC2_TRIM_TO_MCLK: 1");
#else /* configBSP430_BC2_TRIM_TO_MCLK */
  cputtext("\nconfigBSP430_BC2_TRIM_TO_MCLK: 0");
#endif /* configBSP430_BC2_TRIM_TO_MCLK */
#if (BSP430_BC2_TRIM_TO_MCLK - 0)
  cputtext("\nBSP430_BC2_TRIM_TO_MCLK: 1");
#else /* BSP430_BC2_TRIM_TO_MCLK */
  cputtext("\nBSP430_BC2_TRIM_TO_MCLK: 0");
#endif /* BSP430_BC2_TRIM_TO_MCLK */
#endif /* BC2 */
#if defined(__MSP430_HAS_UCS__) || defined(__MSP430_HAS_UCS_RF__)
#if (configBSP430_UCS_TRIM_DCOCLKDIV - 0)
  cputtext("\nconfigBSP430_UCS_TRIM_DCOCLKDIV: 1");
#else /* configBSP430_UCS_TRIM_DCOCLKDIV */
  cputtext("\nconfigBSP430_UCS_TRIM_DCOCLKDIV: 0");
#endif /* configBSP430_UCS_TRIM_DCOCLKDIV */
#if (BSP430_UCS_TRIM_DCOCLKDIV - 0)
  cputtext("\nBSP430_UCS_TRIM_DCOCLKDIV: 1");
#else /* BSP430_UCS_TRIM_DCOCLKDIV */
  cputtext("\nBSP430_UCS_TRIM_DCOCLKDIV: 0");
#endif /* BSP430_UCS_TRIM_DCOCLKDIV */
#endif /* UCS */

  cputtext("\nBSP430_CLOCK_PUC_MCLK_HZ: ");
  cputul(BSP430_CLOCK_PUC_MCLK_HZ, 10);
  cputtext("\nBSP430_CLOCK_NOMINAL_MCLK_HZ: ");
  cputul(BSP430_CLOCK_NOMINAL_MCLK_HZ, 10);
  cputtext("\nBSP430_CLOCK_LFXT1_IS_FAULTED_NI(): ");
  cputu(BSP430_CLOCK_LFXT1_IS_FAULTED_NI(), 10);
  cputtext("\nBSP430_CLOCK_NOMINAL_VLOCLK_HZ: ");
  cputu(BSP430_CLOCK_NOMINAL_VLOCLK_HZ, 10);
  cputtext("\nBSP430_CLOCK_NOMINAL_XT1CLK_HZ: ");
  cputul(BSP430_CLOCK_NOMINAL_XT1CLK_HZ, 10);
#if defined(BSP430_CLOCK_NOMINAL_XT2CLK_HZ)
  cputtext("\nBSP430_PLATFORM_BOOT_CONFIGURE_XT2: ");
  cputu(BSP430_PLATFORM_BOOT_CONFIGURE_XT2, 10);
  cputtext("\nBSP430_CLOCK_XT2_IS_FAULTED_NI(): ");
  cputu(BSP430_CLOCK_XT2_IS_FAULTED_NI(), 10);
  cputtext("\nBSP430_CLOCK_NOMINAL_XT2CLK_HZ: ");
  cputul(BSP430_CLOCK_NOMINAL_XT2CLK_HZ, 10);
#endif /* BSP430_CLOCK_NOMINAL_XT2CLK_HZ */
  cputtext("\nulBSP430clockMCLK_Hz_ni(): ");
  cputul(ulBSP430clockMCLK_Hz_ni(), 10);
  cputtext("\nBSP430_PLATFORM_BOOT_SMCLK_DIVIDING_SHIFT: ");
  cputi(BSP430_PLATFORM_BOOT_SMCLK_DIVIDING_SHIFT, 10);
  cputtext("\nulBSP430clockSMCLK_Hz_ni(): ");
  smclk_Hz = ulBSP430clockSMCLK_Hz_ni();
  cputul(smclk_Hz, 10);
  cputtext("\nBSP430_PLATFORM_BOOT_ACLK_DIVIDING_SHIFT: ");
  cputi(BSP430_PLATFORM_BOOT_ACLK_DIVIDING_SHIFT, 10);
  cputtext("\nulBSP430clockACLK_Hz_ni(): ");
  aclk_Hz = ulBSP430clockACLK_Hz_ni();
  cputul(aclk_Hz, 10);

#if (BSP430_TIMER_CCACLK - 0)
  if (1000000UL <= aclk_Hz) {
    cputtext("\nUnable to use high-speed ACLK for differential timing of SMCLK");
  } else {
    do {
      const unsigned int SAMPLE_PERIOD_ACLK = 10;
      volatile sBSP430hplTIMER * tp = xBSP430hplLookupTIMER(BSP430_TIMER_CCACLK_PERIPH_HANDLE);
      unsigned int cc_delta;
      unsigned long aclk_rel_smclk_Hz;
      unsigned long smclk_rel_aclk_Hz;

      if (! tp) {
        cputtext("\nUnable to access configured CCACLK timer");
        break;
      }
      /* Capture the SMCLK ticks between adjacent ACLK ticks */
      tp->ctl = TASSEL_2 | MC_2 | TACLR;
      cc_delta = uiBSP430timerCaptureDelta_ni(BSP430_TIMER_CCACLK_PERIPH_HANDLE,
                                              BSP430_TIMER_CCACLK_ACLK_CCIDX,
                                              CM_2,
                                              BSP430_TIMER_CCACLK_ACLK_CCIS,
                                              SAMPLE_PERIOD_ACLK);
      tp->ctl = 0;
      if (-1 == cc_delta) {
        cputtext("\nCCACLK measurement failed");
        break;
      }
      cputchar('\n');
      cputu(SAMPLE_PERIOD_ACLK, 10);
      cputtext(" ticks of ACLK produced ");
      cputu(cc_delta, 10);
      cputtext(" ticks of SMCLK");
      cputtext("\nComparison with measured values:");
      cputtext("\n SMCLK (Hz) (if measured ACLK correct): ");
      smclk_rel_aclk_Hz = (cc_delta * aclk_Hz) / SAMPLE_PERIOD_ACLK;
      cputul(smclk_rel_aclk_Hz, 10);
      cputtext(" (error ");
      cputl(smclk_rel_aclk_Hz - smclk_Hz, 10);
      cputtext(" = ");
      cputl(1000 * labs(smclk_rel_aclk_Hz - smclk_Hz) / smclk_Hz, 10);
      cputtext(" kHz/MHz)");
      cputtext("\n ACLK (Hz) (if measured SMCLK correct): ");
      aclk_rel_smclk_Hz = SAMPLE_PERIOD_ACLK * smclk_Hz / cc_delta;
      cputul(aclk_rel_smclk_Hz, 10);
      cputtext(" (error ");
      cputl(aclk_rel_smclk_Hz - aclk_Hz, 10);
      cputtext(" = ");
      cputl(1000 * labs(aclk_rel_smclk_Hz - aclk_Hz) / aclk_Hz, 10);
      cputtext(" Hz/kHz)");
    } while (0);
  }
#else /* BSP430_TIMER_CCACLK */
  cputtext("\nNo CCACLK timer available for ACLK/SMCLK comparison");
#endif /* BSP430_TIMER_CCACLK */

  cputchar('\n');

#if defined(__MSP430_HAS_BC2__)
  cputtext("\nBC2: DCO ");
  cputu(DCOCTL, 16);
  cputtext(" CTL1  ");
  cputu(BCSCTL1, 16);
  cputtext(" CTL2  ");
  cputu(BCSCTL2, 16);
  cputtext(" CTL3  ");
  cputu(BCSCTL3, 16);

#endif

#if defined(__MSP430_HAS_FLL__) || defined(__MSP430_HAS_FLLPLUS__)
  cprintf("\nFLL: SCF QCTL %02x I0 %02x I1 %02x ; CTL0 %02x CTL1 %02x CTL2 %02x\n",
          SCFQCTL, SCFI0, SCFI1, FLL_CTL0, FLL_CTL1,
#if defined(FLL_CTL2_)
          FLL_CTL2
#else /* FLL_CTL2 */
          ~0
#endif /* FLL_CTL2 */
         );
#endif /* FLL/PLUS */

#if defined(__MSP430_HAS_UCS__) || defined(__MSP430_HAS_UCS_RF__)
  cputtext("\nBSP430_UCS_FLL_SELREF: "
#if SELREF__XT2CLK <= BSP430_UCS_FLL_SELREF
           "XT2CLK"
#elif SELREF__REFOCLK <= BSP430_UCS_FLL_SELREF
           "REFOCLK"
#else /* BSP430_UCS_FLL_SELREF */
           "XT1CLK"
#endif /* BSP430_UCS_FLL_SELREF */
          );
  cprintf("\nUCS RSEL %d DCO %d MOD %d:"
          "\n CTL0 %04x CTL1 %04x CTL2 %04x CTL3 %04x"
          "\n CTL4 %04x CTL5 %04x CTL6 %04x CTL7 %04x",
          0x1F & (UCSCTL1 / DCORSEL0), 0x1F & (UCSCTL0 / DCO0), 0x1F & (UCSCTL0 / MOD0),
          UCSCTL0, UCSCTL1, UCSCTL2, UCSCTL3,
          UCSCTL4, UCSCTL5, UCSCTL6, UCSCTL7);
#endif /* UCS */

#if defined(__MSP430_HAS_CS__) || defined(__MSP430_HAS_CS_A__)
  cprintf("\nCS %s : RSEL %d DCOFSEL %d:"
          "\n CTL0 %04x CTL1 %04x CTL2 %04x CTL3 %04x"
          "\n CTL4 %04x CTL5 %04x CTL6 %04x"
          "\n FRCTL0 %04x",
#if (BSP430_CS_IS_FR57XX - 0)
          "FR57xx"
#endif
#if (BSP430_CS_IS_FR58XX - 0)
          "FR58xx"
#endif
          "", !!(DCORSEL & CSCTL1), 0x07 & (CSCTL1 / DCOFSEL0),
          CSCTL0, CSCTL1, CSCTL2, CSCTL3,
          CSCTL4, CSCTL5, CSCTL6, FRCTL0);
#endif /* CS */

#endif /* BSP430_CONSOLE */

  if (0 == iBSP430platformConfigurePeripheralPins_ni(BSP430_PERIPH_EXPOSED_CLOCKS, 0, 1)) {
#if (BSP430_CONSOLE - 0)
    cputtext("\n\nClock signals exposed:\n ");
    help = NULL;
#ifdef BSP430_PLATFORM_PERIPHERAL_HELP
    help = xBSP430platformPeripheralHelp(BSP430_PERIPH_EXPOSED_CLOCKS, 0);
#endif /* BSP430_PLATFORM_PERIPHERAL_HELP */
    if (NULL == help) {
      help = "Go look at the data sheet and source, because nobody told me where they are";
    }
    cputtext(help);
    cputtext("\nStatus register LPM bits: ");
    cputu(__read_status_register() & BSP430_CORE_LPM_SR_MASK, 16);
    cputtext("\nIFG1 bits: ");
#if defined(__MSP430_HAS_MSP430XV2_CPU__)
    cputu(SFRIFG1, 16);
#else /* CPUX */
    cputu(IFG1, 16);
#endif /* CPUX */
    cputtext(" with OFIFG ");
    cputu(OFIFG, 16);
    cputchar('\n');
#endif /* BSP430_CONSOLE */

    /* Spin here with CPU active.  In LPM0, MCLK is disabled.  Other
     * clocks get disabled at deeper sleep modes; if you fall off the
     * bottom, you might end up in LPM4 with all clocks disabled. */
    while (1) {
      vBSP430ledSet(0, -1);
      BSP430_CORE_WATCHDOG_CLEAR();
      BSP430_CORE_DELAY_CYCLES(BSP430_CLOCK_NOMINAL_MCLK_HZ / 2);
    }
  } else {
#if (BSP430_CONSOLE - 0)
    cputtext("\nFailed to expose clock signals\n");
#endif /* BSP430_CONSOLE */
  }
}
Пример #8
0
void main ()
{
  hBSP430halSERIAL i2c;
  sBSP430bq24210 bq24210;
  union {
    sBQ27510 state;
    uint16_t raw[1];
  } u;
  const int nwords = sizeof(u.state)/sizeof(u.raw[0]);
  unsigned long resample_interval_utt;
  unsigned long resample_wake_utt;
  unsigned int flags;
  int rc;

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();

  cprintf("\nbattpack " __DATE__ " " __TIME__ "\n");

  bq24210.chg_port = xBSP430hplLookupPORT(APP_CHGn_PORT_PERIPH_HANDLE);
  bq24210.en_port = xBSP430hplLookupPORT(APP_ENn_PORT_PERIPH_HANDLE);
  bq24210.pg_port = xBSP430hplLookupPORT(APP_PGn_PORT_PERIPH_HANDLE);
  bq24210.chg_bit = APP_CHGn_PORT_BIT;
  bq24210.en_bit = APP_ENn_PORT_BIT;
  bq24210.pg_bit = APP_PGn_PORT_BIT;

  cprintf("CHGn on %s.%u\n",
          xBSP430portName(xBSP430periphFromHPL(bq24210.chg_port)),
          iBSP430portBitPosition(bq24210.chg_bit));
  cprintf("ENn on %s.%u\n",
          xBSP430portName(xBSP430periphFromHPL(bq24210.en_port)),
          iBSP430portBitPosition(bq24210.en_bit));
  cprintf("PGn on %s.%u\n",
          xBSP430portName(xBSP430periphFromHPL(bq24210.pg_port)),
          iBSP430portBitPosition(bq24210.pg_bit));
  if (! (bq24210.chg_port && bq24210.en_port && bq24210.pg_port)) {
    cprintf("One of the ports is missing\n");
    return;
  }

  /* Charge signal is an input (active low) to the MCU.  Configure as
   * input with internal pull-up. */
  bq24210.chg_port->dir &= ~bq24210.chg_bit;
  bq24210.chg_port->out |= bq24210.chg_bit;
  bq24210.chg_port->ren |= bq24210.chg_bit;

  /* Power-good signal is an input (active low) to the MCU.  Configure
   * as input with internal pull-up. */
  bq24210.pg_port->dir &= ~bq24210.pg_bit;
  bq24210.pg_port->out |= bq24210.pg_bit;
  bq24210.pg_port->ren |= bq24210.pg_bit;

  /* Enable signal is an output (active low) from the MCU.  Start
   * active. */
  bq24210.en_port->out &= ~bq24210.en_bit;
  bq24210.en_port->dir |= bq24210.en_bit;

  cprintf("I2C on %s at address 0x%02x\nPins: %s\n",
          xBSP430serialName(APP_BQ27510_I2C_PERIPH_HANDLE),
          APP_BQ27510_I2C_ADDRESS,
          xBSP430platformPeripheralHelp(APP_BQ27510_I2C_PERIPH_HANDLE, BSP430_PERIPHCFG_SERIAL_I2C));

  /* NOTE: At default BSP430_SERIAL_I2C_BUS_SPEED_HZ 400kHz this
   * devices supports only single-byte write operations.  Further,
   * ensure a 66us delay between packets. */
  i2c = hBSP430serialOpenI2C(hBSP430serialLookup(APP_BQ27510_I2C_PERIPH_HANDLE),
                             BSP430_SERIAL_ADJUST_CTL0_INITIALIZER(UCMST),
                             0, 0);
  if (! i2c) {
    cprintf("I2C open failed\n");
    return;
  }
  (void)iBSP430i2cSetAddresses_rh(i2c, -1, APP_BQ27510_I2C_ADDRESS);

  resample_interval_utt = BSP430_UPTIME_MS_TO_UTT(1000UL * RESAMPLE_INTERVAL_S);
  resample_wake_utt = ulBSP430uptime_ni();
  flags = FLG_DUMP_STATE | FLG_UPDATE_INTERVAL;

  BSP430_CORE_ENABLE_INTERRUPT();
  while (1) {
    char astext_buf[BSP430_UPTIME_AS_TEXT_LENGTH];
    uint16_t temperature_dC;

    if (FLG_DUMP_STATE & flags) {
      memset(&u, 0, sizeof(u));
      rc = readBQ27510(i2c, 0, nwords, u.raw);
      cprintf("Device ID %04x rc %d\n", u.state.device_id, rc);
      cprintf("%.30s = %d\n", "atRate_mA", u.state.atRate_mA);
      cprintf("%.30s = %u\n", "atRatetimeToEmpty_min", u.state.atRatetimeToEmpty_min);
      cprintf("%.30s = %u\n", "temperature_dK", u.state.temperature_dK);
      cprintf("%.30s = %u\n", "voltage_mV", u.state.voltage_mV);
      cprintf("%.30s = 0x%04x\n", "flags", u.state.flags);
      cprintf("%.30s = %u\n", "nominalAvailableCapacity_mAh", u.state.nominalAvailableCapacity_mAh);
      cprintf("%.30s = %u\n", "fullAvailableCapacity_mAh", u.state.fullAvailableCapacity_mAh);
      cprintf("%.30s = %u\n", "remainingCapacity_mAh", u.state.remainingCapacity_mAh);
      cprintf("%.30s = %u\n", "fullChargeCapacity_mAh", u.state.fullChargeCapacity_mAh);
      cprintf("%.30s = %d\n", "averageCurrent_mA", u.state.averageCurrent_mA);
      cprintf("%.30s = %u\n", "timeToEmpty_min", u.state.timeToEmpty_min);
      cprintf("%.30s = %d\n", "standbyCurrent_mA", u.state.standbyCurrent_mA);
      cprintf("%.30s = %u\n", "standbyTimeToEmpty_min", u.state.standbyTimeToEmpty_min);
      cprintf("%.30s = %u\n", "stateOfHealth_ppcpx", u.state.stateOfHealth_ppcpx);
      cprintf("%.30s = %u\n", "cycleCount", u.state.cycleCount);
      cprintf("%.30s = %u\n", "stateOfCharge_ppc", u.state.stateOfCharge_ppc);
      cprintf("%.30s = %d\n", "instantaneousCurrent_mA", u.state.instantaneousCurrent_mA);
      cprintf("%.30s = %u\n", "internalTemperature_dK", u.state.internalTemperature_dK);
      cprintf("%.30s = %u\n", "reistanceScale", u.state.reistanceScale);
      cprintf("%.30s = %u\n", "operationConfiguration", u.state.operationConfiguration);
      cprintf("%.30s = %u\n", "designCapacity_mAh", u.state.designCapacity_mAh);
      cprintf("flags %02x ; ENn state %d\n", flags, (bq24210.en_port->out & bq24210.en_bit));
      flags &= ~FLG_DUMP_STATE;
    }
    if (FLG_TOGGLE_ENABLE & flags) {
      bq24210.en_port->out ^= bq24210.en_bit;
      flags &= ~FLG_TOGGLE_ENABLE;
    }
    vBSP430ledSet(BSP430_LED_GREEN, !(bq24210.en_port->out & bq24210.en_bit));

    rc = readBQ27510(i2c, 0, nwords, u.raw);
    temperature_dC = u.state.temperature_dK - 2733;

    cprintf("%s: %c%c%c % 2d.%dC  %4dmV ; SoC %u%% ; Cap %4d / %4d ; %dmA ~ %dmA / %u\n",
            xBSP430uptimeAsText(ulBSP430uptime(), astext_buf),
            (bq24210.en_port->out & bq24210.en_bit) ? ' ' : 'E',
            (bq24210.chg_port->in & bq24210.chg_bit) ? ' ' : 'C',
            (bq24210.pg_port->in & bq24210.pg_bit) ? ' ' : 'P',
            (temperature_dC / 10), (temperature_dC % 10),
            u.state.voltage_mV,
            u.state.stateOfCharge_ppc,
            u.state.remainingCapacity_mAh,
            u.state.fullAvailableCapacity_mAh,
            u.state.instantaneousCurrent_mA,
            u.state.averageCurrent_mA,
            u.state.cycleCount
            );
    if (FLG_UPDATE_INTERVAL & flags) {
      resample_wake_utt += resample_interval_utt;
      flags &= ~FLG_UPDATE_INTERVAL;
    }
    flags = 0;
    while (! flags) {
      iBSP430consoleFlush();
      if (0 >= lBSP430uptimeSleepUntil(resample_wake_utt, LPM3_bits)) {
        flags |= FLG_UPDATE_INTERVAL;
      }
      while (0 <= ((rc = cgetchar()))) {
        if ('!' == rc) {
          flags |= FLG_TOGGLE_ENABLE;
        } else if (' ' == rc) {
          flags |= FLG_DUMP_STATE;
        }
      }
    }

  }
}
Пример #9
0
static void
process_mux_flag ()
{
  vBSP430ledSet(BSP430_LED_GREEN, -1);
}
Пример #10
0
void main ()
{
  BSP430_CORE_INTERRUPT_STATE_T istate;
#if BSP430_MODULE_SYS - 0
  unsigned long reset_causes = 0;
  unsigned int reset_flags = 0;
#endif /* BSP430_MODULE_SYS */

#if BSP430_MODULE_SYS - 0
  {
    unsigned int sysrstiv;

    /* Record all the reset causes */
    while (0 != ((sysrstiv = uiBSP430sysSYSRSTGenerator_ni(&reset_flags)))) {
      reset_causes |= 1UL << (sysrstiv / 2);
    }
  }
#endif /* BSP430_MODULE_SYS */

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();

#if BSP430_MODULE_SYS - 0
  cprintf("System reset bitmask %lx; causes:\n", reset_causes);
  {
    int bit = 0;
    while (bit < (8 * sizeof(reset_causes))) {
      if (reset_causes & (1UL << bit)) {
        cprintf("\t%s\n", xBSP430sysSYSRSTIVDescription(2*bit));
      }
      ++bit;
    }
  }

  cputtext_ni("System reset included:");
  if (reset_flags) {
    if (reset_flags & BSP430_SYS_FLAG_SYSRST_BOR) {
      cputtext_ni(" BOR");
    }
    if (reset_flags & BSP430_SYS_FLAG_SYSRST_LPM5WU) {
      cputtext_ni(" LPM5WU");
    }
    if (reset_flags & BSP430_SYS_FLAG_SYSRST_POR) {
      cputtext_ni(" POR");
    }
    if (reset_flags & BSP430_SYS_FLAG_SYSRST_PUC) {
      cputtext_ni(" PUC");
    }
  } else {
    cputtext_ni(" no data");
  }
  cputchar_ni('\n');
#endif /* BSP430_MODULE_SYS */

  BSP430_CORE_ENABLE_INTERRUPT();

  memcpy(ivect, VECTOR_OFFSET, VECTOR_LENGTH_BYTES);
  dumpRegion("Cached vectors", ivect, VECTOR_LENGTH_BYTES);
  dumpRegion("Vectors", VECTOR_OFFSET, VECTOR_LENGTH_BYTES);

  BSP430_CORE_SAVE_INTERRUPT_STATE(istate);
  BSP430_CORE_DISABLE_INTERRUPT();
  do {
    /* Note: You need to flush the console if you want to see the
     * output now; otherwise it'll be printed once interrupt-driven
     * transmission is re-enabled. */
    iBSP430consoleFlush();
    iBSP430consoleTransmitUseInterrupts_ni(0);
    (void)iBSP430flashEraseSegment_ni((void*)0xFE00);
    dumpRegion("Vectors with 0xFE00", VECTOR_OFFSET, VECTOR_LENGTH_BYTES);
    (void)iBSP430flashEraseSegment_ni(VECTOR_OFFSET);
    vBSP430ledSet(0, 1);
    dumpRegion("Vectors as erased", VECTOR_OFFSET, VECTOR_LENGTH_BYTES);
    memcpy(erased_ivect, VECTOR_OFFSET, VECTOR_LENGTH_BYTES);
    (void)iBSP430flashWriteData_ni(VECTOR_OFFSET, ivect, VECTOR_LENGTH_BYTES);
    vBSP430ledSet(1, 1);
    iBSP430consoleTransmitUseInterrupts_ni(1);
  } while (0);
  BSP430_CORE_RESTORE_INTERRUPT_STATE(istate);

  dumpRegion("Erased Vectors", erased_ivect, VECTOR_LENGTH_BYTES);
  dumpRegion("Restored Vectors", VECTOR_OFFSET, VECTOR_LENGTH_BYTES);
  iBSP430consoleFlush();
}
Пример #11
0
void main ()
{
  const char * command;
  int flags;

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();
  vBSP430cliSetDiagnosticFunction(iBSP430cliConsoleDiagnostic);
  cprintf("\ncli example " __DATE__ " " __TIME__ "\n");
#if configBSP430_CLI_COMMAND_COMPLETION - 0
  cprintf("Command completion is available.\n");
#endif /* configBSP430_CLI_COMMAND_COMPLETION */
  vBSP430ledSet(0, 1);
  cprintf("\nLED lit when not awaiting input\n");

  /* NOTE: The control flow in this is a bit tricky, as we're trying
   * to leave interrupts enabled during the main body of the loop,
   * while they must be disabled when processing input to recognize a
   * command.  The flags variable preserves state across multiple loop
   * iterations until all relevant activities have completed. */
  commandSet = LAST_COMMAND;
  command = NULL;
  flags = eBSP430cliConsole_REPAINT;

  BSP430_CORE_ENABLE_INTERRUPT();
  while (1) {
    if (flags & eBSP430cliConsole_ANY_ESCAPE) {
      int c;
      while (0 <= ((c = cgetchar()))) {
        cprintf("escape char 0x%02x (%u) '%c'\n", c, c, isprint(c) ? c : '.');
        /* Technically CSI is a single character 0x9b representing
         * ESC+[.  In the two-character mode accepted here, we use the
         * value for the second character. */
#define KEY_CSI '['
        if ((KEY_CSI == c) && (flags & eBSP430cliConsole_PROCESS_ESCAPE)) {
          flags &= ~eBSP430cliConsole_PROCESS_ESCAPE;
          flags |= eBSP430cliConsole_IN_ESCAPE;
        } else if ((64 <= c) && (c <= 126)) {
          flags &= ~eBSP430cliConsole_ANY_ESCAPE;
          cprintf("Leaving escape mode\n");
          break;
        }
      }
    }
    if (flags & eBSP430cliConsole_DO_COMPLETION) {
      flags &= ~eBSP430cliConsole_DO_COMPLETION;
      flags |= iBSP430cliConsoleBufferCompletion(commandSet, &command);
    }
    if (flags & eBSP430cliConsole_READY) {
      int rv;

      rv = iBSP430cliExecuteCommand(commandSet, 0, command);
      if (0 != rv) {
        cprintf("Command execution returned %d\n", rv);
      }
      /* Ensure prompt is rewritten, but not the command we just
       * ran */
      flags |= eBSP430cliConsole_REPAINT;
      command = NULL;
    }
    if (flags & eBSP430cliConsole_REPAINT) {
      /* Draw the prompt along with whatever's left in the command
       * buffer.  Note use of leading carriage return in case an edit
       * left material on the current line. */
      cprintf("\r> %s", command ? command : "");
      flags &= ~eBSP430cliConsole_REPAINT;
    }
    if (flags & eBSP430cliConsole_REPAINT_BEL) {
      cputchar('\a');
      flags &= ~eBSP430cliConsole_REPAINT_BEL;
    }
    BSP430_CORE_DISABLE_INTERRUPT();
    do {
      if (flags & eBSP430cliConsole_READY) {
        /* Clear the command we just completed */
        vBSP430cliConsoleBufferClear_ni();
        flags &= ~eBSP430cliConsole_READY;
      }
      do {
        /* Unless we're processing application-specific escape
         * characters let iBSP430cliConsoleBufferProcessInput_ni()
         * process any input characters that have already been
         * received. */
        if (! (flags & eBSP430cliConsole_ANY_ESCAPE)) {
          flags |= iBSP430cliConsoleBufferProcessInput_ni();
        }
        if (0 == flags) {
          /* Sleep until something wakes us, such as console input.
           * Then turn off interrupts and loop back to read that
           * input. */
          vBSP430ledSet(0, 0);
          BSP430_CORE_LPM_ENTER_NI(LPM0_bits);
          BSP430_CORE_DISABLE_INTERRUPT();
          vBSP430ledSet(0, 1);
        }
        /* Repeat if still nothing to do */
      } while (! flags);

      /* Got something to do; get the command contents in place so
       * we can update the screen. */
      command = xBSP430cliConsoleBuffer_ni();
    } while (0);
    BSP430_CORE_ENABLE_INTERRUPT();
  }
}
Пример #12
0
void main (void)
{
  unsigned long wake_utt;
  int rc;
  long lrc;
  char as_text[BSP430_UPTIME_AS_TEXT_LENGTH];
  uint32_u ntp_addr;
  uint32_u self_addr;

  vBSP430platformInitialize_ni();
  (void)iBSP430consoleInitialize();
  cprintf("\nntp " __DATE__ " " __TIME__ "\n");

  /* Initialization can be done with interrupts disabled, since the
   * function does nothing but store callbacks.  We use the same
   * callback for all three update capabilities. */
  rc = iBSP430cc3000spiInitialize(wlan_cb, NULL, NULL, NULL);
  if (0 > rc) {
    cprintf("ERR: Initialization failed: %d\n", rc);
    return;
  }

  BSP430_CORE_ENABLE_INTERRUPT();

  /* Local addresses use all zeros for inet addr.  bind does not
   * support dynamic assignment of unused port through sin_port=0. */
  memset(&local_addr, 0, sizeof(local_addr));
  local_addr.sai.sin_family = AF_INET;
  local_addr.sai.sin_port = htons(60123);

  /* Remote server will be determined by DNS from the NTP pool once we
   * start. */
  remote_addr = local_addr;
  remote_addr.sai.sin_port = htons(123);

  ntp_addr.u32 = 0;
  self_addr.u32 = 0;

  cprintf("Remote: %s:%u\n", net_ipv4AsText(&remote_addr.sai.sin_addr), ntohs(remote_addr.sai.sin_port));
  rc = sizeof(sBSP430uptimeNTPPacketHeader);
  if (48 != rc) {
    cprintf("ERR: NTP header size %d\n", rc);
    return;
  }
  wake_utt = ulBSP430uptime();

  (void)rc;
  while (1) {
    unsigned long timeout_utt;

    do {
      tNetappIpconfigRetArgs ipc;
      unsigned long start_utt;
      unsigned long finished_utt;
      int sfd;
      int nfds;
      fd_set rfds;
      int servers_left;
      int retries_left;

      /* Clear everything as we're starting a cycle */
      BSP430_CORE_DISABLE_INTERRUPT();
      do {
        event_flags_v = 0;
        start_utt = ulBSP430uptime_ni();
      } while (0);
      BSP430_CORE_ENABLE_INTERRUPT();

      /* Start the WAN process.  This is asynchronous; wait up to 2
       * seconds for it to complete. */
      cprintf("%s: ", xBSP430uptimeAsText(start_utt, as_text));
      cputchar('W');
      wlan_start(0);
      vBSP430ledSet(BSP430_LED_RED, 1);
      (void)wlan_set_event_mask(0UL);
      lrc = BSP430_UPTIME_MS_TO_UTT(2000);
      timeout_utt = ulBSP430uptime() + lrc;
      while ((! (EVENT_FLAG_WLANCONN & event_flags_v))
             && (0 < ((lrc = lBSP430uptimeSleepUntil(timeout_utt, LPM0_bits))))) {
      }
      if (! (EVENT_FLAG_WLANCONN & event_flags_v)) {
        cprintf("WLAN start failed\n");
        break;
      }

      /* Wait for IP connectivity (signalled by a DHCP event).
       * Continue using the previous timeout. */
      cputchar('D');
      while ((! (EVENT_FLAG_IPCONN & event_flags_v))
             && (0 < ((lrc = lBSP430uptimeSleepUntil(timeout_utt, LPM0_bits))))) {
      }
      if (! (EVENT_FLAG_IPCONN & event_flags_v)) {
        cprintf("IP conn failed\n");
        break;
      }

      /* Inspect the IP configuration.  Sometimes we get the event,
       * but there's no IP assigned. */
      netapp_ipconfig(&ipc);
      memcpy(self_addr.u8, ipc.aucIP, sizeof(self_addr));
      if (! self_addr.u32) {
        cprintf("IP assignment failed\n");
        break;
      }
      vBSP430ledSet(BSP430_LED_GREEN, 1);

      /* Obtain a UDP socket and bind it for local operations. */
      cputchar('I');
      sfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
      if (0 > sfd) {
        cprintf("socket() failed: %d\n", sfd);
        break;
      }
      cputchar('S');
      lrc = bind(sfd, &local_addr.sa, sizeof(local_addr.sa));
      if (0 > lrc) {
        cprintf("bind() failed: %ld\n", lrc);
        break;
      }
      cputchar('B');

      servers_left = NTP_SERVERS_PER_ATTEMPT;
      retries_left = NTP_REQUESTS_PER_SERVER;
      do {
        sBSP430uptimeNTPPacketHeader ntp0;
        sBSP430uptimeNTPPacketHeader ntp1;
        int have_invalid_epoch;
        struct timeval tv;
        sockaddr_u src;
        socklen_t slen = sizeof(src);
        unsigned long recv_utt;
        uint64_t recv_ntp;
        int64_t adjustment_ntp;
        long adjustment_ms;
        unsigned long rtt_us;

        have_invalid_epoch = 0 != iBSP430uptimeCheckEpochValidity();
        if (! remote_addr.sai.sin_addr.s_addr) {
          const char ntp_fqdn[] = "0.pool.ntp.org";
          ntp_addr.u32 = 0;
          rc = gethostbyname((char *)ntp_fqdn, sizeof(ntp_fqdn)-1, &ntp_addr.u32);
          cputchar('d');
          if (-95 == rc) { /* ARP request failed; retry usually works */
            rc = gethostbyname((char *)ntp_fqdn, sizeof(ntp_fqdn)-1, &ntp_addr.u32);
            cputchar('d');
          }
          if (0 == ntp_addr.u32) {
            cprintf("gethostbyname(%s) failed: %d\n", ntp_fqdn, rc);
            rc = -1;
            break;
          }
          remote_addr.sai.sin_addr.s_addr = htonl(ntp_addr.u32);
          cprintf("{%s}", net_ipv4AsText(&remote_addr.sai.sin_addr));
          retries_left = NTP_REQUESTS_PER_SERVER;
        }

        /* Configure the NTP request and send it */
        iBSP430uptimeInitializeNTPRequest(&ntp0);
        iBSP430uptimeSetNTPXmtField(&ntp0, NULL);
        BSP430_CORE_DISABLE_INTERRUPT();
        do {
          /* Clear the shutdown bit, so we know when it's ok to shut
           * down after this send */
          event_flags_v &= ~EVENT_FLAG_SHUTDOWN;
        } while (0);
        BSP430_CORE_ENABLE_INTERRUPT();
        rc = sendto(sfd, &ntp0, sizeof(ntp0), 0, &remote_addr.sa, sizeof(remote_addr.sai));
        if (sizeof(ntp0) != rc) {
          cprintf("sendto %s:%u failed: %d\n", net_ipv4AsText(&remote_addr.sai.sin_addr), ntohs(remote_addr.sai.sin_port), rc);
          rc = -1;
          break;
        }
        cputchar('s');

        /* If we get an answer it should be here in less than 100
         * ms, but give it 400 ms just to be kind. */
        tv.tv_sec = 0;
        tv.tv_usec = 400000UL;
        FD_ZERO(&rfds);
        FD_SET(sfd, &rfds);
        nfds = sfd+1;
        rc = select(nfds, &rfds, NULL, NULL, &tv);
        if (! FD_ISSET(sfd, &rfds)) {
          /* We didn't get an answer.  If there are any retries left, use them. */
          if (0 < retries_left--) {
            rc = 1;
            continue;
          }
          /* No retries left on this server: forget about it.  If
           * there are any servers left, try another. */
          cputchar('!');
          remote_addr.sai.sin_addr.s_addr = 0;
          if (0 < servers_left--) {
            rc = 1;
            continue;
          }
          /* No retries from all available servers.  Fail this attempt */
          cprintf("no responsive NTP server found\n");
          rc = -1;
          break;
        }

        /* Got a response.  Record the time it came in and then read
         * it (no high-resolution packet RX time available, but we
         * believe it's here already so set the RX time first).  The
         * message is unacceptable if it isn't an NTP packet. */
        recv_utt = ulBSP430uptime();
        rc = recvfrom(sfd, &ntp1, sizeof(ntp1), 0, &src.sa, &slen);
        if (sizeof(ntp1) != rc) {
          cprintf("recv failed: %d\n", rc);
          rc = -1;
          break;
        }
        cputchar('r');

        /* Convert the RX time to NTP, then process the message to
         * determine the offset. */
        rc = iBSP430uptimeAsNTP(recv_utt, &recv_ntp, have_invalid_epoch);
        if (0 != rc) {
          cprintf("NTP decode failed: %d\n", rc);
          continue;
        }
        rc = iBSP430uptimeProcessNTPResponse(&ntp0, &ntp1, recv_ntp, &adjustment_ntp, &adjustment_ms, &rtt_us);
        if (0 != rc) {
          cprintf("Process failed: %d\n", rc);
          continue;
        }
        if (have_invalid_epoch) {
          rc = iBSP430uptimeSetEpochFromNTP(BSP430_UPTIME_BYPASS_EPOCH_NTP + adjustment_ntp);
          cputchar('E');
          if (0 != rc) {
            cprintf("\nERR: SetEpoch failed: %d\n", rc);
          }
#if (NTP_ADJUST_EACH_ITER - 0)
        } else {
          rc = iBSP430uptimeAdjustEpochFromNTP(adjustment_ntp);
          cputchar('A');
          if (0 != rc) {
            cprintf("\nERR: AdjustEpoch failed: %d\n", rc);
          }
#endif
        }
        cprintf("[%s:%u adj %lld ntp = %ld ms, rtt %lu us]",
                net_ipv4AsText(&remote_addr.sai.sin_addr), ntohs(remote_addr.sai.sin_port),
                adjustment_ntp, adjustment_ms, rtt_us);
      } while (0 != rc);
      if (0 != rc) {
        cprintf("NTP query failed\n");
        break;
      }
#if 0
      /* The shutdown OK seems to arrive about 1000 ms after the last
       * transmit, which is unnecessarily long.  As we're not doing
       * TCP, there's no reason to wait for it. */
      lrc = BSP430_UPTIME_MS_TO_UTT(4000);
      timeout_utt = ulBSP430uptime() + lrc;
      while ((! (EVENT_FLAG_SHUTDOWN & event_flags_v))
             && (0 < ((lrc = lBSP430uptimeSleepUntil(timeout_utt, LPM0_bits))))) {
      }
      if (! (EVENT_FLAG_SHUTDOWN & event_flags_v)) {
        cprintf("SHUTDOWN ok never received\n");
        break;
      }
#endif
      finished_utt = ulBSP430uptime();
      cprintf("[%s]\n", xBSP430uptimeAsText(finished_utt - start_utt, as_text));
    } while (0);

    BSP430_CORE_DISABLE_INTERRUPT();
    do {
      event_flags_v = 0;
    } while (0);
    BSP430_CORE_ENABLE_INTERRUPT();
    wlan_stop();
    vBSP430ledSet(BSP430_LED_GREEN, 0);
    vBSP430ledSet(BSP430_LED_RED, 0);
    wake_utt += 60 * ulBSP430uptimeConversionFrequency_Hz();
    while (0 < lBSP430uptimeSleepUntil(wake_utt, LPM2_bits)) {
    }
  }
  cprintf("Fell off end\n");
}