Skip to content

Commit f7dff92

Browse files
rasmuskleistrleh
authored andcommitted
[example] Add flash examples for STM32G4
1 parent c67fcbc commit f7dff92

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2020, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <modm/board.hpp>
12+
#include <modm/processing.hpp>
13+
14+
#undef MODM_LOG_LEVEL
15+
#define MODM_LOG_LEVEL modm::log::INFO
16+
17+
// ----------------------------------------------------------------------------
18+
int
19+
main()
20+
{
21+
Board::initialize();
22+
23+
MODM_LOG_INFO << "\n\nReboot\n";
24+
if (not Flash::unlock()) {
25+
MODM_LOG_INFO << "Flash unlock failed!" << modm::endl;
26+
}
27+
28+
{
29+
uint32_t err{0};
30+
MODM_LOG_INFO << "Erasing sectors [32, 64)" << modm::endl;
31+
MODM_LOG_INFO.flush();
32+
modm::delay(1s);
33+
34+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
35+
36+
for (uint8_t page{32}; page < 64u; page++)
37+
err |= Flash::erase(page);
38+
39+
const auto diff = (modm::PreciseClock::now() - start);
40+
MODM_LOG_INFO << "Erasing done in " << diff << " with errors: " << err << modm::endl;
41+
MODM_LOG_INFO << "Erasing with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
42+
MODM_LOG_INFO.flush();
43+
}
44+
45+
46+
{
47+
uint32_t err{0};
48+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
49+
for (uint32_t dst_addr{Flash::OriginAddr + Flash::Size/2}, src_addr{Flash::OriginAddr};
50+
src_addr < (Flash::OriginAddr + Flash::Size/2);
51+
src_addr += sizeof(Flash::MaxWordType), dst_addr += sizeof(Flash::MaxWordType))
52+
{
53+
err |= Flash::program(dst_addr, *(Flash::MaxWordType*)src_addr);
54+
}
55+
56+
const auto diff = (modm::PreciseClock::now() - start);
57+
MODM_LOG_INFO << "Programming done in " << diff << " with errors: " << err << modm::endl;
58+
MODM_LOG_INFO << "Programming with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
59+
}
60+
61+
while(1) ;
62+
return 0;
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library>
2+
<extends>modm:nucleo-g431kb</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g431kb/flash</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:gpio</module>
8+
<module>modm:platform:flash</module>
9+
<module>modm:processing:timer</module>
10+
<module>modm:build:scons</module>
11+
</modules>
12+
</library>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2020, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <modm/board.hpp>
12+
#include <modm/processing.hpp>
13+
14+
#undef MODM_LOG_LEVEL
15+
#define MODM_LOG_LEVEL modm::log::INFO
16+
17+
// ----------------------------------------------------------------------------
18+
int
19+
main()
20+
{
21+
Board::initialize();
22+
23+
MODM_LOG_INFO << "\n\nReboot\n";
24+
if (not Flash::unlock()) {
25+
MODM_LOG_INFO << "Flash unlock failed!" << modm::endl;
26+
}
27+
28+
{
29+
uint32_t err{0};
30+
MODM_LOG_INFO << "Erasing sectors [32, 64)" << modm::endl;
31+
MODM_LOG_INFO.flush();
32+
modm::delay(1s);
33+
34+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
35+
36+
for (uint8_t page{32}; page < 64u; page++)
37+
err |= Flash::erase(page);
38+
39+
const auto diff = (modm::PreciseClock::now() - start);
40+
MODM_LOG_INFO << "Erasing done in " << diff << " with errors: " << err << modm::endl;
41+
MODM_LOG_INFO << "Erasing with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
42+
MODM_LOG_INFO.flush();
43+
}
44+
45+
46+
{
47+
uint32_t err{0};
48+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
49+
for (uint32_t dst_addr{Flash::OriginAddr + Flash::Size/2}, src_addr{Flash::OriginAddr};
50+
src_addr < (Flash::OriginAddr + Flash::Size/2);
51+
src_addr += sizeof(Flash::MaxWordType), dst_addr += sizeof(Flash::MaxWordType))
52+
{
53+
err |= Flash::program(dst_addr, *(Flash::MaxWordType*)src_addr);
54+
}
55+
56+
const auto diff = (modm::PreciseClock::now() - start);
57+
MODM_LOG_INFO << "Programming done in " << diff << " with errors: " << err << modm::endl;
58+
MODM_LOG_INFO << "Programming with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
59+
}
60+
61+
while(1) ;
62+
return 0;
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library>
2+
<extends>modm:nucleo-g431rb</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g431rb/flash</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:gpio</module>
8+
<module>modm:platform:flash</module>
9+
<module>modm:processing:timer</module>
10+
<module>modm:build:scons</module>
11+
</modules>
12+
</library>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2020, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <modm/board.hpp>
12+
#include <modm/processing.hpp>
13+
14+
#undef MODM_LOG_LEVEL
15+
#define MODM_LOG_LEVEL modm::log::INFO
16+
17+
// ----------------------------------------------------------------------------
18+
int
19+
main()
20+
{
21+
Board::initialize();
22+
23+
MODM_LOG_INFO << "\n\nReboot\n";
24+
if (not Flash::unlock()) {
25+
MODM_LOG_INFO << "Flash unlock failed!" << modm::endl;
26+
}
27+
28+
{
29+
uint32_t err{0};
30+
MODM_LOG_INFO << "Erasing sectors [32, 64)" << modm::endl;
31+
MODM_LOG_INFO.flush();
32+
modm::delay(1s);
33+
34+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
35+
36+
for (uint8_t page{32}; page < 64u; page++)
37+
err |= Flash::erase(page);
38+
39+
const auto diff = (modm::PreciseClock::now() - start);
40+
MODM_LOG_INFO << "Erasing done in " << diff << " with errors: " << err << modm::endl;
41+
MODM_LOG_INFO << "Erasing with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
42+
MODM_LOG_INFO.flush();
43+
}
44+
45+
46+
{
47+
uint32_t err{0};
48+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
49+
for (uint32_t dst_addr{Flash::OriginAddr + Flash::Size/2}, src_addr{Flash::OriginAddr};
50+
src_addr < (Flash::OriginAddr + Flash::Size/2);
51+
src_addr += sizeof(Flash::MaxWordType), dst_addr += sizeof(Flash::MaxWordType))
52+
{
53+
err |= Flash::program(dst_addr, *(Flash::MaxWordType*)src_addr);
54+
}
55+
56+
const auto diff = (modm::PreciseClock::now() - start);
57+
MODM_LOG_INFO << "Programming done in " << diff << " with errors: " << err << modm::endl;
58+
MODM_LOG_INFO << "Programming with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
59+
}
60+
61+
while(1) ;
62+
return 0;
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<library>
2+
<extends>modm:nucleo-g474re</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g474re/flash</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:gpio</module>
8+
<module>modm:platform:flash</module>
9+
<module>modm:processing:timer</module>
10+
<module>modm:build:scons</module>
11+
</modules>
12+
</library>

0 commit comments

Comments
 (0)