From 2c0786c1817643d8a09420be7139dcc8bedfa887 Mon Sep 17 00:00:00 2001 From: mikaelpatel Date: Thu, 21 Sep 2017 16:58:25 +0200 Subject: [PATCH] Refactoring; moving SRAM driver to Arduino-Storage library --- README.md | 9 +- examples/SRAM/SRAM.ino | 68 ----------- examples/Stream/Stream.ino | 123 -------------------- library.properties | 2 +- mainpage.dox | 2 +- src/Driver/SRAM.h | 230 ------------------------------------- 6 files changed, 3 insertions(+), 431 deletions(-) delete mode 100644 examples/SRAM/SRAM.ino delete mode 100644 examples/Stream/Stream.ino delete mode 100644 src/Driver/SRAM.h diff --git a/README.md b/README.md index 8c526ad..f7f7609 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ bus managers and device driver support. the library contains bus manager implementations for hardware resources (AVR SPI and USI) and software using Ardino-GPIO. -Version: 1.7 +Version: 1.8 ## Classes @@ -13,18 +13,11 @@ Version: 1.7 * [Hardware SPI Bus Manager, Hardware::SPI](./src/Hardware/SPI.h) * [Software SPI Bus Manager, Software::SPI](./src/Software/SPI.h) -## Drivers - -* [23LC1024, 1 Mbit Serial SRAM, SRAM](./src/Driver/SRAM.h) -* [SRAM Stream, SRAM::Stream](./src/Driver/SRAM.h) - ## Example Sketches * [Benchmark](./examples/Benchmark) * [ShiftIn](./examples/ShiftIn) * [ShiftOut](./examples/ShiftOut) -* [SRAM](./examples/SRAM) -* [Stream](./examples/Stream) ## Benchmarks diff --git a/examples/SRAM/SRAM.ino b/examples/SRAM/SRAM.ino deleted file mode 100644 index 0f0b8cb..0000000 --- a/examples/SRAM/SRAM.ino +++ /dev/null @@ -1,68 +0,0 @@ -#include "GPIO.h" -#include "SPI.h" -#include "Driver/SRAM.h" - -// Configuration: SPI -// #define USE_SOFTWARE_SPI -#define USE_HARDWARE_SPI - -#if defined(USE_SOFTWARE_SPI) -#include "Software/SPI.h" -#if defined(ARDUINO_attiny) -Software::SPI spi; -#else -Software::SPI spi; -#endif -#elif defined(USE_HARDWARE_SPI) -#include "Hardware/SPI.h" -Hardware::SPI spi; -#endif - -SRAM sram(spi); - -const size_t BUF_MAX = 1000; -static uint8_t buf[BUF_MAX]; - -void setup() -{ - Serial.begin(57600); - while (!Serial); - - for (size_t i = 0; i < BUF_MAX; i++) buf[i] = i; -} - -void loop() -{ - // SRAM performance: N is number of bytes - // write: 9.875 + N*1.4275 us, max. 700 kbyte/s - // read: 9.875 + N*1.5 us, max. 670 kbyte/s - - // 9.875/9.875 us, protocol overhead - sram.write(0x0000, buf, 0); - sram.read(buf, 0x0000, 0); - - // 11.56/11.62 us/byte, 90 kbyte/s, 1 byte - sram.write(0x0001, buf, 1); - sram.read(buf, 0x0001, 1); - - // 2.45/2.49 us/byte, 410 kbyte/s, 10 byte - sram.write(0x0010, buf, 10); - sram.read(buf, 0x0010, 10); - - // 1.539/1.599 us/byte, 650 kbyte/s, 100 byte - sram.write(0x0100, buf, 100); - sram.read(buf, 0x00100, 100); - - // 1.454/1.522 us/byte, 690 kbyte/s, 1000 byte - sram.write(0x1000, buf, sizeof(buf)); - sram.read(buf, 0x1000, sizeof(buf)); - - // Print buffer and increment values - for (size_t i = 0; i < BUF_MAX; i++) { - Serial.print(buf[i]); - Serial.print(' '); - buf[i] = buf[i] + 1; - } - Serial.println(); - delay(500); -} diff --git a/examples/Stream/Stream.ino b/examples/Stream/Stream.ino deleted file mode 100644 index 0bb4a13..0000000 --- a/examples/Stream/Stream.ino +++ /dev/null @@ -1,123 +0,0 @@ -#include "GPIO.h" -#include "SPI.h" -#include "Driver/SRAM.h" - -// #define USE_SOFTWARE_SPI -#define USE_HARDWARE_SPI - -#if defined(USE_SOFTWARE_SPI) -#include "Software/SPI.h" -#if defined(ARDUINO_attiny) -Software::SPI spi; -#else -Software::SPI spi; -#endif -#elif defined(USE_HARDWARE_SPI) -#include "Hardware/SPI.h" -Hardware::SPI spi; -#endif - -SRAM sram(spi); -SRAM::Stream<10000> ios(sram); -SRAM::Stream<10000> temps(sram, 10000); - -const int N = 1000; -int count = 0; -uint32_t s0, m0, m1, m2, m3, m4, m5, m6, m7; - -void setup() -{ - Serial.begin(57600); - while (!Serial); -} - -void loop() -{ - // Print N analog samples to serial stream - s0 = micros(); - for (int i = 0; i < N; i++) { - Serial.println(analogRead(A0)); - } - Serial.println(); - Serial.flush(); - m0 = micros() - s0; - - // Print N analog samples to sram stream - s0 = micros(); - for (int i = 0; i < N; i++) { - ios.println(analogRead(A0)); - } - ios.println(); - m1 = micros() - s0; - - // Transfer data between two sram streams - count = ios.available(); - s0 = micros(); - while (ios.available()) - temps.write(ios.read()); - m2 = micros() - s0; - - // Print data from sram stream to serial stream - s0 = micros(); - while (temps.available()) - Serial.write(temps.read()); - Serial.flush(); - m3 = micros() - s0; - - // Write to sram stream - s0 = micros(); - for (int i = 0; i < count; i++) ios.write(i); - m4 = micros() - s0; - - // Read from sram stream - s0 = micros(); - uint16_t sum = 0; - for (int i = 0; i < count; i++) sum += ios.read(); - m5 = micros() - s0; - - // Write N analog samples to sram stream - s0 = micros(); - int sample; - for (int i = 0; i < N; i++) { - sample = analogRead(A0); - ios.write((const uint8_t*) &sample, sizeof(sample)); - } - m6 = micros() - s0; - - // Read N analog samples from sram stream and print to serial - s0 = micros(); - while (ios.available()) { - ios.readBytes((uint8_t*) &sample, sizeof(sample)); - Serial.println(sample); - } - Serial.flush(); - m7 = micros() - s0; - - Serial.print(F("Samples, N = ")); - Serial.println(N); - Serial.print(F("Serial.print, m0 = ")); - Serial.println(m0 / N); - Serial.print(F("SRAM::Stream.print, m1 = ")); - Serial.println(m1 / N); - Serial.println(); - Serial.print(F("SRAM::Stream.available, count = ")); - Serial.println(count); - Serial.print(F("SRAM::Stream.write/read, m2 = ")); - Serial.println(m2 / count); - Serial.print(F("SRAM::Stream.read/Serial.write, m3 = ")); - Serial.println(m3 / count); - Serial.print(F("SRAM::Stream.write, m4 = ")); - Serial.println(m4 / count); - Serial.print(F("SRAM::Stream.read, m5 = ")); - Serial.println(m5 / count); - Serial.println(); - Serial.print(F("SRAM::Stream.available, N*2 = ")); - Serial.println(N * 2); - Serial.print(F("SRAM::Stream.write(2), m6 = ")); - Serial.println(m6 / N); - Serial.print(F("SRAM::Stream.read(2)/Serial.print, m7 = ")); - Serial.println(m7 / N); - Serial.println(); - - delay(1000); -} diff --git a/library.properties b/library.properties index 3fba1a7..3f98e08 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Arduino-SPI -version=1.7 +version=1.8 author=Mikael Patel maintainer=Mikael Patel sentence=Serial Peripheral Interface (SPI) library for Arduino. diff --git a/mainpage.dox b/mainpage.dox index e360ea7..898edb2 100644 --- a/mainpage.dox +++ b/mainpage.dox @@ -6,7 +6,7 @@ for bus managers and device driver support (SPI::Device). The library contains bus manager implementations for hardware resources (Hardware::SPI) and using Ardino-GPIO (Software::SPI). -Version: 1.7 +Version: 1.8 */ /** @page License diff --git a/src/Driver/SRAM.h b/src/Driver/SRAM.h deleted file mode 100644 index 1266fed..0000000 --- a/src/Driver/SRAM.h +++ /dev/null @@ -1,230 +0,0 @@ -/** - * @file SRAM.h - * @version 1.1 - * - * @section License - * Copyright (C) 2017, Mikael Patel - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - */ - -#ifndef SRAM_H -#define SRAM_H - -#include "SPI.h" - -/** - * Driver for the Microchip 23LC1024, 1M bit SPI Serial SRAM. - * @param[in] SS_PIN slave select board pin. - * @section Circuit - * @code - * 23LC1024 - * +------------+ - * (SS)----------------1-|CS U VCC|-8----------------(VCC) - * (MISO)--------------2-|SO HOLD|-7---------(VCC/PULLUP) - * (VCC/PULLUP)--------3-|NU SCK|-6----------------(SCK) - * (GND)---------------4-|VSS SI|-5---------------(MOSI) - * +------------+ - * @endcode - */ -template -class SRAM : protected SPI::Device<0, MSBFIRST, FREQ, SS_PIN> { -public: - /** Maximum device clock frequency. */ - static const uint32_t MAX_FREQ = 16000000L; - - /** - * Construct and initiate SRAM device driver with given slave select - * board pin and bus manager. - */ - SRAM(SPI& spi) : - SPI::Device<0, MSBFIRST, MAX_FREQ, SS_PIN>(spi) - {} - - /** - * Read count number of bytes from SRAM address to buffer. - * @param[in] dest destination buffer pointer. - * @param[in] src source memory address on device. - * @param[in] count number of bytes to read from device. - */ - void read(void* dest, uint32_t src, size_t count) - { - uint8_t* header = (uint8_t*) &src; - src = __builtin_bswap32(src); - header[0] = READ; - acquire(); - write(header, sizeof(src)); - read(dest, count); - release(); - } - - /** - * Write count number of bytes to SRAM address from buffer. - * @param[in] dest destination memory address on device. - * @param[in] src source buffer pointer. - * @param[in] count number of bytes to write to device. - */ - void write(uint32_t dest, const void* src, size_t count) - { - uint8_t* header = (uint8_t*) &dest; - dest = __builtin_bswap32(dest); - header[0] = WRITE; - acquire(); - write(header, sizeof(dest)); - write(src, count); - release(); - } - - /** - * SRAM based Stream of given size. Write/print intermediate data - * to the stream that may later be read and transfered. Multiple - * stream may be created on the same device by assigning start - * address and size. - * @param[in] SIZE number of bytes in stream. - */ - template - class Stream : public ::Stream { - public: - /** - * Construct stream on given sram device at the given start - * address. The storage size of the stream is given as a template - * parameter. - * @param[in] sram device for stream. - * @param[in] addr start address for stream (default 0). - */ - Stream(SRAM &sram, uint32_t addr = 0) : - m_sram(sram), - m_addr(addr), - m_put(0), - m_get(0), - m_count(0) - {} - - /** - * @override{Stream} - * Write given byte to stream. Return number of bytes written, - * zero if full. - * @param[in] byte to write. - * @return number of bytes written(1). - */ - virtual size_t write(uint8_t byte) - { - if (m_count == SIZE) return (0); - m_sram.write(m_addr + m_put, &byte, sizeof(byte)); - m_count += 1; - m_put += 1; - if (m_put == SIZE) m_put = 0; - return (sizeof(byte)); - } - - /** - * @override{Stream} - * Write given buffer and numbe of bytes to stream. Return number - * of bytes written. - * @param[in] bufffer to write. - * @param[in] size number of byets to write. - * @return number of bytes. - */ - virtual size_t write(const uint8_t *buffer, size_t size) - { - uint16_t room = SIZE - m_count; - if (room == 0) return (0); - if (size > room) size = room; - size_t res = size; - room = SIZE - m_put; - if (size > room) { - m_sram.write(m_addr + m_put, buffer, room); - buffer += room; - size -= room; - m_count += room; - m_put = 0; - } - m_sram.write(m_addr + m_put, buffer, size); - m_count += size; - m_put += size; - return (res); - } - - /** - * @override{Stream} - * Returns number of bytes available for read(). - * @return bytes available. - */ - virtual int available() - { - return (m_count); - } - - /** - * @override{Stream} - * Return next byte to read if available otherwise negative error - * code(-1). - * @return next byte or negative error code. - */ - virtual int peek() - { - if (m_count == 0) return (-1); - uint8_t res = 0; - m_sram.read(&res, m_addr + m_get, sizeof(res)); - return (res); - } - - /** - * @override{Stream} - * Return next byte if available otherwise negative error - * code(-1). - * @return next byte or negative error code. - */ - virtual int read() - { - if (m_count == 0) return (-1); - uint8_t res = 0; - m_sram.read(&res, m_addr + m_get, sizeof(res)); - m_count -= 1; - m_get += 1; - if (m_get == SIZE) m_get = 0; - return (res); - } - - /** - * @override{Stream} - * Flush all data and reset stream. - */ - virtual void flush() - { - m_put = 0; - m_get = 0; - m_count = 0; - } - - protected: - SRAM& m_sram; - uint32_t m_addr; - uint16_t m_put; - uint16_t m_get; - uint16_t m_count; - }; - -protected: - enum { - READ = 0x03, //!< Read data from memory - WRITE = 0x02, //!< Write data to memory - RDMR = 0x05, //!< Read mode register - WRMR = 0x01 //!< Write mode register - }; - - using SPI::Device<0,MSBFIRST,MAX_FREQ,SS_PIN>::acquire; - using SPI::Device<0,MSBFIRST,MAX_FREQ,SS_PIN>::transfer; - using SPI::Device<0,MSBFIRST,MAX_FREQ,SS_PIN>::read; - using SPI::Device<0,MSBFIRST,MAX_FREQ,SS_PIN>::write; - using SPI::Device<0,MSBFIRST,MAX_FREQ,SS_PIN>::release; -}; -#endif