virtmem
virtual memory library for Arduino
spiram_alloc.h
Go to the documentation of this file.
1 #ifndef VIRTMEM_SPIRAM_ALLOC_H
2 #define VIRTMEM_SPIRAM_ALLOC_H
3 
9 #include <Arduino.h>
10 #include "internal/alloc.h"
11 
12 #include <serialram.h>
13 
14 namespace virtmem {
15 
28 template <typename Properties=DefaultAllocProperties>
29 class SPIRAMVAllocP : public VAlloc<Properties, SPIRAMVAllocP<Properties> >
30 {
31  bool largeAddressing;
32  uint8_t chipSelect;
33  SerialRam::ESPISpeed SPISpeed;
34  SerialRam serialRAM;
35 
36  void doStart(void)
37  {
38  serialRAM.begin(largeAddressing, chipSelect, SPISpeed);
39  }
40 
41  void doStop(void)
42  {
43  serialRAM.end();
44  }
45 
46  void doRead(void *data, VPtrSize offset, VPtrSize size)
47  {
48 // const uint32_t t = micros();
49  serialRAM.read((char *)data, offset, size);
50 // Serial.print("read: "); Serial.print(offset); Serial.print("/"); Serial.print(size); Serial.print("/"); Serial.println(micros() - t);
51  }
52 
53  void doWrite(const void *data, VPtrSize offset, VPtrSize size)
54  {
55 // const uint32_t t = micros();
56  serialRAM.write((const char *)data, offset, size);
57 // Serial.print("write: "); Serial.print(offset); Serial.print("/"); Serial.print(size); Serial.print("/"); Serial.println(micros() - t);
58  }
59 
60 public:
70  SPIRAMVAllocP(VPtrSize ps, bool la, uint8_t cs, SerialRam::ESPISpeed s) :
71  largeAddressing(la), chipSelect(cs), SPISpeed(s) { this->setPoolSize(ps); }
77  SPIRAMVAllocP(VPtrSize ps) { this->setPoolSize(ps); }
78  SPIRAMVAllocP(void) { }
79  ~SPIRAMVAllocP(void) { doStop(); }
80 
87  void setSettings(bool la, uint8_t cs, SerialRam::ESPISpeed s)
88  {
89  largeAddressing = la; chipSelect = cs; SPISpeed = s;
90  }
91 };
92 
94 
100 {
102  uint32_t size;
103  uint8_t chipSelect;
104  SerialRam::ESPISpeed speed;
105 };
106 
134 template <const SPIRamConfig *SPIChips, size_t chipAmount, typename Properties=DefaultAllocProperties>
135 class MultiSPIRAMVAllocP : public VAlloc<Properties, MultiSPIRAMVAllocP<SPIChips, chipAmount, Properties> >
136 {
137  SerialRam serialRAM[chipAmount];
138 
139  void doStart(void)
140  {
141  for (uint8_t i=0; i<chipAmount; ++i)
142  serialRAM[i].begin(SPIChips[i].largeAddressing, SPIChips[i].chipSelect, SPIChips[i].speed);
143  }
144 
145  void doStop(void)
146  {
147  for (uint8_t i=0; i<chipAmount; ++i)
148  serialRAM[i].end();
149  }
150 
151  void doRead(void *data, VPtrSize offset, VPtrSize size)
152  {
153 // const uint32_t t = micros();
154  VPtrNum startptr = 0;
155  for (uint8_t i=0; i<chipAmount; ++i)
156  {
157  const VPtrNum endptr = startptr + SPIChips[i].size;
158  if (offset >= startptr && offset < endptr)
159  {
160  const VPtrNum p = offset - startptr; // address relative in this chip
161  const VPtrSize sz = private_utils::minimal(size, SPIChips[i].size - p);
162  serialRAM[i].read((char *)data, p, sz);
163 
164  if (sz == size)
165  break;
166 
167  size -= sz;
168  data = (char *)data + sz;
169  offset += sz;
170  }
171  startptr = endptr;
172  }
173 // Serial.print("read: "); Serial.print(size); Serial.print("/"); Serial.println(micros() - t);
174  }
175 
176  void doWrite(const void *data, VPtrSize offset, VPtrSize size)
177  {
178 // const uint32_t t = micros();
179  VPtrNum startptr = 0;
180  for (uint8_t i=0; i<chipAmount; ++i)
181  {
182  const VPtrNum endptr = startptr + SPIChips[i].size;
183  if (offset >= startptr && offset < endptr)
184  {
185  const VPtrNum p = offset - startptr; // address relative in this chip
186  const VPtrSize sz = private_utils::minimal(size, SPIChips[i].size - p);
187  serialRAM[i].write((const char *)data, p, sz);
188 
189  if (sz == size)
190  break;
191 
192  size -= sz;
193  data = (const char *)data + sz;
194  offset += sz;
195  }
196  startptr = endptr;
197  }
198 // Serial.print("write: "); Serial.print(size); Serial.print("/"); Serial.println(micros() - t);
199  }
200 
202 
203 public:
209  {
210  uint32_t ps = 0;
211  for (uint8_t i=0; i<chipAmount; ++i)
212  ps += SPIChips[i].size;
213  this->setPoolSize(ps);
214  }
215  ~MultiSPIRAMVAllocP(void) { doStop(); }
216 };
217 
226 }
227 
228 #endif // VIRTMEM_SPIRAM_ALLOC_H
contains all code from virtmem
Definition: base_alloc.cpp:22
SPIRAMVAllocP(VPtrSize ps, bool la, uint8_t cs, SerialRam::ESPISpeed s)
Constructs (but not initializes) the allocator.
Definition: spiram_alloc.h:70
SerialRam::ESPISpeed speed
SPI speed to be used: SerialRam::SPEED_FULL, SerialRam::SPEED_HALF or SerialRam::SPEED_QUARTER.
Definition: spiram_alloc.h:104
Base template class for virtual memory allocators.
Definition: alloc.h:28
SPIRAMVAllocP(VPtrSize ps)
Constructs (but not initializes) the allocator.
Definition: spiram_alloc.h:77
void setPoolSize(VPtrSize ps)
Sets the total size of the memory pool.
Definition: base_alloc.h:155
uint32_t size
Amount of bytes this chip can hold.
Definition: spiram_alloc.h:102
uint32_t VPtrNum
Numeric type used to store raw virtual pointer addresses.
Definition: base_alloc.h:21
MultiSPIRAMVAllocP(void)
Definition: spiram_alloc.h:208
This struct is used to configure each SRAM chip used by a MultiSPIRAMVAllocP allocator.
Definition: spiram_alloc.h:99
virtual memory class header
uint32_t VPtrSize
Numeric type used to store the size of a virtual memory block.
Definition: base_alloc.h:22
SPIRAMVAllocP(void)
Constructs (but not initializes) the allocator.
Definition: spiram_alloc.h:78
bool largeAddressing
Does this chip needs large addressing (true if size >= 1 Mbit)
Definition: spiram_alloc.h:101
void * read(VPtrNum p, VPtrSize size)
Reads a raw block of (virtual) memory.
Definition: base_alloc.cpp:724
Virtual memory allocator that uses SPI (serial) RAM (i.e. the 23LC series from Microchip) as memory p...
Definition: spiram_alloc.h:29
uint8_t chipSelect
Pin assigned as chip select (CS) for this chip.
Definition: spiram_alloc.h:103
Virtual allocator that uses multiple SRAM chips (i.e. 23LC series from Microchip) as memory pool...
Definition: spiram_alloc.h:135
void setSettings(bool la, uint8_t cs, SerialRam::ESPISpeed s)
Configures the allocator.
Definition: spiram_alloc.h:87
SPIRAMVAllocP SPIRAMVAlloc
Shortcut to SPIRAMVAllocP with default template arguments.
Definition: spiram_alloc.h:93