Merge branch 'modularize' of https://git.keydev.me/kerem/ked into modularize
commit
c190bd5ca7
@ -0,0 +1,45 @@
|
||||
/**
|
||||
**************************************************************************************************
|
||||
* @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 <stdint.h>
|
||||
#include "hardwareDescription.h"
|
||||
|
||||
/** This is the spi hardware channel class*/
|
||||
|
||||
void spi_initMaster(spiCH_t spic_hw_ch);
|
||||
|
||||
|
||||
/*!
|
||||
* @brief Transmits and receives on byte of data
|
||||
* @param spi_hw_ch SPI hardware channel
|
||||
* @param tx_data 'tx_data' The pyte to be transmitted"
|
||||
* @return The received data
|
||||
*/
|
||||
uint8_t spi_trx(spiCH_t spi_hw_ch, uint8_t tx_data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _SPI_H_
|
||||
|
@ -0,0 +1,82 @@
|
||||
|
||||
|
||||
#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
|
||||
|
@ -0,0 +1,82 @@
|
||||
#include "spi_ch.h"
|
||||
|
||||
// generic implementation of spi channel class
|
||||
|
||||
|
||||
uint8_t spiCH_readReg(spi_ch_t *spi_ch,
|
||||
uint8_t reg_address) {
|
||||
uitn8_t buf;
|
||||
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of target register
|
||||
spi_trx(spi_ch->spi, reg_address);
|
||||
|
||||
// read from target register
|
||||
buf = spi_trx(spi->spi,0x00);
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void spiCH_autoReadBlock(spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
uint8_t* buffer,
|
||||
uint8_t buf_len) {
|
||||
uint8_t i = 0;
|
||||
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of starting register
|
||||
spi_trx(spi_ch->spi, reg_address);
|
||||
|
||||
// read block from device
|
||||
for(;i < buf_len;i++) {
|
||||
buffer[i] = spi_trx(spi_ch->spi, 0x00);
|
||||
}
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
void spiCH_writeReg(spi_ch_t *spi_ch,
|
||||
uint8_t reg_address,
|
||||
uint8_t data) {
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of target register
|
||||
spi_trx(spi_ch->spi, reg_address);
|
||||
|
||||
// write to target register
|
||||
spi_trx(spi->spi, data);
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
||||
void spiCH_writeBlock(spi_ch_t *spi_ch,
|
||||
uint8_t start_address,
|
||||
const uint8_t *data,
|
||||
uint8_t data_len) {
|
||||
uint8_t i = 0;
|
||||
|
||||
// select target device
|
||||
pinWrite(spi_ch->pin,0);
|
||||
|
||||
// send address of starting register
|
||||
spi_trx(spi_ch->spi, reg_address);
|
||||
|
||||
// read block from device
|
||||
for(;i < buf_len;i++) {
|
||||
spi_trx(spi_ch->spi, data[i]);
|
||||
}
|
||||
|
||||
// release target device
|
||||
pinWrite(spi_ch->pin,1);
|
||||
}
|
||||
|
Loading…
Reference in new issue