/******************************************************************************
 * @file        hal_i2c.c
 * @copyright   Nehemis SARL reserves all rights even in the event of industrial
 *              property rights. We reserve all rights of disposal such as
 *              copying and passing on to third parties.
 *
 * @brief       STM HAL layer wrapper class for I2C.
 *
 ******************************************************************************/

/******************************************************************************
 * Include Header Files
 ******************************************************************************/
#include "hal_i2c.h"

/* CubeMX */
#include "i2c.h"

/******************************************************************************
 * Function Definitions
 ******************************************************************************/
uint32 HalI2CInit(void)
{
    return NMS_ERR_NONE;
}


uint32 HalI2CWrite(MapHalI2cModule_en module_en, uint16 devAddr_u16, uint16 memAddr_u16, uint16 memAddrSize_u16, uint8 *pData_pu8, uint16 size_u16)
{
    uint32 error_u32 = NMS_ERR_NONE;
    HAL_StatusTypeDef halStatus_en = HAL_OK;
    I2C_HandleTypeDef *targetModule_pst;

    error_u32 = MapHalI2cModule(module_en, &targetModule_pst);
    if (error_u32 == NMS_ERR_NONE)
    {
        halStatus_en = HAL_I2C_Mem_Write(targetModule_pst, devAddr_u16, memAddr_u16, memAddrSize_u16, pData_pu8, size_u16, HAL_MAX_DELAY);

        switch (halStatus_en)
        {
            case HAL_BUSY:      error_u32 = NMS_ERR_BUSY;       break;
            case HAL_TIMEOUT:   error_u32 = NMS_ERR_TIMEOUT;    break;
            case HAL_ERROR:     error_u32 = NMS_ERR_INTERNAL;   break;
            case HAL_OK:        error_u32 = NMS_ERR_NONE;       break;
            default:            error_u32 = NMS_ERR_UNKNOWN;    break;
        }
    }
    return error_u32;
}


uint32 HalI2CRead(MapHalI2cModule_en module_en, uint16 devAddr_u16, uint16 memAddr_u16, uint16 memAddrSize_u16, uint8 *pData_pu8, uint16 size_u16)
{
    uint32 error_u32 = NMS_ERR_NONE;
    HAL_StatusTypeDef halStatus_en = HAL_OK;
    I2C_HandleTypeDef *targetModule_pst;

    error_u32 = MapHalI2cModule(module_en, &targetModule_pst);
    if (error_u32 == NMS_ERR_NONE)
    {
        halStatus_en = HAL_I2C_Mem_Read(targetModule_pst, devAddr_u16, memAddr_u16, memAddrSize_u16, pData_pu8, size_u16, HAL_MAX_DELAY);

        switch (halStatus_en)
        {
            case HAL_BUSY:      error_u32 = NMS_ERR_BUSY;       break;
            case HAL_TIMEOUT:   error_u32 = NMS_ERR_TIMEOUT;    break;
            case HAL_ERROR:     error_u32 = NMS_ERR_INTERNAL;   break;
            case HAL_OK:        error_u32 = NMS_ERR_NONE;       break;
            default:            error_u32 = NMS_ERR_UNKNOWN;    break;
        }
    }
    return error_u32;
}


uint32 HalI2CMasterTransmit(MapHalI2cModule_en module_en, uint16 devAddr_u16, uint8 *pData_pu8, uint16 size_u16)
{
    uint32 error_u32 = NMS_ERR_NONE;
    HAL_StatusTypeDef halStatus_en = HAL_OK;
    I2C_HandleTypeDef *targetModule_pst;

    error_u32 = MapHalI2cModule(module_en, &targetModule_pst);
    if (error_u32 == NMS_ERR_NONE)
    {
        halStatus_en = HAL_I2C_Master_Transmit(targetModule_pst, devAddr_u16, pData_pu8, size_u16, HAL_MAX_DELAY);

        switch (halStatus_en)
        {
            case HAL_BUSY:      error_u32 = NMS_ERR_BUSY;       break;
            case HAL_TIMEOUT:   error_u32 = NMS_ERR_TIMEOUT;    break;
            case HAL_ERROR:     error_u32 = NMS_ERR_INTERNAL;   break;
            case HAL_OK:        error_u32 = NMS_ERR_NONE;       break;
            default:            error_u32 = NMS_ERR_UNKNOWN;    break;
        }
    }
    return error_u32;
}
