From bbf77c4ab079451d9630bf4e9b2d654b842ec982 Mon Sep 17 00:00:00 2001 From: silseva Date: Mon, 8 Nov 2021 17:10:00 +0100 Subject: [PATCH] Simple driver for erase and write of MCU's internal flash --- platform/drivers/NVM/mcuFlash.h | 49 +++++++++++++++++ platform/drivers/NVM/mcuFlash_MDx.c | 82 +++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 platform/drivers/NVM/mcuFlash.h create mode 100644 platform/drivers/NVM/mcuFlash_MDx.c diff --git a/platform/drivers/NVM/mcuFlash.h b/platform/drivers/NVM/mcuFlash.h new file mode 100644 index 00000000..fbfceefe --- /dev/null +++ b/platform/drivers/NVM/mcuFlash.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#ifndef MCUFLASH_H +#define MCUFLASH_H + +#include +#include + +/** + * Driver for MCU's internal flash management, allowing for sector erase and data + * writing. + */ + +/** + * Erase one sector of the MCU flash memory. + * + * @param secNum: sector number. + * @return true for successful erase, false otherwise. + */ +bool mcuFlash_eraseSector(const uint8_t secNum); + +/** + * Write data to the MCU flash memory. + * + * @param address: starting address for the write operation. + * @param data: data to be written. + * @param len: data length. + */ +void mcuFlash_write(const uint32_t address, const void *data, const size_t len); + +#endif /* MCUFLASH_H */ \ No newline at end of file diff --git a/platform/drivers/NVM/mcuFlash_MDx.c b/platform/drivers/NVM/mcuFlash_MDx.c new file mode 100644 index 00000000..5df510ab --- /dev/null +++ b/platform/drivers/NVM/mcuFlash_MDx.c @@ -0,0 +1,82 @@ +/*************************************************************************** + * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, see * + ***************************************************************************/ + +#include "mcuFlash.h" + +inline bool unlock() +{ + // Flash already unlocked + if((FLASH->CR & FLASH_CR_LOCK) == 0) + { + return true; + } + + FLASH->KEYR = 0x45670123; + FLASH->KEYR = 0xCDEF89AB; + + // Succesful unlock + if((FLASH->CR & FLASH_CR_LOCK) == 0) + { + return true; + } + + return false; +} + +bool mcuFlash_eraseSector(const uint8_t secNum) +{ + if(secNum > 11) return false; + if(unlock() == false) return false; + + // Flash busy, wait until previous operation finishes + while((FLASH->SR & FLASH_SR_BSY) != 0) ; + + FLASH->CR |= FLASH_CR_PSIZE_1 // 32-bit program parallelism + | (secNum << 3) // Sector number + | FLASH_CR_SER // Sector erase + | FLASH_CR_STRT; // Start erase + + // Wait until erase ends + while((FLASH->SR & FLASH_SR_BSY) != 0) ; + + return true; +} + +void mcuFlash_write(const uint32_t address, const void *data, const size_t len) +{ + if((data == NULL) || (len == 0)) return; + + // Flash busy, wait until previous operation finishes + while((FLASH->SR & FLASH_SR_BSY) != 0) ; + + // Request programming with 8-bit parallelism + FLASH->CR = FLASH_CR_PG; + + const uint8_t *buf = ((uint8_t *) data); + const uint8_t *mem = ((uint8_t *) address); + for(size_t i = 0; i < len; i++) + { + *mem = buf[i]; + mem++; + } + + // Wait until the end of the write operation + while((FLASH->SR & FLASH_SR_BSY) != 0) ; +} \ No newline at end of file