efm32_msc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Flash controller module (MSC) peripheral API for EFM32
  4. * @author Energy Micro AS
  5. * @version 2.0.0
  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. /*******************************************************************************
  73. ************************* PROTOTYPES **************************************
  74. ******************************************************************************/
  75. void MSC_Deinit(void);
  76. void MSC_Init(void);
  77. /***************************************************************************//**
  78. * @brief
  79. * Clear one or more pending MSC interrupts.
  80. *
  81. * @param[in] flags
  82. * Pending MSC intterupt source to clear. Use a bitwise logic OR combination
  83. * of valid interrupt flags for the MSC module (MSC_IF_nnn).
  84. ******************************************************************************/
  85. static __INLINE void MSC_IntClear(uint32_t flags)
  86. {
  87. MSC->IFC = flags;
  88. }
  89. /***************************************************************************//**
  90. * @brief
  91. * Disable one or more MSC interrupts.
  92. *
  93. * @param[in] flags
  94. * MSC interrupt sources to disable. Use a bitwise logic OR combination of
  95. * valid interrupt flags for the MSC module (MSC_IF_nnn).
  96. ******************************************************************************/
  97. static __INLINE void MSC_IntDisable(uint32_t flags)
  98. {
  99. MSC->IEN &= ~(flags);
  100. }
  101. /***************************************************************************//**
  102. * @brief
  103. * Enable one or more MSC interrupts.
  104. *
  105. * @note
  106. * Depending on the use, a pending interrupt may already be set prior to
  107. * enabling the interrupt. Consider using MSC_IntClear() prior to enabling
  108. * if such a pending interrupt should be ignored.
  109. *
  110. * @param[in] flags
  111. * MSC interrupt sources to enable. Use a bitwise logic OR combination of
  112. * valid interrupt flags for the MSC module (MSC_IF_nnn).
  113. ******************************************************************************/
  114. static __INLINE void MSC_IntEnable(uint32_t flags)
  115. {
  116. MSC->IEN |= flags;
  117. }
  118. /***************************************************************************//**
  119. * @brief
  120. * Get pending MSV interrupt flags.
  121. *
  122. * @note
  123. * The event bits are not cleared by the use of this function.
  124. *
  125. * @return
  126. * MSC interrupt sources pending. A bitwise logic OR combination of valid
  127. * interrupt flags for the MSC module (MSC_IF_nnn).
  128. ******************************************************************************/
  129. static __INLINE uint32_t MSC_IntGet(void)
  130. {
  131. return(MSC->IF);
  132. }
  133. /***************************************************************************//**
  134. * @brief
  135. * Set one or more pending MSC interrupts from SW.
  136. *
  137. * @param[in] flags
  138. * MSC interrupt sources to set to pending. Use a bitwise logic OR combination of
  139. * valid interrupt flags for the MSC module (MSC_IF_nnn).
  140. ******************************************************************************/
  141. static __INLINE void MSC_IntSet(uint32_t flags)
  142. {
  143. MSC->IFS = flags;
  144. }
  145. #if _EFM32_TINY_FAMILY == 1
  146. /***************************************************************************//**
  147. * @brief
  148. * Starts measuring cache hit ratio.
  149. * @details
  150. * This function starts the performance counters. It is defined inline to
  151. * minimize the impact of this code on the measurement itself.
  152. ******************************************************************************/
  153. static __INLINE void MSC_StartCacheMeasurement(void)
  154. {
  155. /* Clear CMOF and CHOF to catch these later */
  156. MSC->IFC = MSC_IF_CHOF | MSC_IF_CMOF;
  157. /* Start performance counters */
  158. MSC->CMD = MSC_CMD_STARTPC;
  159. }
  160. /***************************************************************************//**
  161. * @brief
  162. * Stops measuring the hit rate.
  163. * @note
  164. * This function is defined inline to minimize the impact of this
  165. * code on the measurement itself.
  166. * This code only works for relatively short sections of code. If you wish
  167. * to measure longer sections of code you need to implement a IRQ Handler for
  168. * The CHOF and CMOF overflow interrupts. Theses overflows needs to be
  169. * counted and included in the total.
  170. * The functions can then be implemented as follows:
  171. * @verbatim
  172. * volatile uint32_t hitOverflows
  173. * volatile uint32_t missOverflows
  174. *
  175. * void MSC_IRQHandler(void)
  176. * {
  177. * uint32_t flags;
  178. * flags = MSC->IF;
  179. * if (flags & MSC_IF_CHOF)
  180. * {
  181. * MSC->IFC = MSC_IF_CHOF;
  182. * hitOverflows++;
  183. * }
  184. * if (flags & MSC_IF_CMOF)
  185. * {
  186. * MSC->IFC = MSC_IF_CMOF;
  187. * missOverflows++;
  188. * }
  189. * }
  190. *
  191. * void startPerformanceCounters(void)
  192. * {
  193. * hitOverflows = 0;
  194. * missOverflows = 0;
  195. *
  196. * MSC_IntEnable(MSC_IF_CHOF | MSC_IF_CMOF);
  197. * NVIC_EnableIRQ(MSC_IRQn);
  198. *
  199. * MSC_StartCacheMeasurement();
  200. * }
  201. * @endverbatim
  202. * @return
  203. * Returns -1 if there has been no cache accesses.
  204. * Returns -2 if there has been an overflow in the performance counters.
  205. * If not, it will return the percentage of hits versus misses.
  206. ******************************************************************************/
  207. static __INLINE int32_t MSC_GetCacheMeasurement(void)
  208. {
  209. int32_t total;
  210. /* Stop the counter before computing the hit-rate */
  211. MSC->CMD = MSC_CMD_STOPPC;
  212. /* Check for overflows in performance counters */
  213. if (MSC->IF & (MSC_IF_CHOF | MSC_IF_CMOF))
  214. return -2;
  215. /* Because the hits and misses are volatile, we need to split this up into
  216. * two statements to avoid a compiler warning regarding the order of volatile
  217. * accesses. */
  218. total = MSC->CACHEHITS;
  219. total += MSC->CACHEMISSES;
  220. /* To avoid a division by zero. */
  221. if (total == 0)
  222. return -1;
  223. return (MSC->CACHEHITS * 100) / total;
  224. }
  225. /***************************************************************************//**
  226. * @brief
  227. * Flush the contents of the instruction cache.
  228. ******************************************************************************/
  229. static __INLINE void MSC_FlushCache(void)
  230. {
  231. MSC->CMD = MSC_CMD_INVCACHE;
  232. }
  233. /***************************************************************************//**
  234. * @brief
  235. * Enable or disable instruction cache functionality
  236. * @param[in] enable
  237. * Enable instruction cache. Default is on.
  238. ******************************************************************************/
  239. static __INLINE void MSC_EnableCache(bool enable)
  240. {
  241. BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_IFCDIS_SHIFT, ~enable);
  242. }
  243. /***************************************************************************//**
  244. * @brief
  245. * Enable or disable instruction cache functionality in IRQs
  246. * @param[in] enable
  247. * Enable instruction cache. Default is on.
  248. ******************************************************************************/
  249. static __INLINE void MSC_EnableCacheIRQs(bool enable)
  250. {
  251. BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_ICCDIS_SHIFT, ~enable);
  252. }
  253. /***************************************************************************//**
  254. * @brief
  255. * Enable or disable instruction cache flushing when writing to flash
  256. * @param[in] enable
  257. * Enable automatic cache flushing. Default is on.
  258. ******************************************************************************/
  259. static __INLINE void MSC_EnableAutoCacheFlush(bool enable)
  260. {
  261. BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_AIDIS_SHIFT, ~enable);
  262. }
  263. #endif
  264. #ifdef __CC_ARM /* MDK-ARM compiler */
  265. msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes);
  266. msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress);
  267. #endif /* __CC_ARM */
  268. #ifdef __ICCARM__ /* IAR compiler */
  269. __ramfunc msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes);
  270. __ramfunc msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress);
  271. #endif /* __ICCARM__ */
  272. #ifdef __GNUC__ /* GCC based compilers */
  273. #ifdef __CROSSWORKS_ARM /* Rowley Crossworks */
  274. msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes) __attribute__ ((section(".fast")));
  275. msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress) __attribute__ ((section(".fast")));
  276. #else /* Sourcery G++ */
  277. msc_Return_TypeDef MSC_WriteWord(uint32_t *address, void const *data, int numBytes) __attribute__ ((section(".ram")));
  278. msc_Return_TypeDef MSC_ErasePage(uint32_t *startAddress) __attribute__ ((section(".ram")));
  279. #endif /* __GNUC__ */
  280. #endif /* __CROSSWORKS_ARM */
  281. /** @} (end addtogroup MSC) */
  282. /** @} (end addtogroup EFM32_Library) */
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif /* __EFM32_MSC_H */