在Net Micro Framework Porting中使用stm32f10x_stdperiph_lib
//=====================================================================
//TITLE:
// 在Net Micro Framework Porting中使用stm32f10x_stdperiph_lib
//AUTHOR:
// norains
//DATE:
// 第一版:Thursday 30-September-2010
// 第二版:Friday 8-October-2010
// 修正STM32F103ZE的STM32F10X_CL的定义为STM32F10X_HD
//Environment:
// KEIL MDK 4.0
// .NET Micro Framework Porting
//=====================================================================
熟悉STM3210x开发的朋友可能都知道,ST为了方便开发者,特意发布了一套名为stm32f10x_stdperiph_lib的驱动库。其实库的内容很简单,只是将常用的寄存器操作封装为函数。但这些看似简单的函数,却大大简化了开发者的工作量。既然我们要将.Net Micro Framework移植到STM32F103ZE上,为什么不让它成为我们的助力呢?
不过,要搭建这个环境虽然并不复杂,但对于初学者来说,还是有一定难度的。所以,我们就一步一步来吧!
1. 首先在./Solutions/[MySolution]/DeviceCode/路径下建立一个libraries文件夹,接着在该文件夹新建一个libraries.proj文件,并输入如下内容:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AssemblyName>STM32F10x_StdPeriph_Driver</AssemblyName>
<ProjectGuid>{BFB710BE-EA70-40a3-AED1-2BF6BA70DB0B}</ProjectGuid>
<Size>
</Size>
<Description>STM32F10x StdPeriph Driver</Description>
<Level>HAL</Level>
<LibraryFile>STM32F10x_StdPeriph_Driver.$(LIB_EXT)</LibraryFile>
<ProjectPath>$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Libraries.proj</ProjectPath>
<ManifestFile>STM32F10x_StdPeriph_Driver.$(LIB_EXT).manifest</ManifestFile>
<Groups>STM32F10x</Groups>
<Documentation>
</Documentation>
<PlatformIndependent>False</PlatformIndependent>
<CustomFilter>
</CustomFilter>
<Required>False</Required>
<IgnoreDefaultLibPath>False</IgnoreDefaultLibPath>
<IsStub>False</IsStub>
<Directory>Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries</Directory>
<PlatformIndependentBuild>false</PlatformIndependentBuild>
<Version>4.0.0.0</Version>
</PropertyGroup>
<Import Project="$(SPOCLIENT)/tools/targets/Microsoft.SPOT.System.Settings" />
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup>
<ARMBUILD_ONLY>true</ARMBUILD_ONLY>
</PropertyGroup>
<ItemGroup>
<IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Configure" />
<IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/inc" />
<IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/core_cm3.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/system_stm32f10x.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/misc.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_adc.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_can.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_crc.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dac.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_dma.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_pwr.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_sdio.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_spi.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c" />
<Compile Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_wwdg.c" />
</ItemGroup>
<ItemGroup />
<Import Project="$(SPOCLIENT)/tools/targets/Microsoft.SPOT.System.Targets" />
</Project>
2. 解压下载回来的stm32f10x_stdperiph_lib.zip文件,并把Libraries文件夹下的CMSIS和STM32F10x_StdPeriph_Driver拷贝到我们新建的Libraries文件夹,也就是:./Solutions /[MySolution]/ DeviceCode/ Libraries。
3. 要使用这个Libraries,还需要定义一个USE_STDPERIPH_DRIVER宏。按理说,在libraries.proj文件中应该能通过如下语句建立相应的C/C++宏:
<ItemGroup>
<CC_CPP_MARCO_FLAGS Include="USE_STDPERIPH_DRIVER" />
<CC_CPP_MARCO_FLAGS Include="STM32F10X_HD" />
</ItemGroup>
但很可惜的是,经过测试,发现这样的定义无法生效。不知道是不是我的语法有问题,如果大家知道真正的方法,麻烦告诉norains,在此先谢过了。
既然无法在Libraries.proj中动手,就只能手动在.h文件中进行折腾了了。打开./Solutions/[MySolution]/DeviceCode/Libraries/CMSIS/Core/CM3/stm32f10x.H文件,并且将注释掉的USE_STDPERIPH_DRIVER宏恢复,如:
当USE_STDPERIPH_DRIVER宏被启用后,如果没有定义相应的STM32F10X的类型,那么默认就会使用STM32F10X_CL,然而STM32F103ZE并不具备互联网功能,所以需要更改相应的宏定义,如下所示:
4 当定义了USE_STDPERIPH_DRIVER宏后,还需要包含一个stm32f10x_conf.h头文件。因为这个是和平台有关的,所以我们独立给它分配个Configure文件夹,也就是位于./Solutions/[MySolution]/ DeviceCode/Libraries/Configure目录。其实这个头文件在下载的stm32f10x_stdperiph_lib包里面有,如果你觉得麻烦,你完全可以自己建立并输入如下内容:
5. 如果我们要在NativeSample工程用上STM32F10x_StdPeriph_Lib的代码,还需要对NativeSample.proj文件进行一些修改。打开NativeSample.proj文件,增加如下语句:
<ItemGroup>
<DriverLibs Include="STM32F10x_StdPeriph_Driver.$(LIB_EXT)" />
<RequiredProjects Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Libraries.proj" />
</ItemGroup>
<ItemGroup>
<IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/Configure" />
<IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/STM32F10x_StdPeriph_Driver/inc" />
<IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/Libraries/CMSIS/Core/CM3/" />
</ItemGroup>
6. 当顺利完成如上步骤之后,就可以来实际试试这个STM32F10x_StdPeriph_Lib了。如果大家比较关心.Net Micro Framework Porting,可能对叶帆的这篇文章《【.Net Micro Framework PortingKit - 03】调试初步:点亮LED灯》(http://blog.csdn.net/yefanqiu/archive/2010/01/02/5121851.aspx)记忆犹新。只不过在那篇文章中是自己定义类型,然后再对寄存器进行操作。在这里我们简化操作,直接采用STM32F10x_StdPeriph_Lib定义的函数,来对LED进行操控。
NativeSample.cpp的完整代码如下:
相对而言,是不是觉得简单很多呢?