/*
 * Copyright (c) 2020 Great Master Wu. All rights reserved.
 * Author    Date          ChangeLog
 * Master    2021-03-25    Initialize version
 * Master    2021-04-06    twi return real error code
*/

#include "platform_if.h"
#include "sensor_error.h"


#if defined(SENSOR_NRF52_PLATFORM)
#include "nrf_delay.h"
#include "spi.h"
#include "twim.h"
#include "nrf_log.h"
#include "sensor_error.h"


int sensor_serial_init(void)
{
    int ret = SENSOR_SUCCESS;

    twi_init();

    spi_init();

    return ret;
}

int sensor_serial_read_reg(sensor_serial_if_t *serif, 
                           uint8_t * buf, 
                           uint32_t rx_len)
{
    int err_code;
    uint8_t temp[SPI_MAX_RX_LENGTH] = {0x80, 0xFF};

    switch (serif->serif_type) 
    {
    case SERIAL_I2C:
        err_code = twi_read_reg(serif->instance, serif->dev_addr, buf[0], &buf[1], rx_len);
        if(err_code == TWI_SUCCESS)
            err_code = SENSOR_SUCCESS;
        else if(err_code == TWI_ARGS_ERROR)
            err_code = SENSOR_ERROR_ARGS;
        else if(err_code == TWI_XFER_TIMEOUT)
            err_code = SENSOR_ERROR_TIMEOUT;
        else if(err_code == TWI_NACK)
            err_code = SENSOR_ERROR_NOACK;
        else
            err_code = SENSOR_ERROR_IO;
        break;
    case SERIAL_SPI3:
        break;
    case SERIAL_SPI4:
#ifdef SPI_HANDLER
        temp[0] |= buf[0];
        err_code = spi_transfer(tx_buf, 1, buf, rx_len + 1);
#else
        // write read commnd from address of buf[0]
        temp[0] |= buf[0];
        /* only the first tx data is necessary, the other is rubbish data, only used for read
           if read length not +1, data cannot in buf ;
           buf[0] will be clear after read*/
        err_code = spi_transfer(temp, rx_len + 1, buf, rx_len + 1);
#endif
        break;
    case SERIAL_I3C:
        break;
    default:
        return -1;
    }

    return err_code;
}

int sensor_serial_write_reg(sensor_serial_if_t *serif, 
                            uint8_t const* buf, 
                            uint32_t wlen)
{
    int err_code;
    uint8_t temp[SPI_MAX_RX_LENGTH] = {0};

    switch (serif->serif_type) 
    {
    case SERIAL_I2C:
        err_code = twi_write_reg(serif->instance, serif->dev_addr, buf, wlen + 1); // add one bye reg addr
        if(err_code == TWI_SUCCESS)
            err_code = SENSOR_SUCCESS;
        else if(err_code == TWI_ARGS_ERROR)
            err_code = SENSOR_ERROR_ARGS;
        else if(err_code == TWI_XFER_TIMEOUT)
            err_code = SENSOR_ERROR_TIMEOUT;
        else if(err_code == TWI_NACK)
            err_code = SENSOR_ERROR_NOACK;
        else
            err_code = SENSOR_ERROR_IO;
        break;
    case SERIAL_SPI3:
        break;
    case SERIAL_SPI4:
#ifdef SPI_HANDLER
#error "spi used in handler is not implement!"
#else
        err_code = spi_transfer(buf, wlen + 1, temp, wlen + 1); // add one bye reg addr
#endif
        break;
    case SERIAL_I3C:
        break;
    default:
        return SENSOR_ERROR_IO;
    }

    return err_code;
}

int sensor_serial_deinit(sensor_serial_if_t *serif)
{
    int ret = 0;

    twi_disable();
    spi_disable();

    return ret;
}


void sensor_delay_us(uint32_t us)
{
    nrf_delay_us(us);
}


void sensor_delay_ms(uint32_t ms)
{
    nrf_delay_ms(ms);
}

uint64_t sensor_get_time_us(void)
{
     return 0;
}

#endif // SENSOR_NRF52_PLATFORM

