專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

TMS320F2812外設(shè)例程系列之SysCtrl

作者:佚名   來(lái)源:不詳   點(diǎn)擊數(shù):  更新時(shí)間:2014年08月17日   【字體:

 

 
//#####################################################################
//文件:  DSP281x_SysCtrl.c
//說(shuō)明:  初始化系統(tǒng)控制
//#####################################################################
#include "DSP281x_Device.h"     
#include "DSP281x_Examples.h"  
#pragma CODE_SECTION(InitFlash, "ramfuncs");  //將InitFlash函數(shù)存在ramfuncs中
//InitSysCtrl:初始化系統(tǒng)控制
void InitSysCtrl(void)
{
   DisableDog();               //關(guān)看門狗
   InitPll(0xA);               //初始化PLL,用戶要根據(jù)外部晶振的大小來(lái)改變實(shí)參
   InitPeripheralClocks();   //初始化外設(shè)時(shí)鐘
}
//喂狗子程序,如果用戶使用看門狗,就可以調(diào)用該子程序
void KickDog(void)
{
EALLOW;
    SysCtrlRegs.WDKEY = 0x0055;
    SysCtrlRegs.WDKEY = 0x00AA;
EDIS;
}
//關(guān)閉看門狗子程序,在初始系統(tǒng)時(shí),或者用戶不想使用看門狗,可以調(diào)用該程序
void DisableDog(void)
{
    EALLOW;
    SysCtrlRegs.WDCR= 0x0068;
    EDIS;
}
//初始化PLL
void InitPll(Uint16 val)
{
   volatile Uint16 iVol;
   if (SysCtrlRegs.PLLCR.bit.DIV != val)
   {
      EALLOW;
      SysCtrlRegs.PLLCR.bit.DIV = val;
      EDIS;
      DisableDog();                   //確保在進(jìn)入下面循環(huán)前關(guān)閉看門狗
      for(iVol= 0; iVol< ( (131072/2)/12 ); iVol++)
      {
      }                             //等待周期
   }
}
//初始化外設(shè)時(shí)鐘
void InitPeripheralClocks(void)
{
EALLOW;
   SysCtrlRegs.HISPCP.all = 0x0001;     //設(shè)定高速外設(shè)時(shí)鐘預(yù)分頻值
   SysCtrlRegs.LOSPCP.all = 0x0002;     //設(shè)定低速外設(shè)時(shí)鐘預(yù)分頻值 
   //對(duì)選定的外設(shè)進(jìn)行外設(shè)時(shí)鐘使能設(shè)置
   SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;
   SysCtrlRegs.PCLKCR.bit.EVBENCLK=1;
   SysCtrlRegs.PCLKCR.bit.SCIAENCLK=1;
   SysCtrlRegs.PCLKCR.bit.SCIBENCLK=1;
   SysCtrlRegs.PCLKCR.bit.MCBSPENCLK=1;
   SysCtrlRegs.PCLKCR.bit.SPIENCLK=1;
   SysCtrlRegs.PCLKCR.bit.ECANENCLK=1;
   SysCtrlRegs.PCLKCR.bit.ADCENCLK=1;
EDIS;
}
//=====================================================================
//本文件內(nèi)還有一些與flash和CSM相關(guān)的子程序,不予列舉,有興趣的讀者可以參考TI
//文獻(xiàn)sprc097(可以到www.ti.com免費(fèi)下載)。
//=====================================================================
 
 
//---------------------------------------------------------------------------
// Example: InitFlash:
//---------------------------------------------------------------------------
// This function initializes the Flash Control registers
 
//                   CAUTION
// This function MUST be executed out of RAM. Executing it
// out of OTP/Flash will yield unpredictable results
 
void InitFlash(void)
{
   EALLOW;
   //Enable Flash Pipeline mode to improve performance
   //of code executed from Flash.
   FlashRegs.FOPT.bit.ENPIPE = 1;
 
   //                CAUTION
   //Minimum waitstates required for the flash operating
   //at a given CPU rate must be characterized by TI.
   //Refer to the datasheet for the latest information.
 
   //Set the Random Waitstate for the Flash
   FlashRegs.FBANKWAIT.bit.RANDWAIT = 5;
 
   //Set the Paged Waitstate for the Flash
   FlashRegs.FBANKWAIT.bit.PAGEWAIT = 5;
 
   //                CAUTION
   //Minimum cycles required to move between power states
   //at a given CPU rate must be characterized by TI.
   //Refer to the datasheet for the latest information.
 
   //For now use the default count
   //Set number of cycles to transition from sleep to standby
   FlashRegs.FSTDBYWAIT.bit.STDBYWAIT = 0x01FF;
 
   //Set number of cycles to transition from standby to active
   FlashRegs.FACTIVEWAIT.bit.ACTIVEWAIT = 0x01FF;
   EDIS;
 
   //Force a pipeline flush to ensure that the write to
   //the last register configured occurs before returning.
 
   asm(" RPT #7 || NOP");
}
 
//---------------------------------------------------------------------------
// Example: CsmUnlock:
//---------------------------------------------------------------------------
// This function unlocks the CSM. User must replace 0xFFFF's with current
// password for the DSP. Returns 1 if unlock is successful.
#define STATUS_FAIL          0
#define STATUS_SUCCESS       1
 
Uint16 CsmUnlock()
{
    volatile Uint16 temp;
 
    // Load the key registers with the current password. The 0xFFFF's are dummy
// passwords.  User should replace them with the correct password for the DSP.
 
    EALLOW;
    CsmRegs.KEY0 = 0xFFFF;
    CsmRegs.KEY1 = 0xFFFF;
    CsmRegs.KEY2 = 0xFFFF;
    CsmRegs.KEY3 = 0xFFFF;
    CsmRegs.KEY4 = 0xFFFF;
    CsmRegs.KEY5 = 0xFFFF;
    CsmRegs.KEY6 = 0xFFFF;
    CsmRegs.KEY7 = 0xFFFF;
    EDIS;
 
    // Perform a dummy read of the password locations
    // if they match the key values, the CSM will unlock
 
    temp = CsmPwl.PSWD0;
    temp = CsmPwl.PSWD1;
    temp = CsmPwl.PSWD2;
    temp = CsmPwl.PSWD3;
    temp = CsmPwl.PSWD4;
    temp = CsmPwl.PSWD5;
    temp = CsmPwl.PSWD6;
    temp = CsmPwl.PSWD7;
 
    // If the CSM unlocked, return succes, otherwise return
    // failure.
    if (CsmRegs.CSMSCR.bit.SECURE == 0) return STATUS_SUCCESS;
    else return STATUS_FAIL;
 
}
 
//===========================================================================
// No more.
//===========================================================================
 
關(guān)閉窗口