nPZero IC
API and Examples for the nPZero
Loading...
Searching...
No Matches
I2C Sensor Control Example

Description

These examples showcases the usage of the nPZero driver to control a sensor connected via the I2C interface. It demonstrates the initialization, configuration, and data exchange procedures required for sensor interaction using the nPZero driver. Sensor in use here is the AMS AS6212.

Code Example Peripheral Mode 1

Code snippet illustrating the usage of the Peripheral Mode 1 for I2C sensor control: This code will set the sensor peripheral to Low Power Switch #2. The Low Power Switch will then be passed as parameter to every function call that has to do with a specific sensor, so it is clear which sensor register should be changed.

  • The example will firstly setup the configuration registers for Peripheral Mode 1. This is done in SYSCFG1, CFGP2, MODP2, PERP2.
  • Next up we need to configure the communication for Peripheral Mode 1. In this example we will use I2C between sensor and nPZero and so we use ADDRP2 to define I2C Slave Address and set I2C protocol.
  • Set NCMDP to number of I2C commands to use for initialize sensor.
  • After this we write the I2C initialization commands to SRAM.
  • Then define the register that shall be read on wakeup. In this case it is the TVAL register in the AS6212.
  • Set the Over and Under threshold registers in the nPZero by setting THROVP2 and THRUNP2.
  • Lastly set the TCFGP2 for configuration of the TWTP2 register and also set TWTP2 register to define the wait time between power-on/initialization and initialization.
