Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable flash driver for STM32G4 #838

Merged
merged 2 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ Please [discover modm's peripheral drivers for your specific device][discover].
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">✅</td>
<td align="center"></td>
<td align="center"></td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
Expand Down
63 changes: 63 additions & 0 deletions examples/nucleo_g431kb/flash/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020, Niklas Hauser
*
* 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/board.hpp>
#include <modm/processing.hpp>

#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();

MODM_LOG_INFO << "\n\nReboot\n";
if (not Flash::unlock()) {
MODM_LOG_INFO << "Flash unlock failed!" << modm::endl;
}

{
uint32_t err{0};
MODM_LOG_INFO << "Erasing sectors [32, 64)" << modm::endl;
MODM_LOG_INFO.flush();
modm::delay(1s);

const modm::PreciseTimestamp start = modm::PreciseClock::now();

for (uint8_t page{32}; page < 64u; page++)
err |= Flash::erase(page);

const auto diff = (modm::PreciseClock::now() - start);
MODM_LOG_INFO << "Erasing done in " << diff << " with errors: " << err << modm::endl;
MODM_LOG_INFO << "Erasing with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
MODM_LOG_INFO.flush();
}


{
uint32_t err{0};
const modm::PreciseTimestamp start = modm::PreciseClock::now();
for (uint32_t dst_addr{Flash::OriginAddr + Flash::Size/2}, src_addr{Flash::OriginAddr};
src_addr < (Flash::OriginAddr + Flash::Size/2);
src_addr += sizeof(Flash::MaxWordType), dst_addr += sizeof(Flash::MaxWordType))
{
err |= Flash::program(dst_addr, *(Flash::MaxWordType*)src_addr);
}

const auto diff = (modm::PreciseClock::now() - start);
MODM_LOG_INFO << "Programming done in " << diff << " with errors: " << err << modm::endl;
MODM_LOG_INFO << "Programming with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
}

while(1) ;
return 0;
}
12 changes: 12 additions & 0 deletions examples/nucleo_g431kb/flash/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:nucleo-g431kb</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g431kb/flash</option>
</options>
<modules>
<module>modm:platform:gpio</module>
<module>modm:platform:flash</module>
<module>modm:processing:timer</module>
<module>modm:build:scons</module>
</modules>
</library>
63 changes: 63 additions & 0 deletions examples/nucleo_g431rb/flash/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020, Niklas Hauser
*
* 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/board.hpp>
#include <modm/processing.hpp>

#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();

MODM_LOG_INFO << "\n\nReboot\n";
if (not Flash::unlock()) {
MODM_LOG_INFO << "Flash unlock failed!" << modm::endl;
}

{
uint32_t err{0};
MODM_LOG_INFO << "Erasing sectors [32, 64)" << modm::endl;
MODM_LOG_INFO.flush();
modm::delay(1s);

const modm::PreciseTimestamp start = modm::PreciseClock::now();

for (uint8_t page{32}; page < 64u; page++)
err |= Flash::erase(page);

const auto diff = (modm::PreciseClock::now() - start);
MODM_LOG_INFO << "Erasing done in " << diff << " with errors: " << err << modm::endl;
MODM_LOG_INFO << "Erasing with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
MODM_LOG_INFO.flush();
}


{
uint32_t err{0};
const modm::PreciseTimestamp start = modm::PreciseClock::now();
for (uint32_t dst_addr{Flash::OriginAddr + Flash::Size/2}, src_addr{Flash::OriginAddr};
src_addr < (Flash::OriginAddr + Flash::Size/2);
src_addr += sizeof(Flash::MaxWordType), dst_addr += sizeof(Flash::MaxWordType))
{
err |= Flash::program(dst_addr, *(Flash::MaxWordType*)src_addr);
}

const auto diff = (modm::PreciseClock::now() - start);
MODM_LOG_INFO << "Programming done in " << diff << " with errors: " << err << modm::endl;
MODM_LOG_INFO << "Programming with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
}

while(1) ;
return 0;
}
12 changes: 12 additions & 0 deletions examples/nucleo_g431rb/flash/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:nucleo-g431rb</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g431rb/flash</option>
</options>
<modules>
<module>modm:platform:gpio</module>
<module>modm:platform:flash</module>
<module>modm:processing:timer</module>
<module>modm:build:scons</module>
</modules>
</library>
63 changes: 63 additions & 0 deletions examples/nucleo_g474re/flash/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020, Niklas Hauser
*
* 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/board.hpp>
#include <modm/processing.hpp>

#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();

MODM_LOG_INFO << "\n\nReboot\n";
if (not Flash::unlock()) {
MODM_LOG_INFO << "Flash unlock failed!" << modm::endl;
}

{
uint32_t err{0};
MODM_LOG_INFO << "Erasing sectors [32, 64)" << modm::endl;
MODM_LOG_INFO.flush();
modm::delay(1s);

const modm::PreciseTimestamp start = modm::PreciseClock::now();

for (uint8_t page{32}; page < 64u; page++)
err |= Flash::erase(page);

const auto diff = (modm::PreciseClock::now() - start);
MODM_LOG_INFO << "Erasing done in " << diff << " with errors: " << err << modm::endl;
MODM_LOG_INFO << "Erasing with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
MODM_LOG_INFO.flush();
}


{
uint32_t err{0};
const modm::PreciseTimestamp start = modm::PreciseClock::now();
for (uint32_t dst_addr{Flash::OriginAddr + Flash::Size/2}, src_addr{Flash::OriginAddr};
src_addr < (Flash::OriginAddr + Flash::Size/2);
src_addr += sizeof(Flash::MaxWordType), dst_addr += sizeof(Flash::MaxWordType))
{
err |= Flash::program(dst_addr, *(Flash::MaxWordType*)src_addr);
}

const auto diff = (modm::PreciseClock::now() - start);
MODM_LOG_INFO << "Programming done in " << diff << " with errors: " << err << modm::endl;
MODM_LOG_INFO << "Programming with " << (Flash::Size/2 / (diff.count() >> 10) ) << "kiB/s" << modm::endl;
}

while(1) ;
return 0;
}
12 changes: 12 additions & 0 deletions examples/nucleo_g474re/flash/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:nucleo-g474re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g474re/flash</option>
</options>
<modules>
<module>modm:platform:gpio</module>
<module>modm:platform:flash</module>
<module>modm:processing:timer</module>
<module>modm:build:scons</module>
</modules>
</library>
3 changes: 2 additions & 1 deletion src/modm/platform/flash/stm32/flash.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Flash::erase(uint8_t index)
FLASH->CR = FLASH_CR_STRT | FLASH_CR_SER | uint32_t(size) |
((index << FLASH_CR_SNB_Pos) & FLASH_CR_SNB_Msk);
%% else
%% if family == "g0"
%% if family in ["g0","g4"]
FLASH->CR = FLASH_CR_STRT | FLASH_CR_PER |
((index << FLASH_CR_PNB_Pos) & FLASH_CR_PNB_Msk);
%% else
Expand Down Expand Up @@ -154,6 +154,7 @@ Flash::program(uintptr_t addr, MaxWordType data)
FLASH->CR &= ~FLASH_CR_PG;
return FLASH->SR & FLASH_SR_PGERR;
%% else
FLASH->SR |= FLASH_SR_EOP;
rleh marked this conversation as resolved.
Show resolved Hide resolved
FLASH->CR = 0;
return FLASH->SR & FLASH_SR_ERR;
%% endif
Expand Down
1 change: 0 additions & 1 deletion src/modm/platform/flash/stm32/flash.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public:
%% if not has_sectors and family == "g0"
Rcc::disable<Peripheral::Flash>();
%% endif

rleh marked this conversation as resolved.
Show resolved Hide resolved
}

static bool
Expand Down
6 changes: 5 additions & 1 deletion src/modm/platform/flash/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def init(module):

def prepare(module, options):
device = options[":target"]
if device.identifier.family not in ["g0", "f1", "f4"]:
if device.identifier.family not in ["g0","g4", "f1", "f4"]:
return False
if not device.has_driver("flash:stm32*"):
return False
Expand All @@ -44,6 +44,10 @@ def build(env):
block_shift = 11
ftype = "page"
busy_bit = "FLASH_SR_BSY1"
elif target.family in ["g4"]:
block_shift = 11
ftype = "page"
busy_bit = "FLASH_SR_BSY"

env.substitutions = {
"start": int(flash["start"], 16),
Expand Down