[Firebug-cvs] firebug/project/src/gps gpspacket.nc,NONE,1.1
Brought to you by:
doolin
From: <cs...@us...> - 2003-08-01 19:09:35
|
Update of /cvsroot/firebug/firebug/project/src/gps In directory sc8-pr-cvs1:/tmp/cvs-serv1569 Added Files: gpspacket.nc Log Message: files for new Leadtek --- NEW FILE: gpspacket.nc --- /* tab:4 * * * "Copyright (c) 2000-2002 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." * */ /* tab:4 * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By * downloading, copying, installing or using the software you agree to * this license. If you do not agree to this license, do not download, * install, copy or use the software. * * Intel Open Source License * * Copyright (c) 2002 Intel Corporation * All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the Intel Corporation nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * */ /* * * Authors: * Date last modified: 6/25/02 * */ /* This component handles the gps control and packet abstraction */ includes sensorboard; includes gps; module GpsPacket { provides { interface StdControl as Control; interface BareSendMsg as Send; interface ReceiveMsg as Receive; interface SendVarLenPacket; interface GpsCmd; command result_t txBytes(uint8_t *bytes, uint8_t numBytes); /* Effects: start sending 'numBytes' bytes from 'bytes' */ // command result_t GpsPower(uint8_t PowerState); // /* 0 => gps power off; 1 => gps power on */ } uses { interface ByteComm; interface StdControl as ByteControl; interface Leds; interface StdControl as SwitchControl; interface Switch as Switch1; interface Switch as SwitchI2W; } } implementation { GPS_Msg buffer; //GPS_Msg* bufferPtr; TOS_MsgPtr bufferPtr; //really a GPS_Msg pointer enum {GPS_SWITCH_IDLE, //GPS I2C switches are not using the I2C bus GPS_PWR_SWITCH_WAIT, //Waiting for GPS I2C power switch to set GPS_EN_SWITCH_WAIT, //Waiting for GPS I2C enable switch to set GPS_TX_SWITCH_WAIT, //Waiting for GPS I2C tx switch to set GPS_RX_SWITCH_WAIT, //Waiting for GPS I2C rx switch to set BUSY, BUSY_0, BUSY_1, GET_SAMPLE_0, GET_SAMPLE_1, OPENSCK, OPENDATA, CLOSESCK, CLOSEDATA, POWEROFF, MAIN_SWITCH_ON, MAIN_SWITCH_OFF, WAIT_SWITCH_ON, WAIT_SWITCH_OFF, TIMER}; uint8_t state_gps; //state of I2C switches uint8_t power_gps; //gps on off uint16_t rxCount, rxLength, txCount, txLength; // TOS_Msg buffers[2]; // TOS_Msg* bufferPtrs[2]; uint8_t bufferIndex; uint8_t *recPtr; uint8_t *sendPtr; enum { IDLE, PACKET, BYTES }; uint8_t state; /* state == IDLE, nothing is being sent state == PACKET, this level is sending a packet state == BYTES, this level is just transferring bytes The purpose of adding the new state, to simply transfer bytes, is because certain applications may want to just send a sequence of bytes without the packet abstraction. One such example is the UART. */ /* Initialization of this component */ command result_t Control.init() { recPtr = (uint8_t *)&buffer; bufferIndex = 0; (GPS_Msg*) bufferPtr = &buffer; // bufferPtrs[1] = &buffers[1]; state_gps = GPS_SWITCH_IDLE; state = IDLE; txCount = rxCount = 0; // make sure we always read up to the type (which determines length) rxLength = GPS_DATA_LENGTH + 2; dbg(DBG_BOOT, "Packet handler initialized.\n"); return call ByteControl.init(); } /* Command to control the power of the network stack */ command result_t Control.start() { // apply your power management algorithm return call ByteControl.start(); call SwitchControl.start(); } /* Command to control the power of the network stack */ command result_t Control.stop() { // apply your power management algorithm return call ByteControl.stop(); } command result_t txBytes(uint8_t *bytes, uint8_t numBytes) { if (txCount == 0) { txCount = 1; txLength = numBytes; sendPtr = bytes; /* send the first byte */ if (call ByteComm.txByte(sendPtr[0])) return SUCCESS; else txCount = 0; } return FAIL; } /* Command to transmit a packet */ command result_t Send.send(TOS_MsgPtr msg) { state = PACKET; msg->crc = 1; /* Fake out the CRC as passed. */ return call txBytes((uint8_t *)msg, TOS_MsgLength(msg->type)); } /* Command to transfer a variable length packet */ command result_t SendVarLenPacket.send(uint8_t* packet, uint8_t numBytes) { state = BYTES; return call txBytes(packet, numBytes); } task void sendDoneFailTask() { txCount = 0; state = IDLE; signal Send.sendDone((TOS_MsgPtr)sendPtr, FAIL); } task void sendDoneSuccessTask() { txCount = 0; state = IDLE; signal Send.sendDone((TOS_MsgPtr)sendPtr, SUCCESS); } task void sendVarLenFailTask() { txCount = 0; state = IDLE; signal SendVarLenPacket.sendDone((uint8_t*)sendPtr, FAIL); } task void sendVarLenSuccessTask() { txCount = 0; state = IDLE; signal SendVarLenPacket.sendDone((uint8_t*)sendPtr, SUCCESS); } void sendComplete(result_t success) { if (state == PACKET) { TOS_MsgPtr msg = (TOS_MsgPtr)sendPtr; /* This is a non-ack based layer */ if (success) { msg->ack = TRUE; post sendDoneSuccessTask(); } else { post sendDoneFailTask(); } } else if (state == BYTES) { if (success) { post sendVarLenSuccessTask(); } else { post sendVarLenFailTask(); } } else { txCount = 0; state = IDLE; } } default event result_t SendVarLenPacket.sendDone(uint8_t* packet, result_t success) { return success; } default event result_t Send.sendDone(TOS_MsgPtr msg, result_t success){ return success; } /* Byte level component signals it is ready to accept the next byte. Send the next byte if there are data pending to be sent */ event result_t ByteComm.txByteReady(bool success) { if (txCount > 0) { if (!success) { dbg(DBG_ERROR, "TX_packet failed, TX_byte_failed"); sendComplete(FAIL); } else if (txCount < txLength) { dbg(DBG_PACKET, "PACKET: byte sent: %x, COUNT: %d\n", sendPtr[txCount], txCount); if (!call ByteComm.txByte(sendPtr[txCount++])) sendComplete(FAIL); } } return SUCCESS; } event result_t ByteComm.txDone() { if (txCount == txLength) sendComplete(TRUE); return SUCCESS; } /////////////////////////////////////////////////////////////////////////// // need to hold off getting more gps bytes until buffer released!!!!! ////////////////////////////////////////////////////////////////////////// task void receiveTask() { // TOS_MsgPtr tmp = signal Receive.receive(bufferPtrs[bufferIndex ^ 1]); // if (tmp) { // bufferPtrs[bufferIndex ^ 1] = tmp; TOS_Msg* tmp = signal Receive.receive(bufferPtr); // } } /****************************************************************************** * Modify this routine for the GPS *****************************************************************************/ /* The handles the latest decoded byte propagated by the Byte Level component*/ event result_t ByteComm.rxByteReady(uint8_t data, bool error, uint16_t strength) { dbg(DBG_PACKET, "PACKET: byte arrived: %x, COUNT: %d\n", data, rxCount); if (error) { rxCount = 0; return FAIL; } recPtr[rxCount++] = data; if (rxCount == rxLength){ bufferIndex = bufferIndex ^ 1; // recPtr = (uint8_t*)bufferPtrs[bufferIndex]; recPtr = (uint8_t*)bufferPtr; dbg(DBG_PACKET, "got packet\n"); rxCount = 0; post receiveTask(); return FAIL; } return SUCCESS; } /****************************************************************************** * Turn Gps on/off * PowerState = 0 then GPS power off, GPS enable off, tx and rx disabled * = 1 then GPS power on, GPS enable on, tx and rx enabled * NOTE - rx,tx share pressure lines. * - GPS switching power supply is enabled by a lo, disabled by a hi *****************************************************************************/ command result_t GpsCmd.GpsPower(uint8_t PowerState){ if (state_gps == GPS_SWITCH_IDLE){ power_gps = PowerState; if (power_gps){ if (call Switch1.set(MICAWB_GPS_POWER,0) == SUCCESS) state_gps = GPS_PWR_SWITCH_WAIT; } else{ if (call Switch1.set(MICAWB_GPS_POWER,1) == SUCCESS) state_gps = GPS_PWR_SWITCH_WAIT; } return SUCCESS; } return FAIL; } event result_t Switch1.getDone(char value) { return SUCCESS; } event result_t Switch1.setDone(bool local_result) { if (state_gps == GPS_PWR_SWITCH_WAIT) { if (call Switch1.set(MICAWB_GPS_ENABLE ,power_gps) == SUCCESS) { state_gps = GPS_EN_SWITCH_WAIT; } } else if (state_gps == GPS_EN_SWITCH_WAIT) { if (call SwitchI2W.set( MICAWB_GPS_TX_SELECT ,power_gps) == SUCCESS) { state_gps = GPS_TX_SWITCH_WAIT; } } return SUCCESS; } event result_t SwitchI2W.setAllDone(bool local_result) { return SUCCESS; } event result_t Switch1.setAllDone(bool local_result) { return SUCCESS; } event result_t SwitchI2W.getDone(char value) { return SUCCESS; } event result_t SwitchI2W.setDone(bool local_result) { if (state_gps == GPS_TX_SWITCH_WAIT) { if (call SwitchI2W.set( MICAWB_GPS_RX_SELECT ,power_gps) == SUCCESS) { state_gps = GPS_RX_SWITCH_WAIT; } } else if (state_gps == GPS_RX_SWITCH_WAIT) { state_gps = GPS_SWITCH_IDLE; } return SUCCESS; } // else if (state == OPENSCK) { // state = OPENDATA; // return call SwitchI2W.set(MICAWB_HUMIDITY_DATA,1); // } else if (state == OPENDATA) { // state = TIMER; // return call Timer.start(TIMER_ONE_SHOT, 100); // } else if (state == CLOSESCK) { // state = CLOSEDATA; // return call SwitchI2W.set(MICAWB_HUMIDITY_DATA,0); // } else if (state == CLOSEDATA) { // uint16_t l_result = result; // state = GPS_SWITCH_IDLE; // if (id == MICAWB_HUMIDITY) // signal Humidity.dataReady(l_result); // else if (id == MICAWB_HUMIDITY_TEMP) // signal Temperature.dataReady(l_result); // } // return SUCCESS; // } } |