eeprom_stm32.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #if defined(EEPROM_EMU_STM32F303xC)
  28. #define MCU_STM32F303CC
  29. #elif defined(EEPROM_EMU_STM32F103xB)
  30. #define MCU_STM32F103RB
  31. #else
  32. #error "not implemented."
  33. #endif
  34. #ifndef EEPROM_PAGE_SIZE
  35. #if defined (MCU_STM32F103RB)
  36. #define EEPROM_PAGE_SIZE (uint16_t)0x400 /* Page size = 1KByte */
  37. #elif defined (MCU_STM32F103ZE) || defined (MCU_STM32F103RE) || defined (MCU_STM32F103RD) || defined (MCU_STM32F303CC)
  38. #define EEPROM_PAGE_SIZE (uint16_t)0x800 /* Page size = 2KByte */
  39. #else
  40. #error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
  41. #endif
  42. #endif
  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. /* Pages 0 and 1 base and end addresses */
  57. #define EEPROM_PAGE0_BASE ((uint32_t)(EEPROM_START_ADDRESS + 0x000))
  58. #define EEPROM_PAGE1_BASE ((uint32_t)(EEPROM_START_ADDRESS + EEPROM_PAGE_SIZE))
  59. /* Page status definitions */
  60. #define EEPROM_ERASED ((uint16_t)0xFFFF) /* PAGE is empty */
  61. #define EEPROM_RECEIVE_DATA ((uint16_t)0xEEEE) /* PAGE is marked to receive data */
  62. #define EEPROM_VALID_PAGE ((uint16_t)0x0000) /* PAGE containing valid data */
  63. /* Page full define */
  64. enum uint16_t
  65. {
  66. EEPROM_OK = ((uint16_t)0x0000),
  67. EEPROM_OUT_SIZE = ((uint16_t)0x0081),
  68. EEPROM_BAD_ADDRESS = ((uint16_t)0x0082),
  69. EEPROM_BAD_FLASH = ((uint16_t)0x0083),
  70. EEPROM_NOT_INIT = ((uint16_t)0x0084),
  71. EEPROM_SAME_VALUE = ((uint16_t)0x0085),
  72. EEPROM_NO_VALID_PAGE = ((uint16_t)0x00AB)
  73. };
  74. #define EEPROM_DEFAULT_DATA 0xFFFF
  75. uint16_t EEPROM_init(void);
  76. uint16_t EEPROM_format(void);
  77. uint16_t EEPROM_erases(uint16_t *);
  78. uint16_t EEPROM_read (uint16_t address, uint16_t *data);
  79. uint16_t EEPROM_write(uint16_t address, uint16_t data);
  80. uint16_t EEPROM_update(uint16_t address, uint16_t data);
  81. uint16_t EEPROM_count(uint16_t *);
  82. uint16_t EEPROM_maxcount(void);
  83. #endif /* __EEPROM_H */