eeprom_stm32.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 http://engsta.com/stm32-flash-memory-eeprom-emulator/ by
  14. * Artur F.
  15. *
  16. * Modifications for QMK and STM32F303 by Yiancar
  17. *
  18. * This library assumes 8-bit data locations. To add a new MCU, please provide the flash
  19. * page size and the total flash size in Kb. The number of available pages must be a multiple
  20. * of 2. Only half of the pages account for the total EEPROM size.
  21. * This library also assumes that the pages are not used by the firmware.
  22. */
  23. #ifndef __EEPROM_H
  24. #define __EEPROM_H
  25. #include <ch.h>
  26. #include <hal.h>
  27. #include "flash_stm32.h"
  28. // HACK ALERT. This definition may not match your processor
  29. // To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc
  30. #if defined(EEPROM_EMU_STM32F303xC)
  31. # define MCU_STM32F303CC
  32. #elif defined(EEPROM_EMU_STM32F103xB)
  33. # define MCU_STM32F103RB
  34. #elif defined(EEPROM_EMU_STM32F072xB)
  35. # define MCU_STM32F072CB
  36. #elif defined(EEPROM_EMU_STM32F042x6)
  37. # define MCU_STM32F042K6
  38. #else
  39. # error "not implemented."
  40. #endif
  41. #ifndef EEPROM_PAGE_SIZE
  42. # if defined(MCU_STM32F103RB) || defined(MCU_STM32F042K6)
  43. # define FEE_PAGE_SIZE (uint16_t)0x400 // Page size = 1KByte
  44. # define FEE_DENSITY_PAGES 2 // How many pages are used
  45. # elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB)
  46. # define FEE_PAGE_SIZE (uint16_t)0x800 // Page size = 2KByte
  47. # define FEE_DENSITY_PAGES 4 // How many pages are used
  48. # else
  49. # error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
  50. # endif
  51. #endif
  52. #ifndef EEPROM_START_ADDRESS
  53. # if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB)
  54. # define FEE_MCU_FLASH_SIZE 128 // Size in Kb
  55. # elif defined(MCU_STM32F042K6)
  56. # define FEE_MCU_FLASH_SIZE 32 // Size in Kb
  57. # elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE)
  58. # define FEE_MCU_FLASH_SIZE 512 // Size in Kb
  59. # elif defined(MCU_STM32F103RD)
  60. # define FEE_MCU_FLASH_SIZE 384 // Size in Kb
  61. # elif defined(MCU_STM32F303CC)
  62. # define FEE_MCU_FLASH_SIZE 256 // Size in Kb
  63. # else
  64. # error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)."
  65. # endif
  66. #endif
  67. // DONT CHANGE
  68. // Choose location for the first EEPROM Page address on the top of flash
  69. #define FEE_PAGE_BASE_ADDRESS ((uint32_t)(0x8000000 + FEE_MCU_FLASH_SIZE * 1024 - FEE_DENSITY_PAGES * FEE_PAGE_SIZE))
  70. #define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 2) * FEE_DENSITY_PAGES - 1)
  71. #define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES))
  72. #define FEE_EMPTY_WORD ((uint16_t)0xFFFF)
  73. #define FEE_ADDR_OFFSET(Address) (Address * 2) // 1Byte per Word will be saved to preserve Flash
  74. // Use this function to initialize the functionality
  75. uint16_t EEPROM_Init(void);
  76. void EEPROM_Erase(void);
  77. uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte);
  78. uint8_t EEPROM_ReadDataByte(uint16_t Address);
  79. #endif /* __EEPROM_H */