1 /***************************************************************************//**
\r
3 * @brief Flash controller module (MSC) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2014 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
41 #include "em_bitband.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
74 /*******************************************************************************
\r
75 ************************* TYPEDEFS ****************************************
\r
76 ******************************************************************************/
\r
78 /** Return codes for writing/erasing the flash */
\r
81 mscReturnOk = 0, /**< Flash write/erase successful. */
\r
82 mscReturnInvalidAddr = -1, /**< Invalid address. Write to an address that is not flash. */
\r
83 mscReturnLocked = -2, /**< Flash address is locked. */
\r
84 mscReturnTimeOut = -3, /**< Timeout while writing to flash. */
\r
85 mscReturnUnaligned = -4 /**< Unaligned access to flash. */
\r
86 } MSC_Status_TypeDef;
\r
89 #if defined( _MSC_READCTRL_BUSSTRATEGY_MASK )
\r
90 /** Strategy for prioritized bus access */
\r
92 mscBusStrategyCPU = MSC_READCTRL_BUSSTRATEGY_CPU, /**< Prioritize CPU bus accesses */
\r
93 mscBusStrategyDMA = MSC_READCTRL_BUSSTRATEGY_DMA, /**< Prioritize DMA bus accesses */
\r
94 mscBusStrategyDMAEM1 = MSC_READCTRL_BUSSTRATEGY_DMAEM1, /**< Prioritize DMAEM1 for bus accesses */
\r
95 mscBusStrategyNone = MSC_READCTRL_BUSSTRATEGY_NONE /**< No unit has bus priority */
\r
96 } MSC_BusStrategy_Typedef;
\r
99 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
100 /* Legacy type names */
\r
101 #define mscBusStrategy_Typedef MSC_BusStrategy_Typedef
\r
102 #define msc_Return_TypeDef MSC_Status_TypeDef
\r
105 /*******************************************************************************
\r
106 ************************* PROTOTYPES **************************************
\r
107 ******************************************************************************/
\r
109 void MSC_Init(void);
\r
110 void MSC_Deinit(void);
\r
112 /***************************************************************************//**
\r
114 * Clear one or more pending MSC interrupts.
\r
117 * Pending MSC intterupt source to clear. Use a bitwise logic OR combination
\r
118 * of valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
119 ******************************************************************************/
\r
120 __STATIC_INLINE void MSC_IntClear(uint32_t flags)
\r
125 /***************************************************************************//**
\r
127 * Disable one or more MSC interrupts.
\r
130 * MSC interrupt sources to disable. Use a bitwise logic OR combination of
\r
131 * valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
132 ******************************************************************************/
\r
133 __STATIC_INLINE void MSC_IntDisable(uint32_t flags)
\r
135 MSC->IEN &= ~(flags);
\r
139 /***************************************************************************//**
\r
141 * Enable one or more MSC interrupts.
\r
144 * Depending on the use, a pending interrupt may already be set prior to
\r
145 * enabling the interrupt. Consider using MSC_IntClear() prior to enabling
\r
146 * if such a pending interrupt should be ignored.
\r
149 * MSC interrupt sources to enable. Use a bitwise logic OR combination of
\r
150 * valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
151 ******************************************************************************/
\r
152 __STATIC_INLINE void MSC_IntEnable(uint32_t flags)
\r
158 /***************************************************************************//**
\r
160 * Get pending MSC interrupt flags.
\r
163 * The event bits are not cleared by the use of this function.
\r
166 * MSC interrupt sources pending. A bitwise logic OR combination of valid
\r
167 * interrupt flags for the MSC module (MSC_IF_nnn).
\r
168 ******************************************************************************/
\r
169 __STATIC_INLINE uint32_t MSC_IntGet(void)
\r
175 /***************************************************************************//**
\r
177 * Get enabled and pending MSC interrupt flags.
\r
178 * Useful for handling more interrupt sources in the same interrupt handler.
\r
181 * Interrupt flags are not cleared by the use of this function.
\r
184 * Pending and enabled MSC interrupt sources
\r
185 * The return value is the bitwise AND of
\r
186 * - the enabled interrupt sources in MSC_IEN and
\r
187 * - the pending interrupt flags MSC_IF
\r
188 ******************************************************************************/
\r
189 __STATIC_INLINE uint32_t MSC_IntGetEnabled(void)
\r
194 return MSC->IF & ien;
\r
198 /***************************************************************************//**
\r
200 * Set one or more pending MSC interrupts from SW.
\r
203 * MSC interrupt sources to set to pending. Use a bitwise logic OR combination of
\r
204 * valid interrupt flags for the MSC module (MSC_IF_nnn).
\r
205 ******************************************************************************/
\r
206 __STATIC_INLINE void MSC_IntSet(uint32_t flags)
\r
212 #if defined( MSC_IF_CHOF ) && defined( MSC_IF_CMOF )
\r
213 /***************************************************************************//**
\r
215 * Starts measuring cache hit ratio.
\r
217 * This function starts the performance counters. It is defined inline to
\r
218 * minimize the impact of this code on the measurement itself.
\r
219 ******************************************************************************/
\r
220 __STATIC_INLINE void MSC_StartCacheMeasurement(void)
\r
222 /* Clear CMOF and CHOF to catch these later */
\r
223 MSC->IFC = MSC_IF_CHOF | MSC_IF_CMOF;
\r
225 /* Start performance counters */
\r
226 #if defined( _MSC_CACHECMD_MASK )
\r
227 MSC->CACHECMD = MSC_CACHECMD_STARTPC;
\r
229 MSC->CMD = MSC_CMD_STARTPC;
\r
234 /***************************************************************************//**
\r
236 * Stops measuring the hit rate.
\r
238 * This function is defined inline to minimize the impact of this
\r
239 * code on the measurement itself.
\r
240 * This code only works for relatively short sections of code. If you wish
\r
241 * to measure longer sections of code you need to implement a IRQ Handler for
\r
242 * The CHOF and CMOF overflow interrupts. Theses overflows needs to be
\r
243 * counted and included in the total.
\r
244 * The functions can then be implemented as follows:
\r
246 * volatile uint32_t hitOverflows
\r
247 * volatile uint32_t missOverflows
\r
249 * void MSC_IRQHandler(void)
\r
253 * if (flags & MSC_IF_CHOF)
\r
255 * MSC->IFC = MSC_IF_CHOF;
\r
258 * if (flags & MSC_IF_CMOF)
\r
260 * MSC->IFC = MSC_IF_CMOF;
\r
265 * void startPerformanceCounters(void)
\r
267 * hitOverflows = 0;
\r
268 * missOverflows = 0;
\r
270 * MSC_IntEnable(MSC_IF_CHOF | MSC_IF_CMOF);
\r
271 * NVIC_EnableIRQ(MSC_IRQn);
\r
273 * MSC_StartCacheMeasurement();
\r
277 * Returns -1 if there has been no cache accesses.
\r
278 * Returns -2 if there has been an overflow in the performance counters.
\r
279 * If not, it will return the percentage of hits versus misses.
\r
280 ******************************************************************************/
\r
281 __STATIC_INLINE int32_t MSC_GetCacheMeasurement(void)
\r
284 /* Stop the counter before computing the hit-rate */
\r
285 #if defined( _MSC_CACHECMD_MASK )
\r
286 MSC->CACHECMD = MSC_CACHECMD_STOPPC;
\r
288 MSC->CMD = MSC_CMD_STOPPC;
\r
291 /* Check for overflows in performance counters */
\r
292 if (MSC->IF & (MSC_IF_CHOF | MSC_IF_CMOF))
\r
295 /* Because the hits and misses are volatile, we need to split this up into
\r
296 * two statements to avoid a compiler warning regarding the order of volatile
\r
298 total = MSC->CACHEHITS;
\r
299 total += MSC->CACHEMISSES;
\r
301 /* To avoid a division by zero. */
\r
305 return (MSC->CACHEHITS * 100) / total;
\r
309 /***************************************************************************//**
\r
311 * Flush the contents of the instruction cache.
\r
312 ******************************************************************************/
\r
313 __STATIC_INLINE void MSC_FlushCache(void)
\r
315 #if defined( _MSC_CACHECMD_MASK )
\r
316 MSC->CACHECMD = MSC_CACHECMD_INVCACHE;
\r
318 MSC->CMD = MSC_CMD_INVCACHE;
\r
323 /***************************************************************************//**
\r
325 * Enable or disable instruction cache functionality
\r
326 * @param[in] enable
\r
327 * Enable instruction cache. Default is on.
\r
328 ******************************************************************************/
\r
329 __STATIC_INLINE void MSC_EnableCache(bool enable)
\r
331 BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_IFCDIS_SHIFT, ~enable);
\r
335 #if defined( MSC_READCTRL_ICCDIS )
\r
336 /***************************************************************************//**
\r
338 * Enable or disable instruction cache functionality in IRQs
\r
339 * @param[in] enable
\r
340 * Enable instruction cache. Default is on.
\r
341 ******************************************************************************/
\r
342 __STATIC_INLINE void MSC_EnableCacheIRQs(bool enable)
\r
344 BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_ICCDIS_SHIFT, ~enable);
\r
349 /***************************************************************************//**
\r
351 * Enable or disable instruction cache flushing when writing to flash
\r
352 * @param[in] enable
\r
353 * Enable automatic cache flushing. Default is on.
\r
354 ******************************************************************************/
\r
355 __STATIC_INLINE void MSC_EnableAutoCacheFlush(bool enable)
\r
357 BITBAND_Peripheral(&(MSC->READCTRL), _MSC_READCTRL_AIDIS_SHIFT, ~enable);
\r
359 #endif /* defined( MSC_IF_CHOF ) && defined( MSC_IF_CMOF ) */
\r
362 #if defined( _MSC_READCTRL_BUSSTRATEGY_MASK )
\r
363 /***************************************************************************//**
\r
365 * Configure which unit should get priority on system bus.
\r
367 * Unit to prioritize bus accesses for.
\r
368 ******************************************************************************/
\r
369 __STATIC_INLINE void MSC_BusStrategy(mscBusStrategy_Typedef mode)
\r
371 MSC->READCTRL = (MSC->READCTRL & ~(_MSC_READCTRL_BUSSTRATEGY_MASK)) | mode;
\r
376 #ifdef __CC_ARM /* MDK-ARM compiler */
\r
377 MSC_Status_TypeDef MSC_WriteWord(uint32_t *address, void const *data, uint32_t numBytes);
\r
378 #if !defined( _EFM32_GECKO_FAMILY )
\r
379 MSC_Status_TypeDef MSC_WriteWordFast(uint32_t *address, void const *data, uint32_t numBytes);
\r
381 MSC_Status_TypeDef MSC_ErasePage(uint32_t *startAddress);
\r
383 #if defined( _MSC_MASSLOCK_MASK )
\r
384 MSC_Status_TypeDef MSC_MassErase(void);
\r
386 #endif /* __CC_ARM */
\r
388 #ifdef __ICCARM__ /* IAR compiler */
\r
389 __ramfunc MSC_Status_TypeDef MSC_WriteWord(uint32_t *address, void const *data, uint32_t numBytes);
\r
390 #if !defined( _EFM32_GECKO_FAMILY )
\r
391 __ramfunc MSC_Status_TypeDef MSC_WriteWordFast(uint32_t *address, void const *data, uint32_t numBytes);
\r
393 __ramfunc MSC_Status_TypeDef MSC_ErasePage(uint32_t *startAddress);
\r
395 #if defined( _MSC_MASSLOCK_MASK )
\r
396 __ramfunc MSC_Status_TypeDef MSC_MassErase(void);
\r
398 #endif /* __ICCARM__ */
\r
400 #ifdef __GNUC__ /* GCC based compilers */
\r
401 #ifdef __CROSSWORKS_ARM /* Rowley Crossworks (GCC based) */
\r
402 MSC_Status_TypeDef MSC_WriteWord(uint32_t *address, void const *data, uint32_t numBytes) __attribute__ ((section(".fast")));
\r
403 #if !defined( _EFM32_GECKO_FAMILY )
\r
404 MSC_Status_TypeDef MSC_WriteWordFast(uint32_t *address, void const *data, uint32_t numBytes) __attribute__ ((section(".fast")));
\r
406 MSC_Status_TypeDef MSC_ErasePage(uint32_t *startAddress) __attribute__ ((section(".fast")));
\r
408 #if defined( _MSC_MASSLOCK_MASK )
\r
409 MSC_Status_TypeDef MSC_MassErase(void) __attribute__ ((section(".fast")));
\r
413 MSC_Status_TypeDef MSC_WriteWord(uint32_t *address, void const *data, uint32_t numBytes) __attribute__ ((section(".ram")));
\r
414 #if !defined( _EFM32_GECKO_FAMILY )
\r
415 MSC_Status_TypeDef MSC_WriteWordFast(uint32_t *address, void const *data, uint32_t numBytes) __attribute__ ((section(".ram")));
\r
417 MSC_Status_TypeDef MSC_ErasePage(uint32_t *startAddress) __attribute__ ((section(".ram")));
\r
419 #if defined( _MSC_MASSLOCK_MASK )
\r
420 MSC_Status_TypeDef MSC_MassErase(void) __attribute__ ((section(".ram")));
\r
423 #endif /* __GNUC__ */
\r
424 #endif /* __CROSSWORKS_ARM */
\r
426 /** @} (end addtogroup MSC) */
\r
427 /** @} (end addtogroup EM_Library) */
\r
433 #endif /* defined(MSC_COUNT) && (MSC_COUNT > 0) */
\r
434 #endif /* __SILICON_LABS_EM_MSC_H__ */
\r