•  创建平台目录

每个平台都需要自己的文件夹存放相关驱动代码,位于tos/platforms/目录中。

  $cd /opt/tinyos-2.1.2/tos/platforms

  $mkdir yamp

  • 创建.platform文件

每个平台目录都必须包含名为 的文件,包含该平台的基本编译参数。

tos/platforms/yamp/.platform :

  1. # Includes that should take precedence come first.  Platforms come before 
  2. # chips because they may override files.  These must be specified as 
  3. # @includes instead of -I's to @opts, otherwise the %T won't be processed 
  4. by ncc. 
  5.  
  6. push( @includes, qw( 
  7.  
  8.   %T/chips/cc2420 
  9.   %T/chips/msp430 
  10.   %T/chips/msp430/adc12 
  11.   %T/chips/msp430/dma 
  12.   %T/chips/msp430/pins 
  13.   %T/chips/msp430/timer 
  14.   %T/chips/msp430/usart 
  15.   %T/chips/msp430/sensors 
  16.   %T/lib/timer 
  17.   %T/lib/serial 
  18.   %T/lib/power 
  19. ) ); 
  20.  
  21. @opts = qw( 
  22.  
  23.   -gcc=msp430-gcc 
  24.   -mmcu=msp430f1611 
  25.   -fnesc-target=msp430 
  26.   -fnesc-no-debug 
  27.   -fnesc-scheduler=TinySchedulerC,TinySchedulerC.TaskBasic,TaskBasic,TaskBasic,runTask,postTask 
  28.  
  29. ); 
  • 创建hardware.h文件

当编译基于该平台的应用程序时,编译器会默认地包含该文件。文件中可以定义相关平台的常量、引脚名称或者包含一些头文件。

tos/platforms/yamp/hardware.h : 

  1. #ifndef _H_hardware_h 
  2. #define _H_hardware_h 
  3.  
  4. #include "msp430hardware.h" 
  5.  
  6. // LEDs 
  7. TOSH_ASSIGN_PIN(RED_LED, 5, 4); 
  8. TOSH_ASSIGN_PIN(GREEN_LED, 5, 5); 
  9. TOSH_ASSIGN_PIN(YELLOW_LED, 5, 6); 
  10.  
  11. // UART pins 
  12. TOSH_ASSIGN_PIN(SOMI0, 3, 2); 
  13. TOSH_ASSIGN_PIN(SIMO0, 3, 1); 
  14. TOSH_ASSIGN_PIN(UCLK0, 3, 3); 
  15. TOSH_ASSIGN_PIN(UTXD0, 3, 4); 
  16. TOSH_ASSIGN_PIN(URXD0, 3, 5); 
  17. TOSH_ASSIGN_PIN(UTXD1, 3, 6); 
  18. TOSH_ASSIGN_PIN(URXD1, 3, 7); 
  19. TOSH_ASSIGN_PIN(UCLK1, 5, 3); 
  20. TOSH_ASSIGN_PIN(SOMI1, 5, 2); 
  21. TOSH_ASSIGN_PIN(SIMO1, 5, 1); 
  22.  
  23.  
  24. #endif // _H_hardware_h 

引入 msp430hardware.h 头文件实现一些微处理器的重要功能,如休眠、atomic代码块的中断禁用;宏指令重新定义管脚名称。 TOSH_ASSIGN_PIN(RED_LED, 5, 4);   RED_LED (红色LED灯)被绑定到5.4号通道I/O口。

  •  创建platform.h文件

  $cd /opt/tinyos-2.1.2/tos/platforms/yamp

  $touch platform.h

每个平台必须有一个名为“platform.h”的文件,即使内容为空。

 

此时进入app/Null,编译环境:

error显示系统无法识别yamp平台。

  • 创建target文件

support/make/yamp.target :

  1. PLATFORM = yamp 
  2.  
  3. $(call TOSMake_include_platform,msp) 
  4.  
  5. yamp: $(BUILD_DEPS) 
  6.     @: 

