Skip to content

Commit

Permalink
fix SPI operation order #43
Browse files Browse the repository at this point in the history
  • Loading branch information
runger1101001 committed May 10, 2024
1 parent 17be3a5 commit 69287af
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
9 changes: 5 additions & 4 deletions src/encoders/a1334/A1334.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ A1334::~A1334() {

void A1334::init(SPIClass* _spi) {
spi = _spi;
if (nCS>=0)
if (nCS>=0) {
pinMode(nCS, OUTPUT);
digitalWrite(nCS, HIGH);
digitalWrite(nCS, HIGH);
}
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
spi->begin();
readRawAngle(); // read an angle
Expand All @@ -42,13 +43,13 @@ A1334Angle A1334::readRawAngle() {


uint16_t A1334::spi_transfer16(uint16_t outdata) {
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
uint16_t result = spi->transfer16(outdata);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
return result;
};

6 changes: 3 additions & 3 deletions src/encoders/aeat8800q24/AEAT8800Q24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ void AEAT8800Q24::setConf2(AEAT8800Q24_CONF2_t value){
uint16_t AEAT8800Q24::transfer16SPI(uint16_t outValue) {
// delay 1us between switching the CS line to SPI
delayMicroseconds(1);
if (nCS >= 0)
digitalWrite(nCS, LOW);
spi->endTransaction();
spi->beginTransaction(spiSettings);
if (nCS >= 0)
digitalWrite(nCS, LOW);
uint16_t value = spi->transfer16(outValue);
spi->endTransaction();
if (nCS >= 0)
digitalWrite(nCS, HIGH);
spi->endTransaction();
// delay 1us between switching the CS line to SSI
delayMicroseconds(1);
spi->beginTransaction(ssiSettings);
Expand Down
9 changes: 5 additions & 4 deletions src/encoders/as5047/AS5047.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ AS5047::~AS5047() {

void AS5047::init(SPIClass* _spi) {
spi = _spi;
if (nCS>=0)
if (nCS>=0) {
pinMode(nCS, OUTPUT);
digitalWrite(nCS, HIGH);
digitalWrite(nCS, HIGH);
}
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
spi->begin();
readRawAngle(); // read an angle
Expand Down Expand Up @@ -230,13 +231,13 @@ uint16_t AS5047::calcParity(uint16_t data){


uint16_t AS5047::spi_transfer16(uint16_t outdata) {
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
uint16_t result = spi->transfer16(outdata);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
// TODO check parity
errorflag = ((result&AS5047_ERRFLG)>0);
return result;
Expand Down
17 changes: 9 additions & 8 deletions src/encoders/as5047u/AS5047U.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ AS5047U::~AS5047U() {

void AS5047U::init(SPIClass* _spi) {
spi = _spi;
if (nCS>=0)
if (nCS>=0) {
pinMode(nCS, OUTPUT);
digitalWrite(nCS, HIGH);
digitalWrite(nCS, HIGH);
}
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
spi->begin();
readRawAngle(); // read an angle
Expand Down Expand Up @@ -291,13 +292,13 @@ uint16_t AS5047U::nop16(){


uint16_t AS5047U::spi_transfer16(uint16_t outdata) {
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
uint16_t result = spi->transfer16(outdata);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
errorflag = ((result&AS5047U_ERROR)>0);
warningflag = ((result&AS5047U_WARNING)>0);
return result;
Expand All @@ -321,26 +322,26 @@ uint16_t AS5047U::writeRegister24(uint16_t reg, uint16_t data) {
buff[0] = (reg>>8)&0x3F;
buff[1] = reg&0xFF;
buff[2] = calcCRC(reg);
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
spi->transfer(buff, 3);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
errorflag = ((buff[0]&0x40)>0);
warningflag = ((buff[0]&0x80)>0);

buff[0] = (data>>8)&0x3F;
buff[1] = data&0xFF;
buff[2] = calcCRC(data);
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
spi->transfer(buff, 3);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
errorflag = ((buff[0]&0x40)>0);
warningflag = ((buff[0]&0x80)>0);

Expand Down
9 changes: 5 additions & 4 deletions src/encoders/as5048a/AS5048A.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ AS5048A::~AS5048A() {

void AS5048A::init(SPIClass* _spi) {
spi = _spi;
if (nCS>=0)
if (nCS>=0) {
pinMode(nCS, OUTPUT);
digitalWrite(nCS, HIGH);
digitalWrite(nCS, HIGH);
}
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
spi->begin();
readRawAngle(); // read an angle
Expand Down Expand Up @@ -103,13 +104,13 @@ uint16_t AS5048A::nop(){
}

uint16_t AS5048A::spi_transfer16(uint16_t outdata) {
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
uint16_t result = spi->transfer16(outdata);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();
// TODO check parity
errorflag = ((result&AS5048A_ERRFLG)>0);
return result;
Expand Down
4 changes: 2 additions & 2 deletions src/encoders/mt6816/MT6816.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ bool MT6816::parityCheck(uint16_t data) {
}

uint16_t MT6816::spi_transfer16(uint16_t outdata) {
spi->beginTransaction(settings);
if (nCS>=0)
digitalWrite(nCS, 0);
spi->beginTransaction(settings);
uint16_t result = spi->transfer16(outdata);
spi->endTransaction();
if (nCS>=0)
digitalWrite(nCS, 1);
spi->endTransaction();

return result;
}

0 comments on commit 69287af

Please sign in to comment.