fmc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**************************************************************************//**
  2. * @file FMC.h
  3. * @version V3.0
  4. * $Revision: 13 $
  5. * $Date: 14/01/28 10:49a $
  6. * @brief M051 Series Flash Memory Controller Driver Header File
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #ifndef __FMC_H__
  13. #define __FMC_H__
  14. #include "M051Series.h"
  15. #ifdef __cplusplus
  16. extern "C"
  17. {
  18. #endif
  19. /** @addtogroup M051_Device_Driver M051 Device Driver
  20. @{
  21. */
  22. /** @addtogroup M051_FMC_Driver FMC Driver
  23. @{
  24. */
  25. /** @addtogroup M051_FMC_EXPORTED_CONSTANTS FMC Exported Constants
  26. @{
  27. */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. /* Define Base Address */
  30. /*---------------------------------------------------------------------------------------------------------*/
  31. #define FMC_APROM_BASE 0x00000000UL /*!< APROM Base Address */
  32. #define FMC_LDROM_BASE 0x00100000UL /*!< LDROM Base Address */
  33. #define FMC_CONFIG_BASE 0x00300000UL /*!< CONFIG Base Address */
  34. #define FMC_FLASH_PAGE_SIZE 0x200 /*!< Flash Page Size (512 Bytes) */
  35. #define FMC_LDROM_SIZE 0x1000 /*!< LDROM Size (4 KBytes) */
  36. /*---------------------------------------------------------------------------------------------------------*/
  37. /* ISPCON constant definitions */
  38. /*---------------------------------------------------------------------------------------------------------*/
  39. #define FMC_ISPCON_BS_LDROM 0x2 /*!< ISPCON setting to select to boot from LDROM */
  40. #define FMC_ISPCON_BS_APROM 0x0 /*!< ISPCON setting to select to boot from APROM */
  41. /*---------------------------------------------------------------------------------------------------------*/
  42. /* ISPCMD constant definitions */
  43. /*---------------------------------------------------------------------------------------------------------*/
  44. #define FMC_ISPCMD_READ 0x00 /*!< ISP Command: Read Flash */
  45. #define FMC_ISPCMD_PROGRAM 0x21 /*!< ISP Command: Program Flash */
  46. #define FMC_ISPCMD_PAGE_ERASE 0x22 /*!< ISP Command: Page Erase Flash */
  47. #define FMC_ISPCMD_VECMAP 0x2e /*!< ISP Command: Set VECMAP */
  48. #define FMC_ISPCMD_READ_UID 0x04 /*!< ISP Command: Read Unique ID */
  49. #define FMC_ISPCMD_READ_CID 0x0B /*!< ISP Command: Read Company ID */
  50. #define FMC_ISPCMD_READ_DID 0x0C /*!< ISP Command: Read Device ID */
  51. /*@}*/ /* end of group M051_FMC_EXPORTED_CONSTANTS */
  52. /** @addtogroup M051_FMC_EXPORTED_FUNCTIONS FMC Exported Functions
  53. @{
  54. */
  55. /*---------------------------------------------------------------------------------------------------------*/
  56. /* FMC Macro Definitions */
  57. /*---------------------------------------------------------------------------------------------------------*/
  58. #define FMC_ENABLE_ISP() (FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk) /*!< Enable ISP Function */
  59. #define FMC_DISABLE_ISP() (FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk) /*!< Disable ISP Function */
  60. #define FMC_ENABLE_LD_UPDATE() (FMC->ISPCON |= FMC_ISPCON_LDUEN_Msk) /*!< Enable LDROM Update Function */
  61. #define FMC_DISABLE_LD_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_LDUEN_Msk) /*!< Disable LDROM Update Function */
  62. #define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCON |= FMC_ISPCON_CFGUEN_Msk) /*!< Enable CONFIG Update Function */
  63. #define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_CFGUEN_Msk) /*!< Disable CONFIG Update Function */
  64. #define FMC_ENABLE_AP_UPDATE() (FMC->ISPCON |= FMC_ISPCON_APUEN_Msk) /*!< Enable APROM Update Function */
  65. #define FMC_DISABLE_AP_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_APUEN_Msk) /*!< Disable APROM Update Function */
  66. #define FMC_GET_FAIL_FLAG() ((FMC->ISPCON & FMC_ISPCON_ISPFF_Msk) ? 1 : 0) /*!< Get ISP fail flag */
  67. #define FMC_SET_APROM_BOOT() (FMC->ISPCON &= ~FMC_ISPCON_BS_Msk) /*!< Select booting from APROM */
  68. #define FMC_SET_LDROM_BOOT() (FMC->ISPCON |= FMC_ISPCON_BS_Msk) /*!< Select booting from LDROM */
  69. /*---------------------------------------------------------------------------------------------------------*/
  70. /* inline functions */
  71. /*---------------------------------------------------------------------------------------------------------*/
  72. /**
  73. * @brief Program 32-bit data into specified address of flash
  74. *
  75. * @param[in] u32addr Flash address include APROM, LDROM, Data Flash, and CONFIG
  76. * @param[in] u32data 32-bit Data to program
  77. *
  78. * @details To program word data into Flash include APROM, LDROM, Data Flash, and CONFIG.
  79. * The corresponding functions in CONFIG are listed in FMC section of Technical Reference Manual.
  80. *
  81. * @note
  82. * Please make sure that Register Write-Protection Function has been disabled
  83. * before using this function.
  84. */
  85. static __INLINE void FMC_Write(uint32_t u32addr, uint32_t u32data)
  86. {
  87. FMC->ISPCMD = FMC_ISPCMD_PROGRAM; /* Set ISP Command Code */
  88. FMC->ISPADR = u32addr; /* Set Target ROM Address. The address must be word alignment. */
  89. FMC->ISPDAT = u32data; /* Set Data to Program */
  90. FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */
  91. __ISB(); /* To make sure ISP/CPU be Synchronized */
  92. while(FMC->ISPTRG); /* Waiting for ISP Done */
  93. }
  94. /**
  95. * @brief Read 32-bit Data from specified address of flash
  96. *
  97. * @param[in] u32addr Flash address include APROM, LDROM, Data Flash, and CONFIG
  98. *
  99. * @return The data of specified address
  100. *
  101. * @details To read word data from Flash include APROM, LDROM, Data Flash, and CONFIG.
  102. *
  103. * @note
  104. * Please make sure that Register Write-Protection Function has been disabled
  105. * before using this function.
  106. */
  107. static __INLINE uint32_t FMC_Read(uint32_t u32addr)
  108. {
  109. FMC->ISPCMD = FMC_ISPCMD_READ; /* Set ISP Command Code */
  110. FMC->ISPADR = u32addr; /* Set Target ROM Address. The address must be word alignment. */
  111. FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */
  112. __ISB(); /* To make sure ISP/CPU be Synchronized */
  113. while(FMC->ISPTRG); /* Waiting for ISP Done */
  114. return FMC->ISPDAT;
  115. }
  116. /**
  117. * @brief Flash page erase
  118. *
  119. * @param[in] u32addr Flash address including APROM, LDROM, Data Flash, and CONFIG
  120. *
  121. * @details To do flash page erase. The target address could be APROM, LDROM, Data Flash, or CONFIG.
  122. * The page size is 512 bytes.
  123. *
  124. * @retval 0: Success
  125. * @retval -1: Erase failed
  126. *
  127. * @note
  128. * Please make sure that Register Write-Protection Function has been disabled
  129. * before using this function.
  130. */
  131. static __INLINE int32_t FMC_Erase(uint32_t u32addr)
  132. {
  133. FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE; /* Set ISP Command Code */
  134. FMC->ISPADR = u32addr; /* Set Target ROM Address. The address must be page alignment. */
  135. FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */
  136. __ISB(); /* To make sure ISP/CPU be Synchronized */
  137. while(FMC->ISPTRG); /* Waiting for ISP Done */
  138. /* Check ISPFF flag to know whether erase OK or fail. */
  139. if(FMC->ISPCON & FMC_ISPCON_ISPFF_Msk)
  140. {
  141. FMC->ISPCON = FMC_ISPCON_ISPFF_Msk;
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. /**
  147. * @brief Read Unique ID
  148. *
  149. * @param[in] u8index UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
  150. *
  151. * @return The 32-bit unique ID data of specified UID index.
  152. *
  153. * @details To read out 96-bit Unique ID.
  154. *
  155. * @note
  156. * Please make sure that Register Write-Protection Function has been disabled
  157. * before using this function.
  158. */
  159. static __INLINE uint32_t FMC_ReadUID(uint8_t u8index)
  160. {
  161. FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */
  162. FMC->ISPADR = (u8index << 2); /* Set UID Address. It must be word alignment. */
  163. FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */
  164. __ISB(); /* To make sure ISP/CPU be Synchronized */
  165. while(FMC->ISPTRG); /* Waiting for ISP Done */
  166. return FMC->ISPDAT;
  167. }
  168. /**
  169. * @brief Read company ID
  170. *
  171. * @retval The company ID (32-bit)
  172. *
  173. * @details The company ID of Nuvoton is fixed to be 0xDA
  174. */
  175. static __INLINE uint32_t FMC_ReadCID(void)
  176. {
  177. FMC->ISPCMD = FMC_ISPCMD_READ_CID; /* Set ISP Command Code */
  178. FMC->ISPADR = 0x0; /* Must keep 0x0 when read CID */
  179. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  180. __ISB(); /* To make sure ISP/CPU be Synchronized */
  181. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; /* Waiting for ISP Done */
  182. return FMC->ISPDAT;
  183. }
  184. /**
  185. * @brief Read device ID
  186. *
  187. * @retval The device ID (32-bit)
  188. *
  189. */
  190. static __INLINE uint32_t FMC_ReadDID(void)
  191. {
  192. FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */
  193. FMC->ISPADR = 0; /* Must keep 0x0 when read DID */
  194. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  195. __ISB(); /* To make sure ISP/CPU be Synchronized */
  196. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk); /* Waiting for ISP Done */
  197. return FMC->ISPDAT;
  198. }
  199. /**
  200. * @brief Read product ID
  201. *
  202. * @retval The product ID (32-bit)
  203. *
  204. */
  205. static __INLINE uint32_t FMC_ReadPID(void)
  206. {
  207. FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */
  208. FMC->ISPADR = 0x04; /* Must keep 0x4 when read PID */
  209. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  210. __ISB(); /* To make sure ISP/CPU be Synchronized */
  211. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk); /* Waiting for ISP Done */
  212. return FMC->ISPDAT;
  213. }
  214. /**
  215. * @brief To read UCID
  216. *
  217. * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
  218. *
  219. * @retval The UCID of specified index
  220. *
  221. */
  222. static __INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
  223. {
  224. FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */
  225. FMC->ISPADR = (0x04 * u32Index) + 0x10; /* The UCID is at offset 0x10 with word alignment. */
  226. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  227. __ISB(); /* To make sure ISP/CPU be Synchronized */
  228. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk); /* Waiting for ISP Done */
  229. return FMC->ISPDAT;
  230. }
  231. /**
  232. * @brief Set vector mapping address
  233. *
  234. * @param[in] u32MapAddr The page address to remap to address 0x0. The address must be page alignment.
  235. *
  236. * @details To set VECMAP to remap specified page address to 0x0.
  237. *
  238. * @note
  239. * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
  240. *
  241. */
  242. static __INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
  243. {
  244. FMC->ISPCMD = FMC_ISPCMD_VECMAP; /* Set ISP Command Code */
  245. FMC->ISPADR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */
  246. FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */
  247. __ISB(); /* To make sure ISP/CPU be Synchronized */
  248. while(FMC->ISPTRG); /* Waiting for ISP Done */
  249. }
  250. /**
  251. * @brief Get current vector mapping address.
  252. *
  253. * @return The current vector mapping address.
  254. *
  255. * @details To get VECMAP value which is the page address for remapping to 0x0.
  256. *
  257. * @note
  258. * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
  259. *
  260. */
  261. static __INLINE uint32_t FMC_GetVECMAP(void)
  262. {
  263. return (FMC->ISPSTA & FMC_ISPSTA_VECMAP_Msk);
  264. }
  265. extern void FMC_Open(void);
  266. extern void FMC_Close(void);
  267. extern void FMC_EnableAPUpdate(void);
  268. extern void FMC_DisableAPUpdate(void);
  269. extern void FMC_EnableConfigUpdate(void);
  270. extern void FMC_DisableConfigUpdate(void);
  271. extern void FMC_EnableLDUpdate(void);
  272. extern void FMC_DisableLDUpdate(void);
  273. extern int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count);
  274. extern int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count);
  275. extern void FMC_SetBootSource(int32_t i32BootSrc);
  276. extern int32_t FMC_GetBootSource(void);
  277. extern uint32_t FMC_ReadDataFlashBaseAddr(void);
  278. /*@}*/ /* end of group M051_FMC_EXPORTED_FUNCTIONS */
  279. /*@}*/ /* end of group M051_FMC_Driver */
  280. /*@}*/ /* end of group M051_Device_Driver */
  281. #ifdef __cplusplus
  282. }
  283. #endif
  284. #endif