From ed5597dad00d02e5ebe382e37fa8e8151f49742d Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Wed, 4 Nov 2015 15:49:08 -0500 Subject: [PATCH] Add SPI.transfer16(...) API's --- hardware/arduino/sam/libraries/SPI/SPI.cpp | 17 +++++++++++++++++ hardware/arduino/sam/libraries/SPI/SPI.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/hardware/arduino/sam/libraries/SPI/SPI.cpp b/hardware/arduino/sam/libraries/SPI/SPI.cpp index a3bdfea5a..ec1aaa647 100644 --- a/hardware/arduino/sam/libraries/SPI/SPI.cpp +++ b/hardware/arduino/sam/libraries/SPI/SPI.cpp @@ -196,6 +196,23 @@ byte SPIClass::transfer(byte _pin, uint8_t _data, SPITransferMode _mode) { return d & 0xFF; } +uint16_t SPIClass::transfer16(byte _pin, uint16_t _data, SPITransferMode _mode) { + union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t; + uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(_pin); + + t.val = _data; + + if (bitOrder[ch] == LSBFIRST) { + t.lsb = transfer(_pin, t.lsb, _mode); + t.msb = transfer(_pin, t.msb, _mode); + } else { + t.msb = transfer(_pin, t.msb, _mode); + t.lsb = transfer(_pin, t.lsb, _mode); + } + + return t.val; +} + void SPIClass::transfer(byte _pin, void *_buf, size_t _count, SPITransferMode _mode) { if (_count == 0) return; diff --git a/hardware/arduino/sam/libraries/SPI/SPI.h b/hardware/arduino/sam/libraries/SPI/SPI.h index da5b8f64d..ef5b5141a 100644 --- a/hardware/arduino/sam/libraries/SPI/SPI.h +++ b/hardware/arduino/sam/libraries/SPI/SPI.h @@ -82,9 +82,11 @@ class SPIClass { // Transfer functions byte transfer(byte _pin, uint8_t _data, SPITransferMode _mode = SPI_LAST); + uint16_t transfer16(byte _pin, uint16_t _data, SPITransferMode _mode = SPI_LAST); void transfer(byte _pin, void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST); // Transfer functions on default pin BOARD_SPI_DEFAULT_SS byte transfer(uint8_t _data, SPITransferMode _mode = SPI_LAST) { return transfer(BOARD_SPI_DEFAULT_SS, _data, _mode); } + uint16_t transfer16(uint16_t _data, SPITransferMode _mode = SPI_LAST) { return transfer16(BOARD_SPI_DEFAULT_SS, _data, _mode); } void transfer(void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST) { transfer(BOARD_SPI_DEFAULT_SS, _buf, _count, _mode); } // Transaction Functions