⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 co2c.nc

📁 tinyos2.0版本驱动
💻 NC
字号:
// $Id: CO2C.nc,v 1.5 2008/06/26 03:38:26 regehr Exp $

/*									tab:4
 * "Copyright (c) 2000-2005 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */


#include "Timer.h"
#include "CO2.h"

module CO2C 
{
  uses interface Boot;

  uses interface Timer<TMilli> as Timer0;
  uses interface Timer<TMilli> as Timer1;

  uses interface Leds;

  // SerialActiveMessageC  
  uses interface SplitControl as SerialControl;
  uses interface AMSend;
  uses interface Packet;

  // Serial0C
  uses interface StdControl as Uart0StdControl;
  uses interface UartStream;
}
implementation
{

  const uint16_t crc_tab[256]= //CRC校验表  {
	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
	0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
	0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
	0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
	0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
	0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
	0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
	0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
	0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
	0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
	0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
	0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
	0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
	0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
	0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
	0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
	0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
	0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
	0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
	0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
	0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
	0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
	0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
	0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
	0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
	0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
	0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
	0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
	0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
	0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
	0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
	0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
  };

  /* Enum and variables */
  enum {
    STATE_IDLE,
    STATE_BUSY,
  };

  enum {
    WAIT_FOR_FIRST_PREAMBLE,
    WAIT_FOR_SECOND_PREAMBLE,
    WAIT_FOR_ADDRESS,
    WAIT_FOR_LENGTH,
    WAIT_FOR_DATA,
  };

  uint8_t sendCmdBuff[4]={0x02,0x03};
  uint8_t receiveDataBuff[16];

  uint8_t sendPacketBuff[16];
  uint8_t receivePacketBuff[20];

  uint8_t read_length;
  norace uint8_t receive_pos;
  norace uint8_t receive_machine_state;
  uint16_t sendPacketLen; 
  norace uint8_t mState;
  message_t packet;


   /* Functions arear*/
   /**
   * TODO: Reduce the computation by compute all command packet in prior.
   * 
   * @param wAccum
   * @param byte 
   * @return 
   */
  uint16_t calc_CRC(uint16_t wAccum, uint8_t byte) {
    uint8_t index = (uint8_t)(wAccum>>8) ^ byte;
    uint16_t temp=crc_tab[index];
    return ( wAccum << 8 ) ^ temp;
  }

  /**
   * Frame data to lower packet format of CO2 6004.
   * TODO: Check the length in case of buffer overflow.
   * 
   * @param sendData The pointer of sending buffer.
   * @param sdLen The send data buffer length.
   * @param sendpacket The pointer of framed packet.
   * @param spLen The send packet buffer length.
   * @return Framed packet length.
   */
  uint8_t assemble_packet(uint8_t *sendData, uint8_t sdLen, uint8_t *sendPacket, uint8_t spLen) {
	uint8_t j = 0;
	uint8_t i;
	uint16_t wCrc;
	sendPacket[j++] = 0xff;
	sendPacket[j++] = 0xff;     //前导码	sendPacket[j++] = 0xfe;	//地址,所有命令都一样	wCrc = calc_CRC(0,0xfe);
	sendPacket[j++] = (uint8_t)sdLen; //长度	if (sdLen == 0xff)
	{
		sendPacket[j++] = 0;
	}
	wCrc = calc_CRC(wCrc,sdLen);
	for (i = 0; i < sdLen; i++)
	{
		sendPacket[j++] = sendData[i];
		wCrc = calc_CRC(wCrc,sendData[i]);
		if (sendData[i] == 0xff) 
		{
			sendPacket[j++] = 0; // No CRC on transport material
		}
	}
	sendPacket[j++] = (uint8_t)wCrc;
	if (sendPacket[j-1] == 0xff)
	{
		sendPacket[j++] = 0;
	}
	sendPacket[j++] = (uint8_t)((wCrc & 0xFF00)>>8);
	if (sendPacket[j-1] == 0xff)
	{
		sendPacket[j++] = 0;
	}
	
        return j;
  }

  /**
   * Extract data from lower packet format of CO2 6004.
   * TODO: Check the length in case of buffer overflow.
   * 
   * @param receivePacket The pointer of received buffer.
   * @param rpLen The received packet buffer length.
   * @param receiveData The pointer of data buffer. 
   * @param rdLen The received data buffer length.
   * @return Received data length.
   */
  inline int16_t parse_packet(uint8_t * receivePacket, uint8_t rpLen, uint8_t * receiveData, uint8_t rdLen) {
	uint8_t j = 0;
	uint8_t i = 0;
	int16_t len;
	uint16_t crc;
	
        if(  receivePacket[i++]  != 0xFA ) return -1;
	crc = calc_CRC(0,receivePacket[i-1]);
	len = receivePacket[i++];
	crc = calc_CRC(crc,receivePacket[i-1]);
	while( j < len )
	{
		if( (receiveData[j++] = receivePacket[i++]) == 0xff ) i++;
		crc = calc_CRC(crc,receiveData[j-1]);
	}

	if( (uint8_t)crc != receivePacket[i++] ) return -1;
	if( receivePacket[i-1] == 0xff ) i++;
	if( (uint8_t)(crc >> 8) != receivePacket[i++] )  return -1;  //不能return 0 因为应答包的长度为0

	return len;
  }

  /**
   * Initialization.
   * 
   */
  void init() {
    mState = STATE_IDLE;
    receive_machine_state = WAIT_FOR_FIRST_PREAMBLE;
    receive_pos = 0;
    sendPacketLen = assemble_packet(sendCmdBuff, 2, sendPacketBuff, 16);
    call Uart0StdControl.start();
    call Timer1.startOneShot( 5000 );
  }

  /* events */
  event void Boot.booted()  {
    call SerialControl.start();
  }

  event void SerialControl.stopDone(error_t error) { }

  event void SerialControl.startDone(error_t error) {
    if (error == SUCCESS) {
      init();
    }
  }

  event void Timer0.fired()  {
    error_t error;

    if (1/*mState == STATE_IDLE*/) {
      // start a new round of data fetching 
      mState = STATE_BUSY;
      error = call UartStream.send(sendPacketBuff, sendPacketLen);
      if (error == SUCCESS) {
        call Leds.led0Toggle();
      } else {
        mState = STATE_IDLE;
      }
    }
  }

  event void Timer1.fired()  {
    call Timer0.startPeriodic( 2000 );
  }

  event void AMSend.sendDone(message_t* bufPtr, error_t error) {
    mState = STATE_IDLE;
    return;
  }
 
  /* Tasks */
  task void ParsePacket() {
    int16_t length = 0;

    length = parse_packet(receivePacketBuff, 20, receiveDataBuff, 16);
    if( length != -1 ) {	
      CO2SensorMsg *rcm = (CO2SensorMsg *)call Packet.getPayload(&packet, sizeof(CO2SensorMsg));
      if (rcm == NULL) {return;}
      if (call Packet.maxPayloadLength() < sizeof(CO2SensorMsg)) { return; }
      rcm -> mCO2Data[0] = receiveDataBuff[0];
      rcm -> mCO2Data[1] = receiveDataBuff[1];

      //call Leds.led2Toggle();
      call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(CO2SensorMsg));
    } else {
      mState = STATE_IDLE;
    }

    return;
  }

  task void SendingTask() {
    CO2SensorMsg *rcm = (CO2SensorMsg *)call Packet.getPayload(&packet, sizeof(CO2SensorMsg));
    if (rcm == NULL) {return;}
    if (call Packet.maxPayloadLength() < sizeof(CO2SensorMsg)) {
      return;
    }
  //  rcm -> mCO2Data[0] = mDataBuffer[1];
  // rcm -> mCO2Data[1] = mDataBuffer[2];
    call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(CO2SensorMsg));
    return;
  }
  
  /* UartStream events implementation */
  /**
   * Signal completion of sending a stream.
   *
   * @param 'uint8_t* COUNT(len) buf' Bytes sent.
   * @param len Number of bytes sent.
   * @param error SUCCESS if the transmission was successful, FAIL otherwise.
   */
  async event void UartStream.sendDone( uint8_t* buf, uint16_t len, error_t error ) {
    if (error == SUCCESS) {
      call Leds.led1Toggle();
    } else {
      mState = STATE_IDLE;
    }
  }

  /**
   * Signals the receipt of a byte.
   *
   * @param byte The byte received.
   */
  async event void UartStream.receivedByte( uint8_t byte ) {
        call Leds.led2Toggle();
	switch (receive_machine_state)
	{
		case WAIT_FOR_FIRST_PREAMBLE:
			if (byte == 0xFF)
			{
				receive_machine_state = WAIT_FOR_SECOND_PREAMBLE;
			}
			break;
		case WAIT_FOR_SECOND_PREAMBLE:
			if (byte == 0xFF)
			{
				receive_machine_state = WAIT_FOR_ADDRESS;
			}
			else
			{
				receive_machine_state = WAIT_FOR_FIRST_PREAMBLE;
			}
			break;
		case WAIT_FOR_ADDRESS:
			receivePacketBuff[receive_pos] = byte;
			if( receivePacketBuff[receive_pos] == 0XFA )
			{
				receive_machine_state = WAIT_FOR_LENGTH;
				receive_pos++;
			}
			else
			{
				receive_machine_state = WAIT_FOR_FIRST_PREAMBLE;
			}
			break;
		case WAIT_FOR_LENGTH:
			read_length = byte;
			receivePacketBuff[receive_pos] = read_length; 
			receive_pos++;
			read_length += 2;  //加两个字节crc
			receive_machine_state = WAIT_FOR_DATA;
			break;
		case WAIT_FOR_DATA:
			receivePacketBuff[receive_pos] = byte;
			//if( receivePacketBuff[receive_pos] != 0xff ) read_length--;
			//receive_pos++;
			if(!((receivePacketBuff[receive_pos]==0x00)&&(receivePacketBuff[receive_pos-1]==0xff))) //jia
		        {
				read_length--;
				receive_pos++;
			}  //jia
			if(receive_pos >= 16 ) receive_pos = 0;
			if(read_length == 0) 
			{
			/*	if( (length=parse_packet(receivePacketBuff, receiveData)) != -1 ) {	
				   receiveSuccess = SUCCESS;
				} else {
				  receiveSuccess=FAIL;
				}
			*/
				post ParsePacket();
				receive_pos = 0;
				receive_machine_state = WAIT_FOR_FIRST_PREAMBLE;
			}  
			break;
	}
    
  }

  /**
   * Signal completion of receiving a stream.
   *
   * @param 'uint8_t* COUNT(len) buf' Buffer for bytes received.
   * @param len Number of bytes received.
   * @param error SUCCESS if the reception was successful, FAIL otherwise.
   */
  async event void UartStream.receiveDone( uint8_t* buf, uint16_t len, error_t error ) {}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -