stm8s_flash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_flash.h
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief This file contains all functions prototype and macros for the FLASH peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Define to prevent recursive inclusion -------------------------------------*/
  28. #ifndef __STM8S_FLASH_H
  29. #define __STM8S_FLASH_H
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm8s.h"
  32. /* Exported constants --------------------------------------------------------*/
  33. /** @addtogroup FLASH_Exported_Constants
  34. * @{
  35. */
  36. #define FLASH_PROG_START_PHYSICAL_ADDRESS ((uint32_t)0x008000) /*!< Program memory: start address */
  37. #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
  38. #define FLASH_PROG_END_PHYSICAL_ADDRESS ((uint32_t)0x027FFF) /*!< Program memory: end address */
  39. #define FLASH_PROG_BLOCKS_NUMBER ((uint16_t)1024) /*!< Program memory: total number of blocks */
  40. #define FLASH_DATA_START_PHYSICAL_ADDRESS ((uint32_t)0x004000) /*!< Data EEPROM memory: start address */
  41. #define FLASH_DATA_END_PHYSICAL_ADDRESS ((uint32_t)0x0047FF) /*!< Data EEPROM memory: end address */
  42. #define FLASH_DATA_BLOCKS_NUMBER ((uint16_t)16) /*!< Data EEPROM memory: total number of blocks */
  43. #define FLASH_BLOCK_SIZE ((uint8_t)128) /*!< Number of bytes in a block (common for Program and Data memories) */
  44. #endif /* STM8S208, STM8S207, STM8S007, STM8AF52Ax, STM8AF62Ax */
  45. #if defined(STM8S105) || defined(STM8S005) || defined(STM8AF626x)
  46. #define FLASH_PROG_END_PHYSICAL_ADDRESS ((uint32_t)0xFFFF) /*!< Program memory: end address */
  47. #define FLASH_PROG_BLOCKS_NUMBER ((uint16_t)256) /*!< Program memory: total number of blocks */
  48. #define FLASH_DATA_START_PHYSICAL_ADDRESS ((uint32_t)0x004000) /*!< Data EEPROM memory: start address */
  49. #define FLASH_DATA_END_PHYSICAL_ADDRESS ((uint32_t)0x0043FF) /*!< Data EEPROM memory: end address */
  50. #define FLASH_DATA_BLOCKS_NUMBER ((uint16_t)8) /*!< Data EEPROM memory: total number of blocks */
  51. #define FLASH_BLOCK_SIZE ((uint8_t)128) /*!< Number of bytes in a block (common for Program and Data memories) */
  52. #endif /* STM8S105 or STM8AF626x */
  53. #if defined(STM8S103) || defined(STM8S003) || defined(STM8S001) || defined(STM8S903) || defined(STM8AF622x)
  54. #define FLASH_PROG_END_PHYSICAL_ADDRESS ((uint32_t)0x9FFF) /*!< Program memory: end address */
  55. #define FLASH_PROG_BLOCKS_NUMBER ((uint16_t)128) /*!< Program memory: total number of blocks */
  56. #define FLASH_DATA_START_PHYSICAL_ADDRESS ((uint32_t)0x004000) /*!< Data EEPROM memory: start address */
  57. #define FLASH_DATA_END_PHYSICAL_ADDRESS ((uint32_t)0x00427F) /*!< Data EEPROM memory: end address */
  58. #define FLASH_DATA_BLOCKS_NUMBER ((uint16_t)10) /*!< Data EEPROM memory: total number of blocks */
  59. #define FLASH_BLOCK_SIZE ((uint8_t)64) /*!< Number of bytes in a block (common for Program and Data memories) */
  60. #endif /* STM8S103 or STM8S003 or STM8S001 or STM8S903 or STM8AF622x*/
  61. #define FLASH_RASS_KEY1 ((uint8_t)0x56) /*!< First RASS key */
  62. #define FLASH_RASS_KEY2 ((uint8_t)0xAE) /*!< Second RASS key */
  63. #define OPTION_BYTE_START_PHYSICAL_ADDRESS ((uint16_t)0x4800)
  64. #define OPTION_BYTE_END_PHYSICAL_ADDRESS ((uint16_t)0x487F)
  65. #define FLASH_OPTIONBYTE_ERROR ((uint16_t)0x5555) /*!< Error code option byte
  66. (if value read is not equal to complement value read) */
  67. /**
  68. * @}
  69. */
  70. /* Exported types ------------------------------------------------------------*/
  71. /** @addtogroup FLASH_Exported_Types
  72. * @{
  73. */
  74. /**
  75. * @brief FLASH Memory types
  76. */
  77. typedef enum {
  78. FLASH_MEMTYPE_PROG = (uint8_t)0xFD, /*!< Program memory */
  79. FLASH_MEMTYPE_DATA = (uint8_t)0xF7 /*!< Data EEPROM memory */
  80. } FLASH_MemType_TypeDef;
  81. /**
  82. * @brief FLASH programming modes
  83. */
  84. typedef enum {
  85. FLASH_PROGRAMMODE_STANDARD = (uint8_t)0x00, /*!< Standard programming mode */
  86. FLASH_PROGRAMMODE_FAST = (uint8_t)0x10 /*!< Fast programming mode */
  87. } FLASH_ProgramMode_TypeDef;
  88. /**
  89. * @brief FLASH fixed programming time
  90. */
  91. typedef enum {
  92. FLASH_PROGRAMTIME_STANDARD = (uint8_t)0x00, /*!< Standard programming time fixed at 1/2 tprog */
  93. FLASH_PROGRAMTIME_TPROG = (uint8_t)0x01 /*!< Programming time fixed at tprog */
  94. } FLASH_ProgramTime_TypeDef;
  95. /**
  96. * @brief FLASH Low Power mode select
  97. */
  98. typedef enum {
  99. FLASH_LPMODE_POWERDOWN = (uint8_t)0x04, /*!< HALT: Power-Down / ACTIVE-HALT: Power-Down */
  100. FLASH_LPMODE_STANDBY = (uint8_t)0x08, /*!< HALT: Standby / ACTIVE-HALT: Standby */
  101. FLASH_LPMODE_POWERDOWN_STANDBY = (uint8_t)0x00, /*!< HALT: Power-Down / ACTIVE-HALT: Standby */
  102. FLASH_LPMODE_STANDBY_POWERDOWN = (uint8_t)0x0C /*!< HALT: Standby / ACTIVE-HALT: Power-Down */
  103. }
  104. FLASH_LPMode_TypeDef;
  105. /**
  106. * @brief FLASH status of the last operation
  107. */
  108. typedef enum {
  109. #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
  110. defined(STM8S005) || defined (STM8AF52Ax) || defined (STM8AF62Ax) || defined(STM8AF626x)
  111. FLASH_STATUS_END_HIGH_VOLTAGE = (uint8_t)0x40, /*!< End of high voltage */
  112. #endif /* STM8S208, STM8S207, STM8S105, STM8AF62Ax, STM8AF52Ax, STM8AF626x */
  113. FLASH_STATUS_SUCCESSFUL_OPERATION = (uint8_t)0x04, /*!< End of operation flag */
  114. FLASH_STATUS_TIMEOUT = (uint8_t)0x02, /*!< Time out error */
  115. FLASH_STATUS_WRITE_PROTECTION_ERROR = (uint8_t)0x01 /*!< Write attempted to protected page */
  116. } FLASH_Status_TypeDef;
  117. /**
  118. * @brief FLASH flags definition
  119. * - Warning : FLAG value = mapping position register
  120. */
  121. typedef enum {
  122. #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
  123. defined(STM8S005) || defined (STM8AF52Ax) || defined (STM8AF62Ax) || defined(STM8AF626x)
  124. FLASH_FLAG_HVOFF = (uint8_t)0x40, /*!< End of high voltage flag */
  125. #endif /* STM8S208, STM8S207, STM8S105, STM8AF62Ax, STM8AF52Ax, STM8AF626x */
  126. FLASH_FLAG_DUL = (uint8_t)0x08, /*!< Data EEPROM unlocked flag */
  127. FLASH_FLAG_EOP = (uint8_t)0x04, /*!< End of programming (write or erase operation) flag */
  128. FLASH_FLAG_PUL = (uint8_t)0x02, /*!< Flash Program memory unlocked flag */
  129. FLASH_FLAG_WR_PG_DIS = (uint8_t)0x01 /*!< Write attempted to protected page flag */
  130. } FLASH_Flag_TypeDef;
  131. /**
  132. * @}
  133. */
  134. /* Private macros ------------------------------------------------------------*/
  135. /**
  136. * @brief Macros used by the assert function in order to check the different functions parameters.
  137. * @addtogroup FLASH_Private_Macros
  138. * @{
  139. */
  140. /**
  141. * @brief Macro used by the assert function in order to check the different sensitivity values for the flash program Address
  142. */
  143. #define IS_FLASH_PROG_ADDRESS_OK(ADDRESS) (((ADDRESS) >= FLASH_PROG_START_PHYSICAL_ADDRESS) && \
  144. ((ADDRESS) <= FLASH_PROG_END_PHYSICAL_ADDRESS))
  145. /**
  146. * @brief Macro used by the assert function in order to check the different sensitivity values for the data eeprom Address
  147. */
  148. #define IS_FLASH_DATA_ADDRESS_OK(ADDRESS) (((ADDRESS) >= FLASH_DATA_START_PHYSICAL_ADDRESS) && \
  149. ((ADDRESS) <= FLASH_DATA_END_PHYSICAL_ADDRESS))
  150. /**
  151. * @brief Macro used by the assert function in order to check the different sensitivity values for the data eeprom and flash program Address
  152. */
  153. #define IS_FLASH_ADDRESS_OK(ADDRESS)((((ADDRESS) >= FLASH_PROG_START_PHYSICAL_ADDRESS) && ((ADDRESS) <= FLASH_PROG_END_PHYSICAL_ADDRESS)) || \
  154. (((ADDRESS) >= FLASH_DATA_START_PHYSICAL_ADDRESS) && ((ADDRESS) <= FLASH_DATA_END_PHYSICAL_ADDRESS)))
  155. /**
  156. * @brief Macro used by the assert function in order to check the different sensitivity values for the flash program Block number
  157. */
  158. #define IS_FLASH_PROG_BLOCK_NUMBER_OK(BLOCKNUM) ((BLOCKNUM) < FLASH_PROG_BLOCKS_NUMBER)
  159. /**
  160. * @brief Macro used by the assert function in order to check the different sensitivity values for the data eeprom Block number
  161. */
  162. #define IS_FLASH_DATA_BLOCK_NUMBER_OK(BLOCKNUM) ((BLOCKNUM) < FLASH_DATA_BLOCKS_NUMBER)
  163. /**
  164. * @brief Macro used by the assert function in order to check the different sensitivity values for the flash memory type
  165. */
  166. #define IS_MEMORY_TYPE_OK(MEMTYPE) (((MEMTYPE) == FLASH_MEMTYPE_PROG) || \
  167. ((MEMTYPE) == FLASH_MEMTYPE_DATA))
  168. /**
  169. * @brief Macro used by the assert function in order to check the different sensitivity values for the flash program mode
  170. */
  171. #define IS_FLASH_PROGRAM_MODE_OK(MODE) (((MODE) == FLASH_PROGRAMMODE_STANDARD) || \
  172. ((MODE) == FLASH_PROGRAMMODE_FAST))
  173. /**
  174. * @brief Macro used by the assert function in order to check the program time mode
  175. */
  176. #define IS_FLASH_PROGRAM_TIME_OK(TIME) (((TIME) == FLASH_PROGRAMTIME_STANDARD) || \
  177. ((TIME) == FLASH_PROGRAMTIME_TPROG))
  178. /**
  179. * @brief Macro used by the assert function in order to check the different
  180. * sensitivity values for the low power mode
  181. */
  182. #define IS_FLASH_LOW_POWER_MODE_OK(LPMODE) (((LPMODE) == FLASH_LPMODE_POWERDOWN) || \
  183. ((LPMODE) == FLASH_LPMODE_STANDBY) || \
  184. ((LPMODE) == FLASH_LPMODE_POWERDOWN_STANDBY) || \
  185. ((LPMODE) == FLASH_LPMODE_STANDBY_POWERDOWN))
  186. /**
  187. * @brief Macro used by the assert function in order to check the different
  188. * sensitivity values for the option bytes Address
  189. */
  190. #define IS_OPTION_BYTE_ADDRESS_OK(ADDRESS) (((ADDRESS) >= OPTION_BYTE_START_PHYSICAL_ADDRESS) && \
  191. ((ADDRESS) <= OPTION_BYTE_END_PHYSICAL_ADDRESS))
  192. /**
  193. * @brief Macro used by the assert function in order to check the different flags values
  194. */
  195. #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
  196. defined(STM8S005) || defined (STM8AF52Ax) || defined (STM8AF62Ax) || defined(STM8AF626x)
  197. #define IS_FLASH_FLAGS_OK(FLAG) (((FLAG) == FLASH_FLAG_HVOFF) || \
  198. ((FLAG) == FLASH_FLAG_DUL) || \
  199. ((FLAG) == FLASH_FLAG_EOP) || \
  200. ((FLAG) == FLASH_FLAG_PUL) || \
  201. ((FLAG) == FLASH_FLAG_WR_PG_DIS))
  202. #else /* STM8S103, STM8S001, STM8S903, STM8AF622x */
  203. #define IS_FLASH_FLAGS_OK(FLAG) (((FLAG) == FLASH_FLAG_DUL) || \
  204. ((FLAG) == FLASH_FLAG_EOP) || \
  205. ((FLAG) == FLASH_FLAG_PUL) || \
  206. ((FLAG) == FLASH_FLAG_WR_PG_DIS))
  207. #endif /* STM8S208, STM8S207, STM8S105, STM8AF62Ax, STM8AF52Ax, STM8AF626x */
  208. /**
  209. * @}
  210. */
  211. /* Exported functions ------------------------------------------------------- */
  212. /** @addtogroup FLASH_Exported_Functions
  213. * @{
  214. */
  215. void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType);
  216. void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType);
  217. void FLASH_DeInit(void);
  218. void FLASH_ITConfig(FunctionalState NewState);
  219. void FLASH_EraseByte(uint32_t Address);
  220. void FLASH_ProgramByte(uint32_t Address, uint8_t Data);
  221. uint8_t FLASH_ReadByte(uint32_t Address);
  222. void FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  223. uint16_t FLASH_ReadOptionByte(uint16_t Address);
  224. void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data);
  225. void FLASH_EraseOptionByte(uint16_t Address);
  226. void FLASH_SetLowPowerMode(FLASH_LPMode_TypeDef FLASH_LPMode);
  227. void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime);
  228. FLASH_LPMode_TypeDef FLASH_GetLowPowerMode(void);
  229. FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void);
  230. uint32_t FLASH_GetBootSize(void);
  231. FlagStatus FLASH_GetFlagStatus(FLASH_Flag_TypeDef FLASH_FLAG);
  232. /**
  233. @code
  234. All the functions declared below must be executed from RAM exclusively, except
  235. for the FLASH_WaitForLastOperation function which can be executed from Flash.
  236. Steps of the execution from RAM differs from one toolchain to another.
  237. for more details refer to stm8s_flash.c file.
  238. To enable execution from RAM you can either uncomment the following define
  239. in the stm8s.h file or define it in your toolchain compiler preprocessor
  240. - #define RAM_EXECUTION (1)
  241. @endcode
  242. */
  243. IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType));
  244. IN_RAM(void FLASH_ProgramBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType,
  245. FLASH_ProgramMode_TypeDef FLASH_ProgMode, uint8_t *Buffer));
  246. IN_RAM(FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef FLASH_MemType));
  247. /**
  248. * @}
  249. */
  250. #endif /*__STM8S_FLASH_H */
  251. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/