forked from BlockoS/arduino-dataflash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-pageTest.cpp
149 lines (115 loc) · 3.26 KB
/
main-pageTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdint.h>
#include "wirish.h"
#include "at45db161d/at45db161d.h"
#define NUM_PAGES 8
// Force init to be called *first*, i.e. before static object allocation.
// Otherwise, statically allocated objects that need libmaple may fail.
__attribute__((constructor)) void premain()
{
init();
}
int main()
{
HardwareSPI SPI(1);
AT45DB161D dataflash(&SPI, 5, 6, 7); // A reference to our HardwareSPI interface is required.
uint8_t loop_cnt;
uint16_t page;
uint8_t status;
AT45DB161D::ID id;
uint16_t i, j;
char message[] = "@ write test ";
char overflow[] = "\nOVERFLOW!\n";
uint8_t data;
/* Initialize SPI */
SPI.begin();
Serial2.begin(9600);
/* Let's wait 1 second, allowing use to press the serial monitor button :p */
delay(1000);
delay(10);
/* Read status register */
status = dataflash.ReadStatusRegister();
/* Read manufacturer and device ID */
dataflash.ReadManufacturerAndDeviceID(&id);
/* Display status register */
Serial2.print("Status register: 0b");
Serial2.print(status, BIN);
Serial2.print('\n');
/* Display manufacturer and device ID */
Serial2.print("Manufacturer ID: 0x"); // Should be 0x1F
Serial2.print(id.manufacturer, HEX);
Serial2.print('\n');
Serial2.print("Device ID (part 1): 0x"); // Should be 0x26
Serial2.print(id.device[0], HEX);
Serial2.print('\n');
Serial2.print("Device ID (part 2): 0x"); // Should be 0x00
Serial2.print(id.device[1], HEX);
Serial2.print('\n');
Serial2.print("Extended Device Information String Length: 0x"); // 0x00
Serial2.print(id.extendedInfoLength, HEX);
Serial2.print('\n');
loop_cnt = 0;
page = 0;
while(1)
{
/* Set dataflash so that any call to spi_tranfer will write the byte
* given as argument to the Buffer 1 */
dataflash.BufferWrite(DATAFLASH_BUFFER1, 0);
/* Transfer the message */
for(i = 0; message[i] != '\0'; ++i)
SPI.transfer(message[i]);
/* Transfer variable length data */
for(i = 0; i < page+1; i++)
{
for(j = 0; j < 10; j++)
SPI.transfer('0' + j);
SPI.transfer(' ');
}
SPI.transfer('\n');
++loop_cnt;
if(loop_cnt == 0)
{
/* loop_cnt overflow */
/* To celebrate this we write the string "\nOVERFLOW!\n" to Buffer 1 */
for( i =0; overflow[i] != '\0'; ++i)
SPI.transfer(overflow[i]);
}
/* Write '\0' to buffer 1. This will help us know that we must stop reading from it. */
SPI.transfer('\0');
/* Transfer buffer 1 to 'page' page (with builtin erase) */
dataflash.BufferToPage(DATAFLASH_BUFFER1, page, 1);
++page;
/* When we wrote the number of pages we wanted (NUM_PAGES), we display their contents by
* using 2 methods alternatively ;
* - PageToBuffer + BufferRead
* - PageRead
*/
if(page >= NUM_PAGES)
{
for(i = 0; i < NUM_PAGES; ++i)
{
if(i & 1)
{
Serial2.println("Page to buffer");
dataflash.PageToBuffer(i, DATAFLASH_BUFFER1);
dataflash.BufferRead(DATAFLASH_BUFFER1, 0);
}
else
{
Serial2.println("Page read");
dataflash.ReadMainMemoryPage(i, 0);
}
do
{
data = SPI.transfer(0xff);
if(data != '\0')
Serial2.print((char)data);
}while(data != '\0');
/* Add a little delay otherwise the display will be too fast */
delay(100);
}
page = 0;
}
}
return 0;
}