HAL API — npz_hal.h / npz_hal.c
The Hardware Abstraction Layer decouples the driver from MCU-specific I2C code.
Adapt Src/npz_hal.c for your platform; see Platform Porting for examples.
Constants
#define NPZ_I2C_ADDRESS 0x7A // 0x3D shifted left by 1
#define I2C_TRANSMISSION_TIMEOUT_MS 1300
| Constant | Value | Description |
|---|---|---|
NPZ_I2C_ADDRESS | 0x7A | nPZero 7-bit address (0x3D) shifted left by 1, as required by most HAL APIs. |
I2C_TRANSMISSION_TIMEOUT_MS | 1300 | Default timeout (ms) for all I²C transactions. |
Return Type
All HAL functions return npz_status_e:
| Value | Meaning |
|---|---|
OK (0x00) | Operation succeeded. |
ERR (0x01) | I2C transaction failed. |
INVALID_PARAM (0x02) | A parameter was invalid. |
npz_hal_init
npz_status_e npz_hal_init(void);
Brief: Initializes the MCU I2C peripheral for communication with the nPZero G1S.
Description:
Must be the first driver function called. The reference STM32 implementation configures I2C1 with 7-bit addressing, disables analog and digital filters, and calls HAL_I2C_Init. Replace this body when porting.
Parameters: None.
Returns: OK on success, ERR if the I2C peripheral could not be initialized.
Example:
if (npz_hal_init() != OK) {
// I2C init failed — check clock config and pin assignments
}
npz_hal_read
npz_status_e npz_hal_read(uint8_t slave_address, uint8_t slave_register,
uint8_t *pData, uint16_t size, uint32_t timeout);
Brief: Blocking I2C register read from the nPZero IC.
Description:
First writes slave_register to set the read pointer, then reads size bytes into pData. The reference implementation uses HAL_I2C_Mem_Read. This function is called internally by all npz_read_* functions — do not call it directly in application code.
Parameters:
| Name | Direction | Description |
|---|---|---|
slave_address | in | I2C device address (use NPZ_I2C_ADDRESS = 0x7A). |
slave_register | in | Target register address on the nPZero IC. |
pData | out | Buffer to store the bytes read from the IC. |
size | in | Number of bytes to read. |
timeout | in | Timeout in milliseconds before aborting. |
Returns: OK on success, ERR on I2C failure.
The device's 7-bit address value in the datasheet must be shifted to the left before calling this function.
This function is blocking.
npz_hal_write
npz_status_e npz_hal_write(uint8_t slave_address, uint8_t *pData,
uint16_t size, uint32_t timeout);
Brief: Blocking I2C write to the nPZero IC.
Description:
Transmits size bytes from pData. The first byte in pData must be the target register address; subsequent bytes are data. Uses HAL_I2C_Master_Transmit in the reference implementation. Called internally by all npz_write_* functions.
Parameters:
| Name | Direction | Description |
|---|---|---|
slave_address | in | I2C device address (use NPZ_I2C_ADDRESS = 0x7A). |
pData | in | Buffer where pData[0] is the register address and the rest is data. |
size | in | Total number of bytes to transmit (register address + data). |
timeout | in | Timeout in milliseconds before aborting. |
Returns: OK on success, ERR on I2C failure.
The device's 7-bit address value in the datasheet must be shifted to the left before calling this function.
This function is blocking.