efm32_msc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Flash controller module (MSC) peripheral API for EFM32
  4. * @author Energy Micro AS
  5. * @version 2.3.2
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2010 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * This source code is the property of Energy Micro AS. The source and compiled
  12. * code may only be used on Energy Micro "EFM32" microcontrollers.
  13. *
  14. * This copyright notice may not be removed from the source code nor changed.
  15. *
  16. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  17. * obligation to support this Software. Energy Micro AS is providing the
  18. * Software "AS IS", with no express or implied warranties of any kind,
  19. * including, but not limited to, any implied warranties of merchantability
  20. * or fitness for any particular purpose or warranties against infringement
  21. * of any proprietary rights of a third party.
  22. *
  23. * Energy Micro AS will not be liable for any consequential, incidental, or
  24. * special damages, or any other relief, or for any claim by any third party,
  25. * arising from your use of this Software.
  26. *
  27. ******************************************************************************/
  28. #ifndef __EFM32_MSC_H
  29. #define __EFM32_MSC_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include <stdint.h>
  34. #include <stdbool.h>
  35. #include "efm32.h"
  36. #include "efm32_bitband.h"
  37. /***************************************************************************//**
  38. * @addtogroup EFM32_Library
  39. * @{
  40. ******************************************************************************/
  41. /***************************************************************************//**
  42. * @addtogroup MSC
  43. * @brief Flash controller (MSC) peripheral API for EFM32
  44. * @{
  45. ******************************************************************************/
  46. /*******************************************************************************
  47. ************************* DEFINES *****************************************
  48. ******************************************************************************/
  49. /**
  50. * @brief
  51. * The timeout used while waiting for the flash to become ready after
  52. * a write. This number indicates the number of iterations to perform before
  53. * issuing a timeout.
  54. * @note
  55. * This timeout is set very large (in the order of 100x longer than
  56. * necessary). This is to avoid any corner cases.
  57. *
  58. */
  59. #define MSC_PROGRAM_TIMEOUT 10000000ul
  60. /*******************************************************************************
  61. ************************* TYPEDEFS ****************************************
  62. ******************************************************************************/
  63. /** Return codes for writing/erasing the flash */
  64. typedef enum
  65. {
  66. mscReturnOk = 0, /**< Flash write/erase successful. */
  67. mscReturnInvalidAddr = -1, /**< Invalid address. Write to an address that is not flash. */
  68. mscReturnLocked = -2, /**< Flash address is locked. */
  69. mscReturnTimeOut = -3, /**< Timeout while writing to flash. */
  70. mscReturnUnaligned = -4 /**< Unaligned access to flash. */
  71. } msc_Return_TypeDef;
  72. #if defined (_EFM32_GIANT_FAMILY)
  73. /** Strategy for prioritized bus access */
  74. typedef enum {
  75. mscBusStrategyCPU = MSC_READCTRL_BUSSTRATEGY_CPU, /**< Prioritize CPU bus accesses */
  76. mscBusStrategyDMA = MSC_READCTRL_BUSSTRATEGY_DMA, /**< Prioritize DMA bus accesses */
  77. mscBusStrategyDMAEM2 = MSC_READCTRL_BUSSTRATEGY_DMAEM2, /**< Prioritize DMAEM2 for bus accesses */
  78. mscBusStrategyNone = MSC_READCTRL_BUSSTRATEGY_NONE /**< No unit has bus priority */
  79. } mscBusStrategy_Typedef;
  80. #endif
  81. /*******************************************************************************
  82. ************************* PROTOTYPES **************************************
  83. ******************************************************************************/
  84. void MSC_Deinit(void);
  85. void MSC_Init(void);
  86. /***************************************************************************//**
  87. * @brief
  88. * Clear one or more pending MSC interrupts.
  89. *
  90. * @param[in] flags
  91. * Pending MSC intterupt source to clear. Use a bitwise logic OR combination
  92. * of valid interrupt flags for the MSC module (MSC_IF_nnn).
  93. ******************************************************************************/
  94. static __INLINE void MSC_IntClear(uint32_t flags)
  95. {
  96. MSC->IFC = flags;
  97. }
  98. /***************************************************************************//**
  99. * @brief
  100. * Disable one or more MSC interrupts.
  101. *
  102. * @param[in] flags
  103. * MSC interrupt sources to disable. Use a bitwise logic OR combination of
  104. * valid interrupt flags for the MSC module (MSC_IF_nnn).
  105. ******************************************************************************/
  106. static __INLINE void MSC_IntDisable(uint32_t flags)
  107. {
  108. MSC->IEN &= ~(flags);
  109. }
  110. /***************************************************************************//**
  111. * @brief
  112. * Enable one or more MSC interrupts.
  113. *
  114. * @note
  115. * Depending on the use, a pending interrupt may already be set prior to
  116. * enabling the interrupt. Consider using MSC_IntClear() prior to enabling
  117. * if such a pending interrupt should be ignored.
  118. *
  119. * @param[in] flags
  120. * MSC interrupt sources to enable. Use a bitwise logic OR combination of
  121. * valid interrupt flags for the MSC module (MSC_IF_nnn).
  122. ******************************************************************************/
  123. static __INLINE void MSC_IntEnable(uint32_t flags)
  124. {
  125. MSC->IEN |= flags;
  126. }
  127. /***************************************************************************//**
  128. * @brief
  129. * Get pending MSV interrupt flags.
  130. *
  131. * @note
  132. * The event bits are not cleared by the use of this function.
  133. *
  134. * @return
  135. * MSC interrupt sources pending. A bitwise logic OR combination of valid
  136. * interrupt flags for the MSC module (MSC_IF_nnn).
  137. ******************************************************************************/
  138. static __INLINE uint32_t MSC_IntGet(void)
  139. {
  140. return(MSC->IF);
  141. }
  142. /***************************************************************************//**
  143. * @brief
  144. * Set one or more pending MSC interrupts from SW.
  145. *
  146. * @param[in] flags
  147. * MSC interrupt sources to set to pending. Use a bitwise logic OR combination of
  148. * valid interrupt flags for the MSC module (MSC_IF_nnn).
  149. ******************************************************************************/
  150. static __INLINE void MSC_IntSet(uint32_t flags)
  151. {
  152. MSC->IFS = flags;
  153. }
  154. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  155. /***************************************************************************//**
  156. * @brief
  157. * Starts measuring cache hit ratio.
  158. * @details
  159. * This function starts the performance counters. It is defined inline to
  160. * minimize the impact of this code on the measurement itself.
  161. ******************************************************************************/
  162. static __INLINE void MSC_StartCacheMeasurement(void)
  163. {
  164. /* Clear CMOF and CHOF to catch these later */
  165. MSC->IFC = MSC_IF_CHOF | MSC_IF_CMOF;
  166. /* Start performance counters */
  167. MSC->CMD = MSC_CMD_STARTPC;
  168. }
  169. /***************************************************************************//**
  170. * @brief
  171. * Stops measuring the hit rate.
  172. * @note
  173. * This function is defined inline to minimize the impact of this
  174. * code on the measurement itself.
  175. * This code only works for relatively short sections of code. If you wish
  176. * to measure longer sections of code you need to implement a IRQ Handler for
  177. * The CHOF and CMOF overflow interrupts. Theses overflows needs to be
  178. * counted and included in the total.
  179. * The functions can then be implemented as follows:
  180. * @verbatim
  181. * volatile uint32_t hitOverflows
  182. * volatile uint32_t missOverflows
  183. *
  184. * void MSC_IRQHandler(void)
  185. * {
  186. * uint32_t flags;
  187. * flags = MSC->IF;
  188. * if (flags & MSC_IF_CHOF)
  189. * {
  190. * MSC->IFC = MSC_IF_CHOF;
  191. * hitOverflows++;
  192. * }
  193. * if (flags & MSC_IF_CMOF)
  194. * {
  195. * MSC->IFC = MSC_IF_CMOF;
  196. * missOverflows++;
  197. * }
  198. * }
  199. *
  200. * void startPerformanceCounters(void)
  201. * {
  202. * hitOverflows = 0;
  203. * missOverflows = 0;
  204. *
  205. * MSC_IntEnable(MSC_IF_CHOF | MSC_IF_CMOF);
  206. * NVIC_EnableIRQ(MSC_IRQn);
  207. *
  208. * MSC_StartCacheMeasurement();
  209. * }
  210. * @endverbatim
  211. * @return
  212. * Returns -1 if there has been no cache accesses.
  213. * Returns -2 if there has been an overflow in the performance counters.
  214. * If not, it will return the percentage of hits versus misses.
  215. ******************************************************************************/
  216. static __INLINE int32_t MSC_GetCacheMeasurement(void)
  217. {
  218. int32_t total;
  219. /* Stop the counter before computing the hit-rate */
  220. MSC->CMD = MSC_CMD_STOPPC;
  221. /* Check for overflows in performance counters */
  222. if (MSC->IF & (MSC_IF_CHOF | MSC_IF_CMOF))
  223. return -2;
  224. /* Because the hits and misses are volatile, we need to split this up into
  225. * two statements to avoid a compiler warning regarding the order of volatile
  226. * accesses. */
  227. total = MSC->CACHEHITS;
  228. total += MSC->CACHEMISSES;
  229. /* To avoid a division by zero. */
  230. if (total == 0)
  231. return -1;
  232. return (MSC->CACHEHITS * 100) / total;
  233. }
  234. /***************************************************************************//**
  235. * @brief
  236. * Flush the contents of the instruction cache.
  237. ******************************************************************************/
  238. static __INLINE void MSC_FlushCache(void)
  239. {
  240. MSC->CMD = MSC_CMD_INVCACHE;
  241. }
  242. /***************************************************************************//**
  243. * @brief
  244. * Enable or disable instruction cache functionality
  245. * @param[in] enable
  246. * Enable instruction cache. Default is on.
  247. ******************************************************************************/
  248. static __INLINE void MSC_EnableCache(bool enable)
  249. {
  250. BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_IFCDIS_SHIFT, ~enable);
  251. }
  252. /***************************************************************************//**
  253. * @brief
  254. * Enable or disable instruction cache functionality in IRQs
  255. * @param[in] enable
  256. * Enable instruction cache. Default is on.
  257. ******************************************************************************/
  258. static __INLINE void MSC_EnableCacheIRQs(bool enable)
  259. {
  260. BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_ICCDIS_SHIFT, ~enable);
  261. }
  262. /***************************************************************************//**
  263. * @brief
  264. * Enable or disable instruction cache flushing when writing to flash
  265. * @param[in] enable
  266. * Enable automatic cache flushing. Default is on.
  267. ******************************************************************************/
  268. static __INLINE void MSC_EnableAutoCacheFlush(bool enable)
  269. {
  270. BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_AIDIS_SHIFT, ~enable);
  271. }
  272. #endif
  273. #if defined(_EFM32_GIANT_FAMILY)
  274. /***************************************************************************//**
  275. * @brief
  276. * Configure which unit should get priority on system bus.
  277. * @param[in] mode
  278. * Unit to prioritize bus accesses for.
  279. ******************************************************************************/
  280. static __INLINE void MSC_BusStrategy(mscBusStrategy_Typedef mode)
  281. {
  282. MSC->READCTRL = (MSC->READCTRL & ~(_MSC_READCTRL_BUSSTRATEGY_MASK))|mode;
  283. }
  284. #endif
  285. #ifdef __CC_ARM /* MDK-ARM compiler */
  286. msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes);
  287. msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress);
  288. #if defined (_EFM32_GIANT_FAMILY)
  289. msc_Return_TypeDef MSC_MassErase(void);
  290. #endif
  291. #endif /* __CC_ARM */
  292. #ifdef __ICCARM__ /* IAR compiler */
  293. __ramfunc msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes);
  294. __ramfunc msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress);
  295. #if defined (_EFM32_GIANT_FAMILY)
  296. __ramfunc msc_Return_TypeDef MSC_MassErase(void);
  297. #endif
  298. #endif /* __ICCARM__ */
  299. #ifdef __GNUC__ /* GCC based compilers */
  300. #ifdef __CROSSWORKS_ARM /* Rowley Crossworks */
  301. msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes) __attribute__ ((section(".fast")));
  302. msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress) __attribute__ ((section(".fast")));
  303. #if defined (_EFM32_GIANT_FAMILY)
  304. msc_Return_TypeDef MSC_MassErase(void) __attribute__ ((section(".fast")));
  305. #endif
  306. #else /* Sourcery G++ */
  307. msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes) __attribute__ ((section(".ram")));
  308. msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress) __attribute__ ((section(".ram")));
  309. #if defined (_EFM32_GIANT_FAMILY)
  310. msc_Return_TypeDef MSC_MassErase(void) __attribute__ ((section(".ram")));
  311. #endif
  312. #endif /* __GNUC__ */
  313. #endif /* __CROSSWORKS_ARM */
  314. /** @} (end addtogroup MSC) */
  315. /** @} (end addtogroup EFM32_Library) */
  316. #ifdef __cplusplus
  317. }
  318. #endif
  319. #endif /* __EFM32_MSC_H */