eeprom_stm32.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // This file must be modified if the MCU is not defined below.
  19. // This library also assumes that the pages are not used by the firmware.
  20. #ifndef __EEPROM_H
  21. #define __EEPROM_H
  22. #include "ch.h"
  23. #include "hal.h"
  24. #include "flash_stm32.h"
  25. // HACK ALERT. This definition may not match your processor
  26. // To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc
  27. #define MCU_STM32F303CC
  28. #ifndef EEPROM_PAGE_SIZE
  29. #if defined (MCU_STM32F103RB)
  30. #define EEPROM_PAGE_SIZE (uint16_t)0x400 /* Page size = 1KByte */
  31. #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE) || defined (MCU_STM32F103RD) || defined (MCU_STM32F303CC)
  32. #define EEPROM_PAGE_SIZE (uint16_t)0x800 /* Page size = 2KByte */
  33. #else
  34. #error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
  35. #endif
  36. #endif
  37. #ifndef EEPROM_START_ADDRESS
  38. #if defined (MCU_STM32F103RB)
  39. #define EEPROM_START_ADDRESS ((uint32_t)(0x8000000 + 128 * 1024 - 2 * EEPROM_PAGE_SIZE))
  40. #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE)
  41. #define EEPROM_START_ADDRESS ((uint32_t)(0x8000000 + 512 * 1024 - 2 * EEPROM_PAGE_SIZE))
  42. #elif defined (MCU_STM32F103RD)
  43. #define EEPROM_START_ADDRESS ((uint32_t)(0x8000000 + 384 * 1024 - 2 * EEPROM_PAGE_SIZE))
  44. #elif defined (MCU_STM32F303CC)
  45. #define EEPROM_START_ADDRESS ((uint32_t)(0x8000000 + 256 * 1024 - 2 * EEPROM_PAGE_SIZE))
  46. #else
  47. #error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
  48. #endif
  49. #endif
  50. /* Pages 0 and 1 base and end addresses */
  51. #define EEPROM_PAGE0_BASE ((uint32_t)(EEPROM_START_ADDRESS + 0x000))
  52. #define EEPROM_PAGE1_BASE ((uint32_t)(EEPROM_START_ADDRESS + EEPROM_PAGE_SIZE))
  53. /* Page status definitions */
  54. #define EEPROM_ERASED ((uint16_t)0xFFFF) /* PAGE is empty */
  55. #define EEPROM_RECEIVE_DATA ((uint16_t)0xEEEE) /* PAGE is marked to receive data */
  56. #define EEPROM_VALID_PAGE ((uint16_t)0x0000) /* PAGE containing valid data */
  57. /* Page full define */
  58. enum uint16_t
  59. {
  60. EEPROM_OK = ((uint16_t)0x0000),
  61. EEPROM_OUT_SIZE = ((uint16_t)0x0081),
  62. EEPROM_BAD_ADDRESS = ((uint16_t)0x0082),
  63. EEPROM_BAD_FLASH = ((uint16_t)0x0083),
  64. EEPROM_NOT_INIT = ((uint16_t)0x0084),
  65. EEPROM_SAME_VALUE = ((uint16_t)0x0085),
  66. EEPROM_NO_VALID_PAGE = ((uint16_t)0x00AB)
  67. };
  68. #define EEPROM_DEFAULT_DATA 0xFFFF
  69. uint16_t EEPROM_init(void);
  70. uint16_t EEPROM_format(void);
  71. uint16_t EEPROM_erases(uint16_t *);
  72. uint16_t EEPROM_read (uint16_t address, uint16_t *data);
  73. uint16_t EEPROM_write(uint16_t address, uint16_t data);
  74. uint16_t EEPROM_update(uint16_t address, uint16_t data);
  75. uint16_t EEPROM_count(uint16_t *);
  76. uint16_t EEPROM_maxcount(void);
  77. #endif /* __EEPROM_H */