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.
181 lines
3.8 KiB
181 lines
3.8 KiB
#include "spi.h"
|
|
|
|
// generic implementation of spi channel class
|
|
|
|
void spiSetupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin)
|
|
{
|
|
ch->pin = chipselectPin;
|
|
ch->spi = spi_hw_ch;
|
|
pinWrite(chipselectPin, 0);
|
|
}
|
|
|
|
uint8_t spiReadReg(spi_ch_t *spi_ch,
|
|
uint8_t reg_address) {
|
|
uint8_t buf;
|
|
|
|
// select target device
|
|
pinWrite(spi_ch->pin,0);
|
|
|
|
// send address of target register
|
|
spiTrx(spi_ch->spi, reg_address);
|
|
|
|
// read from target register
|
|
buf = spiTrx(spi_ch->spi,0x00);
|
|
|
|
// release target device
|
|
pinWrite(spi_ch->pin,1);
|
|
|
|
return buf;
|
|
}
|
|
|
|
void spiAutoReadBlock(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
|
|
spiTrx(spi_ch->spi, start_address);
|
|
|
|
// read block from device
|
|
for(;i < buf_len;i++) {
|
|
buffer[i] = spiTrx(spi_ch->spi, 0x00);
|
|
}
|
|
|
|
// release target device
|
|
pinWrite(spi_ch->pin,1);
|
|
}
|
|
|
|
void spiWriteReg(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
|
|
spiTrx(spi_ch->spi, reg_address);
|
|
|
|
// write to target register
|
|
spiTrx(spi_ch->spi, data);
|
|
|
|
// release target device
|
|
pinWrite(spi_ch->pin,1);
|
|
}
|
|
|
|
void spiWriteBlock(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
|
|
spiTrx(spi_ch->spi, start_address);
|
|
|
|
// read block from device
|
|
for(;i < data_len;i++) {
|
|
spiTrx(spi_ch->spi, data[i]);
|
|
}
|
|
|
|
// release target device
|
|
pinWrite(spi_ch->pin,1);
|
|
}
|
|
|
|
void spiWrite8bit(spi_ch_t *spi_ch,
|
|
uint8_t bits)
|
|
{
|
|
pinWrite(spi_ch->pin,0);
|
|
spiTrx(spi_ch->spi,bits);
|
|
pinWrite(spi_ch->pin,1);
|
|
}
|
|
|
|
uint8_t spiReadWrite8bit(spi_ch_t *spi_ch,
|
|
uint8_t bits)
|
|
{
|
|
uint8_t buf;
|
|
pinWrite(spi_ch->pin,0);
|
|
buf = spiTrx(spi_ch->spi,bits);
|
|
pinWrite(spi_ch->pin,1);
|
|
return buf;
|
|
}
|
|
|
|
void spiWrite16bit(spi_ch_t *spi_ch,
|
|
uint16_t bits)
|
|
{
|
|
pinWrite(spi_ch->pin,0);
|
|
if(spi_ch->format == LSB_FIRST) {
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 8));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
} else {
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 8));
|
|
}
|
|
pinWrite(spi_ch->pin,1);
|
|
}
|
|
|
|
uint16_t spiReadWrite16bit(spi_ch_t *spi_ch,
|
|
uint16_t bits)
|
|
{
|
|
uint16_t buf;
|
|
|
|
pinWrite(spi_ch->pin,0);
|
|
if(spi_ch->format == LSB_FIRST) {
|
|
buf = spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) << 8;
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
} else {
|
|
buf = spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) << 8;
|
|
}
|
|
pinWrite(spi_ch->pin,1);
|
|
|
|
return buf;
|
|
}
|
|
|
|
void spiWrite32bit(spi_ch_t *spi_ch,
|
|
uint32_t bits)
|
|
{
|
|
pinWrite(spi_ch->pin,0);
|
|
if(spi_ch->format == LSB_FIRST) {
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 32));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 16));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 8));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
} else {
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 8));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 16));
|
|
spiTrx(spi_ch->spi,(uint8_t)(bits >> 32));
|
|
}
|
|
pinWrite(spi_ch->pin,1);
|
|
}
|
|
|
|
uint32_t spiReadWrite32bit(spi_ch_t *spi_ch,
|
|
uint8_t bits)
|
|
{
|
|
uint32_t buf;
|
|
|
|
pinWrite(spi_ch->pin,0);
|
|
if(spi_ch->format == LSB_FIRST) {
|
|
buf = spiTrx(spi_ch->spi,(uint8_t)(bits >> 24)) << 24;
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 16)) << 16;
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) << 8;
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
} else {
|
|
buf = spiTrx(spi_ch->spi,(uint8_t)(bits));
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 8)) >> 8;
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 16)) >> 16;
|
|
buf |= spiTrx(spi_ch->spi,(uint8_t)(bits >> 24)) >> 24;
|
|
}
|
|
pinWrite(spi_ch->pin,1);
|
|
|
|
return buf;
|
|
}
|
|
|
|
|
|
|