【.Net Micro Framework PortingKit - 03】调试初步:点亮LED灯

在上一篇文章《STM3210E平台构建》中,我们已经构建好了STM3210的基本平台,并且已经编译通过,所以我们下一步就要考虑用MDK进行下载调试了。

由于我们用到了片外SRAM,所以我们还得写一个MDK 脚本,实现三点功能,一是SRAM初始化,二是下载镜像文件,三是设置PC指针。

EM-STM3210E随机光盘中有一些开发板的测试工程,所以我们没必要先查看手册,我们先在代码中看看有没有收获,在测试工程的启动代码stm32f10x_vector.s中,我们可以找到了片外SRAM初始化代码,具体内容如下:

; FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is

; required, then adjust the Register Addresses

 

; Enable FSMC clock

                                            LDR R0,= 0x00000114

                                            LDR R1,= 0x40021014

                                            STR R0,[R1]                    

            

; Enable GPIOD, GPIOE, GPIOF and GPIOG clocks

                                            LDR R0,= 0x000001E0

                                            LDR R1,= 0x40021018

                                            STR R0,[R1]      

 

; SRAM Data lines, NOE and NWE configuration

; SRAM Address lines configuration

; NOE and NWE configuration  

; NE3 configuration

; NBL0, NBL1 configuration

 

                                            LDR R0,= 0x44BB44BB

                                            LDR R1,= 0x40011400

                                            STR R0,[R1]            

                                    

                                            LDR R0,= 0xBBBBBBBB

                                            LDR R1,= 0x40011404

                                            STR R0,[R1]            

                                    

                                            LDR R0,= 0xB44444BB

                                            LDR R1,= 0x40011800

                                            STR R0,[R1]            

                                    

                                            LDR R0,= 0xBBBBBBBB

                                            LDR R1,= 0x40011804

                                            STR R0,[R1]            

                                    

                                            LDR R0,= 0x44BBBBBB

                                            LDR R1,= 0x40011C00

                                            STR R0,[R1]       

 

                                            LDR R0,= 0xBBBB4444

                                            LDR R1,= 0x40011C04

                                            STR R0,[R1]       

 

                                            LDR R0,= 0x44BBBBBB

                                            LDR R1,= 0x40012000

                                            STR R0,[R1]            

 

                                            LDR R0,= 0x44444B44

                                            LDR R1,= 0x40012004

                                            STR R0,[R1]     

                                           

; FSMC Configuration  

; Enable FSMC Bank1_SRAM Bank

 

                                           ;LDR R0,= 0x00001011

                                           ;LDR R1,= 0xA0000010

                                           ;STR R0,[R1]    

 

                                           ;LDR R0,= 0x00000200

                                           ;LDR R1,= 0xA0000014

                                           ;STR R0,[R1]

                                                     

                                            LDR R0,= 0x00001000

                                           LDR R1,= 0xA0000010

                                           STR R0,[R1]     

 

                                           LDR R0,= 0x00000200

                                           LDR R1,= 0xA0000014

                                           STR R0,[R1]     

                                                     

                                            LDR R0,= 0x0FFFFFFF

                                           LDR R1,= 0xA0000114

                                           STR R0,[R1]     

                                                     

                                            LDR R0,= 0x00001001

                                           LDR R1,= 0xA0000010

                                           STR R0,[R1]       

 

好了,我们以此为样本,编写我们的脚本文件Config.ini,最终的内容如下:

//初始化SRAM3 0x68000000 - 0x68020000   

FUNC void InitSRAM3(void)

{

   //使能FSMC时钟

   _WDWORD(0x40021014,0x00000114);

   //使能FSMC相关的GPIO的时钟

   _WDWORD(0x40021018,0x000001E0);

 

   //配置SRAM数据线,NOENWE配置

   //SRAM地址线配置

   //NOENWE配置

   //NE3配置

   //NBL0,NBL1配置

   _WDWORD(0x40011400,0x44BB44BB);

   _WDWORD(0x40011404,0xBBBBBBBB);

   _WDWORD(0x40011800,0xB44444BB);

   _WDWORD(0x40011804,0xBBBBBBBB);  

   _WDWORD(0x40011C00,0x44BBBBBB);

   _WDWORD(0x40011C04,0xBBBB4444);

   _WDWORD(0x40012000,0x44BBBBBB);

   _WDWORD(0x40012004,0x44444B44);

 

   //FSMC配置

   //使能 FSMC Bank1_SRAM Bank

   _WDWORD(0xA0000010,0x00001000);

   _WDWORD(0xA0000014,0x00000200);

   _WDWORD(0xA0000114,0x0FFFFFFF);

   _WDWORD(0xA0000010,0x00001001);

}

 

