-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathmain.cpp
More file actions
96 lines (78 loc) · 2.42 KB
/
main.cpp
File metadata and controls
96 lines (78 loc) · 2.42 KB
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
/*
* Copyright (c) 2018, Raphael Lehmann
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <modm/platform.hpp>
#include <modm/debug/logger.hpp>
#include <modm/driver/storage/block_device_file.hpp>
#include <iostream>
void printMemoryContent(const uint8_t* address, std::size_t size) {
for (std::size_t i = 0; i < size; i++) {
MODM_LOG_INFO.printf("%x", address[i]);
}
}
struct Filename {
static constexpr const char* name = "test.bin~";
};
int
main()
{
/**
* This example/test writes alternating patterns into
* a `modm::BdFile` block device with a size of 1M.
* The memory content is afterwards read and compared
* to the pattern.
* Write and read operations are done on 64 byte blocks.
*/
// create empty `test.bin~` file
std::ofstream testfile(Filename::name);
testfile.close();
constexpr uint32_t BlockSize = 64;
constexpr uint32_t MemorySize = 1024*1024;
uint8_t bufferA[BlockSize];
uint8_t bufferB[BlockSize];
uint8_t bufferC[BlockSize];
std::memset(bufferA, 0xAA, BlockSize);
std::memset(bufferB, 0x55, BlockSize);
modm::BdFile<Filename, MemorySize> storageDevice;
if(!storageDevice.initialize()) {
MODM_LOG_INFO << "Error: Unable to initialize device.";
exit(1);
}
if(!storageDevice.erase(0, MemorySize)) {
MODM_LOG_INFO << "Error: Unable to erase device.";
exit(1);
}
MODM_LOG_INFO << "Starting memory test!" << modm::endl;
for(uint16_t iteration = 0; iteration < 10; iteration++) {
uint8_t* pattern = (iteration % 2 == 0) ? bufferA : bufferB;
for(uint32_t i = 0; i < MemorySize; i += BlockSize) {
if(!storageDevice.write(pattern, i, BlockSize)) {
MODM_LOG_INFO << "Error: Unable to write data.";
exit(1);
}
}
for(uint32_t i = 0; i < MemorySize; i += BlockSize) {
if(!storageDevice.read(bufferC, i, BlockSize)) {
MODM_LOG_INFO << "Error: Unable to read data.";
exit(1);
}
else if(std::memcmp(pattern, bufferC, BlockSize)) {
MODM_LOG_INFO << "Error: Read '";
printMemoryContent(bufferC, BlockSize);
MODM_LOG_INFO << "', expected: '";
printMemoryContent(pattern, BlockSize);
MODM_LOG_INFO << "'." << modm::endl;
exit(1);
}
}
MODM_LOG_INFO << ".";
}
MODM_LOG_INFO << modm::endl << "Finished!" << modm::endl;
return 0;
}