TinyOS的UART模块分析

TinyOS的UART模块分析  [嵌入式]发布时间:2007-01-03 23:28:02 

一.Tinyos中的USART组件


 


Tinyos中提供了配置USART模块的两个组件:


HPLUSART1M组件(位置:tinyos-1.x/tos./plarform/telosb/HPLUSART1M.nc)


HPLUARTM组件(位置:tinyos-1.x/tos/platform/telosb/ HPLUARTM.nc)


(1). HPLUSART1M组件(位置:tinyos-1.x/tos./plarform/telosb/HPLUSART1M.nc)


 HPLUART1M组件提供了对MSP430UART硬件模块的所有配置,是Tinyos中直接控制硬件的最底层组件,可以通过调用HPLUART1M提供的命令对UART所有寄存器进行设置,实现我们需要的功能。同时HPLUART1M通过自己提供的事件,把接受到的数据通知上层组件。HPLUART1M为上层组件提供了HPLUSARTControl和HPLUSARTFeedback接口。


 在调试中我们使用的是UART 模式,用到的命令和事件有:


   TOSH_SIGNAL(UART1RX_VECTOR) {


    uint8_t temp = U1RXBUF;


    signal USARTData.rxDone(temp);       //接收中断


  }


 


  TOSH_SIGNAL(UART1TX_VECTOR) {          //发送中断


    signal USARTData.txDone();


  }


  async command bool USARTControl.isUART() //判断工作模式是UART


  async command bool USARTControl.isUARTtx()//发送模式


  async command bool USARTControl.isUARTrx() //接收模式


  async command msp430_usartmode_t USARTControl.getMode()//获取工作模式


  async command void USARTControl.setMode()//设置工作模式


  async command void USARTControl.enableUART()//使能UART


  async command void USARTControl.disableUART()//关闭UART


  async command void USARTControl.enableUARTTx()//使能UART的发送状态


  async command void USARTControl.disableUARTTx()//关闭UART的发送状态


  async command void USARTControl.enableUARTRx()//使能UART的接收状态


  async command void USARTControl.disableUARTRx()//关闭UART的接收状


  async command void USARTControl.setModeUART_TX()//设置发送工作模式


  async command void USARTControl.setModeUART_RX()//设置接收工作模式


  async command void USARTControl.setModeUART()//设置UART工作模式


  async command void USARTControl.setClockSource()//设置时钟源


  async command void USARTControl.setClockRate()//设置波特率


  async command result_t USARTControl.disableRxIntr()//禁止接收中断


  async command result_t USARTControl.disableTxIntr()//禁止发送中断


  async command result_t USARTControl.enableRxIntr()//使能接收中断


  async command result_t USARTControl.enableTxIntr()//使能发送中断


  async command result_t USARTControl.tx()//发送数据


  async command uint8_t USARTControl.rx()//接收数据


  default async event result_t USARTData.txDone()//发送完成,上层组件实现


  default async event result_t USARTData.rxDone()//接收完成,向上层传递数据


(2).HPLUARTM组件(位置:tinyos-1.x/tos/platform/telosb/ HPLUARTM.nc)


   HPLUSARTM组件是HPLUART1M的上层组件,通过HPLUART1M提供的HPLUSARTControl和HPLUSARTFeedback接口,调用HPLUART1M的命令实现对硬件的控制。同时也为实际的Tinyos应用程序提供了HPLUART接口。


   async command result_t UART.init() {             //初始化


    call USARTControl.setModeUART();                //设置UART模式 


    call USARTControl.setClockSource(SSEL_ACLK);    //时钟源为ACK


    call USARTControl.setClockRate(UBR_ACLK_57600, UMCTL_ACLK_57600);//波特率57600   


    call USARTControl.enableRxIntr();


    call USARTControl.enableTxIntr();              //使能中断


    return SUCCESS;


  }


 


  async command result_t UART.stop() {


    call USARTControl.disableRxIntr();


call USARTControl.disableTxIntr();         //禁止中断


call USARTControl.disableUART();           //禁止UART,可以工作在低功耗模式


    return SUCCESS;


  }


 


  async event result_t USARTData.rxDone(uint8_t b) {  //实现HPLUART1M中的事件,获得数据传给b


    return signal UART.get(b);                        //通知UART.get()


  }


 


  async event result_t USARTData.txDone() {          


    return signal UART.putDone();


  }


 


  async command result_t UART.put(uint8_t data){     //发送数据


    return call USARTControl.tx(data);               //调用HPLUART1M中的发送命令,把data发送出去


  }


  //应用组件实现,获得数据data


  default async event result_t UART.get(uint8_t data) { return SUCCESS; }


 


  default async event result_t UART.putDone() { return SUCCESS; }


}


(3)工作流程


   数据接收:串口接收到数据,产生中断TOSH_SIGNAL(UART1RX_VECTOR),读接收缓存中的数据temp = U1RXBUF,通知signal USARTData.rxDone(temp),在HPLUART中实现事件USARTData.rxDone(),通知signal UART.get(b),b=data,在应用程序中实现UART.get(),处理数据。


   数据发送:发送数据,产生中断TOSH_SIGNAL(UART1TX_VECTOR),通知signal USARTData.txDone(),在HPLUART中实现事件USARTData.txDone(),通知UART.putDone(),在应用程序中实现UART.putDone(),处理发送事件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值