InitSRAM3();

exec("LOAD C:\\MicroFramework_CortexM3\\BuildOutput\\THUMB2\\MDK3.1\\le\\RAM\\debug\\STM3210E\\bin\\NativeSample.axf INCREMENTAL");

PC = 0x20001000;

 

先不要忙下载,我们还要编写一个LED灯闪烁的代码。

我们在.\Solutions\STM3210E\NativeSample\NativeSample.cpp文件中添加如下代码:

typedef volatile unsigned long vu32;

typedef unsigned long u32;

typedef unsigned short u16;

typedef struct

{

 vu32 CRL;

 vu32 CRH;

 vu32 IDR;

 vu32 ODR;

 vu32 BSRR;

 vu32 BRR;

 vu32 LCKR;

} GPIO_TypeDef;

 

#define PERIPH_BASE           ((u32)0x40000000)

#define APB2PERIPH_BASE       (PERIPH_BASE + 0x10000)

#define GPIOF_BASE            (APB2PERIPH_BASE + 0x1C00)

#define GPIOF                 ((GPIO_TypeDef *) GPIOF_BASE)

#define GPIO_Pin_6                 ((u16)0x0040)

#define GPIO_Pin_7                 ((u16)0x0080)

 

void ApplicationEntryPoint()

{

     //..................

  

    //初始化

    u32 GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;

    u32 pinpos = 0x00,pos = 0x00,pinmask = 0x00;

    u32 tmpreg = GPIOF->CRL;

    for (int pinpos = 0x00; pinpos < 0x08; pinpos++)

    {

      pos = ((u32)0x01) << pinpos;

      if ((GPIO_Pin & pos) == pos)

      {

        pos = pinpos << 2;

        //Clear the corresponding low control register bits

        pinmask = ((u32)0x0F) << pos;

        tmpreg &= ~pinmask;    

                //Write the mode configuration in the corresponding bits

                tmpreg |= ((u32)0x3) << pos;

      }

    }

    GPIOF->CRL = tmpreg;

 

    //设置

    while(TRUE)

    {

       GPIOF->BSRR = GPIO_Pin_6;

          GPIOF->BSRR = GPIO_Pin_7;

       for(long i=0;i<1000000;i++);

       GPIOF->BRR = GPIO_Pin_6;

          GPIOF->BRR = GPIO_Pin_7;

          for(long i=0;i<1000000;i++);

    }

}

好了,在MDK中新建一个基于Cortex-M3的工程,并选择我们编写好ini文件,然后调试开始(MDK 详细的调试方式,请参见相关文档,由于MDK本地化很出色,所以很容易上手)。下面是调试过程中的MDK界面:

【.Net Micro Framework PortingKit - 03】调试初步:点亮LED灯

然后在看一下EM-STM3210E开发板,哈!你会发现D1D2 LED灯正在闪烁。

不过不要高兴太早,我们现在完成的工作,恰是万里长征的第一步,更烦琐复杂的工作在后面呢。因为我们选用的是Cortex-M3架构的芯片,目前.Net Micro Framework并不支持。关键问题在于Cortex-M3的中断架构和ARM7ARM9有了很大的不同,并且中断向量表的位置也不一定放在0地址起始处,而是通过NVIC来进行配置,并且该开发板内存较小,所以相关代码要放到Nor Flash中进行启动,这和.Net Micro Framework惯常的方式不同,所以我们需要自行重建和修改很多代码,方能使我们的开发板正常工作(不过.Net Micro Framework的初学者也不要为此过多的担忧,如果你选用的是基于ARM7ARM9架构的芯片,那么你的工作就会容易的多)。

下面我们要做的工作依次为:编写中断向量表、修改启动代码、调整和原先中断相关的代码、时钟初始化、NVIC初始化、FMSC初始化、SRAM初始化、串口初始化、IO驱动开发、Nand Flash驱动开发、I2C驱动开发、SPI驱动开发、LED驱动开发、LCD驱动开发… …

路漫漫其修远兮,吾将上下而求索 -- 其实嵌入式开发是一个充满挑战和乐趣的事!

你可能感兴趣的:(framework)