天天看點

【.Net Micro Framework PortingKit - 06】設定晶片時鐘

   1、建立CortexM3.h頭檔案

   在./DeviceCode/Targets/Native/CortexM3目錄中建立 CortexM3.h檔案,并編寫如下代碼:

#ifndef _CORTEXM3_H_

#define _CORTEXM3_H_1

#include <cores/arm/include/cpu.h>

typedef volatile unsigned long  VU32;

typedef volatile unsigned short VU16;

typedef volatile unsigned char  VU8;

extern "C"

{

    void BootstrapCode_Clocks();

}

/*------------------------ Reset and Clock Control ---------------------------*/

struct CortexM3_RCC

    static const UINT32 c_Base = 0x40021000;

    static const    UINT8 FLAG_HSIRDY = ((UINT8)0x20);

    static const    UINT8 FLAG_HSERDY = ((UINT8)0x31);

    /****/ volatile UINT32  CR;

    static const    UINT32  CR_HSEBYP_Reset = ((UINT32)0xFFFBFFFF);

    static const    UINT32  CR_HSEBYP_Set = ((UINT32)0x00040000);

    static const    UINT32  CR_HSEON_Reset = ((UINT32)0xFFFEFFFF);

    static const    UINT32  CR_HSEON_Set = ((UINT32)0x00010000);

    static const    UINT32  CR_HSITRIM_Mask = ((UINT32)0xFFFFFF07);

    /****/ volatile UINT32  CFGR;

    static const    UINT32  CFGR_SYSCLK_Div1 = ((UINT32)0x00000000);

    static const    UINT32  CFGR_SYSCLK_Div2 = ((UINT32)0x00000080);

    static const    UINT32  CFGR_SYSCLK_Div4 = ((UINT32)0x00000090);

    static const    UINT32  CFGR_SYSCLK_Div8 = ((UINT32)0x000000A0);

    static const    UINT32  CFGR_SYSCLK_Div16 = ((UINT32)0x000000B0);

    static const    UINT32  CFGR_HCLK_Div1 = ((UINT32)0x00000000);

    static const    UINT32  CFGR_HCLK_Div2 = ((UINT32)0x00000400);

    static const    UINT32  CFGR_HCLK_Div4 = ((UINT32)0x00000500);

    // 省略部分代碼 .....

    /****/ volatile UINT32  APB1RSTR;

    /****/ volatile UINT32  AHBENR;

    /****/ volatile UINT32  BDCR;

    /****/ volatile UINT32  CSR;

    static void Initialize();

    static bool GetFlagStatus(UINT8 Flag);

};

struct CortexM3

    static CortexM3_RCC  & RCC()   { return *(CortexM3_RCC *)(size_t)(CortexM3_RCC::c_Base); }

#endif // _CORTEXM3_H_1

   2、編寫BootStrap代碼

   我們從./DeviceCode/Drivers/Stubs/Processor/stubs_bootstrap目錄到./DeviceCode/Targets/Native/CortexM3/DeviceCode,并修改目錄的名字為BootStrap,下一步我們在BootStrap.cpp檔案編寫如下代碼:

   #include <tinyhal.h>

#include "../CortexM3.h"

//--//

#pragma arm section code = "SectionForBootstrapOperations"

void __section(SectionForBootstrapOperations) CortexM3_RCC::Initialize (void)

         CortexM3_RCC &RCC = CortexM3::RCC();

        // RCC system reset(for debug purpose)

         /* Set HSION bit */

         RCC.CR |= (UINT32)0x00000001;

         /* Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], ADCPRE[1:0] and MCO[2:0] bits */

         RCC.CFGR &= (UINT32)0xF8FF0000;

         /* Reset HSEON, CSSON and PLLON bits */

         RCC.CR &= (UINT32)0xFEF6FFFF;

         // 省略部分代碼 .....

         /* Enable GPIOA, GPIOB, GPIOF, GPIOG and AFIO clocks */

         RCC.APB2ENR |= CortexM3_RCC::APB2RSTR_GPIOA | CortexM3_RCC::APB2RSTR_GPIOB | CortexM3_RCC::APB2RSTR_GPIOF |CortexM3_RCC::APB2RSTR_GPIOG |CortexM3_RCC::APB2RSTR_AFIO;

bool __section(SectionForBootstrapOperations)  CortexM3_RCC::GetFlagStatus(UINT8 Flag)

         UINT32 tmp = 0;

         UINT32 statusreg = 0;

         /* Get the RCC register index */

         tmp = Flag >> 5;

         if (tmp == 1)               /* The flag to check is in CR register */

         {

                   statusreg = RCC.CR;

         }

         else if (tmp == 2)          /* The flag to check is in BDCR register */

                   statusreg = RCC.BDCR;

         else                       /* The flag to check is in CSR register */

                   statusreg = RCC.CSR;

         /* Get the flag position */

         tmp = Flag & ((UINT8)0x1F);

         return ((statusreg & ((UINT32)1 << tmp)) != 0);

void __section(SectionForBootstrapOperations) BootstrapCode_Clocks()

     CortexM3_RCC::Initialize();

void __section(SectionForBootstrapOperations) BootstrapCode()

          BootstrapCode_Clocks();

#pragma arm section code

  3、修改./Solutions/STM3210E/NativeSample/NativeSample.proj檔案

   在NativeSample.proj檔案中作如下修改: 

  <ItemGroup>

    <RequiredProjects Include="$(SPOCLIENT)/DeviceCode/Targets/Native/CortexM3/DeviceCode/stubs_bootstrap/dotNetMF.proj" />

    <DriverLibs Include="cpu_bootstrap_stubs.$(LIB_EXT)" />

  </ItemGroup>

  修改為:

    <RequiredProjects Include="$(SPOCLIENT)/DeviceCode/Targets/Native/CortexM3/DeviceCode/BootStrap/dotNetMF.proj" />

    <DriverLibs Include="BootStrap.$(LIB_EXT)" />

  4、編譯修改并調試運作,我想這次會有很大的成就感,因為LED閃爍的節奏明顯的加快了,我們的CPU在高速運作了!

       即将過去的2010年的前三天,對我來說是辛苦的三天,平均每天工作16個小時以上,不僅編寫了相關代碼,還寫了以上六篇文章。希望我的這些努力能點燃.Net Micro Framework愛好者心中的熱情,動起手來一起移植.Net Micro Framework,其實這對自己的嵌入式開發功力的提高也大有裨益。明天就要上班了,我将又回到開發Wifi相關接口的工作上來,後續的文章我想隻有到下周末才能相見了,到那時我們将編寫序列槽驅動,系統的一些調試資訊将可以通過序列槽傳輸給PC機上的序列槽調試程式,這一步将是關鍵的一步,非常值得的期待,希望我能順利完成這步工作!

繼續閱讀