From 3498d548a9ee36d70ecec0bca1069d88549b8f86 Mon Sep 17 00:00:00 2001 From: Steve Lascos Date: Sun, 12 Nov 2017 14:44:39 -0500 Subject: [PATCH] Fixed a read bug in BASpiMemory for transfer16() --- src/BASpiMemory.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ src/BASpiMemory.h | 3 +++ 2 files changed, 68 insertions(+) diff --git a/src/BASpiMemory.cpp b/src/BASpiMemory.cpp index 89dadad..46471ab 100644 --- a/src/BASpiMemory.cpp +++ b/src/BASpiMemory.cpp @@ -132,6 +132,36 @@ void BASpiMemory::write(int address, int data) digitalWrite(m_csPin, HIGH); } +void BASpiMemory::write16(int address, uint16_t data) +{ + switch (m_memDeviceId) { + case SpiDeviceId::SPI_DEVICE0 : + SPI.beginTransaction(m_settings); + digitalWrite(m_csPin, LOW); + SPI.transfer16((SPI_WRITE_CMD << 8) | (address >> 16) ); + SPI.transfer16(address & 0xFFFF); + SPI.transfer16(data); + SPI.endTransaction(); + break; + +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) + case SpiDeviceId::SPI_DEVICE1 : + SPI1.beginTransaction(m_settings); + digitalWrite(m_csPin, LOW); + SPI1.transfer16((SPI_WRITE_CMD << 8) | (address >> 16) ); + SPI1.transfer16(address & 0xFFFF); + SPI1.transfer16(data); + SPI1.endTransaction(); + break; +#endif + + default : + break; + // unreachable + } + digitalWrite(m_csPin, HIGH); +} + // single address read int BASpiMemory::read(int address) { @@ -172,4 +202,39 @@ int BASpiMemory::read(int address) return data; } +uint16_t BASpiMemory::read16(int address) +{ + + uint16_t data=0; + + switch (m_memDeviceId) { + case SpiDeviceId::SPI_DEVICE0 : + SPI.beginTransaction(m_settings); + digitalWrite(m_csPin, LOW); + SPI.transfer16((SPI_READ_CMD << 8) | (address >> 16) ); + SPI.transfer16(address & 0xFFFF); + data = SPI.transfer16(0); + SPI.endTransaction(); + break; + +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) + case SpiDeviceId::SPI_DEVICE1 : + SPI1.beginTransaction(m_settings); + digitalWrite(m_csPin, LOW); + SPI1.transfer16((SPI_READ_CMD << 8) | (address >> 16) ); + SPI1.transfer16(address & 0xFFFF); + data = SPI1.transfer16(0); + SPI1.endTransaction(); + break; +#endif + + default: + break; + // unreachable + } + + digitalWrite(m_csPin, HIGH); + return data; +} + } /* namespace BAGuitar */ diff --git a/src/BASpiMemory.h b/src/BASpiMemory.h index 9dcc84b..316fbb2 100644 --- a/src/BASpiMemory.h +++ b/src/BASpiMemory.h @@ -60,10 +60,13 @@ public: /// @param data the value to write void write(int address, int data); + void write16(int address, uint16_t data); + /// read a single data word from the specified address /// @param address the address in the SPI RAM to read from /// @return the data that was read int read(int address); + uint16_t read16(int address); private: SpiDeviceId m_memDeviceId; // the MEM device being control with this instance