Skip to content

Commit 7b1797c

Browse files
authored
STM32 EEPROM Kütüphanesi
1 parent b802b31 commit 7b1797c

File tree

3 files changed

+299
-0
lines changed

3 files changed

+299
-0
lines changed

ee.c

+249
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
2+
#include "ee.h"
3+
#include "eeConfig.h"
4+
#include <string.h>
5+
6+
#define PAGE 0
7+
#define SECTOR 1
8+
9+
#if defined(STM32F103xB)
10+
#define _EE_SIZE 1024
11+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
12+
#define _EE_FLASH_BANK FLASH_BANK_1
13+
#define _EE_PAGE_OR_SECTOR PAGE
14+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
15+
#error "Please Enter currect address, maximum is (127)"
16+
#endif
17+
#endif
18+
19+
#if defined(STM32F103x8)
20+
#define _EE_MAX_SIZE 1024
21+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_MAX_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
22+
#define _EE_FLASH_BANK FLASH_BANK_1
23+
#define _EE_PAGE_OR_SECTOR PAGE
24+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
25+
#error "Please Enter currect address, maximum is (63)"
26+
#endif
27+
#endif
28+
29+
#if defined(STM32F103xC)
30+
#define _EE_SIZE 2048
31+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
32+
#define _EE_FLASH_BANK FLASH_BANK_1
33+
#define _EE_PAGE_OR_SECTOR PAGE
34+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
35+
#error "Please Enter currect address, maximum is (127)"
36+
#endif
37+
#endif
38+
39+
#if defined(STM32F103xD)
40+
#define _EE_SIZE 2048
41+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
42+
#define _EE_FLASH_BANK FLASH_BANK_1
43+
#define _EE_PAGE_OR_SECTOR PAGE
44+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 191)
45+
#error "Please Enter currect address, maximum is (191)"
46+
#endif
47+
#endif
48+
49+
#if defined(STM32F103xE)
50+
#define _EE_SIZE 2048
51+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
52+
#define _EE_FLASH_BANK FLASH_BANK_1
53+
#define _EE_PAGE_OR_SECTOR PAGE
54+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 255)
55+
#error "Please Enter currect address, maximum is (255)"
56+
#endif
57+
#endif
58+
59+
#if defined(STM32F030x4) || defined(STM32F042x4) || defined(STM32F070x4)
60+
#define _EE_SIZE 1024
61+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
62+
#define _EE_PAGE_OR_SECTOR PAGE
63+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 15)
64+
#error "Please Enter currect address, maximum is (15)"
65+
#endif
66+
#endif
67+
68+
#if defined(STM32F030x6) || defined(STM32F042x6) || defined(STM32F070x6)
69+
#define _EE_SIZE 1024
70+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
71+
#define _EE_PAGE_OR_SECTOR PAGE
72+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 31)
73+
#error "Please Enter currect address, maximum is (31)"
74+
#endif
75+
#endif
76+
77+
#if defined(STM32F030x8) || defined(STM32F042x8)
78+
#define _EE_SIZE 1024
79+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
80+
#define _EE_PAGE_OR_SECTOR PAGE
81+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
82+
#error "Please Enter currect address, maximum is (63)"
83+
#endif
84+
#endif
85+
86+
#if defined(STM32F070xB)
87+
#define _EE_SIZE 2048
88+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
89+
#define _EE_PAGE_OR_SECTOR PAGE
90+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 63)
91+
#error "Please Enter currect address, maximum is (63)"
92+
#endif
93+
#endif
94+
95+
#if defined(STM32F070xC)
96+
#define _EE_SIZE 2048
97+
#define _EE_ADDR_INUSE (((uint32_t)0x08000000) | (_EE_SIZE * _EE_USE_FLASH_PAGE_OR_SECTOR))
98+
#define _EE_PAGE_OR_SECTOR PAGE
99+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 127)
100+
#error "Please Enter currect address, maximum is (127)"
101+
#endif
102+
#endif
103+
104+
105+
#if defined(STM32F405xx) || defined(STM32F407xx) || defined(STM32F415xx) || defined(STM32F417xx)
106+
#define _EE_SIZE (1024 * 128)
107+
#define _EE_ADDR_INUSE (((uint32_t)0x08020000) | (_EE_SIZE*(_EE_USE_FLASH_PAGE_OR_SECTOR - 5)))
108+
#define _EE_FLASH_BANK FLASH_BANK_1
109+
#define _EE_VOLTAGE_RANGE _EE_VOLTAGE
110+
#define _EE_PAGE_OR_SECTOR PAGE
111+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR > 11)
112+
#error "Please Enter currect address, maximum is (11)"
113+
#endif
114+
#if (_EE_USE_FLASH_PAGE_OR_SECTOR < 5)
115+
#error "Please Enter currect address, minimum is (5)"
116+
#endif
117+
#endif
118+
119+
#if (_EE_USE_RAM_BYTE > 0)
120+
uint8_t ee_ram[_EE_USE_RAM_BYTE];
121+
#endif
122+
123+
//##########################################################################################################
124+
bool ee_init(void)
125+
{
126+
#if (_EE_USE_RAM_BYTE > 0)
127+
return ee_read(0, _EE_USE_RAM_BYTE, NULL);
128+
#else
129+
return true;
130+
#endif
131+
}
132+
//##########################################################################################################
133+
bool ee_format(bool keepRamData)
134+
{
135+
uint32_t error;
136+
HAL_FLASH_Unlock();
137+
FLASH_EraseInitTypeDef flashErase;
138+
#if _EE_PAGE_OR_SECTOR == PAGE
139+
flashErase.NbPages = 1;
140+
flashErase.PageAddress = _EE_ADDR_INUSE;
141+
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
142+
#else
143+
flashErase.NbSectors = 1;
144+
flashErase.Sector = _EE_ADDR_INUSE;
145+
flashErase.TypeErase = FLASH_TYPEERASE_SECTORS;
146+
#endif
147+
#ifdef _EE_FLASH_BANK
148+
flashErase.Banks = _EE_FLASH_BANK;
149+
#endif
150+
#ifdef _EE_VOLTAGE_RANGE
151+
flashErase.VoltageRange = _EE_VOLTAGE_RANGE;
152+
#endif
153+
if (HAL_FLASHEx_Erase(&flashErase, &error) == HAL_OK)
154+
{
155+
HAL_FLASH_Lock();
156+
if(error != 0xFFFFFFFF)
157+
return false;
158+
else
159+
{
160+
#if (_EE_USE_RAM_BYTE > 0)
161+
if (keepRamData == false)
162+
memset(ee_ram, 0xFF, _EE_USE_RAM_BYTE);
163+
#endif
164+
return true;
165+
}
166+
}
167+
HAL_FLASH_Lock();
168+
return false;
169+
}
170+
//##########################################################################################################
171+
bool ee_read(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
172+
{
173+
if ((startVirtualAddress + len) > _EE_SIZE)
174+
return false;
175+
for (uint32_t i = startVirtualAddress ; i < len + startVirtualAddress ; i++)
176+
{
177+
if (data != NULL)
178+
{
179+
*data = (*(__IO uint8_t*)(i + _EE_ADDR_INUSE));
180+
data++;
181+
}
182+
#if (_EE_USE_RAM_BYTE > 0)
183+
if ( i < _EE_USE_RAM_BYTE)
184+
ee_ram[i] = (*(__IO uint8_t*)(i + _EE_ADDR_INUSE));
185+
#endif
186+
}
187+
return true;
188+
}
189+
//##########################################################################################################
190+
bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
191+
{
192+
if ((startVirtualAddress + len) > _EE_SIZE)
193+
return false;
194+
if (data == NULL)
195+
return false;
196+
HAL_FLASH_Unlock();
197+
#ifdef FLASH_TYPEPROGRAM_BYTE
198+
for (uint32_t i = 0; i < len ; i++)
199+
{
200+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint64_t)(data[i])) != HAL_OK)
201+
{
202+
HAL_FLASH_Lock();
203+
return false;
204+
}
205+
}
206+
#else
207+
for (uint32_t i = 0; i < len ; i+=2)
208+
{
209+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint64_t)(data[i] | (data[i+1] << 8))) != HAL_OK)
210+
{
211+
HAL_FLASH_Lock();
212+
return false;
213+
}
214+
}
215+
#endif
216+
HAL_FLASH_Lock();
217+
return true;
218+
}
219+
//##########################################################################################################
220+
bool ee_writeToRam(uint32_t startVirtualAddress, uint32_t len, uint8_t* data)
221+
{
222+
#if (_EE_USE_RAM_BYTE > 0)
223+
if ((startVirtualAddress + len) > _EE_USE_RAM_BYTE)
224+
return false;
225+
if (data == NULL)
226+
return false;
227+
memcpy(&ee_ram[startVirtualAddress], data, len);
228+
return true;
229+
#else
230+
return false;
231+
#endif
232+
}
233+
//##########################################################################################################
234+
bool ee_commit(void)
235+
{
236+
#if (_EE_USE_RAM_BYTE > 0)
237+
if (ee_format(true) == false)
238+
return false;
239+
return ee_write(0, _EE_USE_RAM_BYTE, ee_ram);
240+
#else
241+
return false;
242+
#endif
243+
}
244+
//##########################################################################################################
245+
uint32_t ee_maxVirtualAddress(void)
246+
{
247+
return (_EE_SIZE);
248+
}
249+
//##########################################################################################################

