CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_FlashIface/examples/jedec_test_bl/jedec_test.cpp
Views: 1799
1
#include <AP_HAL/AP_HAL.h>
2
#include <AP_FlashIface/AP_FlashIface.h>
3
#include <stdio.h>
4
#include <support.h>
5
#include <hal.h>
6
7
AP_FlashIface_JEDEC jedec_dev;
8
9
static UNUSED_FUNCTION void test_page_program()
10
{
11
uint8_t *data = new uint8_t[jedec_dev.get_page_size()];
12
if (data == nullptr) {
13
uprintf("Failed to allocate data for program");
14
}
15
uint8_t *rdata = new uint8_t[jedec_dev.get_page_size()];
16
if (rdata == nullptr) {
17
uprintf("Failed to allocate data for read");
18
}
19
20
// fill program data with its own address
21
for (uint32_t i = 0; i < jedec_dev.get_page_size(); i++) {
22
data[i] = i;
23
}
24
uprintf("Writing Page #1\n");
25
uint32_t delay_us, timeout_us;
26
uint64_t start_time_us = AP_HAL::micros64();
27
if (!jedec_dev.start_program_page(0, data, delay_us, timeout_us)) {
28
uprintf("Page write command failed\n");
29
return;
30
}
31
while (true) {
32
chThdSleep(chTimeUS2I(delay_us));
33
if (AP_HAL::micros64() > (start_time_us+delay_us)) {
34
if (!jedec_dev.is_device_busy()) {
35
uprintf("Page Program Successful, elapsed %ld us\n", uint32_t(AP_HAL::micros64() - start_time_us));
36
break;
37
} else {
38
uprintf("Typical page program time reached, Still Busy?!\n");
39
}
40
}
41
if (AP_HAL::micros64() > (start_time_us+timeout_us)) {
42
uprintf("Page Program Timed out, elapsed %lld us\n", AP_HAL::micros64() - start_time_us);
43
return;
44
}
45
}
46
if (!jedec_dev.read(0, rdata, jedec_dev.get_page_size())) {
47
uprintf("Failed to read Flash page\n");
48
} else {
49
if (memcmp(data, rdata, jedec_dev.get_page_size()) != 0) {
50
uprintf("Program Data Mismatch!\n");
51
} else {
52
uprintf("Program Data Verified Good!\n");
53
}
54
}
55
56
// Now test XIP mode here as well
57
uint8_t *chip_data = nullptr;
58
if (!jedec_dev.start_xip_mode((void**)&chip_data)) {
59
uprintf("Failed to setup XIP mode\n");
60
}
61
if (chip_data == nullptr) {
62
uprintf("Invalid address!\n");
63
}
64
// Here comes the future!
65
if (memcmp(data, chip_data, jedec_dev.get_page_size()) != 0) {
66
uprintf("Program Data Mismatch in XIP mode!\n");
67
} else {
68
uprintf("Program Data Verified Good in XIP mode!\n");
69
}
70
jedec_dev.stop_xip_mode();
71
}
72
73
static UNUSED_FUNCTION void test_sector_erase()
74
{
75
uint32_t delay_ms, timeout_ms;
76
if (!jedec_dev.start_sector_erase(0, delay_ms, timeout_ms)) { // erase first sector
77
uprintf("Sector erase command failed\n");
78
return;
79
}
80
uint32_t erase_start = AP_HAL::millis();
81
uint32_t next_check_ms = 0;
82
uprintf("Erasing Sector #1 ");
83
while (true) {
84
if (AP_HAL::millis() > next_check_ms) {
85
uprintf("\n");
86
if (!jedec_dev.is_device_busy()) {
87
if (next_check_ms == 0) {
88
uprintf("Sector Erase happened too fast\n");
89
return;
90
}
91
uprintf("Sector Erase Successful, elapsed %ld ms\n", AP_HAL::millis() - erase_start);
92
break;
93
} else {
94
uprintf("Still busy erasing, elapsed %ld ms\n", AP_HAL::millis() - erase_start);
95
}
96
if ((AP_HAL::millis() - erase_start) > timeout_ms) {
97
uprintf("Sector Erase Timed Out, elapsed %ld ms\n", AP_HAL::millis() - erase_start);
98
return;
99
}
100
next_check_ms = erase_start+delay_ms;
101
}
102
chThdSleep(chTimeMS2I((delay_ms/100) + 10));
103
uprintf("*");
104
}
105
if (!jedec_dev.verify_sector_erase(0)) {
106
uprintf("Erase Verification Failed!\n");
107
} else {
108
uprintf("Erase Verification Successful!\n");
109
}
110
}
111
112
static UNUSED_FUNCTION void test_mass_erase()
113
{
114
uint32_t delay_ms, timeout_ms;
115
if (!jedec_dev.start_mass_erase(delay_ms, timeout_ms)) {
116
uprintf("Mass erase command failed\n");
117
return;
118
}
119
uint32_t erase_start = AP_HAL::millis();
120
uint32_t next_check_ms = 0;
121
uprintf("Mass Erasing ");
122
while (true) {
123
if (AP_HAL::millis() > next_check_ms) {
124
uprintf("\n");
125
if (!jedec_dev.is_device_busy()) {
126
if (next_check_ms == 0) {
127
uprintf("Sector Erase happened too fast\n");
128
return;
129
}
130
uprintf("Mass Erase Successful, elapsed %ld ms\n", AP_HAL::millis() - erase_start);
131
return;
132
} else {
133
uprintf("Still busy erasing, elapsed %ld ms\n", AP_HAL::millis() - erase_start);
134
}
135
if ((AP_HAL::millis() - erase_start) > timeout_ms) {
136
uprintf("Mass Erase Timed Out, elapsed %ld ms\n", AP_HAL::millis() - erase_start);
137
return;
138
}
139
next_check_ms = erase_start+delay_ms;
140
}
141
chThdSleep(chTimeMS2I(delay_ms/100));
142
uprintf("*");
143
}
144
}
145
146
int main()
147
{
148
init_uarts();
149
150
while (true) {
151
// Start on user input
152
while (cin(0) < 0) {}
153
uprintf("\n\n******************Starting Test********************\n");
154
jedec_dev.init();
155
test_sector_erase();
156
test_page_program();
157
// test_mass_erase();
158
chThdSleep(chTimeMS2I(1000));
159
}
160
}
161
162