void example_peripheral_modes_config_i2c_PM1(np0_device_s device, np0_peripheral_s sensor)
{
uint8_t sram_reg = REG_SRAM_START;
sensor.periph = PSW_LP2;
switch (sensor.periph)
{
case PSW_LP1:
device.syscfg1.wup1 = ENABLED;
break;
case PSW_LP2:
device.syscfg1.wup2 = ENABLED;
break;
case PSW_LP3:
device.syscfg1.wup3 = ENABLED;
break;
case PSW_LP4:
device.syscfg1.wup4 = ENABLED;
break;
}
np0_write_CFGP(sensor.periph, sensor.cfgp);
sensor.modp.wunak = ENABLED;
np0_write_MODP(sensor.periph, sensor.modp);
// Wakeup peripheral every 30 seconds with 10Hz clock
sensor.perp.perp_l = 0x2C;
sensor.perp.perp_h = 0x01;
np0_write_PERP(sensor.periph, sensor.perp);
sensor.ncmdp.ncmdp = 2; //# of I2C commands to be sent from SRAM
np0_write_NCMDP(sensor.periph, sensor.ncmdp);
sensor.addrp.addrp = AS6212;
sensor.addrp.spi_en = I2C;
np0_write_ADDRP(sensor.periph, sensor.addrp);
/*================INIT CMDs======================*/
device.sram.value = 0x01; //Config register (Base register when multi-byte transfer enabled)
np0_write_SRAM(sram_reg, device.sram);
sram_reg += SRAM_REG_SIZE;
device.sram.value = 0x02; //Config register++ (base register +1 since multi-byte is enabled)
np0_write_SRAM(sram_reg, device.sram);
sram_reg += SRAM_REG_SIZE;
device.sram.value = 0xC5; //MSB first
np0_write_SRAM(sram_reg, device.sram);
sram_reg += SRAM_REG_SIZE;
device.sram.value = 0x00;
np0_write_SRAM(sram_reg, device.sram);
sram_reg += SRAM_REG_SIZE;
/*================INIT CMDs END====================*/
//AS6212 TVAL register
sensor.rregp.rregp = 0x00;
np0_write_RREGP(sensor.periph, sensor.rregp);
//0x900 = 18 degrees C
sensor.thrunp.thrunp_l = 0x00;
sensor.thrunp.thrunp_h = 0x09;
np0_write_THRUNP(sensor.periph, sensor.thrunp);
//0xC80 = 25 degrees C
sensor.throvp.throvp_l = 0x80;
sensor.throvp.throvp_h = 0x0C;
np0_write_THROVP(sensor.periph, sensor.throvp);
sensor.twtp.twtp = 0x0C; /* 12 (0x0C) * 4096 = 50k clock cycles = 125ms (400kHz)
* 12 (0x0C) * 256 = 3k clock cycles = 7.5ms*/
np0_write_TWTP(sensor.periph, sensor.twtp);
sensor.tcfgp.twt_en = ENABLED;
sensor.tcfgp.i2cret = 3;
np0_write_TCFGP(sensor.periph, sensor.tcfgp);
}
np0_status_e np0_read_SYSCFG1(np0_register_syscfg1_s *syscfg1)
Reads the syscfg1 register and writes it to np0_register_syscfg1_s struct.
Definition: np0.c:99
np0_status_e np0_write_THROVP(const np0_psw_e sw, const np0_register_throvp_s throvp)
Writes the throvp struct to the throvp register that is connected to the low power switch.
Definition: np0.c:613
np0_status_e np0_write_SYSCFG1(const np0_register_syscfg1_s syscfg1)
Writes the syscfg1 struct to the syscfg1 register.
Definition: np0.c:84
@ CMOD_CMP_OUTSIDE_THRESHOLD_MODE
Definition: np0.h:104
@ BIG_ENDIAN
Definition: np0.h:175
np0_status_e np0_write_SRAM(const uint8_t sram_reg, const np0_register_sram_s sram)
Write one byte to one register in SRAM.
Definition: np0.c:239
@ PWMOD_PERIODIC
Definition: np0.h:68
@ I2C
Definition: np0.h:167
np0_status_e np0_write_NCMDP(const np0_psw_e sw, const np0_register_ncmdp_s ncmdp)
Writes the ncmdp struct to the ncmdp register that is connected to the low power switch.
Definition: np0.c:453
@ DTYPE_INT16
Definition: np0.h:111
np0_status_e np0_write_CFGP(const np0_psw_e sw, const np0_register_cfgp_s cfgp)
Writes the cfgp struct to the cfgp register that is connected to the low power switch.
Definition: np0.c:254
np0_status_e np0_write_TCFGP(const np0_psw_e sw, const np0_register_tcfgp_s tcfgp)
Writes the tcfgp struct to the tcfgp register that is connected to the low power switch.
Definition: np0.c:823
np0_status_e np0_write_MODP(const np0_psw_e sw, const np0_register_modp_s modp)
Writes the modp struct to the modp register that is connected to the low power switch.
Definition: np0.c:309
np0_status_e np0_write_RREGP(const np0_psw_e sw, const np0_register_rregp_s rregp)
Writes the rregp struct to the rregp register that is connected to the low power switch.
Definition: np0.c:560
@ ENABLED
Definition: np0.h:154
np0_status_e np0_write_ADDRP(const np0_psw_e sw, const np0_register_addrp_s addrp)
Writes the addrp struct to the addrp register that is connected to the low power switch.
Definition: np0.c:506
np0_status_e np0_write_PERP(const np0_psw_e sw, const np0_register_perp_s perp)
Writes the perp struct to the perp register that is connected to the low power switch.
Definition: np0.c:366
@ TMOD_PERIODIC_INIT_CMP_THRESHOLD
Definition: np0.h:75
@ PSW_LP3
Definition: np0.h:60
@ PSW_LP1
Definition: np0.h:58
@ PSW_LP4
Definition: np0.h:61
@ PSW_LP2
Definition: np0.h:59
np0_status_e np0_write_THRUNP(const np0_psw_e sw, const np0_register_thrunp_s thrunp)
Writes the thrunp struct to the thrunp register that is connected to the low power switch.
Definition: np0.c:692
@ ENABLE_MULTIBYTE_TRANSFER
Definition: np0.h:161
@ PSWMOD_POWER_SW
Definition: np0.h:86
np0_status_e np0_write_TWTP(const np0_psw_e sw, const np0_register_twtp_s twtp)
Writes the twtp struct to the twtp register that is connected to the low power switch.
Definition: np0.c:771
#define REG_SRAM_START
Definition: registers.h:100
#define SRAM_REG_SIZE
Definition: registers.h:102
Definition: np0.h:467
np0_register_sram_s sram
Definition: np0.h:483
np0_register_syscfg1_s syscfg1
Definition: np0.h:473
Definition: np0.h:488
np0_register_twtp_s twtp
Definition: np0.h:497
np0_register_tcfgp_s tcfgp
Definition: np0.h:498
np0_register_modp_s modp
Definition: np0.h:490
np0_register_thrunp_s thrunp
Definition: np0.h:496
np0_register_cfgp_s cfgp
Definition: np0.h:489
np0_register_addrp_s addrp
Definition: np0.h:493
np0_register_throvp_s throvp
Definition: np0.h:495
np0_register_ncmdp_s ncmdp
Definition: np0.h:492
np0_register_rregp_s rregp
Definition: np0.h:494
np0_psw_e periph
Definition: np0.h:500
np0_register_perp_s perp
Definition: np0.h:491
uint8_t addrp
Definition: np0.h:355
uint8_t spi_en
Definition: np0.h:357
np0_pwmod_p pwmod
Definition: np0.h:306
np0_tmod_p tmod
Definition: np0.h:307
np0_pswmod_p pswmod
Definition: np0.h:308
uint8_t wunak
Definition: np0.h:324
np0_dtype_p dtype
Definition: np0.h:321
uint8_t seqrw
Definition: np0.h:322
np0_cmod_cmp_p cmod
Definition: np0.h:316
uint8_t swprreg
Definition: np0.h:326
uint8_t ncmdp
Definition: np0.h:345
uint8_t perp_h
Definition: np0.h:337
uint8_t perp_l
Definition: np0.h:335
uint8_t rregp
Definition: np0.h:363
uint8_t value
Definition: np0.h:462
uint8_t wup4
Definition: np0.h:250
uint8_t wup3
Definition: np0.h:248
uint8_t wup2
Definition: np0.h:246
uint8_t wup1
Definition: np0.h:244
uint8_t twt_en
Definition: np0.h:397
uint8_t tinit_ext
Definition: np0.h:402
uint8_t i2cret
Definition: np0.h:405
uint8_t tinit_en
Definition: np0.h:401
uint8_t throvp_h
Definition: np0.h:372
uint8_t throvp_l
Definition: np0.h:370
uint8_t thrunp_h
Definition: np0.h:381
uint8_t thrunp_l
Definition: np0.h:379
uint8_t twtp
Definition: np0.h:388

Note

  • Modify the code example according to your sensor's specifications and communication protocol.
  • Refer to the nPZero driver function documentation for specific function usage.