Skip to content

Commit 096f609

Browse files
committed
Use SPI.write* only for ESP, fallback to transfer*
generic arduino SPI API only knows about SPI.transfer*. SPI.write* is ESP3266 specific.
1 parent 9ecd296 commit 096f609

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/VS1053.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ uint16_t VS1053::read_register(uint8_t _reg) const {
4040
uint16_t result;
4141

4242
control_mode_on();
43-
SPI.write(3); // Read operation
44-
SPI.write(_reg); // Register to write (0..0xF)
43+
spi_write(3); // Read operation
44+
spi_write(_reg); // Register to write (0..0xF)
4545
// Note: transfer16 does not seem to work
4646
result = (SPI.transfer(0xFF) << 8) | // Read 16 bits data
4747
(SPI.transfer(0xFF));
@@ -52,9 +52,9 @@ uint16_t VS1053::read_register(uint8_t _reg) const {
5252

5353
void VS1053::writeRegister(uint8_t _reg, uint16_t _value) const {
5454
control_mode_on();
55-
SPI.write(2); // Write operation
56-
SPI.write(_reg); // Register to write (0..0xF)
57-
SPI.write16(_value); // Send 16 bits data
55+
spi_write(2); // Write operation
56+
spi_write(_reg); // Register to write (0..0xF)
57+
spi_write16(_value); // Send 16 bits data
5858
await_data_request();
5959
control_mode_off();
6060
}
@@ -71,7 +71,7 @@ void VS1053::sdi_send_buffer(uint8_t *data, size_t len) {
7171
chunk_length = vs1053_chunk_size;
7272
}
7373
len -= chunk_length;
74-
SPI.writeBytes(data, chunk_length);
74+
spi_write_bytes(data, chunk_length);
7575
data += chunk_length;
7676
}
7777
data_mode_off();
@@ -90,7 +90,7 @@ void VS1053::sdi_send_fillers(size_t len) {
9090
}
9191
len -= chunk_length;
9292
while (chunk_length--) {
93-
SPI.write(endFillByte);
93+
spi_write(endFillByte);
9494
}
9595
}
9696
data_mode_off();

src/VS1053.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@ class VS1053 {
111111

112112
void sdi_send_fillers(size_t length);
113113

114+
inline void spi_write(uint8_t data) const {
115+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
116+
SPI.write(data);
117+
#else
118+
(void)SPI.transfer(data);
119+
#endif
120+
}
121+
122+
inline void spi_write16(uint16_t data) const {
123+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
124+
SPI.write16(data);
125+
#else
126+
(void)SPI.transfer16(data);
127+
#endif
128+
}
129+
130+
inline void spi_write_bytes(const uint8_t * data, uint32_t size) const {
131+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
132+
SPI.writeBytes(data, size);
133+
#else
134+
for (int i = 0; i < size; ++i) {
135+
SPI.transfer(data[i]);
136+
}
137+
#endif
138+
}
139+
114140
void wram_write(uint16_t address, uint16_t data);
115141

116142
uint16_t wram_read(uint16_t address);

0 commit comments

Comments
 (0)