distortos  v0.4.0
object-oriented C++ RTOS for microcontrollers
SpiEeprom.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_DEVICES_MEMORY_SPIEEPROM_HPP_
13 #define INCLUDE_DISTORTOS_DEVICES_MEMORY_SPIEEPROM_HPP_
14 
16 
17 namespace distortos
18 {
19 
20 namespace devices
21 {
22 
30 class SpiEeprom
31 {
33  constexpr static size_t pageSizeShift_ {0};
34 
36  constexpr static size_t pageSizeWidth_ {3};
37 
39  constexpr static size_t pageSizeMask_ {((1 << pageSizeWidth_) - 1) << pageSizeShift_};
40 
42  constexpr static size_t capacityShift_ {pageSizeWidth_};
43 
45  constexpr static size_t capacityWidth_ {8 - pageSizeWidth_};
46 
48  constexpr static size_t capacityMask_ {((1 << capacityWidth_) - 1) << capacityShift_};
49 
50 public:
51 
53  enum class Type : uint8_t
54  {
91 
116 
141 
166 
205  };
206 
218  constexpr SpiEeprom(SpiMaster& spiMaster, OutputPin& slaveSelectPin, const Type type, const bool mode3 = {},
219  const uint32_t maxClockFrequency = 1000000) :
220  spiDevice_{spiMaster, slaveSelectPin, mode3 == false ? SpiMode::_0 : SpiMode::_3, maxClockFrequency,
221  8, false},
222  type_{type}
223  {
224 
225  }
226 
238  int close();
239 
244  size_t getCapacity() const
245  {
246  return 128 * (1 << ((static_cast<uint8_t>(type_) & capacityMask_) >> capacityShift_));
247  }
248 
253  size_t getPageSize() const
254  {
255  return 8 * (1 << ((static_cast<uint8_t>(type_) & pageSizeMask_) >> pageSizeShift_));
256  }
257 
268  std::pair<int, bool> isWriteInProgress();
269 
281  bool lock();
282 
294  int open();
295 
312  std::pair<int, size_t> read(uint32_t address, void* buffer, size_t size);
313 
326  void unlock(bool previousLockState);
327 
339 
355  std::pair<int, size_t> write(uint32_t address, const void* buffer, size_t size);
356 
357 private:
358 
367  std::pair<int, uint8_t> readStatusRegister();
368 
376  int writeEnable();
377 
392  std::pair<int, size_t> writePage(uint32_t address, const void* buffer, size_t size);
393 
396 
399 };
400 
401 } // namespace devices
402 
403 } // namespace distortos
404 
405 #endif // INCLUDE_DISTORTOS_DEVICES_MEMORY_SPIEEPROM_HPP_
int close()
Closes SPI EEPROM.
Definition: SpiEeprom.cpp:103
Definition: SpiEeprom.hpp:30
std::pair< int, size_t > writePage(uint32_t address, const void *buffer, size_t size)
Writes single page.
Definition: SpiEeprom.cpp:221
static constexpr size_t pageSizeMask_
bit mask of field with page size encoded in device&#39;s type
Definition: SpiEeprom.hpp:39
std::pair< int, bool > isWriteInProgress()
Checks whether any write operation is currently in progress.
Definition: SpiEeprom.cpp:108
int waitWhileWriteInProgress()
Waits while any write operation is currently in progress.
Definition: SpiEeprom.cpp:160
static constexpr size_t capacityWidth_
bit width of field with capacity encoded in device&#39;s type
Definition: SpiEeprom.hpp:45
Definition: OutputPin.hpp:29
int writeEnable()
Enables writes in SPI EEPROM.
Definition: SpiEeprom.cpp:215
std::pair< int, size_t > write(uint32_t address, const void *buffer, size_t size)
Writes data to SPI EEPROM.
Definition: SpiEeprom.cpp:174
bool lock()
Wrapper for SpiDevice::lock()
Definition: SpiEeprom.cpp:114
Definition: SpiDevice.hpp:38
static constexpr size_t pageSizeWidth_
bit width of field with page size encoded in device&#39;s type
Definition: SpiEeprom.hpp:36
constexpr SpiEeprom(SpiMaster &spiMaster, OutputPin &slaveSelectPin, const Type type, const bool mode3={}, const uint32_t maxClockFrequency=1000000)
SpiEeprom&#39;s constructor.
Definition: SpiEeprom.hpp:218
int open()
Opens SPI EEPROM.
Definition: SpiEeprom.cpp:119
Type type_
type of SPI EEPROM
Definition: SpiEeprom.hpp:398
static constexpr size_t capacityMask_
bit mask of field with capacity encoded in device&#39;s type
Definition: SpiEeprom.hpp:48
Top-level namespace of distortos project.
void unlock(bool previousLockState)
Wrapper for SpiDevice::unlock()
Definition: SpiEeprom.cpp:155
32kB (256kb) capacity, 64 bytes per page
SpiDevice class header.
16kB (128kb) capacity, 64 bytes per page
std::pair< int, uint8_t > readStatusRegister()
Reads value of status register of SPI EEPROM.
Definition: SpiEeprom.cpp:207
mode 0 - CPOL == 0, CPHA == 0
Definition: SpiMaster.hpp:37
64kB (512kb) capacity, 128 bytes per page
static constexpr size_t capacityShift_
bit shift of field with capacity encoded in device&#39;s type
Definition: SpiEeprom.hpp:42
mode 3 - CPOL == 1, CPHA == 1
size_t getCapacity() const
Definition: SpiEeprom.hpp:244
SpiDevice spiDevice_
internal SPI slave device
Definition: SpiEeprom.hpp:395
std::pair< int, size_t > read(uint32_t address, void *buffer, size_t size)
Reads data from SPI EEPROM.
Definition: SpiEeprom.cpp:124
static constexpr size_t pageSizeShift_
bit shift of field with page size encoded in device&#39;s type
Definition: SpiEeprom.hpp:33
Type
type of device - determines capacity and page size
Definition: SpiEeprom.hpp:53
size_t getPageSize() const
Definition: SpiEeprom.hpp:253