Arduino-Storage
External Memory Storage library for Arduino
Block.ino
Go to the documentation of this file.
1 #include "Storage.h"
2 #include "TWI.h"
3 #include "Driver/AT24CXX.h"
4 
5 // Configure: TWI bus manager
6 // #define USE_SOFTWARE_TWI
7 
8 // Configure: Hardware TWI bus clock frequency
9 // #define FREQ 800000UL
10 // #define FREQ 400000UL
11 #define FREQ 100000UL
12 
13 #if defined(USE_SOFTWARE_TWI)
14 #include "GPIO.h"
15 #include "Software/TWI.h"
16 Software::TWI<BOARD::D18, BOARD::D19> twi;
17 #else
18 #include "Hardware/TWI.h"
19 Hardware::TWI twi(FREQ);
20 #endif
21 
22 // AT24C32 eeprom(twi, 7);
24 
25 const uint32_t BLOCK_MAX = 16*eeprom.PAGE_MAX;
27 
28 void setup()
29 {
30  Serial.begin(57600);
31  while (!Serial);
32 
33  // Check that the eeprom is ready
34  while (!eeprom.is_ready()) {
35  Serial.println(F("eeprom:error: check sub-address and wiring"));
36  delay(5000);
37  }
38 
39  // Initiate eeprom; fill block with zero
40  uint8_t page[eeprom.PAGE_MAX];
41  memset(page, 0, sizeof(page));
42  for (uint32_t offset = 0; offset < BLOCK_MAX; offset += eeprom.PAGE_MAX) {
43  block.write(offset, page, sizeof(page));
44  }
45 }
46 
47 void loop()
48 {
49  uint8_t page[eeprom.PAGE_MAX];
50  for (uint32_t offset = 0; offset < BLOCK_MAX; offset += eeprom.PAGE_MAX) {
51  // Read page buffer from block storage
52  block.read(page, offset, sizeof(page));
53  // Print page contents and update
54  if (offset < 0x10) Serial.print('0');
55  if (offset < 0x100) Serial.print('0');
56  Serial.print(offset, HEX);
57  Serial.print(':');
58  for (size_t i = 0; i < sizeof(page); i++) {
59  uint8_t v = page[i];
60  Serial.print(' ');
61  if (v < 0x10) Serial.print('0');
62  Serial.print(v, HEX);
63  page[i] = v + 1;
64  }
65  Serial.println();
66  // Write page back to block storage
67  block.write(offset, page, sizeof(page));
68  }
69  Serial.println();
70  delay(5000);
71 }
int read(void *buf, uint32_t offset, size_t size)
Definition: Storage.h:132
AT24C32 eeprom(twi)
int write(uint32_t offset, const void *buf, size_t size)
Definition: Storage.h:148
const uint16_t PAGE_MAX
Definition: AT24CXX.h:55
void loop()
Definition: Block.ino:47
const uint32_t BLOCK_MAX
Definition: Block.ino:25
bool is_ready()
Definition: AT24CXX.h:62
int16_t v[32]
Definition: Cache.ino:35
Hardware::TWI twi(FREQ)
Storage::Block block(eeprom, BLOCK_MAX)
void setup()
Definition: Block.ino:28
#define FREQ
Definition: Block.ino:11