SysMem.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * SysMem.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "CustomDevice.h"
14 
15 namespace Storage
16 {
20 class SysMem : public CustomDevice
21 {
22 public:
23  String getName() const override
24  {
25  return F("SysMem");
26  }
27 
28  size_t getBlockSize() const override
29  {
30  return sizeof(uint32_t);
31  }
32 
33  size_t getSize() const override
34  {
35  return 0x80000000;
36  }
37 
38  Type getType() const override
39  {
40  return Type::sysmem;
41  }
42 
43  bool read(uint32_t address, void* buffer, size_t len) override
44  {
45  if(isFlashPtr(reinterpret_cast<const void*>(address))) {
46  memcpy_P(buffer, reinterpret_cast<const void*>(address), len);
47  } else {
48  memcpy(buffer, reinterpret_cast<const void*>(address), len);
49  }
50  return true;
51  }
52 
53  bool write(uint32_t address, const void* data, size_t len) override
54  {
55  if(isFlashPtr(reinterpret_cast<const void*>(address))) {
56  return false;
57  }
58 
59  memcpy(reinterpret_cast<void*>(address), data, len);
60  return true;
61  }
62 
63  bool erase_range(uint32_t address, size_t len) override
64  {
65  if(isFlashPtr(reinterpret_cast<const void*>(address))) {
66  return false;
67  }
68 
69  memset(&address, 0xFF, len);
70  return true;
71  }
72 
74 
78  Partition createPartition(const String& name, const FSTR::ObjectBase& fstr, Partition::Type type, uint8_t subtype);
79 
80  template <typename T> Partition createPartition(const String& name, const FSTR::ObjectBase& fstr, T subType)
81  {
82  return createPartition(name, fstr, Partition::Type(T::partitionType), uint8_t(subType));
83  }
84 };
85 
86 extern SysMem sysMem;
87 
88 } // namespace Storage
Partition createPartition(const String &name, const FSTR::ObjectBase &fstr, T subType)
Definition: SysMem.h:80
#define isFlashPtr(ptr)
Simple check to determine if a pointer refers to flash memory.
Definition: Arch/Esp8266/Components/libc/include/sys/pgmspace.h:27
String getName() const override
Obtain unique device name.
Definition: SysMem.h:23
Used when defining data structures.
Definition: ObjectBase.hpp:32
bool read(uint32_t address, void *buffer, size_t len) override
Read data from the storage device.
Definition: SysMem.h:43
size_t getBlockSize() const override
Obtain smallest allocation unit for erase operations.
Definition: SysMem.h:28
Partition createPartition(const String &name, const FSTR::ObjectBase &fstr, Partition::Type type, uint8_t subtype)
Create partition for FlashString data access.
The String class.
Definition: WString.h:136
Class to support dynamic partitions.
Definition: CustomDevice.h:16
Type
Storage type.
Definition: Components/Storage/src/include/Storage/Device.h:41
SysMem sysMem
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:113
bool erase_range(uint32_t address, size_t len) override
Erase a region of storage in preparation for writing.
Definition: SysMem.h:63
void * memcpy_P(void *dest, const void *src_P, size_t length)
Partition createPartition(const Partition::Info &info)
Type getType() const override
Obtain device type.
Definition: SysMem.h:38
Type
Definition: Partition.h:81
Storage device to access system memory, e.g. RAM.
Definition: SysMem.h:20
size_t getSize() const override
Obtain addressable size of this device.
Definition: SysMem.h:33
Represents a flash partition.
Definition: Partition.h:78
bool write(uint32_t address, const void *data, size_t len) override
Write data to the storage device.
Definition: SysMem.h:53
Definition: FileDevice.h:23