em_msc.h 13 KB

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