basic/i2c/i2c.c

See examples/basic/i2c

/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
/* Documentation for this app is available online.
* See https://docs.zentri.com/wifi/sdk/latest/examples/basic/i2c
*/
#include "zos.h"
#define LIS3DH_ADDRESS 0x19
#define LIS3DH_REG_WHO_AM_I 0x0F
#define LIS3DH_CTRL_REG 0x20
#define LIS3DH_AUTO_INCREMENT 0x80
static const zos_i2c_device_t i2c_device =
{
.port = PLATFORM_STD_I2C,
.address = LIS3DH_ADDRESS,
.retries = 0,
.flags = 0
};
/*************************************************************************************************/
void zn_app_init(void)
{
uint8_t reg_val;
uint8_t ctrl_reg_write_data[4] = { 0x11, 0x22, 0x33, 0x44};
uint8_t ctrl_reg_read_data[4];
ZOS_LOG("I2C Example starting...");
ZOS_LOG("Reading 'Who Am I' register");
zn_i2c_master_read_reg8(&i2c_device, LIS3DH_REG_WHO_AM_I, &reg_val);
if(reg_val != 0x33)
{
ZOS_LOG(" Error, invalid response: 0x%02X", reg_val);
return;
}
else
{
ZOS_LOG(" Response valid: 0x%02X", reg_val);
}
// Write CTRL_REG 1-4 (0x20 - 0x23), use LIS3DH's register auto-increment feature
// by setting MSb of register address
ZOS_LOG("Writing CTRL_REG 1-4 (0x20 - 0x23)");
zn_i2c_master_write_reg(&i2c_device, LIS3DH_CTRL_REG|LIS3DH_AUTO_INCREMENT, ctrl_reg_write_data, 4);
// Read back registers 0x20-0x23
// Set MSb of register address so the register address is automatically incremented
// (NOTE: The auto-increment is a feature in the LIS3DH, not ZentriOS)
ZOS_LOG("Reading CTRL_REG 1-4 (0x20 - 0x23)");
zn_i2c_master_read_reg(&i2c_device, LIS3DH_CTRL_REG|LIS3DH_AUTO_INCREMENT, ctrl_reg_read_data, 4);
ZOS_LOG("Verifying registers");
if(memcmp(ctrl_reg_write_data, ctrl_reg_read_data, 4) != 0)
{
ZOS_LOG(" Error, data written does not equal data read");
}
else
{
ZOS_LOG(" Success, read back data valid");
}
}
/*************************************************************************************************/
zos_bool_t zn_app_idle(void)
{
return ZOS_FALSE;
}