ee.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef __EEPROM_H
2+
#define __EEPROM_H
3+
4+
/*
5+
Author: Nima Askari
6+
WebSite: http://www.github.com/NimaLTD
7+
Instagram: http://instagram.com/github.NimaLTD
8+
Youtube: https://www.youtube.com/channel/UCUhY7qY1klJm1d2kulr9ckw
9+
10+
Version: 2.0.1
11+
12+
(2.0.1)
13+
Change function name to ee_commit().
14+
15+
Reversion History:
16+
(2.0.0)
17+
Rewrite again.
18+
19+
*/
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
#include <stdbool.h>
26+
#include "gpio.h"
27+
28+
//################################################################################################################
29+
bool ee_init(void);
30+
bool ee_format(bool keepRamData);
31+
bool ee_read(uint32_t startVirtualAddress, uint32_t len, uint8_t* data);
32+
bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t* data);
33+
bool ee_writeToRam(uint32_t startVirtualAddress, uint32_t len, uint8_t* data); // only use when _EE_USE_RAM_BYTE is enabled
34+
bool ee_commit(void); // only use when _EE_USE_RAM_BYTE is enabled
35+
uint32_t ee_maxVirtualAddress(void);
36+
37+
//################################################################################################################
38+
39+
#ifdef __cplusplus
40+
}
41+
#endif
42+
43+
#endif

eeConfig.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef __EECONFIG_H
2+
#define __EECONFIG_H
3+
4+
#define _EE_USE_FLASH_PAGE_OR_SECTOR (127)
5+
#define _EE_USE_RAM_BYTE (1024)
6+
#define _EE_VOLTAGE FLASH_VOLTAGE_RANGE_3
7+
#endif

0 commit comments

Comments
 (0)