GlobLib
HAL and API libraries for MCUs and hardware.
stm32f103cb_flash.c
Go to the documentation of this file.
1 /*!**************************************************************************
2  @file stm32f103cb_flash.c
3  @brief Source file for stm32f103cb flash
4  @author Stuart Ianna
5  @version 0.1
6  @date June 2018
7  @copyright GNU GPLv3
8  @warning None
9  @bug
10 
11  @details
12 
13  @par Compilers
14  - arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
15 ******************************************************************************/
16 
17 #include "stm32f103cb_flash.h"
18 
19 #define FLASH_PAGE_SIZE 255
20 #define FLASH_PAGE_BASE 0x8000000
21 
22 mcu_error FLASH_clearPage(uint32_t pageNumber){
23 
24  if(pageNumber > 127){
25 
26  //Page out of bounds
27  if(MCU_debugIsEnabled()){
28 
30  }
31  return E_FLASH_PAGE;
32  }
33 
34  flash_unlock();
35  flash_erase_page(FLASH_PAGE_BASE + (pageNumber<<10));
36  flash_lock();
37 
38  return E_FLASH_NOERROR;
39 }
40 
41 mcu_error FLASH_write(uint32_t pageNumber, uint32_t address, uint32_t data){
42 
43  //Check if page is within bounds
44  if(pageNumber > 127){
45 
46  //Page out of bounds
47  if(MCU_debugIsEnabled()){
48 
50  }
51  return E_FLASH_PAGE;
52  }
53 
54  flash_unlock();
55  flash_program_word(FLASH_PAGE_BASE + (pageNumber<<10) + (address<<2), data);
56  flash_lock();
57 
58  return E_FLASH_NOERROR;
59 }
60 
61 mcu_error FLASH_writes(uint32_t pageNumber, uint32_t address, uint32_t *data, uint16_t size){
62 
63  //Check if page is within bounds
64  if(pageNumber > 127){
65 
66  //Page out of bounds
67  if(MCU_debugIsEnabled()){
68 
70  }
71  return E_FLASH_PAGE;
72  }
73 
74  flash_unlock();
75 
76  pageNumber = FLASH_PAGE_BASE + (pageNumber<<10) + (address << 2);
77 
78  for(int i = 0; i < size; i++){
79 
80  flash_program_word(pageNumber + (i<<2), *(data++));
81  }
82 
83  flash_lock();
84 
85  return E_FLASH_NOERROR;
86 }
87 
88 uint32_t FLASH_read(uint32_t pageNumber, uint32_t address){
89 
90  //No error checking here, shouldn't matter as it is just reading a memory address
91 
92  return *((uint32_t*)(FLASH_PAGE_BASE + (pageNumber<<10) + (address<<2)));
93 
94 }
95 
96 mcu_error FLASH_reads(uint32_t pageNumber, uint32_t address, uint32_t *container, uint16_t size){
97 
98  //Check if page is within bounds
99  if(pageNumber > 127){
100 
101  //Page out of bounds
102  if(MCU_debugIsEnabled()){
103 
105  }
106  return E_FLASH_PAGE;
107  }
108 
109  pageNumber = FLASH_PAGE_BASE + (pageNumber<<10) + (address << 2);
110 
111  for(uint16_t i = 0; i < size; i ++){
112 
113  *(container++) = *((uint32_t*)( pageNumber + (i<<2)));
114  }
115  return E_FLASH_NOERROR;
116 
117 }
118 
119 mcu_error FLASH_readPage(uint32_t pageNumber, uint32_t *container){
120 
121  //Check if page is within bounds
122  if(pageNumber > 127){
123 
124  //Page out of bounds
125  if(MCU_debugIsEnabled()){
126 
128  }
129  return E_FLASH_PAGE;
130  }
131 
132  pageNumber = FLASH_PAGE_BASE + (pageNumber << 10);
133 
134  for(uint16_t i = 0; i < FLASH_PAGE_SIZE; i ++){
135 
136  *(container++) = *((uint32_t*)(pageNumber + (i<<2)));
137  }
138  return E_FLASH_NOERROR;
139 
140 }
141 
142 uint8_t FLASH_pageEmpty(uint32_t pageNumber){
143 
144  pageNumber = FLASH_PAGE_BASE + (pageNumber << 10);
145 
146  for(uint16_t i = 0; i < FLASH_PAGE_SIZE; i++){
147 
148  //Check if each location is zero
149  if((*((uint32_t*)( pageNumber + (i<<2)))) != 0xFFFFFFFF){
150 
151  return 0;
152  }
153 
154  }
155  return 1;
156 }
157 
158 uint8_t FLASH_firstEmptyPage(void){
159 
160  for(uint8_t i = 0; i < 127; i++){
161 
162  if(FLASH_pageEmpty(i)){
163 
164  return i;
165  }
166  }
167 
168  //Return zero if no empty pages found.
169  return 0;
170 
171 }
mcu_error FLASH_readPage(uint32_t pageNumber, uint32_t *container)
Read an entire page from flash.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
#define FLASH_PAGE_SIZE
The size of each page in words, used internally.
#define FLASH_PAGE_BASE
The flash starting address, used internally.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
Error FLASH: Page number out of bounds.
uint8_t FLASH_firstEmptyPage(void)
Get the first empty page is flash.
mcu_error FLASH_writes(uint32_t pageNumber, uint32_t address, uint32_t *data, uint16_t size)
Write multiple words to flash, address incremented automatically.
Error FLASH: No error.
uint8_t FLASH_pageEmpty(uint32_t pageNumber)
Determine if a given page is empty.
uint32_t FLASH_read(uint32_t pageNumber, uint32_t address)
Read a single word from flash memory.
mcu_error FLASH_clearPage(uint32_t pageNumber)
Clear a page (1kb) of flash.
Header file for stm32f103cb FLASH.
mcu_error FLASH_reads(uint32_t pageNumber, uint32_t address, uint32_t *container, uint16_t size)
Read multiple words from flash.
mcu_error FLASH_write(uint32_t pageNumber, uint32_t address, uint32_t data)
Write a single word to flash.
mcu_error
Error enumerators for the Debug peripheral.