distortos  v0.3.0
Advanced real-time operating system for deeply embedded targets
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 
236  int close();
237 
242  size_t getCapacity() const
243  {
244  return 128 * (1 << ((static_cast<uint8_t>(type_) & capacityMask_) >> capacityShift_));
245  }
246 
251  size_t getPageSize() const
252  {
253  return 8 * (1 << ((static_cast<uint8_t>(type_) & pageSizeMask_) >> pageSizeShift_));
254  }
255 
265  std::pair<int, bool> isWriteInProgress();
266 
276  bool lock();
277 
287  int open();
288 
303  std::pair<int, size_t> read(uint32_t address, void* buffer, size_t size);
304 
315  void unlock(bool previousLockState);
316 
326 
340  std::pair<int, size_t> write(uint32_t address, const void* buffer, size_t size);
341 
342 private:
343 
352  std::pair<int, uint8_t> readStatusRegister();
353 
361  int writeEnable();
362 
377  std::pair<int, size_t> writePage(uint32_t address, const void* buffer, size_t size);
378 
381 
384 };
385 
386 } // namespace devices
387 
388 } // namespace distortos
389 
390 #endif // INCLUDE_DISTORTOS_DEVICES_MEMORY_SPIEEPROM_HPP_
int close()
Closes SPI EEPROM.
Definition: SpiEeprom.cpp:101
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:215
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:106
int waitWhileWriteInProgress()
Waits while any write operation is currently in progress.
Definition: SpiEeprom.cpp:156
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:209
std::pair< int, size_t > write(uint32_t address, const void *buffer, size_t size)
Writes data to SPI EEPROM.
Definition: SpiEeprom.cpp:170
bool lock()
Wrapper for SpiDevice::lock()
Definition: SpiEeprom.cpp:112
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:117
Type type_
type of SPI EEPROM
Definition: SpiEeprom.hpp:383
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:151
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:201
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:242
SpiDevice spiDevice_
internal SPI slave device
Definition: SpiEeprom.hpp:380
std::pair< int, size_t > read(uint32_t address, void *buffer, size_t size)
Reads data from SPI EEPROM.
Definition: SpiEeprom.cpp:122
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:251