-
Notifications
You must be signed in to change notification settings - Fork 6
/
serial logger.ino
194 lines (137 loc) · 4.8 KB
/
serial logger.ino
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
A small arduino program to log data from the UART port
to an SD card very quickly. Works at 115200 baud.
Files on the SD card are named LOG00000.TXT, LOG00001.TXT, etc
Each file is always 512,000 bytes, and the parts that have not been written to yet are all 0's
You can change the arduino's internal serial buffer at C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino\HardwareSerial.cpp
by changing the line "#define SERIAL_BUFFER_SIZE 64"
I had it set to 500
Adopted from the RawWrite.ino example found here:
https://github.com/greiman/FreeRTOS-Arduino/blob/master/libraries/SdFat/examples/RawWrite/RawWrite.ino
Also used code from OpenLog
https://github.com/sparkfun/OpenLog
*/
#include <SdFat.h>
#include <SdFatUtil.h>
// SD chip select pin
#define chipSelect SS
// number of blocks in the contiguous file
const uint32_t BLOCK_COUNT = 1000UL;
//make another file if the one fills up
uint32_t currBlock = 0;
// file system
SdFat sd;
// test file
SdFile file;
// file extent
uint32_t bgnBlock, endBlock;
// Serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
#define printn Serial.print
#define printf(...) Serial.print(F(__VA_ARGS__))
unsigned int bufferIndex;
uint8_t* pCache;
void makeFile(void)
{
//Search for next available log spot
char new_file_name[] = "LOG00000.TXT";
int new_file_number=0;
while(1)
{
new_file_number++;
//There is a max of 65534 logs
if(new_file_number > 65533)
{
printf(("!Too many logs:2!"));
return; //Bail!
}
//Splice the new file number into this file name
sprintf(new_file_name, "LOG%05d.TXT", new_file_number);
//Try to open file, if fail (file doesn't exist), then break
if (file.open(new_file_name, O_CREAT | O_EXCL | O_WRITE)) break;
}
//Close the new file that was just opened
file.close();
// delete existing file that was just created, so it can be recreated as continuous
sd.remove(new_file_name);
// create a contiguous file
if (!file.createContiguous(sd.vwd(), new_file_name, 512UL*BLOCK_COUNT)) {
error("createContiguous failed");
}
// get the location of the file's blocks
if (!file.contiguousRange(&bgnBlock, &endBlock)) {
error("contiguousRange failed");
}
//*********************NOTE**************************************
// NO SdFile calls are allowed while cache is used for raw writes
//***************************************************************
// clear the cache and use it as a 512 byte buffer
pCache = (uint8_t*)sd.vol()->cacheClear();
// tell card to setup for multiple block write with pre-erase
if (!sd.card()->erase(bgnBlock, endBlock)) error("card.erase failed");
if (!sd.card()->writeStart(bgnBlock, BLOCK_COUNT)) {
error("writeStart failed");
}
printf("begin:");
printn(bgnBlock);
printf("\n");
printf("end:");
printn(endBlock);
printf("\n");
}
void setup(void) {
Serial.begin(115200);
while (!Serial) {} // wait for Leonardo
cout << pstr("Free RAM: ") << FreeRam() << endl;
// initialize the SD card at SPI_FULL_SPEED for best performance.
// try SPI_HALF_SPEED if bus errors occur.
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
makeFile();
}
char chr;
//------------------------------------------------------------------------------
void loop(void) {
// write data
while (Serial.available())
{
//read the data and record it in the buffer
chr=Serial.read();
pCache[bufferIndex]=chr;
bufferIndex++;
//flush whenever the the buffer fills up, or when an 18 is recieved
if (bufferIndex==511 || chr==18)
{
signed int t=millis();
// write a 512 byte block
if (!sd.card()->writeData(pCache))
{
error("writeData failed");
}
//log some info
printf("flushing took ");
printn((signed)millis()-t);
printf(" ms\n");
printn((char)18);
if(chr==18){
printf("chr\n");
}
if (bufferIndex==511)
{
printf("buf\n");
}
//reset the count
bufferIndex=0;
currBlock++;
if(currBlock==BLOCK_COUNT){
if (!sd.card()->writeStop()) error("writeStop failed");
file.close();
makeFile();
currBlock=0;
}
}
}
}