Skip to content

Access to temperature readout from LPS22HB #3

Description

@sekocabas

Hello,
LPS22HB includes a temperature sensor as well. Would you consider making the following additions to the library? I could send a pull request if you prefer.

// Addition to Baro.cpp
#define LPS22HB_TEMP_OUT_L_REG     0x2b
#define LPS22HB_TEMP_OUT_H_REG     0x2c

float LPS22HBClass::readTemperature()
{
  // trigger one shot
  i2cWrite(LPS22HB_CTRL2_REG, 0x01);

  // wait for completion
  while ((i2cRead(LPS22HB_STATUS_REG) & 0x02) == 0) {
    yield();
  }

  int16_t temp_int = (i2cRead(LPS22HB_TEMP_OUT_L_REG) |
          (i2cRead(LPS22HB_TEMP_OUT_H_REG) << 8));
  float reading =  temp_int/ 100.0;

  return reading;
}

// Addition to Baro.h
float readTemperature();

Also, there is a temperature sensor within the nRF52840 controller of Nano 33 BLE Sense. The following code snippet (a bunch of copy-pasted lines from the indicated URLs) allows access to that register. It may perhaps be useful to those who are trying to access the nRF52840 internals.

// read the ARM chip in-built TEMP register
// See https://infocenter.nordicsemi.com/topic/ps_nrf52840/temp.html?cp=4_0_0_5_27
int32_t volatile temp_builtin;
float temp_builtin_float;

// The following is from https://github.com/particle-iot/nrf5_sdk/blob/master/modules/nrfx/hal/nrf_temp.h
#define MASK_SIGN           (0x00000200UL)
#define MASK_SIGN_EXTENSION (0xFFFFFC00UL)

// Initialization of temp
// Equivalent to 'nrf_temp_init();' call in main.c below, copied from nrf_temp.h
/**@note Workaround for PAN_028 rev2.0A anomaly 31 - TEMP: Temperature offset value has to be manually loaded to the TEMP module */
*(uint32_t *) 0x4000C504 = 0;

// from https://github.com/particle-iot/nrf5_sdk/blob/master/examples/peripheral/temperature/main.c
NRF_TEMP->TASKS_START = 1; /** Start the temperature measurement. */
// Wait until measurement done
while (NRF_TEMP->EVENTS_DATARDY == 0)
{
    // Do nothing.
}
NRF_TEMP->EVENTS_DATARDY = 0;

/**@note Workaround for PAN_028 rev2.0A anomaly 28 - TEMP: Negative measured values are not represented correctly */
temp_builtin = ((NRF_TEMP->TEMP & MASK_SIGN) != 0) ? (int32_t)(NRF_TEMP->TEMP | MASK_SIGN_EXTENSION) : (NRF_TEMP->TEMP);

/**@note Workaround for PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs. */
NRF_TEMP->TASKS_STOP = 1; /** Stop the temperature measurement. */

// Convert temp_builtin to degC
temp_builtin_float = temp_builtin * 0.25f;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions