CuVoodoo STM32F1 firmware template
flash_internal.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
21 /* standard libraries */
22 #include <stdint.h> // standard integer types
23 #include <stdlib.h> // general utilities
24 
25 /* STM32 (including CM3) libraries */
26 #include <libopencm3/stm32/flash.h> // flash utilities
27 #include <libopencm3/stm32/desig.h> // device signature definitions
28 #include <libopencm3/stm32/dbgmcu.h> // debug definitions
29 
30 #include "flash_internal.h" // flash storage library API
31 #include "global.h" // global definitions
32 
33 bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size)
34 {
35  // sanity checks
36  if (address<FLASH_BASE || address>(UINT32_MAX-size) || buffer==NULL || size==0) {
37  return false;
38  }
39 
40  // verify if it's in the flash area
41 #if defined(__flash_end)
42  if (address<FLASH_BASE || (address+size)>(uint32_t)&__flash_end)
43 #else
44  if (address<FLASH_BASE || (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024))
45 #endif
46  {
47  return false;
48  }
49 
50  // copy data byte per byte (a more efficient way would be to copy words, than the remaining bytes)
51  for (size_t i=0; i<size; i++) {
52  buffer[i] = *((uint8_t*)address+i);
53  }
54 
55  return true;
56 }
57 
58 bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
59 {
60  // sanity checks
61  if (address<FLASH_BASE || address>(UINT32_MAX-size) || buffer==NULL || size==0 || size%2) {
62  return false;
63  }
64 
65  // verify if it's in the flash area
66 #if defined(__flash_end)
67  if (address<FLASH_BASE || (address+size)>(uint32_t)&__flash_end)
68 #else
69  if (address<FLASH_BASE || (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024))
70 #endif
71  {
72  return false;
73  }
74 
75  // get page size
76  uint16_t page_size = 0;
77  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
78  page_size = 1024;
79  } 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
80  page_size = 2048;
81  } else { // unknown device type (or unreadable type, see errata), deduce page size from flash size
82  if (DESIG_FLASH_SIZE<256) {
83  page_size = 1024;
84  } else {
85  page_size = 2048;
86  }
87  }
88 
89  flash_unlock(); // unlock flash to be able to write it
90  while (size) { // write page by page until all data has been written
91  uint32_t page_start = address-(address%page_size); // get start of the current page
92  bool erase = false; // verify if the flash to write is erased of if we need to erase the page
93  for (uint32_t flash=address; flash<(address+size) && flash<(page_start+page_size); flash += 2) { // go through page
94  if (*(uint16_t*)(flash)!=0xffff) { // is flash not erased
95  erase = true; // the erase flash
96  }
97  }
98  if (erase) { // make copy of the page to erase and erase it
99  uint8_t page_data[page_size]; // a copy of the complete page before the erase it
100  uint16_t page_i = 0; // index for page data
101  // copy page before address
102  for (uint32_t flash=page_start; flash<address && flash<(page_start+page_size) && page_i<page_size; flash++) {
103  page_data[page_i++] = *(uint8_t*)(flash);
104  }
105  // copy data starting at address
106  while (size>0 && page_i<page_size) {
107  page_data[page_i++] = *buffer;
108  buffer++;
109  address++;
110  size--;
111  }
112  // copy data after buffer until end of page
113  while (page_i<page_size) {
114  page_data[page_i] = *(uint8_t*)(page_start+page_i);
115  page_i++;
116  }
117  flash_erase_page(page_start); // erase current page
118  if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
119  flash_lock(); // lock back flash to protect it
120  return false;
121  }
122  for (uint16_t i=0; i<page_size/2; i++) { // write whole page
123  flash_program_half_word(page_start+i*2, *((uint16_t*)(page_data+i*2)));
124  if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
125  flash_lock(); // lock back flash to protect it
126  return false;
127  }
128  if (*((uint16_t*)(page_data+i*2))!=*((uint16_t*)(page_start+i*2))) { // verify the programmed data is right
129  flash_lock(); // lock back flash to protect it
130  return false;
131  }
132  }
133  } else { // simply data until end of page
134  while (size>0 && address<(page_start+page_size)) {
135  flash_program_half_word(address, *((uint16_t*)(buffer)));
136  if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
137  flash_lock(); // lock back flash to protect it
138  return false;
139  }
140  if (*((uint16_t*)address)!=*((uint16_t*)buffer)) { // verify the programmed data is right
141  flash_lock(); // lock back flash to protect it
142  return false;
143  }
144  buffer += 2;
145  address += 2;
146  size -= 2;
147  }
148  }
149  }
150  flash_lock(); // lock back flash to protect it
151 
152  return true;
153 }
global definitions and methods (API)
library to read/write internal flash (API)
bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size)
read data from internal flash
static uint8_t * buffer
input/output buffer for read/write commands/functions
bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
write data to internal flash