2016-08-14 19:25:38 +02:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/** library to read/write internal flash (code)
|
|
|
|
* @file flash_internal.c
|
|
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
|
|
* @date 2016
|
|
|
|
* @note peripherals used: none
|
|
|
|
*/
|
|
|
|
/* standard libraries */
|
|
|
|
#include <stdint.h> // standard integer types
|
|
|
|
#include <stdlib.h> // general utilities
|
|
|
|
|
|
|
|
/* STM32 (including CM3) libraries */
|
|
|
|
#include <libopencm3/stm32/flash.h> // flash utilities
|
2018-02-18 14:16:16 +01:00
|
|
|
#include <libopencm3/stm32/desig.h> // device signature definitions
|
|
|
|
#include <libopencm3/stm32/dbgmcu.h> // debug definitions
|
2016-08-14 19:25:38 +02:00
|
|
|
|
|
|
|
#include "flash_internal.h" // flash storage library API
|
|
|
|
#include "global.h" // global definitions
|
|
|
|
|
|
|
|
bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size)
|
|
|
|
{
|
2018-02-18 13:54:45 +01:00
|
|
|
// sanity checks
|
|
|
|
if (address<FLASH_BASE || address>(UINT32_MAX-size) || buffer==NULL || size==0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify if it's in the flash area
|
|
|
|
#if defined(__flash_end)
|
|
|
|
if (address<FLASH_BASE || (address+size)>(uint32_t)&__flash_end)
|
|
|
|
#else
|
|
|
|
if (address<FLASH_BASE || (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024))
|
|
|
|
#endif
|
|
|
|
{
|
2016-08-14 19:25:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-15 13:57:02 +02:00
|
|
|
// copy data byte per byte (a more efficient way would be to copy words, than the remaining bytes)
|
2016-08-14 19:25:38 +02:00
|
|
|
for (size_t i=0; i<size; i++) {
|
|
|
|
buffer[i] = *((uint8_t*)address+i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
|
|
|
|
{
|
2018-02-18 13:54:45 +01:00
|
|
|
// sanity checks
|
|
|
|
if (address<FLASH_BASE || address>(UINT32_MAX-size) || buffer==NULL || size==0 || size%2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify if it's in the flash area
|
|
|
|
#if defined(__flash_end)
|
|
|
|
if (address<FLASH_BASE || (address+size)>(uint32_t)&__flash_end)
|
|
|
|
#else
|
|
|
|
if (address<FLASH_BASE || (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024))
|
|
|
|
#endif
|
|
|
|
{
|
2016-08-14 19:25:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:16:16 +01:00
|
|
|
// get page size
|
|
|
|
uint16_t page_size = 0;
|
|
|
|
if ((0x412==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x412==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK))) { // low-density (16-32 KB flash) and medium-density (64-128 KB flash) devices have 1 KB flash pages
|
|
|
|
page_size = 1024;
|
|
|
|
} else if ((0x414==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x430==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) || (0x418==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK))) { // high-density (256-512 KB flash), XL-density (768-1024 KB flash) devices and connectivity line have 2 KB flash pages
|
|
|
|
page_size = 2048;
|
|
|
|
} else { // unknown device type (or unreadable type, see errata), deduce page size from flash size
|
|
|
|
if (DESIG_FLASH_SIZE<256) {
|
|
|
|
page_size = 1024;
|
|
|
|
} else {
|
|
|
|
page_size = 2048;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 19:25:38 +02:00
|
|
|
flash_unlock(); // unlock flash to be able to write it
|
2017-04-15 13:57:02 +02:00
|
|
|
while (size) { // write page by page until all data has been written
|
2018-02-18 14:16:16 +01:00
|
|
|
uint32_t page_start = address-(address%page_size); // get start of the current page
|
2017-04-15 13:57:02 +02:00
|
|
|
bool erase = false; // verify if the flash to write is erased of if we need to erase the page
|
2018-02-18 14:16:16 +01:00
|
|
|
for (uint32_t flash=address; flash<(address+size) && flash<(page_start+page_size); flash += 2) { // go through page
|
2017-04-15 13:57:02 +02:00
|
|
|
if (*(uint16_t*)(flash)!=0xffff) { // is flash not erased
|
|
|
|
erase = true; // the erase flash
|
2016-08-14 19:25:38 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-15 13:57:02 +02:00
|
|
|
if (erase) { // make copy of the page to erase and erase it
|
2018-02-18 14:16:16 +01:00
|
|
|
uint8_t page_data[page_size]; // a copy of the complete page before the erase it
|
2017-04-15 13:57:02 +02:00
|
|
|
uint16_t page_i = 0; // index for page data
|
|
|
|
// copy page before address
|
2018-02-18 14:16:16 +01:00
|
|
|
for (uint32_t flash=page_start; flash<address && flash<(page_start+page_size) && page_i<page_size; flash++) {
|
2017-04-15 13:57:02 +02:00
|
|
|
page_data[page_i++] = *(uint8_t*)(flash);
|
|
|
|
}
|
|
|
|
// copy data starting at address
|
2018-02-18 14:16:16 +01:00
|
|
|
while (size>0 && page_i<page_size) {
|
2017-04-15 13:57:02 +02:00
|
|
|
page_data[page_i++] = *buffer;
|
|
|
|
buffer++;
|
|
|
|
address++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
// copy data after buffer until end of page
|
2018-02-18 14:16:16 +01:00
|
|
|
while (page_i<page_size) {
|
2017-04-15 13:57:02 +02:00
|
|
|
page_data[page_i] = *(uint8_t*)(page_start+page_i);
|
|
|
|
page_i++;
|
|
|
|
}
|
|
|
|
flash_erase_page(page_start); // erase current page
|
2016-08-14 19:25:38 +02:00
|
|
|
if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
|
|
|
|
flash_lock(); // lock back flash to protect it
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-18 14:16:16 +01:00
|
|
|
for (uint16_t i=0; i<page_size/2; i++) { // write whole page
|
2017-04-15 13:57:02 +02:00
|
|
|
flash_program_half_word(page_start+i*2, *((uint16_t*)(page_data+i*2)));
|
|
|
|
if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
|
|
|
|
flash_lock(); // lock back flash to protect it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (*((uint16_t*)(page_data+i*2))!=*((uint16_t*)(page_start+i*2))) { // verify the programmed data is right
|
|
|
|
flash_lock(); // lock back flash to protect it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // simply data until end of page
|
2018-02-18 14:16:16 +01:00
|
|
|
while (size>0 && address<(page_start+page_size)) {
|
2017-04-15 13:57:02 +02:00
|
|
|
flash_program_half_word(address, *((uint16_t*)(buffer)));
|
|
|
|
if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
|
|
|
|
flash_lock(); // lock back flash to protect it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (*((uint16_t*)address)!=*((uint16_t*)buffer)) { // verify the programmed data is right
|
|
|
|
flash_lock(); // lock back flash to protect it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
buffer += 2;
|
|
|
|
address += 2;
|
|
|
|
size -= 2;
|
2016-08-14 19:25:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flash_lock(); // lock back flash to protect it
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|