@@ -282,6 +282,36 @@ void Adafruit_SPIDevice::endTransaction(void) {
282
282
}
283
283
}
284
284
285
+ /* !
286
+ * @brief Assert/Deassert the CS pin if it is defined
287
+ * @param value The state the CS is set to
288
+ */
289
+ void Adafruit_SPIDevice::setChipSelect (int value) {
290
+ if (_cs != -1 ) {
291
+ digitalWrite (_cs, value);
292
+ }
293
+ }
294
+
295
+ /* !
296
+ * @brief Write a buffer or two to the SPI device, with transaction
297
+ * management.
298
+ * @brief Manually begin a transaction (calls beginTransaction if hardware
299
+ * SPI) with asserting the CS pin
300
+ */
301
+ void Adafruit_SPIDevice::beginTransactionWithAssertingCS () {
302
+ beginTransaction ();
303
+ setChipSelect (LOW);
304
+ }
305
+
306
+ /* !
307
+ * @brief Manually end a transaction (calls endTransaction if hardware SPI)
308
+ * with deasserting the CS pin
309
+ */
310
+ void Adafruit_SPIDevice::endTransactionWithDeassertingCS () {
311
+ setChipSelect (HIGH);
312
+ endTransaction ();
313
+ }
314
+
285
315
/* !
286
316
* @brief Write a buffer or two to the SPI device, with transaction
287
317
* management.
@@ -296,11 +326,8 @@ void Adafruit_SPIDevice::endTransaction(void) {
296
326
bool Adafruit_SPIDevice::write (const uint8_t *buffer, size_t len,
297
327
const uint8_t *prefix_buffer,
298
328
size_t prefix_len) {
299
- if (_spi) {
300
- _spi->beginTransaction (*_spiSetting);
301
- }
329
+ beginTransactionWithAssertingCS ();
302
330
303
- setChipSelect (LOW);
304
331
// do the writing
305
332
#if defined(ARDUINO_ARCH_ESP32)
306
333
if (_spi) {
@@ -320,11 +347,7 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
320
347
transfer (buffer[i]);
321
348
}
322
349
}
323
- setChipSelect (HIGH);
324
-
325
- if (_spi) {
326
- _spi->endTransaction ();
327
- }
350
+ endTransactionWithDeassertingCS ();
328
351
329
352
#ifdef DEBUG_SERIAL
330
353
DEBUG_SERIAL.print (F (" \t SPIDevice Wrote: " ));
@@ -361,17 +384,10 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
361
384
*/
362
385
bool Adafruit_SPIDevice::read (uint8_t *buffer, size_t len, uint8_t sendvalue) {
363
386
memset (buffer, sendvalue, len); // clear out existing buffer
364
- if (_spi) {
365
- _spi->beginTransaction (*_spiSetting);
366
- }
367
387
368
- setChipSelect (LOW );
388
+ beginTransactionWithAssertingCS ( );
369
389
transfer (buffer, len);
370
- setChipSelect (HIGH);
371
-
372
- if (_spi) {
373
- _spi->endTransaction ();
374
- }
390
+ endTransactionWithDeassertingCS ();
375
391
376
392
#ifdef DEBUG_SERIAL
377
393
DEBUG_SERIAL.print (F (" \t SPIDevice Read: " ));
@@ -405,11 +421,7 @@ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
405
421
bool Adafruit_SPIDevice::write_then_read (const uint8_t *write_buffer,
406
422
size_t write_len, uint8_t *read_buffer,
407
423
size_t read_len, uint8_t sendvalue) {
408
- if (_spi) {
409
- _spi->beginTransaction (*_spiSetting);
410
- }
411
-
412
- setChipSelect (LOW);
424
+ beginTransactionWithAssertingCS ();
413
425
// do the writing
414
426
#if defined(ARDUINO_ARCH_ESP32)
415
427
if (_spi) {
@@ -455,11 +467,7 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
455
467
DEBUG_SERIAL.println ();
456
468
#endif
457
469
458
- setChipSelect (HIGH);
459
-
460
- if (_spi) {
461
- _spi->endTransaction ();
462
- }
470
+ endTransactionWithDeassertingCS ();
463
471
464
472
return true ;
465
473
}
@@ -475,29 +483,11 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
475
483
* writes
476
484
*/
477
485
bool Adafruit_SPIDevice::write_and_read (uint8_t *buffer, size_t len) {
478
- if (_spi) {
479
- _spi->beginTransaction (*_spiSetting);
480
- }
481
-
482
- setChipSelect (LOW);
486
+ beginTransactionWithAssertingCS ();
483
487
transfer (buffer, len);
484
- setChipSelect (HIGH);
485
-
486
- if (_spi) {
487
- _spi->endTransaction ();
488
- }
488
+ endTransactionWithDeassertingCS ();
489
489
490
490
return true ;
491
491
}
492
492
493
- /* !
494
- * @brief Assert/Deassert the CS pin if it is defined
495
- * @param value The state the CS is set to
496
- */
497
- void Adafruit_SPIDevice::setChipSelect (int value) {
498
- if (_cs == -1 )
499
- return ;
500
- digitalWrite (_cs, value);
501
- }
502
-
503
493
#endif // SPI exists
0 commit comments