Skip to main content

Device Control API — npz_device_control.h / npz_device_control.c

High-level functions for configuring the nPZero device, reading sensor values after wake-up, handling ADC results, and controlling idle/reset states. Include npz_device_control.h in your application.


npz_device_configure

void npz_device_configure(npz_device_config_s *device_config);

Brief: Applies a complete device configuration to the nPZero G1S IC.

Description: Orchestrates the full configuration sequence in this order:

  1. Global settings — timeout, SYSCFG1/2/3, power switch control, interrupt pin config.
  2. Peripherals — for each non-NULL entry in device_config->peripherals[], writes CFGP, MODP, PERP, NCMDP, ADDRP, RREGP, THROVP, THRUNP, TWTP, TCFGP and uploads init bytes to SRAM.
  3. Internal ADC — if adc_channels[0]->wakeup_enable == 1, writes THROVA1 and THRUNA1.
  4. External ADC — if adc_channels[1]->wakeup_enable == 1 and adc_ext_sampling_enable == 1, writes THROVA2 and THRUNA2.

Any step failure prints a diagnostic message via printf and returns early without further configuration.

note

Call npz_hal_init() before this function. After this call, send the device to idle mode with npz_device_go_to_idle() to begin autonomous operation.

Parameters:

NameDirectionDescription
device_configinPointer to a fully populated npz_device_config_s. Must not be NULL.

Returns: void. Check printf output or add custom error hooks for production use.


npz_device_go_to_idle

void npz_device_go_to_idle(void);

Brief: Commands the nPZero G1S IC to enter idle mode and assume autonomous control.

Description: Writes 0xFF to the IDLE_RST register (0x00). The IC shuts down the host power switch (SW_HP), takes control of the I2C bus, and begins autonomous peripheral management according to the previously applied configuration.

Call this after npz_device_configure() to enter the low power idle mode.

Parameters: None.

Returns: void.

Example:

npz_device_configure(&device_config);
npz_device_go_to_idle();
// Host will now be powered off

npz_device_soft_reset

void npz_device_soft_reset(void);

Brief: Performs a software reset of the nPZero G1S IC via I2C.

Description: Writes 0xA5 to the IDLE_RST register. The IC resets and enters Standby state, reverting all registers to power-on defaults. The host retains I²C bus control after reset.

After a soft reset, re-apply configuration with npz_device_configure() before calling npz_device_go_to_idle().

Parameters: None.

Returns: void.


npz_device_read_peripheral_value

bool npz_device_read_peripheral_value(npz_psw_e psw_lp, int index, int *peripheral_value);

Brief: Reads the last captured value from a peripheral after it triggered a wake-up.

Description: Reads the CFGP register of the given peripheral to determine its polling mode. If the mode involves threshold comparison (POLLING_MODE_PERIODIC_READ_COMPARE_THRESHOLD or POLLING_MODE_PERIODIC_WAIT_INTERRUPT_COMPARE_THRESHOLD), reads the VALP register and combines the two bytes (MSB from valp_h, LSB from valp_l) into peripheral_value. Works for both I2C and SPI peripherals.

Call this in the host wake-up handler when npz_register_sta2_s.perN_triggered is set.

Parameters:

NameDirectionDescription
psw_lpinLow-power switch ID for the peripheral (PSW_LP1PSW_LP4).
indexinPeripheral index (0–3), used for diagnostic messages.
peripheral_valueoutPointer to an int that receives the 16-bit peripheral reading.

Returns: true on success, false if any register read fails.

Example:

npz_register_sta2_s sta2 = {0};
npz_read_STA2(&sta2);

if (sta2.per1_triggered) {
int value = 0;
if (npz_device_read_peripheral_value(PSW_LP1, 0, &value)) {
// value holds the last sensor reading
}
}

npz_device_handle_adc_internal

bool npz_device_handle_adc_internal(void);

Brief: Reads and decodes the internal ADC (VBAT) value after a triggered wake-up.

Description: Reads the ADC_CORE register and maps the 5-bit code to a millivolt value using an internal lookup table covering 1.5 V to 3.6 V. Prints the result via printf.

Call this in the wake-up handler when npz_register_sta1_s.int_adc_triggered is set.

VBAT Code-to-Voltage Map:

CodeVoltageCodeVoltage
0x041.5 V0x102.6 V
0x061.6 V0x112.7 V
0x081.7 V0x112.8 V
0x091.8 V0x122.9 V
0x0A1.9 V0x123.0 V
0x0B2.0 V0x133.1 V
0x0C2.1 V0x133.2 V
0x0D2.2 V0x133.3 V
0x0E2.3 V0x143.4 V
0x0F2.4 V0x143.5 V
0x0F2.5 V0x143.6 V

Parameters: None.

Returns: true on success, false if the register read fails or the code is not in the map.


npz_device_handle_adc_external

bool npz_device_handle_adc_external(void);

Brief: Reads and decodes the external ADC (ADC_IN) value after a triggered wake-up.

Description: First verifies that external ADC is enabled in SYSCFG1 and SYSCFG2. Reads the ADC_EXT register and maps the 6-bit code to a millivolt value.

Call this in the wake-up handler when npz_register_sta1_s.ext_adc_triggered is set.

ADC_IN Code-to-Voltage Map:

CodeVoltageCodeVoltageCodeVoltage
0x000.6 V0x261.6 V0x302.6 V
0x080.7 V0x281.7 V0x312.7 V
0x0F0.8 V0x291.8 V0x312.8 V
0x150.9 V0x2A1.9 V0x322.9 V
0x191.0 V0x2B2.0 V0x323.0 V
0x1C1.1 V0x2C2.1 V0x333.1 V
0x1F1.2 V0x2D2.2 V0x333.2 V
0x221.3 V0x2E2.3 V0x333.3 V
0x231.4 V0x2F2.4 V0x343.4 V
0x241.5 V0x2F2.5 V0x343.6 V

Parameters: None.

Returns: true on success, false on register read failure or unrecognized code.