/*
 * Copyright (c) 2020 Great Master Wu. All rights reserved.
 * Author    Date          ChangeLog
 * Master    2020-08-09    Initialize version
*/

#ifndef __PLATFORM_IF_H__
#define __PLATFORM_IF_H__
#include <stdint.h>
#include <stdlib.h>

#include "sensor_config.h"

#if defined(SENSOR_NRF52_PLATFORM)
#include "nrf_log.h"
/* about log print */
#define SENSOR_ERR(...)                        NRF_LOG_ERROR(__VA_ARGS__)
#define SENSOR_WARNING(...)                    NRF_LOG_WARNING( __VA_ARGS__)
#define SENSOR_INFO(...)                       NRF_LOG_INFO( __VA_ARGS__)
#define SENSOR_DEBUG(...)                      NRF_LOG_DEBUG( __VA_ARGS__)
#elif defined(SENSOR_STM32_PLATFORM)
#error "not implement"
#endif  // SENSOR_DEFAULT_PLATFORM


/** @brief enumeration  of serial interfaces available on any platform */
typedef enum
{
    SERIAL_I2C = 0,
    SERIAL_SPI3,
    SERIAL_SPI4,
    SERIAL_I3C,  
}SERIAL_IF_TYPE_t;
 
/** 
 * @brief base sensor serial interface
 * some soc sdk spi does not have separate register address as a parameter,
 * in order to avoid memcpy copy, make the api does not have register address
 */
typedef struct sensor_serial_if {
    void      *context;   /* reserved, normal set to NULL */
    int       (*read_reg)(struct sensor_serial_if * serif, 
                          uint8_t * buf,                  /* first byte in buffer is register address */
                          uint32_t len);                  /* read data length, not buffer length */
    int       (*write_reg)(struct sensor_serial_if * serif, 
                           const uint8_t * buf,           /* first byte in buffer is register address */ 
                           uint32_t len);                 /* write data length, not buffer length */
    int       (*configure)(struct sensor_serial_if * serif);
    uint32_t  max_read;
    uint32_t  max_write;
    SERIAL_IF_TYPE_t serif_type;
    void     *instance;    /* different platform have different type */
    uint8_t   dev_addr;    /* i2c used(7bit), the other pheripheral may not used*/
}sensor_serial_if_t;


int sensor_serial_init(void);

int sensor_serial_read_reg(sensor_serial_if_t * serif, 
                           uint8_t * rbuffer, 
                           uint32_t rlen);

int sensor_serial_write_reg(sensor_serial_if_t * serif, 
                            const uint8_t * wbuffer, 
                            uint32_t wlen);

int sensor_serial_deinit(sensor_serial_if_t * serif);
                            
void sensor_delay_us(uint32_t us);

void sensor_delay_ms(uint32_t ms);

#endif  // @page end

