-
Notifications
You must be signed in to change notification settings - Fork 27
Open
Description
Hello, nice job!
The arduino's current hardware library has a 16-bit transfer routine. Would it be possible to add this routine to the library via software?
This seems to be the original code (Arduino IDE source):
inline static uint16_t transfer16(uint16_t data) {
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
in.val = data;
if (!(SPCR & _BV(DORD))) {
SPDR = in.msb;
asm volatile("nop"); // See transfer(uint8_t) function
while (!(SPSR & _BV(SPIF))) ;
out.msb = SPDR;
SPDR = in.lsb;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ;
out.lsb = SPDR;
} else {
SPDR = in.lsb;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ;
out.lsb = SPDR;
SPDR = in.msb;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ;
out.msb = SPDR;
}
return out.val;
}
The equivalent could be this way, as below?
inline static uint16_t transfer16(uint16_t data, bool MSB1ST = true) {
union {
uint16_t val;
struct {
uint8_t lsb;
uint8_t msb;
};
} in, out;
in.val = data;
if (MSB1ST) {
out.msb = mySPI.transfer(in.msb);
out.lsb = mySPI.transfer(in.lsb);
} else {
out.lsb = mySPI.transfer(in.lsb);
out.msb = mySPI.transfer(in.msb);
}
return out.val;
}
Thank you!
Metadata
Metadata
Assignees
Labels
No labels