Card.h
Go to the documentation of this file.
1 /*
2 Author: (github.com/)ADiea
3 Project: Sming for ESP8266 - https://github.com/anakod/Sming
4 License: MIT
5 Date: 15.07.2015
6 Descr: Low-level SDCard functions
7 */
8 #pragma once
9 
11 #include <SPIBase.h>
12 #include "CSD.h"
13 #include "CID.h"
14 
15 namespace Storage::SD
16 {
17 class Card : public Disk::BlockDevice
18 {
19 public:
20  /*
21  * Whilst SD V1.XX permits misaligned and partial block reads, later versions do not
22  * and require transfers to be aligned to, and in multiples of, 512 bytes.
23  */
24 
25  Card(const String& name, SPIBase& spi) : BlockDevice(), name(name), spi(spi)
26  {
27  }
28 
30  {
31  end();
32  }
33 
39  bool begin(uint8_t chipSelect, uint32_t freq = 0);
40 
41  void end();
42 
43  /* Storage Device methods */
44 
45  String getName() const override
46  {
47  return name.c_str();
48  }
49 
50  uint32_t getId() const
51  {
52  return 0;
53  }
54 
55  Type getType() const
56  {
57  return Type::sdcard;
58  }
59 
60  size_t getBlockSize() const override
61  {
62  return size_t(mCSD.sector_size() + 1) << sectorSizeShift;
63  }
64 
65  const CID& cid{mCID};
66  const CSD& csd{mCSD};
67 
68 protected:
69  bool raw_sector_read(storage_size_t address, void* dst, size_t size) override;
70  bool raw_sector_write(storage_size_t address, const void* src, size_t size) override;
71  bool raw_sector_erase_range(storage_size_t address, size_t size) override;
72  bool raw_sync() override;
73 
74 private:
75  uint8_t init();
76  bool wait_ready();
77  void deselect();
78  bool select();
79  bool rcvr_datablock(void* buff, size_t btr);
80  bool xmit_datablock(const void* buff, uint8_t token);
81  uint8_t send_cmd(uint8_t cmd, uint32_t arg);
82 
83  CString name;
84  SPIBase& spi;
85  CSD mCSD;
86  CID mCID;
87  uint8_t chipSelect{255};
88  bool initialised{false};
89  uint8_t cardType;
90 }; // namespace SD
91 
92 } // namespace Storage::SD
uint32_t getId() const
Obtain device ID.
Definition: Card.h:50
Type getType() const
Obtain device type.
Definition: Card.h:55
const char * c_str() const
Definition: CString.h:104
bool raw_sector_read(storage_size_t address, void *dst, size_t size) override
uint8_t sectorSizeShift
Definition: BlockDevice.h:115
~Card()
Definition: Card.h:29
The String class.
Definition: WString.h:136
Base class for sector-addressable (block) devices.
Definition: BlockDevice.h:41
const CID & cid
Definition: Card.h:65
Type
Storage type.
Definition: Components/Storage/src/include/Storage/Device.h:42
const CSD & csd
Definition: Card.h:66
bool raw_sector_write(storage_size_t address, const void *src, size_t size) override
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular St...
Definition: CString.h:26
bool raw_sync() override
String getName() const override
Obtain unique device name.
Definition: Card.h:45
bool raw_sector_erase_range(storage_size_t address, size_t size) override
Definition: CID.h:7
size_t getBlockSize() const override
Obtain smallest allocation unit for erase operations.
Definition: Card.h:60
Definition: Card.h:15
Iterator begin() const
Definition: LinkedObject.h:140
Definition: Card.h:17
Definition: SPIBase.h:50
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
Card(const String &name, SPIBase &spi)
Definition: Card.h:25
Definition: CSD.h:68