Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fsl_common.h Source File

fsl_common.h

00001 /*
00002  * Copyright (c) 2015, Freescale Semiconductor, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  * o Redistributions of source code must retain the above copyright notice, this list
00009  *   of conditions and the following disclaimer.
00010  *
00011  * o Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
00016  *   contributors may be used to endorse or promote products derived from this
00017  *   software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00023  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00026  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #ifndef _FSL_COMMON_H_
00032 #define _FSL_COMMON_H_
00033 
00034 // #include <assert.h>
00035 #include <stdbool.h>
00036 #include <stdint.h>
00037 #include <string.h>
00038 #include "fsl_device_registers.h"
00039 #include "util.h"
00040 
00041 /*!
00042  * @addtogroup ksdk_common
00043  * @{
00044  */
00045 
00046 /*******************************************************************************
00047  * Definitions
00048  ******************************************************************************/
00049 
00050 // Redefine assert to DAPLink's assert utility for code size savings.
00051 #define assert(x) util_assert((x));
00052 
00053 /*! @brief Construct a status code value from a group and code number. */
00054 #define MAKE_STATUS(group, code) ((((group)*100) + (code)))
00055 
00056 /*! @brief Construct the version number for drivers. */
00057 #define MAKE_VERSION(major, minor, bugfix) (((major) << 16) | ((minor) << 8) | (bugfix))
00058 
00059 /* Debug console type definition. */
00060 #define DEBUG_CONSOLE_DEVICE_TYPE_NONE 0U   /*!< No debug console.             */
00061 #define DEBUG_CONSOLE_DEVICE_TYPE_UART 1U   /*!< Debug console base on UART.   */
00062 #define DEBUG_CONSOLE_DEVICE_TYPE_LPUART 2U /*!< Debug console base on LPUART. */
00063 #define DEBUG_CONSOLE_DEVICE_TYPE_LPSCI 3U  /*!< Debug console base on LPSCI.  */
00064 #define DEBUG_CONSOLE_DEVICE_TYPE_USBCDC 4U /*!< Debug console base on USBCDC. */
00065 
00066 /*! @brief Status group numbers. */
00067 enum _status_groups
00068 {
00069     kStatusGroup_Generic  = 0,                 /*!< Group number for generic status codes. */
00070     kStatusGroup_FLASH  = 1,                   /*!< Group number for FLASH status codes. */
00071     kStatusGroup_LPSPI  = 4,                   /*!< Group number for LPSPI status codes. */
00072     kStatusGroup_FLEXIO_SPI  = 5,              /*!< Group number for FLEXIO SPI status codes. */
00073     kStatusGroup_DSPI  = 6,                    /*!< Group number for DSPI status codes. */
00074     kStatusGroup_FLEXIO_UART  = 7,             /*!< Group number for FLEXIO UART status codes. */
00075     kStatusGroup_FLEXIO_I2C  = 8,              /*!< Group number for FLEXIO I2C status codes. */
00076     kStatusGroup_LPI2C  = 9,                   /*!< Group number for LPI2C status codes. */
00077     kStatusGroup_UART  = 10,                   /*!< Group number for UART status codes. */
00078     kStatusGroup_I2C  = 11,                    /*!< Group number for UART status codes. */
00079     kStatusGroup_LPSCI  = 12,                  /*!< Group number for LPSCI status codes. */
00080     kStatusGroup_LPUART  = 13,                 /*!< Group number for LPUART status codes. */
00081     kStatusGroup_SPI  = 14,                    /*!< Group number for SPI status code.*/
00082     kStatusGroup_XRDC  = 15,                   /*!< Group number for XRDC status code.*/
00083     kStatusGroup_SEMA42  = 16,                 /*!< Group number for SEMA42 status code.*/
00084     kStatusGroup_SDHC  = 17,                   /*!< Group number for SDHC status code */
00085     kStatusGroup_SDMMC  = 18,                  /*!< Group number for SDMMC status code */
00086     kStatusGroup_SAI  = 19,                    /*!< Group number for SAI status code */
00087     kStatusGroup_MCG  = 20,                    /*!< Group number for MCG status codes. */
00088     kStatusGroup_SCG  = 21,                    /*!< Group number for SCG status codes. */
00089     kStatusGroup_SDSPI  = 22,                  /*!< Group number for SDSPI status codes. */
00090     kStatusGroup_FLEXIO_I2S  = 23,             /*!< Group number for FLEXIO I2S status codes */
00091     kStatusGroup_SDRAMC  = 35,                 /*!< Group number for SDRAMC status codes. */
00092     kStatusGroup_POWER  = 39,                  /*!< Group number for POWER status codes. */
00093     kStatusGroup_ENET  = 40,                   /*!< Group number for ENET status codes. */
00094     kStatusGroup_PHY  = 41,                    /*!< Group number for PHY status codes. */
00095     kStatusGroup_TRGMUX  = 42,                 /*!< Group number for TRGMUX status codes. */
00096     kStatusGroup_SMARTCARD  = 43,              /*!< Group number for SMARTCARD status codes. */
00097     kStatusGroup_LMEM  = 44,                   /*!< Group number for LMEM status codes. */
00098     kStatusGroup_QSPI  = 45,                   /*!< Group number for QSPI status codes. */
00099     kStatusGroup_DMA  = 50,                    /*!< Group number for DMA status codes. */
00100     kStatusGroup_EDMA  = 51,                   /*!< Group number for EDMA status codes. */
00101     kStatusGroup_DMAMGR  = 52,                 /*!< Group number for DMAMGR status codes. */
00102     kStatusGroup_FLEXCAN  = 53,                /*!< Group number for FlexCAN status codes. */
00103     kStatusGroup_LTC  = 54,                    /*!< Group number for LTC status codes. */
00104     kStatusGroup_FLEXIO_CAMERA  = 55,          /*!< Group number for FLEXIO CAMERA status codes. */
00105     kStatusGroup_NOTIFIER  = 98,               /*!< Group number for NOTIFIER status codes. */
00106     kStatusGroup_DebugConsole  = 99,           /*!< Group number for debug console status codes. */
00107     kStatusGroup_ApplicationRangeStart  = 100, /*!< Starting number for application groups. */
00108 };
00109 
00110 /*! @brief Generic status return codes. */
00111 enum _generic_status
00112 {
00113     kStatus_Success = MAKE_STATUS(kStatusGroup_Generic , 0),
00114     kStatus_Fail = MAKE_STATUS(kStatusGroup_Generic , 1),
00115     kStatus_ReadOnly = MAKE_STATUS(kStatusGroup_Generic , 2),
00116     kStatus_OutOfRange = MAKE_STATUS(kStatusGroup_Generic , 3),
00117     kStatus_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic , 4),
00118     kStatus_Timeout = MAKE_STATUS(kStatusGroup_Generic , 5),
00119     kStatus_NoTransferInProgress = MAKE_STATUS(kStatusGroup_Generic , 6),
00120 };
00121 
00122 /*! @brief Type used for all status and error return values. */
00123 typedef int32_t status_t;
00124 
00125 // #include "fsl_clock.h"
00126 
00127 /*! @name Min/max macros */
00128 /* @{ */
00129 // #if !defined(MIN)
00130 // #define MIN(a, b) ((a) < (b) ? (a) : (b))
00131 // #endif
00132 //
00133 // #if !defined(MAX)
00134 // #define MAX(a, b) ((a) > (b) ? (a) : (b))
00135 // #endif
00136 // /* @} */
00137 //
00138 // /*! @brief Computes the number of elements in an array. */
00139 // #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
00140 
00141 /*! @name UINT16_MAX/UINT32_MAX value */
00142 /* @{ */
00143 #if !defined(UINT16_MAX)
00144 #define UINT16_MAX ((uint16_t)-1)
00145 #endif
00146 
00147 #if !defined(UINT32_MAX)
00148 #define UINT32_MAX ((uint32_t)-1)
00149 #endif
00150 /* @} */
00151 
00152 /*! @name Timer utilities */
00153 /* @{ */
00154 /*! Macro to convert a microsecond period to raw count value */
00155 #define USEC_TO_COUNT(us, clockFreqInHz) (uint64_t)((uint64_t)us * clockFreqInHz / 1000000U)
00156 /*! Macro to convert a raw count value to microsecond */
00157 #define COUNT_TO_USEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000000U / clockFreqInHz)
00158 
00159 /*! Macro to convert a millisecond period to raw count value */
00160 #define MSEC_TO_COUNT(ms, clockFreqInHz) (uint64_t)((uint64_t)ms * clockFreqInHz / 1000U)
00161 /*! Macro to convert a raw count value to millisecond */
00162 #define COUNT_TO_MSEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000U / clockFreqInHz)
00163 /* @} */
00164 
00165 /*******************************************************************************
00166  * API
00167  ******************************************************************************/
00168 
00169 #if defined(__cplusplus)
00170 extern "C" {
00171 #endif
00172 
00173 /*!
00174  * @brief Enable specific interrupt.
00175  *
00176  * Enable the interrupt not routed from intmux.
00177  *
00178  * @param interrupt The IRQ number.
00179  */
00180 static inline void EnableIRQ(IRQn_Type interrupt)
00181 {
00182 #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && (FSL_FEATURE_SOC_INTMUX_COUNT > 0)
00183     if (interrupt < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
00184 #endif
00185     {
00186         NVIC_EnableIRQ(interrupt);
00187     }
00188 }
00189 
00190 /*!
00191  * @brief Disable specific interrupt.
00192  *
00193  * Disable the interrupt not routed from intmux.
00194  *
00195  * @param interrupt The IRQ number.
00196  */
00197 static inline void DisableIRQ(IRQn_Type interrupt)
00198 {
00199 #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && (FSL_FEATURE_SOC_INTMUX_COUNT > 0)
00200     if (interrupt < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
00201 #endif
00202     {
00203         NVIC_DisableIRQ(interrupt);
00204     }
00205 }
00206 
00207 /*!
00208  * @brief Disable the global IRQ
00209  *
00210  * Disable the global interrupt and return the current primask register. User is required to provided the primask
00211  * register for the EnableGlobalIRQ().
00212  *
00213  * @return Current primask value.
00214  */
00215 static inline uint32_t DisableGlobalIRQ(void)
00216 {
00217     uint32_t regPrimask = __get_PRIMASK();
00218 
00219     __disable_irq();
00220 
00221     return regPrimask;
00222 }
00223 
00224 /*!
00225  * @brief Enaable the global IRQ
00226  *
00227  * Set the primask register with the provided primask value but not just enable the primask. The idea is for the
00228  * convinience of integration of RTOS. some RTOS get its own management mechanism of primask. User is required to
00229  * use the EnableGlobalIRQ() and DisableGlobalIRQ() in pair.
00230  *
00231  * @param primask value of primask register to be restored. The primask value is supposed to be provided by the
00232  * DisableGlobalIRQ().
00233  */
00234 static inline void EnableGlobalIRQ(uint32_t primask)
00235 {
00236     __set_PRIMASK(primask);
00237 }
00238 
00239 /*!
00240  * @brief install IRQ handler
00241  *
00242  * @param irq IRQ number
00243  * @param irqHandler IRQ handler address
00244  */
00245 void InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler);
00246 
00247 #if defined(__cplusplus)
00248 }
00249 #endif
00250 
00251 /*! @} */
00252 
00253 #endif /* _FSL_COMMON_H_ */