diff --git a/README.md b/README.md index c189592..3706fd7 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,10 @@ To see debug messages please add build flags to your `platformio.ini` as below ( `build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG` +- for RP2040 using [arduino-pico core](https://github.com/earlephilhower/arduino-pico) + +`build_flags = -DDEBUG_RP2040_PORT=Serial -DDEBUG_VS1053=true` + The Serial Interface needs to be initialized in the `setup()`. ```c++ diff --git a/src/ConsoleLogger.h b/src/ConsoleLogger.h index 773b77d..40e5b4d 100644 --- a/src/ConsoleLogger.h +++ b/src/ConsoleLogger.h @@ -22,6 +22,8 @@ #define LOG(...) ESP_LOGD("ESP_VS1053", __VA_ARGS__) #elif defined(ARDUINO_ARCH_ESP8266) && defined(DEBUG_ESP_PORT) #define LOG(...) DEBUG_ESP_PORT.printf(__VA_ARGS__) + #elif defined(ARDUINO_ARCH_RP2040) && defined(DEBUG_RP2040_PORT) && defined(DEBUG_VS1053) + #define LOG(...) DEBUG_RP2040_PORT.printf(__VA_ARGS__) #else #define LOG(...) #endif diff --git a/src/VS1053.cpp b/src/VS1053.cpp index 0f17fe7..0759462 100644 --- a/src/VS1053.cpp +++ b/src/VS1053.cpp @@ -40,8 +40,8 @@ uint16_t VS1053::read_register(uint8_t _reg) const { uint16_t result; control_mode_on(); - SPI.write(3); // Read operation - SPI.write(_reg); // Register to read (0..0xF) + spi_write(3); // Read operation + spi_write(_reg); // Register to read (0..0xF) // Note: transfer16 does not seem to work result = (SPI.transfer(0xFF) << 8) | // Read 16 bits data (SPI.transfer(0xFF)); @@ -52,9 +52,9 @@ uint16_t VS1053::read_register(uint8_t _reg) const { void VS1053::writeRegister(uint8_t _reg, uint16_t _value) const { control_mode_on(); - SPI.write(2); // Write operation - SPI.write(_reg); // Register to write (0..0xF) - SPI.write16(_value); // Send 16 bits data + spi_write(2); // Write operation + spi_write(_reg); // Register to write (0..0xF) + spi_write16(_value); // Send 16 bits data await_data_request(); control_mode_off(); } @@ -71,7 +71,7 @@ void VS1053::sdi_send_buffer(uint8_t *data, size_t len) { chunk_length = vs1053_chunk_size; } len -= chunk_length; - SPI.writeBytes(data, chunk_length); + spi_write_bytes(data, chunk_length); data += chunk_length; } data_mode_off(); @@ -90,7 +90,7 @@ void VS1053::sdi_send_fillers(size_t len) { } len -= chunk_length; while (chunk_length--) { - SPI.write(endFillByte); + spi_write(endFillByte); } } data_mode_off(); diff --git a/src/VS1053.h b/src/VS1053.h index 9faf7b6..8eec1e6 100644 --- a/src/VS1053.h +++ b/src/VS1053.h @@ -40,6 +40,10 @@ #include "patches/vs1053b-patches.h" +#ifndef _BV +#define _BV(bit) (1 << (bit)) +#endif + enum VS1053_I2S_RATE { VS1053_I2S_RATE_192_KHZ, VS1053_I2S_RATE_96_KHZ, @@ -119,6 +123,32 @@ class VS1053 { void sdi_send_fillers(size_t length); + inline void spi_write(uint8_t data) const { +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) + SPI.write(data); +#else + (void)SPI.transfer(data); +#endif + } + + inline void spi_write16(uint16_t data) const { +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) + SPI.write16(data); +#else + (void)SPI.transfer16(data); +#endif + } + + inline void spi_write_bytes(const uint8_t * data, uint32_t size) const { +#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) + SPI.writeBytes(data, size); +#else + for (int i = 0; i < size; ++i) { + SPI.transfer(data[i]); + } +#endif + } + void wram_write(uint16_t address, uint16_t data); uint16_t wram_read(uint16_t address);