You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
KED/ked/csl/interfaces/spi.h

303 lines
7.1 KiB

/**
**************************************************************************************************
* @file spi.h
* @author Kerem Yollu & Edwin Koch
* @date 12.03.2022
* @version 1.0
**************************************************************************************************
* @brief This is the genral interface for spi.
*
* **Detailed Description :**
* This the spi interface and belongs to the interface layer.
*
**************************************************************************************************
*/
#ifndef _SPI_H_
#define _SPI_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "pin.h"
#include "spi.h"
#include <stdint.h>
#include "hardwareDescription.h"
typedef enum{
SPI_SLAVE,
SPI_MASTER
} spi_mode_t;
typedef enum{
NONINVERTED,
INVERTED
}spi_clkPol_t;
typedef enum{
CAPTURE_ON_FIRST_CLK_TRANSITION,
CAPTURE_ON_SECCOND_CLK_TRANSITION
} spi_phase_t;
typedef enum{
MSB_FIRST,
LSB_FIRST
}spi_framef_t;
typedef enum{
SPI_DOUPLEX,
SPI_SIMPLEX
}spi_comMode_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_framef_t format; /*!< spi format */
spi_clkPol_t clkPol; /*!< spi clock polarity*/
}spi_ch_t;
typedef uint8_t (*readReg_t) (uint8_t);
// generic code
/** TODO: setup init with all attributes
* TODO: setup auto pin set alternate function
* @brief This is the spi hardware channel class
* @param spi_hw_ch SPI hardware channel
*/
void spiInit(spiCH_t spi_ch,
spi_mode_t mode,
spi_clkPol_t clockPolarity,
spi_phase_t phase,
spi_framef_t frameFormat,
uint32_t prescaler);
/**
* \brief Set up SPI channel
* Set up a SPI channel by passig a hardware SPI channel and a chipselect pin.
* The chipselect pin will be set to high (chipselect is lowactive).
* \param *ch pointer to SPI channel
* \param spi_hw_ch SPI hardware channel
* \param chipslectPin designated pin for chipslect
*/
void spiSetupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin);
/**
* \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 spiReadReg(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 spiAutoReadBlock(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 spiWriteReg(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 spiWriteBlock(spi_ch_t *spi_ch,
uint8_t start_address,
const uint8_t *data,
uint8_t data_len);
/**
* \brief write 8 bits
* \param bits 8 bits
*/
void spiWrite8bit(spi_ch_t *spi_ch,
uint8_t bits);
/**
* \brief read and write simultainously 8 bits
* \param bits 8 bits
* \return 8 bits
*/
uint8_t spiReadWrite8bit(spi_ch_t *spi_ch,
uint8_t bits);
/**
* \brief write 16 bits
* \param bits 16 bits
*/
void spiWrite16bit(spi_ch_t *spi_ch,
uint16_t bits);
/**
* \brief read and write simultainously 16 bits
* \param bits 16 bits
* \return 16 bits
*/
uint16_t spiReadWrite16bit(spi_ch_t *spi_ch,
uint16_t bits);
/**
* \brief write 32 bits
* \param bits 32 bits
*/
void spiWrite32bit(spi_ch_t *spi_ch,
uint32_t bits);
/**
* \brief read and write simultainously 32 bits
* \param bits 32 bits
* \return 32 bits
*/
uint32_t spiReadWrite32bit(spi_ch_t *spi_ch,
uint8_t bits);
//implementation
/**
* @brief SPI hardware peripheral reset
* @param spi_hw_ch SPI hardware channel
*/
void spiReset(spiCH_t spi_hw_ch);
/**
* @brief Enable Bus for SPI
* @param spi_hw_ch SPI hardware channel
*/
void spiEnableBus(spiCH_t spi_hw_ch);
/**
* @brief Enable SPI hardware channel
* @param spi_hw_ch SPI hardware channel
*/
void spiEnable(spiCH_t spi_hw_ch);
/**
* @brief Dissable SPI hardware channel
* @param spi_hw_ch SPI hardware channel
*/
void spiDissable(spiCH_t spi_hw_ch);
/**
* @brief Set SPI operation mode (MASTER or SLAVE)
* @param spi_hw_ch SPI hardware channel
* @param mode
*/
void spiSetMode(spiCH_t spi_hw_ch, spi_mode_t mode);
/**
* @brief Set SPI clock polarity
* @param spi_hw_ch SPI hardware channel
* @param clkPol Clock polarity
*/
void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol);
/**
* @breif Get SPI polarity
* @param spi_hw_ch SPI hardware channel
* @return polarity
*/
spi_clkPol_t spiGetPolarity(spiCH_t spi_hw_ch);
/**
* @brief Set SPI clock phase
* @param spi_hw_ch SPI hardware channel
* @param phase
*/
void spiSetPhase(spiCH_t spi_hw_ch, spi_phase_t phase);
/**
* @brief Get SPI clock phase
* @param spi_hw_ch SPI hardware channel
* @return phase
*/
spi_phase_t spiGetPhase(spiCH_t spi_hw_ch);
/**
* @brief Set SPI frame format
* @param spi_hw_ch SPI hardware channel
* @param framef Frame format
*/
void spiSetFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef);
/**
* @brief Get SPI frame format
* @param spi_hw_ch SPI hardware channel
* @return Frame format
*/
spi_framef_t spiGetFrameFormat(spiCH_t spi_hw_ch);
/**
* @brief Set Clock Prescaler
* This is dependent on your target device. Please enter in the correct value.
* The entered Value will be masked with the maximal number of bits (truncated)
* @param spi_hw_ch SPI hardware channel
* @param clkDiv
*/
void spiSetClockPrescaler(spiCH_t spi_hw_ch,uint32_t clkDiv);
/**
* @brief Set communication Mode
* @param spi_hw_ch SPI hardware channel
* @param comMode
*/
void spiSetComMode(spiCH_t spi_hw_ch, spi_comMode_t comMode);
/**
* @brief Get Clock Prescaler
* @param spi_hw_ch SPI hardware channel
* @return prescaler
*/
uint32_t spiGetClockPrescaler(spiCH_t spi_hw_ch);
/*!
* @brief Transmits and receives on byte of data
* @param spi_hw_ch SPI hardware channel
* @param tx_data 'tx_data' The byte to be transmitted"
* @return The received data
*/
uint8_t spiTrx(spiCH_t spi_hw_ch, uint8_t tx_data);
#ifdef __cplusplus
}
#endif
#endif // _SPI_H_