Created
November 25, 2014 23:55
-
-
Save esden/a506943ddfa1c92575b2 to your computer and use it in GitHub Desktop.
STM32F4 highlevel flash functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 Piotr Esden-Tempski <[email protected]> | |
* | |
* This file is part of Paparazzi. | |
* | |
* Paparazzi 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 2, or (at your option) | |
* any later version. | |
* | |
* Paparazzi 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 Paparazzi; see the file COPYING. If not, write to | |
* the Free Software Foundation, 59 Temple Place - Suite 330, | |
* Boston, MA 02111-1307, USA. | |
*/ | |
#include <stdint.h> | |
#include <libopencm3/stm32/f4/flash.h> | |
#include "hlf4_flash.h" | |
#define HLF4_FLASH_BASE_ADDR 0x08000000 | |
#define HLF4_FLASH_16KB_SECT 0x00004000 | |
#define HLF4_FLASH_64KB_SECT 0x00010000 | |
#define HLF4_FLASH_128KB_SECT 0x00020000 | |
const struct hlf4_flash_sector_def const hlf4_flash_sector_map[] = { | |
/* sect 0 */ | |
{ .addr = (HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_16KB_SECT * 0)), | |
.size = HLF4_FLASH_16KB_SECT }, | |
/* sect 1 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_16KB_SECT * 1), | |
.size = HLF4_FLASH_16KB_SECT }, | |
/* sect 2 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_16KB_SECT * 2), | |
.size = HLF4_FLASH_16KB_SECT }, | |
/* sect 3 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_16KB_SECT * 3), | |
.size = HLF4_FLASH_16KB_SECT }, | |
/* sect 4 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_64KB_SECT * 1), | |
.size = HLF4_FLASH_64KB_SECT }, | |
/* sect 5 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_128KB_SECT * 1), | |
.size = HLF4_FLASH_128KB_SECT }, | |
/* sect 6 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_128KB_SECT * 2), | |
.size = HLF4_FLASH_128KB_SECT }, | |
/* sect 7 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_128KB_SECT * 3), | |
.size = HLF4_FLASH_128KB_SECT }, | |
/* sect 8 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_128KB_SECT * 4), | |
.size = HLF4_FLASH_128KB_SECT }, | |
/* sect 9 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_128KB_SECT * 5), | |
.size = HLF4_FLASH_128KB_SECT }, | |
/* sect 10 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_128KB_SECT * 6), | |
.size = HLF4_FLASH_128KB_SECT }, | |
/* sect 11 */ | |
{ .addr = HLF4_FLASH_BASE_ADDR + (HLF4_FLASH_128KB_SECT * 7), | |
.size = HLF4_FLASH_128KB_SECT } | |
}; | |
void hlf4_flash_init(void) | |
{ | |
/* Nothing to do. */ | |
} | |
struct hlf4_flash_sector_def const *hlf4_flash_get_sector_def(int sector) | |
{ | |
if ((sector < 0) || (sector >= HLF4_FLASH_SECT_COUNT)) { | |
return NULL; | |
} | |
return &hlf4_flash_sector_map[sector]; | |
} | |
int hlf4_flash_erase_sector(int sector) | |
{ | |
flash_unlock(); | |
/* Erasing sector word wise. | |
* We have to be careful about this, because the size of the simultanous writes allowed is dependent on the MCU power supply. | |
* Refer to the datasheet. | |
*/ | |
flash_erase_sector(sector << 3, FLASH_PROGRAM_X32); | |
flash_lock(); | |
return 0; | |
} | |
int hlf4_flash_get_sector(uint32_t addr) | |
{ | |
int i; | |
for (i=0; i<HLF4_FLASH_SECT_COUNT; i++) { | |
if ((hlf4_flash_sector_map[i].addr <= addr) && | |
(addr < (hlf4_flash_sector_map[i].addr + hlf4_flash_sector_map[i].size))) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
int hlf4_flash_write_word(uint32_t addr, uint32_t word) | |
{ | |
int sector = hl_flash_get_sector(addr); | |
/* Check if address is word aligned. */ | |
if (addr%4) { | |
return -1; /* Return an error if we try to write to an address that is not word aligned. */ | |
} | |
flash_unlock(); | |
flash_program_word(addr, word, FLASH_PROGRAM_X32); | |
flash_lock(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 Piotr Esden-Tempski <[email protected]> | |
* | |
* This file is part of Paparazzi. | |
* | |
* Paparazzi 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 2, or (at your option) | |
* any later version. | |
* | |
* Paparazzi 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 Paparazzi; see the file COPYING. If not, write to | |
* the Free Software Foundation, 59 Temple Place - Suite 330, | |
* Boston, MA 02111-1307, USA. | |
*/ | |
#ifndef HLF4_FLASH_H | |
#define HLF4_FLASH_H | |
#define HLF4_FLASH_SECT_COUNT 12 | |
struct hlf4_flash_sector_def { | |
uint32_t addr; | |
uint32_t size; | |
}; | |
void hlf4_flash_init(void); | |
struct hlf4_flash_sector_def const *hlf4_flash_get_sector_def(int sector); | |
int hlf4_flash_erase_sector(int sector); | |
int hlf4_flash_get_sector(uint32_t addr); | |
int hlf4_flash_write_word(uint32_t addr, uint32_t word); | |
#endif /* HLF4_FLASH_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment