#include #include //#define DEBUG_SERIAL Serial /*! * @brief Create an SPI device with the given CS pin and settins * @param cspin The arduino pin number to use for chip select * @param freq The SPI clock frequency to use, defaults to 1MHz * @param dataOrder The SPI data order to use for bits within each byte, defaults to SPI_MSBFIRST * @param dataMode The SPI mode to use, defaults to SPI_MODE0 * @param theSPI The SPI bus to use, defaults to &theSPI */ Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t cspin, uint32_t freq, BitOrder dataOrder, uint8_t dataMode, SPIClass *theSPI) { _cs = cspin; _spi = theSPI; _begun = false; _spiSetting = new SPISettings(freq, dataOrder, dataMode); } /*! * @brief Initializes SPI bus and sets CS pin high * @return Always returns true because there's no way to test success of SPI init */ bool Adafruit_SPIDevice::begin(void) { _spi->begin(); pinMode(_cs, OUTPUT); digitalWrite(_cs, HIGH); _begun = true; return true; } /*! * @brief Write a buffer or two to the SPI device. * @param buffer Pointer to buffer of data to write * @param len Number of bytes from buffer to write * @param prefix_buffer Pointer to optional array of data to write before buffer. * @param prefix_len Number of bytes from prefix buffer to write * @return Always returns true because there's no way to test success of SPI writes */ bool Adafruit_SPIDevice::write(uint8_t *buffer, size_t len, uint8_t *prefix_buffer, size_t prefix_len) { _spi->beginTransaction(*_spiSetting); digitalWrite(_cs, LOW); // do the writing for (size_t i=0; itransfer(prefix_buffer[i]); } for (size_t i=0; itransfer(buffer[i]); } digitalWrite(_cs, HIGH); _spi->endTransaction(); #ifdef DEBUG_SERIAL DEBUG_SERIAL.print(F("\tSPIDevice Wrote: ")); if ((prefix_len != 0) && (prefix_buffer != NULL)) { for (uint16_t i=0; ibeginTransaction(*_spiSetting); digitalWrite(_cs, LOW); _spi->transfer(buffer, len); digitalWrite(_cs, HIGH); _spi->endTransaction(); #ifdef DEBUG_SERIAL DEBUG_SERIAL.print(F("\tSPIDevice Read: ")); for (uint16_t i=0; ibeginTransaction(*_spiSetting); digitalWrite(_cs, LOW); // do the writing for (size_t i=0; itransfer(write_buffer[i]); } #ifdef DEBUG_SERIAL DEBUG_SERIAL.print(F("\tSPIDevice Wrote: ")); for (uint16_t i=0; itransfer(sendvalue); } #ifdef DEBUG_SERIAL DEBUG_SERIAL.print(F("\tSPIDevice Read: ")); for (uint16_t i=0; iendTransaction(); return true; }