Skip to content

Commit b5cd176

Browse files
committed
SRAM::Stream::write, performance optimization
1 parent c79a3f6 commit b5cd176

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

examples/Stream/Stream.ino

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SRAM<BOARD::D10>::Stream<10000> temps(sram, 10000);
2323

2424
const int N = 1000;
2525
int count = 0;
26-
uint32_t s0, m0, m1, m2, m3, m4, m5;
26+
uint32_t s0, m0, m1, m2, m3, m4, m5, m6, m7;
2727

2828
void setup()
2929
{
@@ -39,8 +39,8 @@ void loop()
3939
Serial.println(analogRead(A0));
4040
}
4141
Serial.println();
42-
m0 = micros() - s0;
4342
Serial.flush();
43+
m0 = micros() - s0;
4444

4545
// Print N analog samples to sram stream
4646
s0 = micros();
@@ -61,6 +61,7 @@ void loop()
6161
s0 = micros();
6262
while (temps.available())
6363
Serial.write(temps.read());
64+
Serial.flush();
6465
m3 = micros() - s0;
6566

6667
// Write to sram stream
@@ -74,12 +75,31 @@ void loop()
7475
for (int i = 0; i < count; i++) sum += ios.read();
7576
m5 = micros() - s0;
7677

78+
// Write N analog samples to sram stream
79+
s0 = micros();
80+
int sample;
81+
for (int i = 0; i < N; i++) {
82+
sample = analogRead(A0);
83+
ios.write((const uint8_t*) &sample, sizeof(sample));
84+
}
85+
m6 = micros() - s0;
86+
87+
// Read N analog samples from sram stream and print to serial
88+
s0 = micros();
89+
while (ios.available()) {
90+
ios.readBytes((uint8_t*) &sample, sizeof(sample));
91+
Serial.println(sample);
92+
}
93+
Serial.flush();
94+
m7 = micros() - s0;
95+
7796
Serial.print(F("Samples, N = "));
7897
Serial.println(N);
7998
Serial.print(F("Serial.print, m0 = "));
8099
Serial.println(m0 / N);
81100
Serial.print(F("SRAM::Stream.print, m1 = "));
82101
Serial.println(m1 / N);
102+
Serial.println();
83103
Serial.print(F("SRAM::Stream.available, count = "));
84104
Serial.println(count);
85105
Serial.print(F("SRAM::Stream.write/read, m2 = "));
@@ -90,6 +110,14 @@ void loop()
90110
Serial.println(m4 / count);
91111
Serial.print(F("SRAM::Stream.read, m5 = "));
92112
Serial.println(m5 / count);
113+
Serial.println();
114+
Serial.print(F("SRAM::Stream.available, N*2 = "));
115+
Serial.println(N * 2);
116+
Serial.print(F("SRAM::Stream.write(2), m6 = "));
117+
Serial.println(m6 / N);
118+
Serial.print(F("SRAM::Stream.read(2)/Serial.print, m7 = "));
119+
Serial.println(m7 / N);
120+
Serial.println();
93121

94122
delay(1000);
95123
}

src/Driver/SRAM.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,34 @@ class SRAM : protected SPI::Device<0, MSBFIRST, FREQ, SS_PIN> {
125125
return (sizeof(byte));
126126
}
127127

128+
/**
129+
* @override{Stream}
130+
* Write given buffer and numbe of bytes to stream. Return number
131+
* of bytes written.
132+
* @param[in] bufffer to write.
133+
* @param[in] size number of byets to write.
134+
* @return number of bytes.
135+
*/
136+
virtual size_t write(const uint8_t *buffer, size_t size)
137+
{
138+
uint16_t room = SIZE - m_count;
139+
if (room == 0) return (0);
140+
if (size > room) size = room;
141+
size_t res = size;
142+
room = SIZE - m_put;
143+
if (size > room) {
144+
m_sram.write(m_addr + m_put, buffer, room);
145+
buffer += room;
146+
size -= room;
147+
m_count += room;
148+
m_put = 0;
149+
}
150+
m_sram.write(m_addr + m_put, buffer, size);
151+
m_count += size;
152+
m_put += size;
153+
return (res);
154+
}
155+
128156
/**
129157
* @override{Stream}
130158
* Returns number of bytes available for read().

0 commit comments

Comments
 (0)