Skip to content

Commit 04ab05c

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 478df8f commit 04ab05c

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
@@ -123,6 +123,32 @@ class VS1053 {
123123

124124
void sdi_send_fillers(size_t length);
125125

126+
inline void spi_write(uint8_t data) const {
127+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
128+
SPI.write(data);
129+
#else
130+
(void)SPI.transfer(data);
131+
#endif
132+
}
133+
134+
inline void spi_write16(uint16_t data) const {
135+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
136+
SPI.write16(data);
137+
#else
138+
(void)SPI.transfer16(data);
139+
#endif
140+
}
141+
142+
inline void spi_write_bytes(const uint8_t * data, uint32_t size) const {
143+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
144+
SPI.writeBytes(data, size);
145+
#else
146+
for (int i = 0; i < size; ++i) {
147+
SPI.transfer(data[i]);
148+
}
149+
#endif
150+
}
151+
126152
void wram_write(uint16_t address, uint16_t data);
127153

128154
uint16_t wram_read(uint16_t address);

0 commit comments

Comments
 (0)