lib_flash.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. ******************************************************************************
  3. * @file lib_flash.h
  4. * @author Application Team
  5. * @version V1.1.0
  6. * @date 2019-10-28
  7. * @brief FLASH library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #ifndef __LIB_FLASH_H
  14. #define __LIB_FLASH_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "target.h"
  19. #define FLASH_BLOCK_0 (0x00000001UL)
  20. #define FLASH_BLOCK_1 (0x00000002UL)
  21. #define FLASH_BLOCK_2 (0x00000004UL)
  22. #define FLASH_BLOCK_3 (0x00000008UL)
  23. #define FLASH_BLOCK_4 (0x00000010UL)
  24. #define FLASH_BLOCK_5 (0x00000020UL)
  25. #define FLASH_BLOCK_6 (0x00000040UL)
  26. #define FLASH_BLOCK_7 (0x00000080UL)
  27. #define FLASH_BLOCK_8 (0x00000100UL)
  28. #define FLASH_BLOCK_9 (0x00000200UL)
  29. #define FLASH_BLOCK_10 (0x00000400UL)
  30. #define FLASH_BLOCK_11 (0x00000800UL)
  31. #define FLASH_BLOCK_12 (0x00001000UL)
  32. #define FLASH_BLOCK_13 (0x00002000UL)
  33. #define FLASH_BLOCK_14 (0x00004000UL)
  34. #define FLASH_BLOCK_15 (0x00008000UL)
  35. #define FLASH_BLOCK_16 (0x00010000UL)
  36. #define FLASH_BLOCK_17 (0x00020000UL)
  37. #define FLASH_BLOCK_18 (0x00040000UL)
  38. #define FLASH_BLOCK_19 (0x00080000UL)
  39. #define FLASH_BLOCK_20 (0x00100000UL)
  40. #define FLASH_BLOCK_21 (0x00200000UL)
  41. #define FLASH_BLOCK_22 (0x00400000UL)
  42. #define FLASH_BLOCK_23 (0x00800000UL)
  43. #define FLASH_BLOCK_24 (0x01000000UL)
  44. #define FLASH_BLOCK_25 (0x02000000UL)
  45. #define FLASH_BLOCK_26 (0x04000000UL)
  46. #define FLASH_BLOCK_27 (0x08000000UL)
  47. #define FLASH_BLOCK_28 (0x10000000UL)
  48. #define FLASH_BLOCK_29 (0x20000000UL)
  49. #define FLASH_BLOCK_30 (0x40000000UL)
  50. #define FLASH_BLOCK_31 (0x80000000UL)
  51. #define FLASH_BLOCK_Msk (0xFFFFFFFFUL)
  52. #define FLASH_BLOCK_ALL FLASH_BLOCK_Msk
  53. #define IS_FLASH_RWBLOCK(__BLOCK__) ((((__BLOCK__) & FLASH_BLOCK_Msk) != 0UL) &&\
  54. (((__BLOCK__) & ~FLASH_BLOCK_Msk) == 0UL))
  55. #define IS_FLASH_BLOCK(__BLOCK__) (((__BLOCK__) == FLASH_BLOCK_0) ||\
  56. ((__BLOCK__) == FLASH_BLOCK_1) ||\
  57. ((__BLOCK__) == FLASH_BLOCK_2) ||\
  58. ((__BLOCK__) == FLASH_BLOCK_3) ||\
  59. ((__BLOCK__) == FLASH_BLOCK_4) ||\
  60. ((__BLOCK__) == FLASH_BLOCK_5) ||\
  61. ((__BLOCK__) == FLASH_BLOCK_6) ||\
  62. ((__BLOCK__) == FLASH_BLOCK_7) ||\
  63. ((__BLOCK__) == FLASH_BLOCK_8) ||\
  64. ((__BLOCK__) == FLASH_BLOCK_9) ||\
  65. ((__BLOCK__) == FLASH_BLOCK_10) ||\
  66. ((__BLOCK__) == FLASH_BLOCK_11) ||\
  67. ((__BLOCK__) == FLASH_BLOCK_12) ||\
  68. ((__BLOCK__) == FLASH_BLOCK_13) ||\
  69. ((__BLOCK__) == FLASH_BLOCK_14) ||\
  70. ((__BLOCK__) == FLASH_BLOCK_15) ||\
  71. ((__BLOCK__) == FLASH_BLOCK_16) ||\
  72. ((__BLOCK__) == FLASH_BLOCK_17) ||\
  73. ((__BLOCK__) == FLASH_BLOCK_18) ||\
  74. ((__BLOCK__) == FLASH_BLOCK_19) ||\
  75. ((__BLOCK__) == FLASH_BLOCK_20) ||\
  76. ((__BLOCK__) == FLASH_BLOCK_21) ||\
  77. ((__BLOCK__) == FLASH_BLOCK_22) ||\
  78. ((__BLOCK__) == FLASH_BLOCK_23) ||\
  79. ((__BLOCK__) == FLASH_BLOCK_24) ||\
  80. ((__BLOCK__) == FLASH_BLOCK_25) ||\
  81. ((__BLOCK__) == FLASH_BLOCK_26) ||\
  82. ((__BLOCK__) == FLASH_BLOCK_27) ||\
  83. ((__BLOCK__) == FLASH_BLOCK_28) ||\
  84. ((__BLOCK__) == FLASH_BLOCK_29) ||\
  85. ((__BLOCK__) == FLASH_BLOCK_30) ||\
  86. ((__BLOCK__) == FLASH_BLOCK_31))
  87. #define FLASH_READ (0)
  88. #define FLASH_WRITE (1)
  89. #define IS_FLASH_OPERATION(__OPERATION__) (((__OPERATION__) == FLASH_READ) ||\
  90. ((__OPERATION__) == FLASH_WRITE))
  91. /************** Bits definition for FLASH_CTRL register ******************/
  92. #define FLASH_CTRL_CSMODE_DISABLE (0x0U << FLASH_CTRL_CSMODE_Pos) /*!< 0x00000000 */
  93. #define FLASH_CTRL_CSMODE_ALWAYSON (0x1U << FLASH_CTRL_CSMODE_Pos) /*!< 0x00000001 */
  94. #define FLASH_CTRL_CSMODE_TIM2OV (0x2U << FLASH_CTRL_CSMODE_Pos) /*!< 0x00000002 */
  95. #define FLASH_CTRL_CSMODE_RTC (0x3U << FLASH_CTRL_CSMODE_Pos) /*!< 0x00000003 */
  96. //CSMode
  97. #define FLASH_CSMODE_DISABLE FLASH_CTRL_CSMODE_DISABLE
  98. #define FLASH_CSMODE_ALWAYSON FLASH_CTRL_CSMODE_ALWAYSON
  99. #define FLASH_CSMODE_TMR2OF FLASH_CTRL_CSMODE_TIM2OV
  100. #define FLASH_CSMODE_RTC FLASH_CTRL_CSMODE_RTC
  101. #define IS_FLASH_CSMODE(__CSMODE__) (((__CSMODE__) == FLASH_CSMODE_DISABLE) ||\
  102. ((__CSMODE__) == FLASH_CSMODE_ALWAYSON) ||\
  103. ((__CSMODE__) == FLASH_CSMODE_TMR2OF) ||\
  104. ((__CSMODE__) == FLASH_CSMODE_RTC))
  105. //INT
  106. #define FLASH_INT_CS FLASH_CTRL_CSINTEN
  107. #define IS_FLASH_INT(__INT__) ((__INT__) == FLASH_INT_CS)
  108. //WriteStatus
  109. #define FLASH_WSTA_BUSY 0
  110. #define FLASH_WRITE_FINISH 1
  111. #define FLASH_WSTA_FINISH FLASH_WRITE_FINISH
  112. #define IS_FLASH_ADDRESS(__ADDRESS__) ((__ADDRESS__) < 0x80000UL)
  113. #define IS_FLASH_ADRRW(__ADDRW__) (((__ADDRW__) < 0x80000UL) &&\
  114. (((__ADDRW__) & 0x3U) == 0U))
  115. #define IS_FLASH_ADRRHW(__ADDRHW__) (((__ADDRHW__) < 0x80000UL) &&\
  116. (((__ADDRHW__) & 0x1U) == 0U))
  117. #define IS_FLASH_CHECKSUMADDR(__ADDRESS1__,__ADDRESS2__) (((__ADDRESS1__) < 0x80000) && ((__ADDRESS2__) < 0x80000) && ((__ADDRESS1__) < (__ADDRESS2__)))
  118. /* Exported Functions ------------------------------------------------------- */
  119. void FLASH_Init(uint32_t CSMode);
  120. void FLASH_INTConfig(uint32_t IntMask, uint32_t NewState);
  121. void FLASH_CycleInit(void);
  122. void FLASH_SectorErase(uint32_t SectorAddr);
  123. void FLASH_ChipErase(void);
  124. void FLASH_ProgramWord(uint32_t Addr, uint32_t *WordBuffer, uint32_t Length);
  125. void FLASH_ProgramHWord(uint32_t Addr, uint16_t *HWordBuffer, uint32_t Length);
  126. void FLASH_ProgramByte(uint32_t Addr, uint8_t *ByteBuffer, uint32_t Length);
  127. void FLASH_SetReadProtection(uint32_t Block);
  128. void FLASH_WriteProtection(uint32_t Block, uint32_t NewState);
  129. void FLASH_ICEProtection(uint32_t NewState);
  130. uint8_t FLASH_GetProtectionStatus(uint32_t Block, uint32_t Operation);
  131. uint32_t FLASH_GetAllProtectionStatus(uint32_t Operation);
  132. void FLASH_SetCheckSumRange(uint32_t AddrStart, uint32_t AddrEnd);
  133. void FLASH_SetCheckSumCompValue(uint32_t Checksum);
  134. uint32_t FLASH_GetCheckSum(void);
  135. uint8_t FLASH_GetINTStatus(uint32_t IntMask);
  136. void FLASH_ClearINTStatus(uint32_t IntMask);
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif /* __LIB_FLASH_H */
  141. /*********************************** END OF FILE ******************************/