/*******************************************************************************
 * @file	system_interface.h
 * @author	CandyPops Co.
 * @version	V1.0.0
 * @date	2022-09-05
 * @brief
 ******************************************************************************/

/*******************************************************************************
 * [Header overview] ICM42670P I2C communication interface declarations
 *
 * Pin definitions and function prototypes for communicating with the
 * ICM42670P IMU sensor via nRF52840 TWI hardware.
 *
 * Pin assignment:
 *   - I2C SCL : P1.14
 *   - I2C SDA : P1.15
 *   - INT1    : P1.13 (data-ready interrupt)
 *   - INT2    : P0.26 (auxiliary interrupt, currently unused)
 *
 * TWI instance: 0
 ******************************************************************************/

#ifndef _SYSTEM_INTERFACE_H_
#define _SYSTEM_INTERFACE_H_

#include "inv_imu_transport.h"
#include <stdbool.h>
/* TODO: Move that somewhere else */
#ifndef TO_MASK
#define TO_MASK(a) (1U << (unsigned)(a))
#endif


#define ICM42670_I2C_INSTANCE 		0 /**< I2C (TWI) instance index */
#define ICM42670_I2C_SDA_PIN    	NRF_GPIO_PIN_MAP(1,15) /**< SDA pin: P1.15 */
#define ICM42670_I2C_SCL_PIN    	NRF_GPIO_PIN_MAP(1,14) /**< SCL pin: P1.14 */
#define ICM42670_INT1_PIN     		NRF_GPIO_PIN_MAP(1,13) /**< INT1 pin: P1.13 (data-ready interrupt) */
#define ICM42670_INT2_PIN     		NRF_GPIO_PIN_MAP(0,26) /**< INT2 pin: P0.26 (auxiliary, currently unused) */

/* I2C transmit wrapper — if no_stop=true, STOP condition is omitted for Repeated START */
uint32_t icm42670_twi_tx(  uint8_t       device_id,
                          uint8_t const *       p_data,
                          uint8_t               length,
                          bool                  no_stop);

/* I2C receive wrapper */
uint32_t icm42670_twi_rx(  uint8_t       device_id,
                          uint8_t *             p_data,
                          uint8_t               length);

/* Generic I2C read (debug/legacy) — reads 8 bytes and returns the first byte */
uint8_t cat_read (uint8_t device_id, uint8_t address, uint8_t *data);

/* Generic I2C write (debug/legacy) — sends address+data 2 bytes */
void cat_write (uint8_t device_id, uint8_t address, uint8_t *data);

/* Release I2C hardware (called before sleep or re-initialization) */
void inv_i2c_master_uninitialize(void);

/* Initialize I2C hardware (100kHz, blocking mode) */
void inv_i2c_master_initialize(void);

/* Initialize serial interface for IMU driver (I2C/SPI branch) */
int inv_io_hal_init(struct inv_imu_serif *serif);

/* IMU driver callback: register read */
int inv_io_hal_read_reg(struct inv_imu_serif *serif, uint8_t reg, uint8_t * rbuffer, uint32_t rlen);

/* IMU driver callback: register write */
int inv_io_hal_write_reg(struct inv_imu_serif *serif, uint8_t reg, const uint8_t * wbuffer, uint32_t wlen);

#endif /* !_SYSTEM_INTERFACE_H_ */
