COSA
An Object-Oriented Platform for Arduino Programming
W25X40CL.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_W25X40CL_HH
22 #define COSA_W25X40CL_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/SPI.hh"
26 #include "Cosa/Flash.hh"
27 
38 class W25X40CL : public Flash::Device, protected SPI::Driver {
39 public:
43  static const size_t PAGE_MAX = 256;
44  static const size_t PAGE_MASK = PAGE_MAX - 1;
45 
50 #if !defined(BOARD_ATTINY)
52  Flash::Device(4 * 1024L, 128),
53  SPI::Driver(csn, SPI::ACTIVE_LOW, SPI::DIV2_CLOCK, 0, SPI::MSB_ORDER, NULL)
54  {}
55 #else
57  Flash::Device(4 * 1024L, 128),
59  {}
60 #endif
61 
69  virtual bool begin();
70 
77  virtual bool is_ready();
78 
89  virtual int read(void* dest, uint32_t src, size_t size);
90 
102  virtual int erase(uint32_t dest, uint8_t size = 4);
103 
114  virtual int write(uint32_t dest, const void* src, size_t size);
115 
125  virtual int write_P(uint32_t dest, const void* buf, size_t size);
126 
130  union status_t {
131  uint8_t as_uint8;
132  struct {
133  uint8_t BUSY:1;
134  uint8_t WEL:1;
135  uint8_t BP:3;
136  uint8_t TB:1;
137  uint8_t RESERVED:1;
138  uint8_t SRP:1;
139  };
140  status_t(uint8_t value = 0)
141  {
142  as_uint8 = value;
143  }
144  operator uint8_t()
145  {
146  return (as_uint8);
147  }
148  };
149 
154  uint8_t read_status()
155  {
156  return (issue(RDSR));
157  }
158 
159 protected:
163  enum Command {
164  WREN = 0x06,
165  WREVSR = 0x50,
166  WRDI = 0x04,
167  RDSR = 0x05,
168  WRR = 0x01,
169  READ = 0x03,
170  FRD = 0x0b,
171  FRDDIO = 0xbb,
172  PP = 0x02,
173  SER = 0x20,
174  B32ER = 0x52,
175  B64ER = 0xd8,
176  CER = 0x60,
177  PWD = 0xb9,
178  RLPWD = 0xab,
179  RDID = 0x90,
180  RDIDDIO = 0x92,
181  RDJID = 0x9f,
182  RDUID = 0x4b
183  } __attribute__((packed));
184 
186  static const uint8_t MANUFACTURER = 0xef;
187 
189  static const uint8_t DEVICE = 0x12;
190 
196  uint8_t issue(Command cmd);
197 
200 };
201 
202 #endif
virtual int read(void *dest, uint32_t src, size_t size)
Definition: W25X40CL.cpp:61
Write Enable.
Definition: W25X40CL.hh:164
uint8_t read_status()
Definition: W25X40CL.hh:154
static const uint8_t DEVICE
Definition: W25X40CL.hh:189
static const size_t PAGE_MAX
Definition: W25X40CL.hh:43
Definition: SPI.hh:57
Read JEDEC ID.
Definition: W25X40CL.hh:181
#define NULL
Definition: Types.h:101
uint8_t issue(Command cmd)
Definition: W25X40CL.cpp:205
Block Erase (32 kB).
Definition: W25X40CL.hh:174
static const size_t PAGE_MASK
Definition: W25X40CL.hh:44
Fast Read.
Definition: W25X40CL.hh:170
Read Unique ID.
Definition: W25X40CL.hh:182
Write Status Register.
Definition: W25X40CL.hh:168
status_t m_status
Definition: W25X40CL.hh:199
uint8_t BP
Block Protect Bits.
Definition: W25X40CL.hh:135
virtual int write(uint32_t dest, const void *src, size_t size)
Definition: W25X40CL.cpp:113
Page Program.
Definition: W25X40CL.hh:172
uint8_t RESERVED
Reserved.
Definition: W25X40CL.hh:137
Release Power-down/Device ID.
Definition: W25X40CL.hh:178
Read Status Register.
Definition: W25X40CL.hh:167
virtual int write_P(uint32_t dest, const void *buf, size_t size)
Definition: W25X40CL.cpp:159
Power-down.
Definition: W25X40CL.hh:177
virtual bool begin()
Definition: W25X40CL.cpp:24
Write Enable for Volatile Status Register.
Definition: W25X40CL.hh:165
uint8_t as_uint8
As unsigned 8-bit value.
Definition: W25X40CL.hh:131
Definition: Flash.hh:26
Read Manufacturer/Device ID Dual I/O.
Definition: W25X40CL.hh:180
Read Data.
Definition: W25X40CL.hh:169
virtual bool is_ready()
Definition: W25X40CL.cpp:46
uint8_t BUSY
< As bit-fields.
Definition: W25X40CL.hh:133
Sector Erase (4 kB).
Definition: W25X40CL.hh:173
status_t(uint8_t value=0)
Definition: W25X40CL.hh:140
uint8_t SRP
Status Register Protect.
Definition: W25X40CL.hh:138
W25X40CL(Board::DigitalPin csn=Board::D15)
Definition: W25X40CL.hh:51
Active low logic during transaction.
Definition: SPI.hh:80
static const uint8_t MANUFACTURER
Definition: W25X40CL.hh:186
uint8_t WEL
Write Enable Latch.
Definition: W25X40CL.hh:134
Divide system clock by 2.
Definition: SPI.hh:61
Read Manufacturer/Device ID.
Definition: W25X40CL.hh:179
Write Disable.
Definition: W25X40CL.hh:166
Most significant bit first.
Definition: SPI.hh:73
Chip Erase.
Definition: W25X40CL.hh:176
Fast Read Dual.
Definition: W25X40CL.hh:171
virtual int erase(uint32_t dest, uint8_t size=4)
Definition: W25X40CL.cpp:80
uint8_t TB
Top/Bottom Protect.
Definition: W25X40CL.hh:136
Driver(Board::DigitalPin cs, Pulse pulse=DEFAULT_PULSE, Clock rate=DEFAULT_CLOCK, uint8_t mode=0, Order order=MSB_ORDER, Interrupt::Handler *irq=NULL)
Definition: SOFT_SPI.cpp:25
Block Erase (64kB).
Definition: W25X40CL.hh:175
Device(uint32_t bytes, uint16_t count)
Definition: Flash.hh:54