Skip to content

Commit 91b230e

Browse files
committed
Use timeout when waiting for dreq in sci mode
Default timeout (50ms) chosen, such that all sci operations have completed within the chosen timeout. See datasheet, p.38.
1 parent 16044ba commit 91b230e

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

src/VS1053.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,24 @@ 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+
await_data_request_with_timeout();
44+
spi_write(3); // Read operation
45+
spi_write(_reg); // Register to write (0..0xF)
4546
// Note: transfer16 does not seem to work
4647
result = (SPI.transfer(0xFF) << 8) | // Read 16 bits data
4748
(SPI.transfer(0xFF));
48-
await_data_request(); // Wait for DREQ to be HIGH again
49+
await_data_request_with_timeout(); // Wait for DREQ to be HIGH again
4950
control_mode_off();
5051
return result;
5152
}
5253

5354
void VS1053::writeRegister(uint8_t _reg, uint16_t _value) const {
5455
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
58-
await_data_request();
56+
await_data_request_with_timeout();
57+
spi_write(2); // Write operation
58+
spi_write(_reg); // Register to write (0..0xF)
59+
spi_write16(_value); // Send 16 bits data
60+
await_data_request_with_timeout();
5961
control_mode_off();
6062
}
6163

src/VS1053.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ class VS1053 {
9191
}
9292
}
9393

94+
// min CLKI is 12MHZ, max time until dreq high is 450CLKI (data sheet p. 38)
95+
// => dreq timeout 50ms is on the safe side
96+
inline void await_data_request_with_timeout(uint32_t timeoutMs = 50) const {
97+
uint32_t absTimeoutMs = millis() + timeoutMs;
98+
while (!digitalRead(dreq_pin) && absTimeoutMs < millis()) {
99+
yield();
100+
}
101+
}
102+
94103
inline void control_mode_on() const {
95104
SPI.beginTransaction(VS1053_SPI); // Prevent other SPI users
96105
digitalWrite(dcs_pin, HIGH); // Bring slave in control mode

0 commit comments

Comments
 (0)