Esempio n. 1
0
int
iBSP430usciSetHold_ni (hBSP430halSERIAL hal,
                       int holdp)
{
  int rc;
  int periph_config = peripheralConfigFlag(SERIAL_HAL_HPL(hal)->ctl0);

  FLUSH_HAL_NI(hal);
  if (holdp) {
    SERIAL_HAL_HPL(hal)->ctl1 |= UCSWRST;
    rc = iBSP430platformConfigurePeripheralPins_ni(xBSP430periphFromHPL(hal->hpl.any), periph_config, 0);
  } else {
    rc = iBSP430platformConfigurePeripheralPins_ni(xBSP430periphFromHPL(hal->hpl.any), periph_config, 1);
    if (0 == rc) {
      SERIAL_HAL_HPL(hal)->ctl1 &= ~UCSWRST;
      /* Release the USCI and enable the interrupts.  Interrupts are
       * disabled and cleared when UCSWRST is set. */
      SERIAL_HAL_HPL(hal)->ctl1 &= ~UCSWRST;
      if (hal->rx_cbchain_ni) {
        *SERIAL_HAL_HPLAUX(hal)->iep |= SERIAL_HAL_HPLAUX(hal)->rx_bit;
      }
    }
  }
  return rc;
}
Esempio n. 2
0
static
hBSP430halSERIAL
usciConfigure (hBSP430halSERIAL hal,
               unsigned char ctl0_byte,
               unsigned char ctl1_byte,
               unsigned int brw,
               int mctl)
{
  BSP430_CORE_INTERRUPT_STATE_T istate;

  BSP430_CORE_SAVE_INTERRUPT_STATE(istate);
  BSP430_CORE_DISABLE_INTERRUPT();
  SERIAL_HAL_HPL(hal)->ctl1 = UCSWRST;
  do {
    int periph_config = peripheralConfigFlag(ctl0_byte);

    if (0 != iBSP430platformConfigurePeripheralPins_ni(xBSP430periphFromHPL(SERIAL_HAL_HPL(hal)), periph_config, 1)) {
      hal = NULL;
      break;
    }
    SERIAL_HAL_HPL(hal)->ctl1 |= ctl1_byte;
    SERIAL_HAL_HPL(hal)->ctl0 = ctl0_byte;
    SERIAL_HAL_HPL(hal)->br0 = brw % 256;
    SERIAL_HAL_HPL(hal)->br1 = brw / 256;
    if (0 <= mctl) {
      SERIAL_HAL_HPL(hal)->mctl = mctl;
    }

    /* Mark the hal active */
    hal->num_rx = hal->num_tx = 0;

    /* Attempt to release the device for use; if that failed, reset it
     * and return an error */
    if (0 != iBSP430usciSetHold_ni(hal, 0)) {
      SERIAL_HAL_HPL(hal)->ctl1 = UCSWRST;
      hal = NULL;
    }
  } while (0);
  BSP430_CORE_RESTORE_INTERRUPT_STATE(istate);

  return hal;
}
Esempio n. 3
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;
        }
      }
    }

  }
}
Esempio n. 4
0
void main ()
{
  hBSP430timerMuxSharedAlarm map;
  tBSP430periphHandle uptime_periph;
  int arc[sizeof(mux_alarms)/sizeof(*mux_alarms)];
  unsigned long last_wake_utt;
  unsigned long last_sleep_utt;
  int rc = 0;

  vBSP430platformInitialize_ni();
  last_wake_utt = ulBSP430uptime_ni();
  (void)iBSP430consoleInitialize();
  BSP430_CORE_ENABLE_INTERRUPT();

  cprintf("\n\nevent demo " __DATE__ " " __TIME__ "\n");
  mux_tag = ucBSP430eventTagAllocate("MuxAlarm");
  stats_tag = ucBSP430eventTagAllocate("Statistics");
  mux_flag = uiBSP430eventFlagAllocate();

  uptime_periph = xBSP430periphFromHPL(hBSP430uptimeTimer()->hpl);

  map = hBSP430timerMuxAlarmStartup(&mux_alarm_base, uptime_periph, UPTIME_MUXALARM_CCIDX);
  cprintf("Mux tag %u, stats tag %u, flag %x, with alarm base %p on %s.%u\n",
          mux_tag, stats_tag, mux_flag, map,
          xBSP430timerName(uptime_periph), UPTIME_MUXALARM_CCIDX);
  if (! map) {
    cprintf("ERR initializing mux shared alarm\n");
    goto err_out;
  }

  /* Processing done entirely in mux callback.  No wakeup. */
  mux_alarms[0].alarm.callback_ni = mux_alarm_callback;
  mux_alarms[0].interval_utt = BSP430_UPTIME_MS_TO_UTT(150);

  /* Processing done by an event flag */
  mux_alarms[1].alarm.callback_ni = mux_alarm_callback;
  mux_alarms[1].interval_utt = BSP430_UPTIME_MS_TO_UTT(500);
  mux_alarms[1].flag = mux_flag;

  /* Processing done by a callback with a timestamped event */
  mux_alarms[2].alarm.callback_ni = mux_alarm_callback;
  mux_alarms[2].interval_utt = BSP430_UPTIME_MS_TO_UTT(700);
  mux_alarms[2].process_fn = process_timestamp;
  mux_alarms[2].tag = mux_tag;

  /* Processing done by a callback with a timestamped event */
  mux_alarms[3].alarm.callback_ni = mux_alarm_callback;
  mux_alarms[3].interval_utt = BSP430_UPTIME_MS_TO_UTT(10000);
  mux_alarms[3].process_fn = process_statistics;
  mux_alarms[3].tag = stats_tag;

  /* Enable the multiplexed alarms */
  BSP430_CORE_DISABLE_INTERRUPT();
  do {
    int i = 0;
    for (i = 0; (0 == rc) && (i < sizeof(mux_alarms)/sizeof(*mux_alarms)); ++i) {
      mux_alarms[i].alarm.setting_tck = ulBSP430uptime_ni();
      arc[i] = iBSP430timerMuxAlarmAdd_ni(map, &mux_alarms[i].alarm);
    }
  } while (0);
  BSP430_CORE_ENABLE_INTERRUPT();

  /* Display the results.  All values should be non-negative for success. */
  {
    int i;
    cprintf("Alarm installation got:");
    rc = 0;
    for (i = 0; i < sizeof(arc)/sizeof(*arc); ++i) {
      cprintf(" %d", arc[i]);
      if (0 > arc[i]) {
        rc = arc[i];
      }
    }
    cputchar('\n');
  }
  if (0 > rc) {
    goto err_out;
  }

  last_sleep_utt = ulBSP430uptime();
  while (1) {
    last_wake_utt = ulBSP430uptime();
    unsigned int events = process_events(uiBSP430eventFlagsGet());
    if (events) {
      cprintf("ERR: Unprocessed event flags %04x\n", events);
    }

    BSP430_CORE_DISABLE_INTERRUPT();
    /* Put back any unprocessed events */
    vBSP430eventFlagsSet_ni(events);
    if (iBSP430eventFlagsEmpty_ni()) {
      /* Nothing pending: go to sleep, then jump back to the loop head
       * when we get woken. */
      statistics_v.sleep_utt += last_wake_utt - last_sleep_utt;
      last_sleep_utt = ulBSP430uptime_ni();
      statistics_v.awake_utt += last_sleep_utt - last_wake_utt;
      statistics_v.sleep_ct += 1;
      BSP430_CORE_LPM_ENTER_NI(LPM4_bits | GIE);
      continue;
    }
    BSP430_CORE_ENABLE_INTERRUPT();
  }
err_out:
  (void)iBSP430consoleFlush();
  return;
}