

#ifndef _SPI_CH_H
#define _SPI_CH_H

#ifdef __cplusplus
extern "C" {
#endif

#include "pin.h"
#include "spi.h"
#include <stdint.h>

// https://stackoverflow.com/questions/17052443/c-function-inside-struct
typedef uint8_t (*readReg_t) (uint8_t); 


/*! \brief SPI cannel class
 * This class cpntains the pin and spi channel number 
 * select.
 * */
typedef struct{
	pinNo_t pin,	/*!< pin number */	
	spiCH_t spi		/*!< spi hardware channel number */
}spi_ch_t;

/**
 * \brief Read register
 * Read one byte from a one register with one byte address.
 * \param *spi_ch spi pointer to spi channel object
 * \param reg_address register address
 * \return register content
 */
uint8_t spiCH_readReg(spi_ch_t *spi_ch,
					  uint8_t reg_address);

/**
 * \brief Read Block
 * Read a block of data starting at a given start address.
 * This function makes use of the auto register increment of the device to be read from. 
 * The address will be sent once and then data is read.
 * \param *spi_ch pointer to spi cannel object
 * \param start_address start address to the first register
 * \param *buffer pointer to the buffer in which the read content is written into
 * \param buf_len length of buffer
 */
void spiCH_autoReadBlock(spi_ch_t *spi_ch,
						 uint8_t start_address,
						 uint8_t* buffer,
						 uint8_t buf_len);

/**
 * \brief Write register
 * Write one byte to one register with one byte address.
 * \param *spi_ch pointer to spi channel object
 * \param reg_address register address
 * \param data data byte to be written into register
 *
 */
void spiCH_writeReg(spi_ch_t *spi_ch,
					uint8_t reg_address,
					uint8_t data);	
/**
 * \brief  Write data  block
 * Write a block of data starting at a given start address.
 * This function makes use of the auto register increment of the device to be written to. The
 * address will be sent once an then data is written.
 * \param *spi_ch pointer to spi channel object
 * \param start_address start address of the first reister
 * \param *data pointer to data to be written
 * \param data_len length of data to be written
 */
void spiCH_writeBlock(spi_ch_t *spi_ch,
					  uint8_t start_address,
					  const uint8_t *data,
					  uint8_t data_len);
#ifdef __cplusplus
}
#endif

#endif //_SPI_CH_H

