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 
38 static bool flash_internal_range(uint32_t address, size_t size) {
39  if (address>(UINT32_MAX-size)) { // on integer overflow will occur
40  return false;
41  }
42  if (address<FLASH_BASE) { // start address is before the start of the internal flash
43  return false;
44  }
45  if ((uint32_t)&__flash_end>=FLASH_BASE) { // check if the end for the internal flash is enforce by the linker script
46  if ((address+size)>(uint32_t)&__flash_end) { // end address is after the end of the enforced internal flash
47  return false;
48  }
49  } else {
50  if ((address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024)) { // end address is after the end of the advertised flash
51  return false;
52  }
53  }
54  return true;
55 }
56 
57 bool flash_internal_read(uint32_t address, uint8_t *buffer, size_t size)
58 {
59  // sanity checks
60  if (buffer==NULL || size==0) {
61  return false;
62  }
63  if (!flash_internal_range(address, size)) {
64  return false;
65  }
66 
67  // copy data byte per byte (a more efficient way would be to copy words, than the remaining bytes)
68  for (size_t i=0; i<size; i++) {
69  buffer[i] = *((uint8_t*)address+i);
70  }
71 
72  return true;
73 }
74 
75 bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
76 {
77  // sanity checks
78  if (buffer==NULL || size==0 || size%2) {
79  return false;
80  }
81  if (!flash_internal_range(address, size)) {
82  return false;
83  }
84 
85  // verify if it's in the flash area
86  if (address<FLASH_BASE) {
87  return false;
88  } else if ((uint32_t)&__flash_end>=FLASH_BASE && (address+size)>(uint32_t)&__flash_end) {
89  return false;
90  } else if ((uint32_t)&__flash_end<FLASH_BASE && (address+size)>(FLASH_BASE+DESIG_FLASH_SIZE*1024)) {
91  return false;
92  }
93 
94  // get page size
95  uint16_t page_size = 0;
96  if ((0x410==(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
97  page_size = 1024;
98  } 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
99  page_size = 2048;
100  } else { // unknown device type (or unreadable type, see errata), deduce page size from flash size
101  if (DESIG_FLASH_SIZE<256) {
102  page_size = 1024;
103  } else {
104  page_size = 2048;
105  }
106  }
107 
108  flash_unlock(); // unlock flash to be able to write it
109  while (size) { // write page by page until all data has been written
110  uint32_t page_start = address-(address%page_size); // get start of the current page
111  bool erase = false; // verify if the flash to write is erased of if we need to erase the page
112  for (uint32_t flash=address; flash<(address+size) && flash<(page_start+page_size); flash += 2) { // go through page
113  if (*(uint16_t*)(flash)!=0xffff) { // is flash not erased
114  erase = true; // the erase flash
115  }
116  }
117  if (erase) { // make copy of the page to erase and erase it
118  uint8_t page_data[page_size]; // a copy of the complete page before the erase it
119  uint16_t page_i = 0; // index for page data
120  // copy page before address
121  for (uint32_t flash=page_start; flash<address && flash<(page_start+page_size) && page_i<page_size; flash++) {
122  page_data[page_i++] = *(uint8_t*)(flash);
123  }
124  // copy data starting at address
125  while (size>0 && page_i<page_size) {
126  page_data[page_i++] = *buffer;
127  buffer++;
128  address++;
129  size--;
130  }
131  // copy data after buffer until end of page
132  while (page_i<page_size) {
133  page_data[page_i] = *(uint8_t*)(page_start+page_i);
134  page_i++;
135  }
136  flash_erase_page(page_start); // erase current page
137  if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
138  flash_lock(); // lock back flash to protect it
139  return false;
140  }
141  for (uint16_t i=0; i<page_size/2; i++) { // write whole page
142  flash_program_half_word(page_start+i*2, *((uint16_t*)(page_data+i*2)));
143  if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
144  flash_lock(); // lock back flash to protect it
145  return false;
146  }
147  if (*((uint16_t*)(page_data+i*2))!=*((uint16_t*)(page_start+i*2))) { // verify the programmed data is right
148  flash_lock(); // lock back flash to protect it
149  return false;
150  }
151  }
152  } else { // simply data until end of page
153  while (size>0 && address<(page_start+page_size)) {
154  flash_program_half_word(address, *((uint16_t*)(buffer)));
155  if (flash_get_status_flags()!=FLASH_SR_EOP) { // operation went wrong
156  flash_lock(); // lock back flash to protect it
157  return false;
158  }
159  if (*((uint16_t*)address)!=*((uint16_t*)buffer)) { // verify the programmed data is right
160  flash_lock(); // lock back flash to protect it
161  return false;
162  }
163  buffer += 2;
164  address += 2;
165  size -= 2;
166  }
167  }
168  }
169  flash_lock(); // lock back flash to protect it
170 
171  return true;
172 }
static bool flash_internal_range(uint32_t address, size_t size)
verify if the data is in the internal flash area
global definitions and methods (API)
library to read/write internal flash (API)
char __flash_end
symbol for end of flash
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