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

#include "i2c.h"
#include "stm32l0xx_ll_usart.h"

static i2c_context_t *i2c_context;

int8_t i2c_init(i2c_context_t *context)
{
	if (context == NULL) {
		return I2C_ERROR;
	}
	i2c_context = context;
	return I2C_OK;
}

int8_t i2c_transmit(uint8_t address, uint8_t *buffer, int len)
{
	LL_I2C_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len,
			LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
	int i = 0;
	/* Autoend mode will raise STOP flag if NACK is detected
	 * (or if desired number of bytes is transmitted) */
	while (!LL_I2C_IsActiveFlag_STOP(i2c_context->i2c)) {
		if (LL_I2C_IsActiveFlag_TXE(i2c_context->i2c)) {
			if (i < len) {
				LL_I2C_TransmitData8(i2c_context->i2c, buffer[i++]);
			}
		}
	}
	LL_I2C_ClearFlag_STOP(i2c_context->i2c);
	LL_I2C_ClearFlag_TXE(i2c_context->i2c);
	if (len != i) {
		/* If we detect NACK during transaction (before the end of the last byte) */
		return I2C_ERROR_TX_INCOMPLETE;
	}
	/* NACK after last byte is ok */
//	if (LL_I2C_IsActiveFlag_NACK(i2c_context->i2c)) {
//
//		return I2C_ERROR_NACK;
//	}
	return I2C_OK;
}

int8_t i2c_receive(uint8_t address, uint8_t *buffer, int len)
{
	LL_I2C_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len,
				LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
	LL_I2C_ClearFlag_STOP(i2c_context->i2c);
	int i = 0;
	while (!LL_I2C_IsActiveFlag_STOP(i2c_context->i2c)) {
		if (LL_I2C_IsActiveFlag_RXNE(i2c_context->i2c)) {
			if (i < len) {
				buffer[i++] = LL_I2C_ReceiveData8(i2c_context->i2c);
			}
		}
	}
	LL_I2C_ClearFlag_STOP(i2c_context->i2c);
	if (len != i) {
		return I2C_ERROR_RX_INCOMPLETE;
	}

	return I2C_OK;
}
