@@ -68,8 +68,8 @@ void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config,
68
68
m_parityMode = static_cast <SoftwareSerialParity>(config & 070 );
69
69
m_stopBits = 1 + ((config & 0300 ) ? 1 : 0 );
70
70
m_pduBits = m_dataBits + static_cast <bool >(m_parityMode) + m_stopBits;
71
- m_bit_us = (1000000 + baud / 2 ) / baud;
72
- m_bitCycles = (ESP.getCpuFreqMHz () * 1000000 + baud / 2 ) / baud;
71
+ m_bit_us = (1000000UL + baud / 2 ) / baud;
72
+ m_bitCycles = (ESP.getCpuFreqMHz () * 1000000UL + baud / 2 ) / baud;
73
73
m_intTxEnabled = true ;
74
74
if (isValidGPIOpin (m_rxPin)) {
75
75
std::unique_ptr<circular_queue<uint8_t > > buffer (new circular_queue<uint8_t >((bufCapacity > 0 ) ? bufCapacity : 64 ));
@@ -116,7 +116,7 @@ void SoftwareSerial::end()
116
116
}
117
117
118
118
uint32_t SoftwareSerial::baudRate () {
119
- return ESP.getCpuFreqMHz () * 1000000 / m_bitCycles;
119
+ return ESP.getCpuFreqMHz () * 1000000UL / m_bitCycles;
120
120
}
121
121
122
122
void SoftwareSerial::setTransmitEnablePin (int8_t txEnablePin) {
@@ -155,7 +155,7 @@ void SoftwareSerial::enableRx(bool on) {
155
155
m_rxCurBit = m_pduBits - 1 ;
156
156
// Init to stop bit level and current cycle
157
157
m_isrLastCycle = (ESP.getCycleCount () | 1 ) ^ m_invert;
158
- if (m_bitCycles >= (ESP.getCpuFreqMHz () * 1000000U ) / 74880U )
158
+ if (m_bitCycles >= (ESP.getCpuFreqMHz () * 1000000UL ) / 74880UL )
159
159
attachInterruptArg (digitalPinToInterrupt (m_rxPin), reinterpret_cast <void (*)(void *)>(rxBitISR), this , CHANGE);
160
160
else
161
161
attachInterruptArg (digitalPinToInterrupt (m_rxPin), reinterpret_cast <void (*)(void *)>(rxBitSyncISR), this , m_invert ? RISING : FALLING);
@@ -187,14 +187,14 @@ int SoftwareSerial::read() {
187
187
return val;
188
188
}
189
189
190
- size_t SoftwareSerial::readBytes (uint8_t * buffer, size_t size) {
191
- if (!m_rxValid) { return - 1 ; }
190
+ size_t SoftwareSerial::read (uint8_t * buffer, size_t size) {
191
+ if (!m_rxValid) { return 0 ; }
192
192
size_t avail;
193
193
if (0 == (avail = m_buffer->pop_n (buffer, size))) {
194
194
rxBits ();
195
195
avail = m_buffer->pop_n (buffer, size);
196
196
}
197
- if (!avail) return - 1 ;
197
+ if (!avail) return 0 ;
198
198
if (m_parityBuffer) {
199
199
uint32_t parityBits = avail;
200
200
while (m_parityOutPos >>= 1 ) ++parityBits;
@@ -204,12 +204,25 @@ size_t SoftwareSerial::readBytes(uint8_t * buffer, size_t size) {
204
204
return avail;
205
205
}
206
206
207
+ size_t SoftwareSerial::readBytes (uint8_t * buffer, size_t size) {
208
+ if (!m_rxValid || !size) { return 0 ; }
209
+ size_t count = 0 ;
210
+ const auto timeout = _timeout * ESP.getCpuFreqMHz () * 1000UL ;
211
+ const auto start = ESP.getCycleCount ();
212
+ do {
213
+ count += read (&buffer[count], size - count);
214
+ if (count >= size) break ;
215
+ yield ();
216
+ } while (ESP.getCycleCount () - start < timeout);
217
+ return count;
218
+ }
219
+
207
220
int SoftwareSerial::available () {
208
221
if (!m_rxValid) { return 0 ; }
209
222
rxBits ();
210
223
int avail = m_buffer->available ();
211
224
if (!avail) {
212
- optimistic_yield (10000 );
225
+ optimistic_yield (10000UL );
213
226
}
214
227
return avail;
215
228
}
@@ -228,7 +241,7 @@ void ICACHE_RAM_ATTR SoftwareSerial::preciseDelay(bool sync) {
228
241
// Disable interrupts again
229
242
if (!m_intTxEnabled) { m_savedPS = xt_rsil (15 ); }
230
243
}
231
- while ((ESP.getCycleCount () - m_periodStart) < m_periodDuration) { if (!sync ) optimistic_yield (10000 ); }
244
+ while ((ESP.getCycleCount () - m_periodStart) < m_periodDuration) { if (!sync ) optimistic_yield (10000UL ); }
232
245
resetPeriodStart ();
233
246
}
234
247
0 commit comments