GPIO驱动初步

来源:互联网 发布:程序员专用 屏幕 编辑:程序博客网 时间:2024/06/05 20:03

//=====================================================================
//TITLE:
//    GPIO驱动初步
//AUTHOR:
//    norains
//DATE:
//    Saturday  9-October-2010
//Environment:
//    KEIL MDK 4.0
//    .NET Micro Framework Porting 4.1
//    RedCow Board
//=====================================================================

 

    .NET Micro Framework Porting下所谓的驱动,其实际只不过是实现微软所留出来的接口函数。比如,以GPIO为例子,如果你想在.NET Micro Framework正常驱动GPIO,那么作为开发者的你,必须要实现如下函数:

 

    这些函数在.NET Micro Framework Porting只有声明,没有实现,因为实现是我们所需要做的。这样的开发模式,和Windows CE下的驱动有异曲同工之妙,基本上就类似于试卷上的填空题。

 

    为什么文章起名为“初步”呢?因为本文的内容,只实现了GPIO的一些基础功能,比如初始化,输出高低电平,读取输入的电平等等。虽然看起来内容不少,但有一个非常重要的功能还没有实现,就是输入中断的处理。具体来说,也就是CPU_GPIO_EnableInputPin和CPU_GPIO_EnableInputPin2并没有完全实现微软规定的功能。不过呢,事情总是从简单到复杂的,我们不妨先来看看雏形吧,至于更完整的,待我们后续再语。如果你是急性子,迫不及待想一窥全貌,那么建议你看看叶帆的《【.Net Micro Framework PortingKit - 08】GPIO驱动》(http://blog.csdn.net/yefanqiu/archive/2010/01/20/5218846.aspx),该文已经有完整的解决思路。如果你不急,那么我们现在就一步一步来看看雏形的实现吧!

 


    1.如果你的目录没有STUB_GPIO的话,那么将./DeviceCode/Drivers/Stubs/Processor/stubs_gpio文件夹拷贝到. /Solutions/STM32F103ZE_RedCow/DeviceCode,并按微软的惯例,将这拷贝完成的文件夹改名为GPIO_HAL。

 


    2.打开拷贝过来的dotNetMF.proj文件,将内容更改如下:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AssemblyName>GPIO_HAL_STM32F103ZE_RedCow</AssemblyName>
    <ProjectGuid>{9fe65202-d536-4d38-a2bb-5948f020bf67}</ProjectGuid>
    <Size>
    </Size>
    <Description>GPIO stub library</Description>
    <Level>HAL</Level>
    <LibraryFile>GPIO_HAL_STM32F103ZE_RedCow.$(LIB_EXT)</LibraryFile>
    <ProjectPath>$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/GPIO_HAL/dotNetMF.proj</ProjectPath>
    <ManifestFile>GPIO_HAL_STM32F103ZE_RedCow.$(LIB_EXT).manifest</ManifestFile>
    <Groups>Processor/stubs</Groups>
    <LibraryCategory>
      <MFComponent xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="GPIO_HAL" Guid="{33AB74F7-8DD4-4766-95A8-E221B80F611C}" ProjectPath="" Conditional="" xmlns="">
        <VersionDependency xmlns="
http://schemas.microsoft.com/netmf/InventoryFormat.xsd">
          <Major>4</Major>
          <Minor>0</Minor>
          <Revision>0</Revision>
          <Build>0</Build>
          <Extra />
          <Date>2009-04-30</Date>
        </VersionDependency>
        <ComponentType xmlns="
http://schemas.microsoft.com/netmf/InventoryFormat.xsd">LibraryCategory</ComponentType>
      </MFComponent>
    </LibraryCategory>
    <Documentation>
    </Documentation>
    <PlatformIndependent>False</PlatformIndependent>
    <CustomFilter>
    </CustomFilter>
    <Required>False</Required>
    <IgnoreDefaultLibPath>False</IgnoreDefaultLibPath>
    <IsStub>True</IsStub>
    <Directory>Solutions/STM32F103ZE_RedCow/DeviceCode/GPIO_HAL</Directory>
    <OutputType>Library</OutputType>
    <PlatformIndependentBuild>false</PlatformIndependentBuild>
    <Version>4.0.0.0</Version>
  </PropertyGroup>
  <Import Project="$(SPOCLIENT)/tools/targets/Microsoft.SPOT.System.Settings" />
  <PropertyGroup />
  <ItemGroup>
    <Compile Include="stubs_functions_gpio.cpp" />
    <Compile Include="GPIO_Adapter.cpp" />
  </ItemGroup>
  <ItemGroup />
 <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>
  <Import Project="$(SPOCLIENT)/tools/targets/Microsoft.SPOT.System.Targets" />
</Project>

 

    3.打开拷贝过来的stubs_functions_gpio.cpp文件,输入如下内容:

 


    4. stubs_functions_gpio.cpp用到了GPIO_Adapter文件,该文件是作为.NET Micro Framework Porting和stm32f10x_stdperiph_lib连接的桥梁,其具体实现如下:

 

     头文件GPIO_Adapter.h:

 


     实现文件GPIO_Adapter.cpp:

 


    5.为了测试这个GPIO,我们还需要对NativeSample.proj进行设置。

 

    原语句:
<ItemGroup>
<DriverLibs Include="cpu_gpio_stubs.$(LIB_EXT)" />
<RequiredProjects Include="$(SPOCLIENT)/DeviceCode/Drivers/stubs/processor/stubs_GPIO/dotNetMF.proj" />
  </ItemGroup>

 

    更改为:
<ItemGroup>
    <DriverLibs Include="GPIO_HAL_STM32F103ZE_RedCow.$(LIB_EXT)" />
<RequiredProjects Include="$(SPOCLIENT)/Solutions/STM32F103ZE_RedCow/DeviceCode/GPIO_HAL/dotNetMF.proj" />
</ItemGroup>

 

    在</Project>之前增加如下语句:
  <ItemGroup>  
   <IncludePaths Include="Solutions/STM32F103ZE_RedCow/DeviceCode/GPIO_HAL" />
  </ItemGroup>
 


     6.万事俱备,只欠东风。我们现在要做的,就是修改NativeSample.cpp代码,让程序跑起来的时候,LED1不停地闪烁,而LED2只有在USER1按钮按下时,才会发亮。具体的代码如下:

 

原创粉丝点击