sim.c 9.8 KB
Newer Older
Z
zhongjiequan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

/******************************************************************************
*
* @brief providing APIs for system integration module (SIM). 
*
*******************************************************************************
*
* provide APIs for SIM
******************************************************************************/
#include "common.h"
#include "sim.h"

/******************************************************************************
* Global variables
******************************************************************************/

/******************************************************************************
* Constants and macros
******************************************************************************/

/******************************************************************************
* Local types
******************************************************************************/

/******************************************************************************
* Local function prototypes
******************************************************************************/

/******************************************************************************
* Local variables
******************************************************************************/

/******************************************************************************
* Local functions
******************************************************************************/

/******************************************************************************
* Global functions
******************************************************************************/

/******************************************************************************
* define SIM API list
*
*//*! @addtogroup sim_api_list
* @{
*******************************************************************************/
#if defined(CPU_NV32)
/*****************************************************************************//*!
   *
   * @brief initialize SIM registers.
   * 
   * @param[in]  pConfig    pointer to SIM configuration.  
   *
   * @return none
   *
   * @ Pass/ Fail criteria: none
   * @see SIM_ConfigType
   *****************************************************************************/
void SIM_Init(SIM_ConfigType *pConfig)
{
    uint32_t    u32Sopt;
    uint32_t    u32PinSel;
    uint32_t    u32Scgc;
    uint32_t    u32BusDiv;
    /*
     * intialize the registers to reset default values
     */
    u32Sopt     = 0x0010000E;                 /* enable SWD, RESET, and NMI pins */
    u32PinSel   = 0;
    u32Scgc     = 0x00003000;           /* enable SWD and FLASH */
    u32BusDiv   = 0;
    u32BusDiv = pConfig->sBits.bBusDiv;
    if(pConfig->sBits.bDisableNMI)
    {
        u32Sopt &= ~SIM_SOPT_NMIE_MASK;        
    }
    if(pConfig->sBits.bDisableRESET)
    {
        u32Sopt &= ~SIM_SOPT_RSTPE_MASK;        
    }   
    if(pConfig->sBits.bDisableSWD)
    {
        u32Sopt &= ~SIM_SOPT_SWDE_MASK;        
    } 
    if(pConfig->sBits.bEnableCLKOUT)
    {
        u32Sopt |= SIM_SOPT_CLKOE_MASK;        
    } 
    if(pConfig->sBits.bETMSYNC)
    {
        u32Sopt |= SIM_SOPT_ETMSYNC_MASK;        
    }  
    if(pConfig->sBits.bRXDCE)
    {
        u32Sopt |= SIM_SOPT_RXDCE_MASK;        
    }
    if(pConfig->sBits.bTXDME)
    {
        u32Sopt |= SIM_SOPT_TXDME_MASK;        
    }
    if(pConfig->sBits.bACIC)
    {
        u32Sopt |= SIM_SOPT_ACIC_MASK;
    }
        if(pConfig->sBits.bRTCC)
    {
        u32Sopt |= SIM_SOPT_RTCC_MASK;        
    }
        if(pConfig->sBits.bRXDFE)
    {
        u32Sopt |= SIM_SOPT_RXDFE_MASK;        
    }
    u32Sopt |= ((pConfig->u8BusRef & 0x07) << 16);        
    u32Sopt |= ((pConfig->u8Delay) << 24);        
    u32Sopt |= ((pConfig->sBits.u8ADHWT & 0x03) << 8);
    u32PinSel = pConfig->u32PinSel;
    u32Scgc = pConfig->u32SCGC;   
    
/* write SIM registers */
    SIM->SOPT = u32Sopt;
    SIM->PINSEL = u32PinSel;
    SIM->SCGC = u32Scgc; 
    SIM->BUSDIV = u32BusDiv;
}
#elif defined(CPU_NV32M3)
/*****************************************************************************//*!
   *
   * @brief initialize SIM registers.
   * 
   * @param[in]  pConfig    pointer to SIM configuration.  
   *
   * @return none
   *
   * @ Pass/ Fail criteria: none
   * @see SIM_ConfigType
   *****************************************************************************/

void SIM_Init(SIM_ConfigType *pConfig)
{
    uint32_t    u32Sopt;
    uint32_t    u32PinSel;
    uint32_t    u32Scgc;
    uint32_t    u32ClockDiv;
    /*
     * intialize the registers to reset default values
     */
    u32Sopt     = 0x0010000E;                 /* enable SWD, RESET, and NMI pins */
    u32PinSel   = 0;
    u32Scgc     = 0x00003000;           /* enable SWD and FLASH */
    u32ClockDiv = 0;
    u32ClockDiv = pConfig->u32CLKDIV;
    if(pConfig->sBits.bDisableNMI)
    {
        u32Sopt &= ~SIM_SOPT_NMIE_MASK;        
    }
    if(pConfig->sBits.bDisableRESET)
    {
        u32Sopt &= ~SIM_SOPT_RSTPE_MASK;        
    }   
    if(pConfig->sBits.bDisableSWD)
    {
        u32Sopt &= ~SIM_SOPT_SWDE_MASK;        
    } 
    if(pConfig->sBits.bEnableCLKOUT)
    {
        u32Sopt |= SIM_SOPT_CLKOE_MASK;        
    } 
    if(pConfig->sBits.bETMSYNC)
    {
        u32Sopt |= SIM_SOPT_ETMSYNC_MASK;        
    }  
    if(pConfig->sBits.bRXDCE)
    {
        u32Sopt |= SIM_SOPT_RXDCE_MASK;        
    }
    if(pConfig->sBits.bTXDME)
    {
        u32Sopt |= SIM_SOPT_TXDME_MASK;        
    }
    if(pConfig->sBits.bACTRG)
    {
        u32Sopt |= SIM_SOPT_ACTRG_MASK;
    }
    u32Sopt |= ((pConfig->u8BusRef & 0x07) << 16);        
    u32Sopt |= ((pConfig->u8Delay) << 24);        
    u32Sopt |= ((pConfig->sBits.u8ADHWT & 0x07) << 20);
    u32Sopt |= ((pConfig->sBits.bRXDFE)&0x03<<8);
    u32Sopt |= ((pConfig->sBits.bETMIC)&0x03<<6);

    u32PinSel = pConfig->u32PinSel;
    u32Scgc = pConfig->u32SCGC;    
/* write SIM registers */
    SIM->SOPT = u32Sopt;
    SIM->PINSEL = u32PinSel;
    SIM->SCGC = u32Scgc;
    SIM->CLKDIV = u32ClockDiv;
}
#elif defined(CPU_NV32M4)
/*****************************************************************************//*!
   *
   * @brief initialize SIM registers.
   * 
   * @param[in]  pConfig    pointer to SIM configuration.  
   *
   * @return none
   *
   * @ Pass/ Fail criteria: none
   * @see SIM_ConfigType
   *****************************************************************************/
void SIM_Init(SIM_ConfigType *pConfig)
{
    uint32_t    u32Sopt;
    uint32_t    u32PinSel;
    uint32_t    u32Scgc;
    uint32_t    u32ClockDiv;
    /*
     * intialize the registers to reset default values
     */
    u32Sopt     = 0x0E;                 /* enable SWD, RESET, and NMI pins */
    u32PinSel   = 0;
    u32Scgc     = 0x00003000;           /* enable SWD and FLASH */
    u32ClockDiv = 0;
    u32ClockDiv = pConfig->u32CLKDIV;
    if(pConfig->sBits.bDisableNMI)
    {
        u32Sopt &= ~SIM_SOPT0_NMIE_MASK;        
    }
    if(pConfig->sBits.bDisableRESET)
    {
        u32Sopt &= ~SIM_SOPT0_RSTPE_MASK;        
    }   
    if(pConfig->sBits.bDisableSWD)
    {
        u32Sopt &= ~SIM_SOPT0_SWDE_MASK;        
    } 
    if(pConfig->sBits.bEnableCLKOUT)
    {
        u32Sopt |= SIM_SOPT0_CLKOE_MASK;        
    } 
    if(pConfig->sBits.bETMSYNC)
    {
        u32Sopt |= SIM_SOPT0_ETMSYNC_MASK;        
    }  
    if(pConfig->sBits.bRXDCE)
    {
        u32Sopt |= SIM_SOPT0_RXDCE_MASK;        
    }
    if(pConfig->sBits.bTXDME)
    {
        u32Sopt |= SIM_SOPT0_TXDME_MASK;        
    }
    if(pConfig->sBits.bACTRG)
    {
        u32Sopt |= SIM_SOPT0_ACTRG_MASK;
    }
    u32Sopt |= ((pConfig->u8BusRef & 0x07) << 16);        
    u32Sopt |= ((pConfig->u8Delay) << 24);        
    u32Sopt |= ((pConfig->sBits.u8ADHWT & 0x07) << 20);
    u32Sopt |= ((pConfig->sBits.bRXDFE)&0x03<<8);
    //u32Sopt |= ((pConfig->sBits.bETMIC)&0x03<<6);

    u32PinSel = pConfig->u32PinSel;
    u32Scgc = pConfig->u32SCGC;    
/* write SIM registers */
    SIM->SOPT0 = u32Sopt;
    SIM->PINSEL = u32PinSel;
    SIM->SCGC = u32Scgc;
    SIM->CLKDIV = u32ClockDiv;
}
#endif

/*****************************************************************************//*!
   *
   * @brief set SIM clock gating registers to enable or disable peripheral clocks.
   * 
   * @param[in]  u32PeripheralMask    peripherial bits mask.  
   * @param[in]  u8GateOn             1: ON, 0: OFF.  
   *
   * @return none
   *
   * @ Pass/ Fail criteria: none
   *****************************************************************************/

void SIM_SetClockGating(uint32_t u32PeripheralMask, uint8_t u8GateOn)
{
    uint32_t    u32Scgc;

    /*
     * save original clock gating value
     */
    u32Scgc     = SIM->SCGC;
    
    if(u8GateOn)
    {
        u32Scgc |= u32PeripheralMask;
    }
    else
    {
        u32Scgc &= ~u32PeripheralMask;        
    }

    SIM->SCGC = u32Scgc;
}


/*****************************************************************************//*!
   *
   * @brief read the corresponding status flags.
   * 
   * @param[in]  u32StatusMask    indicates which status to be read. 
   *
   * @return status.
   *
   * @ Pass/ Fail criteria: none
   *****************************************************************************/
uint32_t SIM_GetStatus(uint32_t u32StatusMask)
{
    uint32_t    u32Status;

    u32Status = SIM->SRSID & u32StatusMask;
    return (u32Status);
}

/*****************************************************************************//*!
   *
   * @brief read the corresponding ID.
   * 
   * @param[in]  u8ID    type of ID. 
   *
   * @return ID
   *
   * @ Pass/ Fail criteria: none.
   * @see   IDType.
   *****************************************************************************/
uint8_t SIM_ReadID(IDType sID)
{
    uint32_t    u32ID;
    uint8_t u8IDOffset[4] = 
    { 
        28, 24, 20,16
    };  
    u32ID = (SIM->SRSID >> u8IDOffset[sID]) & 0x0F;
    return (u32ID);
}
/*! @} End of sim_api_list                                                    */