-
Notifications
You must be signed in to change notification settings - Fork 11
/
TeensySDLogger.h
224 lines (183 loc) · 4.1 KB
/
TeensySDLogger.h
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#ifndef SD_FILE_LOGGER_H_
#define SD_FILE_LOGGER_H_
#include "Arduino.h"
#include "ArduinoLogger.h"
#include "SdFat.h"
#include "internal/circular_buffer.hpp"
#include <kinetis.h>
/** SD File Buffer
*
* Logs to a file on the SD card.
*
* This class uses the SdFat Arduino Library.
*
* @code
* using PlatformLogger =
* PlatformLogger_t<TeensySDLogger>;
* @endcode
*
* @ingroup LoggingSubsystem
*/
class TeensySDLogger final : public LoggerBase
{
private:
static constexpr size_t BUFFER_SIZE = 512;
public:
/// Default constructor
TeensySDLogger() : LoggerBase() {}
/// Default destructor
~TeensySDLogger() noexcept = default;
size_t size() const noexcept final
{
return file_.size();
}
size_t capacity() const noexcept final
{
// size in blocks * bytes per block (512 Bytes = 2^9)
return fs_ ? fs_->card()->sectorCount() << 9 : 0;
}
void log_customprefix() noexcept final
{
print("[%d ms] ", millis());
}
void begin(SdFs& sd_inst)
{
fs_ = &sd_inst;
if(!file_.open(filename_, O_WRITE | O_CREAT))
{
errorHalt("Failed to open file");
}
// Clear current file contents
file_.truncate(0);
log_reset_reason();
// Manually flush, since the file is open
flush();
file_.close();
}
protected:
void log_putc(char c) noexcept final
{
log_buffer_.put(c);
}
size_t internal_size() const noexcept override
{
return log_buffer_.size();
}
size_t internal_capacity() const noexcept override
{
return log_buffer_.capacity();
}
void flush_() noexcept final
{
writeBufferToSDFile();
}
void clear_() noexcept final
{
log_buffer_.reset();
}
private:
void errorHalt(const char* msg)
{
printf("Error: %s\n", msg);
if(fs_->sdErrorCode())
{
if(fs_->sdErrorCode() == SD_CARD_ERROR_ACMD41)
{
printf("Try power cycling the SD card.\n");
}
printSdErrorSymbol(&Serial, fs_->sdErrorCode());
printf(", ErrorData: 0x%x\n", fs_->sdErrorData());
}
while(true)
{
}
}
void writeBufferToSDFile()
{
if(!file_.open(filename_, O_WRITE | O_APPEND))
{
errorHalt("Failed to open file");
}
int bytes_written = 0;
// We need to get the front, the rear, and potentially write the files in two steps
// to prevent ordering problems
size_t head = log_buffer_.head();
size_t tail = log_buffer_.tail();
const char* buffer = log_buffer_.storage();
if((head < tail) || ((tail > 0) && (log_buffer_.size() == log_buffer_.capacity())))
{
// we have a wraparound case
// We will write from buffer[tail] to buffer[size] in one go
// Then we'll reset head to 0 so that we can write 0 to tail next
bytes_written = file_.write(&buffer[tail], log_buffer_.capacity() - tail);
bytes_written += file_.write(buffer, head);
}
else
{
// Write from tail position and send the specified number of bytes
bytes_written = file_.write(&buffer[tail], log_buffer_.size());
}
if(static_cast<size_t>(bytes_written) != log_buffer_.size())
{
errorHalt("Failed to write to log file");
}
log_buffer_.reset();
file_.close();
}
/// Checks the kinetis SoC's reset reason registers and logs them
/// This should only be called during begin().
void log_reset_reason()
{
auto srs0 = RCM_SRS0;
auto srs1 = RCM_SRS1;
// Clear the values
RCM_SRS0 = 0;
RCM_SRS1 = 0;
if(srs0 & RCM_SRS0_LVD)
{
info("Low-voltage Detect Reset\n");
}
if(srs0 & RCM_SRS0_LOL)
{
info("Loss of Lock in PLL Reset\n");
}
if(srs0 & RCM_SRS0_LOC)
{
info("Loss of External Clock Reset\n");
}
if(srs0 & RCM_SRS0_WDOG)
{
info("Watchdog Reset\n");
}
if(srs0 & RCM_SRS0_PIN)
{
info("External Pin Reset\n");
}
if(srs0 & RCM_SRS0_POR)
{
info("Power-on Reset\n");
}
if(srs1 & RCM_SRS1_SACKERR)
{
info("Stop Mode Acknowledge Error Reset\n");
}
if(srs1 & RCM_SRS1_MDM_AP)
{
info("MDM-AP Reset\n");
}
if(srs1 & RCM_SRS1_SW)
{
info("Software Reset\n");
}
if(srs1 & RCM_SRS1_LOCKUP)
{
info("Core Lockup Event Reset\n");
}
}
private:
SdFs* fs_;
const char* filename_ = "log.txt";
mutable FsFile file_;
CircularBuffer<char, BUFFER_SIZE> log_buffer_;
};
#endif // SD_FILE_LOGGER_H_