1 /***************************************************************************//**
\r
3 * @brief Flash controller module (MSC) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
33 #ifndef __SILICON_LABS_EM_MSC_H__
\r
34 #define __SILICON_LABS_EM_MSC_H__
\r
36 #include "em_device.h"
\r
37 #if defined(MSC_COUNT) && (MSC_COUNT > 0)
\r
40 #include <stdbool.h>
\r
47 /***************************************************************************//**
\r
48 * @addtogroup EM_Library
\r
50 ******************************************************************************/
\r
52 /***************************************************************************//**
\r
54 * @brief Flash controller (MSC) peripheral API
\r
56 ******************************************************************************/
\r
58 /*******************************************************************************
\r
59 ************************* DEFINES *****************************************
\r
60 ******************************************************************************/
\r
64 * The timeout used while waiting for the flash to become ready after
\r
65 * a write. This number indicates the number of iterations to perform before
\r
66 * issuing a timeout.
\r
68 * This timeout is set very large (in the order of 100x longer than
\r
69 * necessary). This is to avoid any corner cases.
\r
72 #define MSC_PROGRAM_TIMEOUT 10000000ul
\r
76 * By compiling with the define EM_MSC_RUN_FROM_FLASH the Flash
\r
77 * controller (MSC) peripheral will remain in and execute from flash.
\r
78 * This is useful for targets that don't want to allocate RAM space to
\r
79 * hold the flash functions. Without this define the MSC peripheral
\r
80 * functions will be copied into and run out of RAM.
\r
82 * This define is commented out by default so the MSC controller API
\r
83 * will run from RAM by default.
\r
86 #if defined( DOXY_DOC_ONLY )
\r
87 #define EM_MSC_RUN_FROM_FLASH
\r
89 //#define EM_MSC_RUN_FROM_FLASH
\r
92 /*******************************************************************************
\r
93 ************************* TYPEDEFS ****************************************
\r
94 ******************************************************************************/
\r
96 /** Return codes for writing/erasing the flash */
\r
99 mscReturnOk = 0, /**< Flash write/erase successful. */
\r
100 mscReturnInvalidAddr = -1, /**< Invalid address. Write to an address that is not flash. */
\r
101 mscReturnLocked = -2, /**< Flash address is locked. */
\r
102 mscReturnTimeOut = -3, /**< Timeout while writing to flash. */
\r
103 mscReturnUnaligned = -4 /**< Unaligned access to flash. */
\r
104 } MSC_Status_TypeDef;
\r
107 #if defined( _MSC_READCTRL_BUSSTRATEGY_MASK )
\r
108 /** Strategy for prioritized bus access */
\r
111 mscBusStrategyCPU = MSC_READCTRL_BUSSTRATEGY_CPU, /**< Prioritize CPU bus accesses */
\r
112 mscBusStrategyDMA = MSC_READCTRL_BUSSTRATEGY_DMA, /**< Prioritize DMA bus accesses */
\r
113 mscBusStrategyDMAEM1 = MSC_READCTRL_BUSSTRATEGY_DMAEM1, /**< Prioritize DMAEM1 for bus accesses */
\r
114 mscBusStrategyNone = MSC_READCTRL_BUSSTRATEGY_NONE /**< No unit has bus priority */
\r
115 } MSC_BusStrategy_Typedef;
\r
118 /** Code execution configuration */
\r
121 bool scbtEn; /**< Enable Suppressed Conditional Branch Target Prefetch */
\r
122 bool prefetchEn; /**< Enable MSC prefetching */
\r
123 bool ifcDis; /**< Disable instruction cache */
\r
124 bool aiDis; /**< Disable automatic cache invalidation on write or erase */
\r
125 bool iccDis; /**< Disable automatic caching of fetches in interrupt context */
\r
126 bool useHprot; /**< Use ahb_hprot to determine if the instruction is cacheable or not */
\r
127 } MSC_ExecConfig_TypeDef;
\r
129 /** Default MSC ExecConfig initialization */
\r
130 #define MSC_EXECCONFIG_DEFAULT \
\r
140 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
141 /* Legacy type names */
\r
142 #define mscBusStrategy_Typedef MSC_BusStrategy_Typedef
\r
143 #define msc_Return_TypeDef MSC_Status_TypeDef
\r
146 /*******************************************************************************
\r
147 ************************* PROTOTYPES **************************************
\r
148 ******************************************************************************/
\r
150 void MSC_Init(void);
\r
151 void MSC_Deinit(void);
\r
152 #if !defined( _EFM32_GECKO_FAMILY )
\r
153 void MSC_ExecConfigSet(MSC_ExecConfig_TypeDef *execConfig);
\r
156 /***************************************************************************//**
\r
158 * Clear one or more pending MSC interrupts.
\r
161 * Pending MSC intterupt source to clear. Use a bitwise logic OR combination
\r
162 * of valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
163 ******************************************************************************/
\r
164 __STATIC_INLINE void MSC_IntClear(uint32_t flags)
\r
169 /***************************************************************************//**
\r
171 * Disable one or more MSC interrupts.
\r
174 * MSC interrupt sources to disable. Use a bitwise logic OR combination of
\r
175 * valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
176 ******************************************************************************/
\r
177 __STATIC_INLINE void MSC_IntDisable(uint32_t flags)
\r
179 MSC->IEN &= ~(flags);
\r
183 /***************************************************************************//**
\r
185 * Enable one or more MSC interrupts.
\r
188 * Depending on the use, a pending interrupt may already be set prior to
\r
189 * enabling the interrupt. Consider using MSC_IntClear() prior to enabling
\r
190 * if such a pending interrupt should be ignored.
\r
193 * MSC interrupt sources to enable. Use a bitwise logic OR combination of
\r
194 * valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
195 ******************************************************************************/
\r
196 __STATIC_INLINE void MSC_IntEnable(uint32_t flags)
\r
202 /***************************************************************************//**
\r
204 * Get pending MSC interrupt flags.
\r
207 * The event bits are not cleared by the use of this function.
\r
210 * MSC interrupt sources pending. A bitwise logic OR combination of valid
\r
211 * interrupt flags for the MSC module (MSC_IF_nnn).
\r
212 ******************************************************************************/
\r
213 __STATIC_INLINE uint32_t MSC_IntGet(void)
\r
219 /***************************************************************************//**
\r
221 * Get enabled and pending MSC interrupt flags.
\r
222 * Useful for handling more interrupt sources in the same interrupt handler.
\r
225 * Interrupt flags are not cleared by the use of this function.
\r
228 * Pending and enabled MSC interrupt sources
\r
229 * The return value is the bitwise AND of
\r
230 * - the enabled interrupt sources in MSC_IEN and
\r
231 * - the pending interrupt flags MSC_IF
\r
232 ******************************************************************************/
\r
233 __STATIC_INLINE uint32_t MSC_IntGetEnabled(void)
\r
238 return MSC->IF & ien;
\r
242 /***************************************************************************//**
\r
244 * Set one or more pending MSC interrupts from SW.
\r
247 * MSC interrupt sources to set to pending. Use a bitwise logic OR combination of
\r
248 * valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
249 ******************************************************************************/
\r
250 __STATIC_INLINE void MSC_IntSet(uint32_t flags)
\r
256 #if defined( MSC_IF_CHOF ) && defined( MSC_IF_CMOF )
\r
257 /***************************************************************************//**
\r
259 * Starts measuring cache hit ratio.
\r
261 * This function starts the performance counters. It is defined inline to
\r
262 * minimize the impact of this code on the measurement itself.
\r
263 ******************************************************************************/
\r
264 __STATIC_INLINE void MSC_StartCacheMeasurement(void)
\r
266 /* Clear CMOF and CHOF to catch these later */
\r
267 MSC->IFC = MSC_IF_CHOF | MSC_IF_CMOF;
\r
269 /* Start performance counters */
\r
270 #if defined( _MSC_CACHECMD_MASK )
\r
271 MSC->CACHECMD = MSC_CACHECMD_STARTPC;
\r
273 MSC->CMD = MSC_CMD_STARTPC;
\r
278 /***************************************************************************//**
\r
280 * Stops measuring the hit rate.
\r
282 * This function is defined inline to minimize the impact of this
\r
283 * code on the measurement itself.
\r
284 * This code only works for relatively short sections of code. If you wish
\r
285 * to measure longer sections of code you need to implement a IRQ Handler for
\r
286 * The CHOF and CMOF overflow interrupts. Theses overflows needs to be
\r
287 * counted and included in the total.
\r
288 * The functions can then be implemented as follows:
\r
290 * volatile uint32_t hitOverflows
\r
291 * volatile uint32_t missOverflows
\r
293 * void MSC_IRQHandler(void)
\r
297 * if (flags & MSC_IF_CHOF)
\r
299 * MSC->IFC = MSC_IF_CHOF;
\r
302 * if (flags & MSC_IF_CMOF)
\r
304 * MSC->IFC = MSC_IF_CMOF;
\r
309 * void startPerformanceCounters(void)
\r
311 * hitOverflows = 0;
\r
312 * missOverflows = 0;
\r
314 * MSC_IntEnable(MSC_IF_CHOF | MSC_IF_CMOF);
\r
315 * NVIC_EnableIRQ(MSC_IRQn);
\r
317 * MSC_StartCacheMeasurement();
\r
321 * Returns -1 if there has been no cache accesses.
\r
322 * Returns -2 if there has been an overflow in the performance counters.
\r
323 * If not, it will return the percentage of hits versus misses.
\r
324 ******************************************************************************/
\r
325 __STATIC_INLINE int32_t MSC_GetCacheMeasurement(void)
\r
328 /* Stop the counter before computing the hit-rate */
\r
329 #if defined( _MSC_CACHECMD_MASK )
\r
330 MSC->CACHECMD = MSC_CACHECMD_STOPPC;
\r
332 MSC->CMD = MSC_CMD_STOPPC;
\r
335 /* Check for overflows in performance counters */
\r
336 if (MSC->IF & (MSC_IF_CHOF | MSC_IF_CMOF))
\r
339 /* Because the hits and misses are volatile, we need to split this up into
\r
340 * two statements to avoid a compiler warning regarding the order of volatile
\r
342 total = MSC->CACHEHITS;
\r
343 total += MSC->CACHEMISSES;
\r
345 /* To avoid a division by zero. */
\r
349 return (MSC->CACHEHITS * 100) / total;
\r
353 /***************************************************************************//**
\r
355 * Flush the contents of the instruction cache.
\r
356 ******************************************************************************/
\r
357 __STATIC_INLINE void MSC_FlushCache(void)
\r
359 #if defined( _MSC_CACHECMD_MASK )
\r
360 MSC->CACHECMD = MSC_CACHECMD_INVCACHE;
\r
362 MSC->CMD = MSC_CMD_INVCACHE;
\r
367 /***************************************************************************//**
\r
369 * Enable or disable instruction cache functionality
\r
370 * @param[in] enable
\r
371 * Enable instruction cache. Default is on.
\r
372 ******************************************************************************/
\r
373 __STATIC_INLINE void MSC_EnableCache(bool enable)
\r
375 BUS_RegBitWrite(&(MSC->READCTRL), _MSC_READCTRL_IFCDIS_SHIFT, !enable);
\r
379 #if defined( MSC_READCTRL_ICCDIS )
\r
380 /***************************************************************************//**
\r
382 * Enable or disable instruction cache functionality in IRQs
\r
383 * @param[in] enable
\r
384 * Enable instruction cache. Default is on.
\r
385 ******************************************************************************/
\r
386 __STATIC_INLINE void MSC_EnableCacheIRQs(bool enable)
\r
388 BUS_RegBitWrite(&(MSC->READCTRL), _MSC_READCTRL_ICCDIS_SHIFT, !enable);
\r
393 /***************************************************************************//**
\r
395 * Enable or disable instruction cache flushing when writing to flash
\r
396 * @param[in] enable
\r
397 * Enable automatic cache flushing. Default is on.
\r
398 ******************************************************************************/
\r
399 __STATIC_INLINE void MSC_EnableAutoCacheFlush(bool enable)
\r
401 BUS_RegBitWrite(&(MSC->READCTRL), _MSC_READCTRL_AIDIS_SHIFT, !enable);
\r
403 #endif /* defined( MSC_IF_CHOF ) && defined( MSC_IF_CMOF ) */
\r
406 #if defined( _MSC_READCTRL_BUSSTRATEGY_MASK )
\r
407 /***************************************************************************//**
\r
409 * Configure which unit should get priority on system bus.
\r
411 * Unit to prioritize bus accesses for.
\r
412 ******************************************************************************/
\r
413 __STATIC_INLINE void MSC_BusStrategy(mscBusStrategy_Typedef mode)
\r
415 MSC->READCTRL = (MSC->READCTRL & ~(_MSC_READCTRL_BUSSTRATEGY_MASK)) | mode;
\r
419 #if defined(EM_MSC_RUN_FROM_FLASH)
\r
420 #define MSC_FUNC_PREFIX
\r
421 #define MSC_FUNC_POSTFIX
\r
422 #elif defined(__CC_ARM)
\r
423 #define MSC_FUNC_PREFIX
\r
424 #define MSC_FUNC_POSTFIX
\r
425 #elif defined(__ICCARM__)
\r
426 #define MSC_FUNC_PREFIX __ramfunc
\r
427 #define MSC_FUNC_POSTFIX
\r
428 #elif defined(__GNUC__) && defined(__CROSSWORKS_ARM)
\r
429 #define MSC_FUNC_PREFIX
\r
430 #define MSC_FUNC_POSTFIX __attribute__ ((section(".fast")))
\r
431 #elif defined(__GNUC__)
\r
432 #define MSC_FUNC_PREFIX
\r
433 #define MSC_FUNC_POSTFIX __attribute__ ((section(".ram")))
\r
437 MSC_FUNC_PREFIX MSC_Status_TypeDef
\r
438 MSC_WriteWord(uint32_t *address,
\r
440 uint32_t numBytes) MSC_FUNC_POSTFIX;
\r
442 #if !defined( _EFM32_GECKO_FAMILY )
\r
443 MSC_FUNC_PREFIX MSC_Status_TypeDef
\r
444 MSC_WriteWordFast(uint32_t *address,
\r
446 uint32_t numBytes) MSC_FUNC_POSTFIX;
\r
450 MSC_FUNC_PREFIX MSC_Status_TypeDef
\r
451 MSC_ErasePage(uint32_t *startAddress) MSC_FUNC_POSTFIX;
\r
453 #if defined( _MSC_MASSLOCK_MASK )
\r
454 MSC_FUNC_PREFIX MSC_Status_TypeDef MSC_MassErase(void) MSC_FUNC_POSTFIX;
\r
457 /** @} (end addtogroup MSC) */
\r
458 /** @} (end addtogroup EM_Library) */
\r
464 #endif /* defined(MSC_COUNT) && (MSC_COUNT > 0) */
\r
465 #endif /* __SILICON_LABS_EM_MSC_H__ */
\r