]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/chibios/eeprom_stm32.h
stm32f1xx EEPROM emulation (#3914)
[qmk_firmware.git] / tmk_core / common / chibios / eeprom_stm32.h
1 /*
2  * This software is experimental and a work in progress.
3  * Under no circumstances should these files be used in relation to any critical system(s).
4  * Use of these files is at your own risk.
5  *
6  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
7  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
8  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
9  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
11  * DEALINGS IN THE SOFTWARE.
12  *
13  * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and
14  * https://github.com/leaflabs/libmaple
15  *
16  * Modifications for QMK and STM32F303 by Yiancar
17  */
18
19 // This file must be modified if the MCU is not defined below.
20 // This library also assumes that the pages are not used by the firmware.
21
22 #ifndef __EEPROM_H
23 #define __EEPROM_H
24
25 #include "ch.h"
26 #include "hal.h"
27 #include "flash_stm32.h"
28
29 // HACK ALERT. This definition may not match your processor
30 // To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc
31 #if defined(EEPROM_EMU_STM32F303xC)
32   #define MCU_STM32F303CC
33 #elif defined(EEPROM_EMU_STM32F103xB)
34   #define MCU_STM32F103RB
35 #else
36   #error "not implemented."
37 #endif
38
39 #ifndef EEPROM_PAGE_SIZE
40     #if defined (MCU_STM32F103RB)
41         #define EEPROM_PAGE_SIZE    (uint16_t)0x400  /* Page size = 1KByte */
42     #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE) || defined (MCU_STM32F103RD) || defined (MCU_STM32F303CC)
43         #define EEPROM_PAGE_SIZE    (uint16_t)0x800  /* Page size = 2KByte */
44     #else
45         #error  "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
46     #endif
47 #endif
48
49 #ifndef EEPROM_START_ADDRESS
50     #if defined (MCU_STM32F103RB)
51         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 128 * 1024 - 2 * EEPROM_PAGE_SIZE))
52     #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE)
53         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 512 * 1024 - 2 * EEPROM_PAGE_SIZE))
54     #elif defined (MCU_STM32F103RD)
55         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 384 * 1024 - 2 * EEPROM_PAGE_SIZE))
56     #elif defined (MCU_STM32F303CC)
57         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 256 * 1024 - 2 * EEPROM_PAGE_SIZE))
58     #else
59         #error  "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
60     #endif
61 #endif
62
63 /* Pages 0 and 1 base and end addresses */
64 #define EEPROM_PAGE0_BASE       ((uint32_t)(EEPROM_START_ADDRESS + 0x000))
65 #define EEPROM_PAGE1_BASE       ((uint32_t)(EEPROM_START_ADDRESS + EEPROM_PAGE_SIZE))
66
67 /* Page status definitions */
68 #define EEPROM_ERASED           ((uint16_t)0xFFFF)  /* PAGE is empty */
69 #define EEPROM_RECEIVE_DATA     ((uint16_t)0xEEEE)  /* PAGE is marked to receive data */
70 #define EEPROM_VALID_PAGE       ((uint16_t)0x0000)  /* PAGE containing valid data */
71
72 /* Page full define */
73 enum uint16_t
74     {
75     EEPROM_OK               = ((uint16_t)0x0000),
76     EEPROM_OUT_SIZE         = ((uint16_t)0x0081),
77     EEPROM_BAD_ADDRESS      = ((uint16_t)0x0082),
78     EEPROM_BAD_FLASH        = ((uint16_t)0x0083),
79     EEPROM_NOT_INIT         = ((uint16_t)0x0084),
80     EEPROM_SAME_VALUE       = ((uint16_t)0x0085),
81     EEPROM_NO_VALID_PAGE    = ((uint16_t)0x00AB)
82     };
83
84 #define EEPROM_DEFAULT_DATA     0xFFFF
85
86     uint16_t EEPROM_init(void);
87     uint16_t EEPROM_format(void);
88     uint16_t EEPROM_erases(uint16_t *);
89     uint16_t EEPROM_read (uint16_t address, uint16_t *data);
90     uint16_t EEPROM_write(uint16_t address, uint16_t data);
91     uint16_t EEPROM_update(uint16_t address, uint16_t data);
92     uint16_t EEPROM_count(uint16_t *);
93     uint16_t EEPROM_maxcount(void);
94
95 #endif  /* __EEPROM_H */