parent
41bfb2cdd7
commit
43a61d02b7
@ -0,0 +1,16 @@
|
||||
#ifndef _SPI_HPP_
|
||||
#define _SPI_HPP_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
template <typename Derived>
|
||||
struct SPI
|
||||
{
|
||||
uint8_t trx_u8(const uint8_t& data);
|
||||
|
||||
void tx(const uint8_t& data);
|
||||
|
||||
uint8_t rx();
|
||||
};
|
||||
|
||||
#endif // _SPI_HPP_
|
@ -0,0 +1,68 @@
|
||||
#ifndef _SPICH_HPP_
|
||||
#define _SPICH_HPP_
|
||||
|
||||
#include "pin.hpp"
|
||||
#include "spi.hpp"
|
||||
|
||||
template <typename DerivedPin,
|
||||
typename DerivedSPI>
|
||||
struct SPICH
|
||||
{
|
||||
SPICH(Pin<DerivedPin>& csPin,
|
||||
SPI<DerivedSPI>& spi) :
|
||||
chipSelect(csPin),
|
||||
spiHwCH(spi)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
uint8_t read_write_u8(const uint8_t& data)
|
||||
{
|
||||
uint8_t temp;
|
||||
chipSelect.write(false);
|
||||
|
||||
temp = spiHwCH.trx_u8(data);
|
||||
|
||||
chipSelect.write(true);
|
||||
return temp;
|
||||
}
|
||||
|
||||
void readArray(const uint8_t& address,
|
||||
uint8_t[] buffer,
|
||||
const uint8_t& len)
|
||||
{
|
||||
chipSelect.write(false);
|
||||
|
||||
spiHwCH.tx(address);
|
||||
|
||||
for(uint8_t i = 0; i < len;i++) {
|
||||
buffer[i] = spiHwCH.rx();
|
||||
};
|
||||
|
||||
chipSelect.write(true);
|
||||
}
|
||||
|
||||
|
||||
void writeArray(const uint8_t& address,
|
||||
const uint8_t[] buffer,
|
||||
const uint8_t& len)
|
||||
{
|
||||
chipSelect.write(false);
|
||||
|
||||
spiHwCH.tx(address);
|
||||
|
||||
for(uint8_t i = 0; i < len;i++) {
|
||||
spiHwCH.tx(buffer[i]);
|
||||
};
|
||||
|
||||
chipSelect.write(true);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// hardware resources
|
||||
Pin<DerivedPin> chipSelect;
|
||||
SPI<DerivedSPI> spiHwCH;
|
||||
};
|
||||
|
||||
#endif // _SPICH_HPP_
|
Loading…
Reference in new issue