上两篇《修改启动代码&重写向量表》《SRAM初始化&设置NVIC中断表偏移》文章中,我们设置了中断向量表,初始化了RAM,并重设了向量表的地址,本篇文章是相对重要的一篇,我们将设置芯片时钟。
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;
CortexM3_RCC &RCC = CortexM3::RCC();
/* 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();
}
extern "C"
{
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>
修改为:
<ItemGroup>
<RequiredProjects Include="$(SPOCLIENT)\DeviceCode\Targets\Native\CortexM3\DeviceCode\BootStrap\dotNetMF.proj" />
<DriverLibs Include="BootStrap.$(LIB_EXT)" />
</ItemGroup>
4、编译修改并调试运行,我想这次会有很大的成就感,因为LED闪烁的节奏明显的加快了,我们的CPU在高速运行了!
即将过去的2010年的前三天,对我来说是辛苦的三天,平均每天工作16个小时以上,不仅编写了相关代码,还写了以上六篇文章。希望我的这些努力能点燃.Net Micro Framework爱好者心中的热情,动起手来一起移植.Net Micro Framework,其实这对自己的嵌入式开发功力的提高也大有裨益。明天就要上班了,我将又回到开发Wifi相关接口的工作上来,后续的文章我想只有到下周末才能相见了,到那时我们将编写串口驱动,系统的一些调试信息将可以通过串口传输给PC机上的串口调试程序,这一步将是关键的一步,非常值得的期待,希望我能顺利完成这步工作!