再次测试编译环境:

系统已经识别yamp平台,但是找不到PlatformC组件。在系统启动过程中,特定的硬件的初始化直接由每个平台的PlatformC组件处理,提供Init接口。所以每个平台必须定义该组件。

  • 创建平台必备组件

tos/platforms/yamp/PlatformP.nc : 

  1. /*$ID:PlatformP.nc, v 1.0, 2013-3-29  
  2.  * 
  3.  *The yamp portion which is an imaginary mote is mote-specific.It has a MSP430 
  4.  * microcontroller and a CC2420 radio transceiver. 
  5.  * 
  6.  *@author Nick 
  7.  */ 
  8.  
  9. #include "hardware.h" 
  10.  
  11. module PlatformP{ 
  12.   provides interface Init; 
  13.   uses interface Init as Msp430ClockInit; 
  14.   uses interface Init as LedsInit; 
  15. implementation { 
  16.   command error_t Init.init() { 
  17.     call Msp430ClockInit.init(); 
  18.     call LedsInit.init(); 
  19.     return SUCCESS; 
  20.   } 
  21.   
  22.   default command error_t LedsInit.init() {  
  23.     return SUCCESS; 
  24.   } 
  25.  

tos/platforms/yamp/PlatformC.nc : 

  1. /*$ID:PlatformC.nc, v 1.0, 2013-3-29  
  2.  * 
  3.  *The yamp portion which is an imaginary mote is mote-specific. 
  4.  * 
  5.  *@author Nick 
  6.  */ 
  7. #include "hardware.h" 
  8.  
  9. configuration PlatformC 
  10.   provides interface Init; 
  11. implementation 
  12.   components PlatformP,Msp430ClockC; 
  13.   Init = PlatformP; 
  14.   PlatformP.Msp430ClockInit -> Msp430ClockC.Init; 

再次测试编译环境:

用yamp平台测试Blink程序

error显示在编译LedC组件时无法找到PlatformLedsC组件。PlatformLedsC组件时平台相关组件,通常一个LED灯连接到微处理器一个

I/O引脚,由引脚的输出电平觉得点亮和关闭,在不同的硬件平台上,每个LED灯连接到微处理器的引脚是不同的,所以LED得开关必须根据具体

的硬件平台实现。

  • 添加PlatformLedsC.nc文件

tos/platforms/yamp/PlatformLedsC.nc :

  1. /*$ID:PlatformLedsC.nc, v 1.0, 2013-3-29  
  2.  * 
  3.  *"PlatformLedsC" is a platform-specific component.Thus we need to define this  
  4.  * component for the yamp platform.The PlatformLedsC configuration provides  
  5.  * three GeneralIO interfaces and an Init. 
  6.  * 
  7.  *@author Nick 
  8.  */ 
  9.  
  10. #include "hardware.h" 
  11.  
  12. configuration PlatformLedsC { 
  13.   provides interface GeneralIO as Led0; 
  14.   provides interface GeneralIO as Led1; 
  15.   provides interface GeneralIO as Led2; 
  16.   uses interface Init; 
  17. implementation 
  18.   components  
  19.   HplMsp430GeneralIOC as GeneralIOC, 
  20.   new Msp430GpioC() as Led0Impl, 
  21.   new Msp430GpioC() as Led1Impl, 
  22.   new Msp430GpioC() as Led2Impl; 
  23.   components PlatformP; 
  24.  
  25.   Init = PlatformP.LedsInit; 
  26.   Led0 = Led0Impl; 
  27.   Led0Impl -> GeneralIOC.Port54; 
  28.  
  29.   Led1 = Led1Impl; 
  30.   Led1Impl -> GeneralIOC.Port55; 
  31.  
  32.   Led2 = Led2Impl; 
  33.   Led2Impl -> GeneralIOC.Port56; 
  34.  

3个LED灯分别被连接到MSP430的5.4、5.5、5.6三个引脚。

再次编译Blink程序: