]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/chibios/eeprom_stm32.h
68aa14f6d485d5e3d72d30125954af4d0ffeac22
[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 #define MCU_STM32F303CC
32
33 #ifndef EEPROM_PAGE_SIZE
34     #if defined (MCU_STM32F103RB)
35         #define EEPROM_PAGE_SIZE    (uint16_t)0x400  /* Page size = 1KByte */
36     #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE) || defined (MCU_STM32F103RD) || defined (MCU_STM32F303CC)
37         #define EEPROM_PAGE_SIZE    (uint16_t)0x800  /* Page size = 2KByte */
38     #else
39         #error  "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
40     #endif
41 #endif
42
43 #ifndef EEPROM_START_ADDRESS
44     #if defined (MCU_STM32F103RB)
45         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 128 * 1024 - 2 * EEPROM_PAGE_SIZE))
46     #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE)
47         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 512 * 1024 - 2 * EEPROM_PAGE_SIZE))
48     #elif defined (MCU_STM32F103RD)
49         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 384 * 1024 - 2 * EEPROM_PAGE_SIZE))
50     #elif defined (MCU_STM32F303CC)
51         #define EEPROM_START_ADDRESS    ((uint32_t)(0x8000000 + 256 * 1024 - 2 * EEPROM_PAGE_SIZE))
52     #else
53         #error  "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
54     #endif
55 #endif
56
57 /* Pages 0 and 1 base and end addresses */
58 #define EEPROM_PAGE0_BASE       ((uint32_t)(EEPROM_START_ADDRESS + 0x000))
59 #define EEPROM_PAGE1_BASE       ((uint32_t)(EEPROM_START_ADDRESS + EEPROM_PAGE_SIZE))
60
61 /* Page status definitions */
62 #define EEPROM_ERASED           ((uint16_t)0xFFFF)  /* PAGE is empty */
63 #define EEPROM_RECEIVE_DATA     ((uint16_t)0xEEEE)  /* PAGE is marked to receive data */
64 #define EEPROM_VALID_PAGE       ((uint16_t)0x0000)  /* PAGE containing valid data */
65
66 /* Page full define */
67 enum uint16_t
68     {
69     EEPROM_OK               = ((uint16_t)0x0000),
70     EEPROM_OUT_SIZE         = ((uint16_t)0x0081),
71     EEPROM_BAD_ADDRESS      = ((uint16_t)0x0082),
72     EEPROM_BAD_FLASH        = ((uint16_t)0x0083),
73     EEPROM_NOT_INIT         = ((uint16_t)0x0084),
74     EEPROM_SAME_VALUE       = ((uint16_t)0x0085),
75     EEPROM_NO_VALID_PAGE    = ((uint16_t)0x00AB)
76     };
77
78 #define EEPROM_DEFAULT_DATA     0xFFFF
79
80     uint16_t EEPROM_init(void);
81     uint16_t EEPROM_format(void);
82     uint16_t EEPROM_erases(uint16_t *);
83     uint16_t EEPROM_read (uint16_t address, uint16_t *data);
84     uint16_t EEPROM_write(uint16_t address, uint16_t data);
85     uint16_t EEPROM_update(uint16_t address, uint16_t data);
86     uint16_t EEPROM_count(uint16_t *);
87     uint16_t EEPROM_maxcount(void);
88
89 #endif  /* __EEPROM_H */