/*
 * i2c.h
 *
 *  Created on: Jun 8, 2021
 *      Author: user
 */

#ifndef INC_I2C_H_
#define INC_I2C_H_

#include "stdint.h"
#include "stm32l0xx_ll_i2c.h"

/*
 * Defines & macros
 */

#define NULL 0

/*
 * Return values for I2C functions
 */

#define I2C_OK 0
#define I2C_ERROR -1 // generic error
#define I2C_ERROR_NACK -2 // NACK was received during transfer
#define I2C_ERROR_TX_INCOMPLETE -3 // number of TXed bytes != buffer length
#define I2C_ERROR_RX_INCOMPLETE -4 // number of RXed bytes != buffer length

/*
 * Type definitions
 */

typedef struct {
	I2C_TypeDef *i2c;
} i2c_context_t;

/*
 * Function declarations
 */

int8_t i2c_init(i2c_context_t *context);
int8_t i2c_transmit(uint8_t address, uint8_t *buffer, int len);
int8_t i2c_receive(uint8_t address, uint8_t *buffer, int len);

#endif /* INC_I2C_H_ */
