ch32v30x_flash.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_flash.h
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2024/05/24
  6. * Description : This file contains all the functions prototypes for the FLASH
  7. * firmware library.
  8. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #ifndef __CH32V30x_FLASH_H
  14. #define __CH32V30x_FLASH_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "ch32v30x.h"
  19. /* FLASH Status */
  20. typedef enum
  21. {
  22. FLASH_BUSY = 1,
  23. FLASH_ERROR_PG,
  24. FLASH_ERROR_WRP,
  25. FLASH_COMPLETE,
  26. FLASH_TIMEOUT,
  27. FLASH_OP_RANGE_ERROR = 0xFD,
  28. FLASH_ALIGN_ERROR = 0xFE,
  29. FLASH_ADR_RANGE_ERROR = 0xFF,
  30. }FLASH_Status;
  31. /* Write Protect */
  32. #define FLASH_WRProt_Sectors0 ((uint32_t)0x00000001) /* Write protection of setor 0 ,4K bytes/sector */
  33. #define FLASH_WRProt_Sectors1 ((uint32_t)0x00000002) /* Write protection of setor 1 ,4K bytes/sector */
  34. #define FLASH_WRProt_Sectors2 ((uint32_t)0x00000004) /* Write protection of setor 2 ,4K bytes/sector */
  35. #define FLASH_WRProt_Sectors3 ((uint32_t)0x00000008) /* Write protection of setor 3 ,4K bytes/sector */
  36. #define FLASH_WRProt_Sectors4 ((uint32_t)0x00000010) /* Write protection of setor 4 ,4K bytes/sector */
  37. #define FLASH_WRProt_Sectors5 ((uint32_t)0x00000020) /* Write protection of setor 5 ,4K bytes/sector */
  38. #define FLASH_WRProt_Sectors6 ((uint32_t)0x00000040) /* Write protection of setor 6 ,4K bytes/sector */
  39. #define FLASH_WRProt_Sectors7 ((uint32_t)0x00000080) /* Write protection of setor 7 ,4K bytes/sector */
  40. #define FLASH_WRProt_Sectors8 ((uint32_t)0x00000100) /* Write protection of setor 8 ,4K bytes/sector */
  41. #define FLASH_WRProt_Sectors9 ((uint32_t)0x00000200) /* Write protection of setor 9 ,4K bytes/sector */
  42. #define FLASH_WRProt_Sectors10 ((uint32_t)0x00000400) /* Write protection of setor 10 ,4K bytes/sector */
  43. #define FLASH_WRProt_Sectors11 ((uint32_t)0x00000800) /* Write protection of setor 11 ,4K bytes/sector */
  44. #define FLASH_WRProt_Sectors12 ((uint32_t)0x00001000) /* Write protection of setor 12 ,4K bytes/sector */
  45. #define FLASH_WRProt_Sectors13 ((uint32_t)0x00002000) /* Write protection of setor 13 ,4K bytes/sector */
  46. #define FLASH_WRProt_Sectors14 ((uint32_t)0x00004000) /* Write protection of setor 14 ,4K bytes/sector */
  47. #define FLASH_WRProt_Sectors15 ((uint32_t)0x00008000) /* Write protection of setor 15 ,4K bytes/sector */
  48. #define FLASH_WRProt_Sectors16 ((uint32_t)0x00010000) /* Write protection of setor 16 ,4K bytes/sector */
  49. #define FLASH_WRProt_Sectors17 ((uint32_t)0x00020000) /* Write protection of setor 17 ,4K bytes/sector */
  50. #define FLASH_WRProt_Sectors18 ((uint32_t)0x00040000) /* Write protection of setor 18 ,4K bytes/sector */
  51. #define FLASH_WRProt_Sectors19 ((uint32_t)0x00080000) /* Write protection of setor 19 ,4K bytes/sector */
  52. #define FLASH_WRProt_Sectors20 ((uint32_t)0x00100000) /* Write protection of setor 20 ,4K bytes/sector */
  53. #define FLASH_WRProt_Sectors21 ((uint32_t)0x00200000) /* Write protection of setor 21 ,4K bytes/sector */
  54. #define FLASH_WRProt_Sectors22 ((uint32_t)0x00400000) /* Write protection of setor 22 ,4K bytes/sector */
  55. #define FLASH_WRProt_Sectors23 ((uint32_t)0x00800000) /* Write protection of setor 23 ,4K bytes/sector */
  56. #define FLASH_WRProt_Sectors24 ((uint32_t)0x01000000) /* Write protection of setor 24 ,4K bytes/sector */
  57. #define FLASH_WRProt_Sectors25 ((uint32_t)0x02000000) /* Write protection of setor 25 ,4K bytes/sector */
  58. #define FLASH_WRProt_Sectors26 ((uint32_t)0x04000000) /* Write protection of setor 26 ,4K bytes/sector */
  59. #define FLASH_WRProt_Sectors27 ((uint32_t)0x08000000) /* Write protection of setor 27 ,4K bytes/sector */
  60. #define FLASH_WRProt_Sectors28 ((uint32_t)0x10000000) /* Write protection of setor 28 ,4K bytes/sector */
  61. #define FLASH_WRProt_Sectors29 ((uint32_t)0x20000000) /* Write protection of setor 29 ,4K bytes/sector */
  62. #define FLASH_WRProt_Sectors30 ((uint32_t)0x40000000) /* Write protection of setor 30 ,4K bytes/sector */
  63. #define FLASH_WRProt_Sectors31to127 ((uint32_t)0x80000000) /* Write protection of page 31 to 127 */
  64. #define FLASH_WRProt_AllSectors ((uint32_t)0xFFFFFFFF) /* Write protection of all Sectors */
  65. /* Option_Bytes_IWatchdog */
  66. #define OB_IWDG_SW ((uint16_t)0x0001) /* Software IWDG selected */
  67. #define OB_IWDG_HW ((uint16_t)0x0000) /* Hardware IWDG selected */
  68. /* Option_Bytes_nRST_STOP */
  69. #define OB_STOP_NoRST ((uint16_t)0x0002) /* No reset generated when entering in STOP */
  70. #define OB_STOP_RST ((uint16_t)0x0000) /* Reset generated when entering in STOP */
  71. /* Option_Bytes_nRST_STDBY */
  72. #define OB_STDBY_NoRST ((uint16_t)0x0004) /* No reset generated when entering in STANDBY */
  73. #define OB_STDBY_RST ((uint16_t)0x0000) /* Reset generated when entering in STANDBY */
  74. /* FLASH_Interrupts */
  75. #define FLASH_IT_ERROR ((uint32_t)0x00000400) /* FPEC error interrupt source */
  76. #define FLASH_IT_EOP ((uint32_t)0x00001000) /* End of FLASH Operation Interrupt source */
  77. #define FLASH_IT_BANK1_ERROR FLASH_IT_ERROR /* FPEC BANK1 error interrupt source */
  78. #define FLASH_IT_BANK1_EOP FLASH_IT_EOP /* End of FLASH BANK1 Operation Interrupt source */
  79. /* FLASH_Flags */
  80. #define FLASH_FLAG_BSY ((uint32_t)0x00000001) /* FLASH Busy flag */
  81. #define FLASH_FLAG_EOP ((uint32_t)0x00000020) /* FLASH End of Operation flag */
  82. #define FLASH_FLAG_WRPRTERR ((uint32_t)0x00000010) /* FLASH Write protected error flag */
  83. #define FLASH_FLAG_OPTERR ((uint32_t)0x80000001) /* FLASH Option Byte error flag */
  84. #define FLASH_FLAG_BANK1_BSY FLASH_FLAG_BSY /* FLASH BANK1 Busy flag*/
  85. #define FLASH_FLAG_BANK1_EOP FLASH_FLAG_EOP /* FLASH BANK1 End of Operation flag */
  86. #define FLASH_FLAG_BANK1_WRPRTERR FLASH_FLAG_WRPRTERR /* FLASH BANK1 Write protected error flag */
  87. /* FLASH_Access_CLK */
  88. #define FLASH_Access_SYSTEM_HALF ((uint32_t)0x00000000) /* FLASH Access Clock = SYSTEM/2 */
  89. #define FLASH_Access_SYSTEM ((uint32_t)0x02000000) /* FLASH Access Clock = SYSTEM */
  90. /*Functions used for all devices*/
  91. void FLASH_Unlock(void);
  92. void FLASH_Lock(void);
  93. FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
  94. FLASH_Status FLASH_EraseAllPages(void);
  95. FLASH_Status FLASH_EraseOptionBytes(void);
  96. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  97. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  98. FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data);
  99. FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Sectors);
  100. FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState);
  101. FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY);
  102. uint32_t FLASH_GetUserOptionByte(void);
  103. uint32_t FLASH_GetWriteProtectionOptionByte(void);
  104. FlagStatus FLASH_GetReadOutProtectionStatus(void);
  105. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);
  106. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG);
  107. void FLASH_ClearFlag(uint32_t FLASH_FLAG);
  108. FLASH_Status FLASH_GetStatus(void);
  109. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
  110. void FLASH_Unlock_Fast(void);
  111. void FLASH_Lock_Fast(void);
  112. void FLASH_ErasePage_Fast(uint32_t Page_Address);
  113. void FLASH_EraseBlock_32K_Fast(uint32_t Block_Address);
  114. void FLASH_ProgramPage_Fast(uint32_t Page_Address, uint32_t* pbuf);
  115. void FLASH_Access_Clock_Cfg(uint32_t FLASH_Access_CLK);
  116. void FLASH_Enhance_Mode(FunctionalState NewState);
  117. /* New function used for all devices */
  118. void FLASH_UnlockBank1(void);
  119. void FLASH_LockBank1(void);
  120. FLASH_Status FLASH_EraseAllBank1Pages(void);
  121. FLASH_Status FLASH_GetBank1Status(void);
  122. FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout);
  123. FLASH_Status FLASH_ROM_ERASE(uint32_t StartAddr, uint32_t Length);
  124. FLASH_Status FLASH_ROM_WRITE(uint32_t StartAddr, uint32_t *pbuf, uint32_t Length);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif