ctsrts-sensor.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: ctsrts-sensor.c,v 1.4 2010/02/08 00:00:45 nifi Exp $
00032  */
00033 
00034 /**
00035  * RTS/CTS (Request to Send/Clear to Send) are the signals used for hardware
00036  * flow control. By setting the RTS line to "ON" the host tells the connected
00037  * device that it is ready to receive data. Hardware flow control is not
00038  * implemented yet. This implementation is just so some application can use
00039  * the pins, it would also be possible for rs232.c to use it for hardware
00040  * handshake but as said, that is not implemented yet.
00041  */
00042 
00043 #include "dev/ctsrts-sensor.h"
00044 #include "dev/irq.h"
00045 #include "dev/hwconf.h"
00046 #include <signal.h>
00047 
00048 const struct sensors_sensor ctsrts_sensor;
00049 
00050 HWCONF_PIN(RS232RTS, 1, 7);
00051 
00052 #define RS232CTS_IRQ() 6
00053 HWCONF_PIN(RS232CTS, 1, RS232CTS_IRQ());
00054 HWCONF_IRQ(RS232CTS, 1, RS232CTS_IRQ());
00055 
00056 /*---------------------------------------------------------------------------*/
00057 static int
00058 irq(void)
00059 {
00060   /* Change the flank triggering for the irq so we will detect next shift. */
00061   if(RS232CTS_READ()) {
00062     RS232CTS_IRQ_EDGE_SELECTD();
00063   } else {
00064     RS232CTS_IRQ_EDGE_SELECTU();
00065   }
00066   sensors_changed(&ctsrts_sensor);
00067   return 1;
00068 }
00069 /*---------------------------------------------------------------------------*/
00070 static int
00071 value(int type)
00072 {
00073   /*
00074    * Invert the bit and return.
00075    * This is strange, accordingly to the MSP430 manual section 9.2.1, Input
00076    * Register PxIN the bit should be low when input is low. In RealTerm on
00077    * the PC I set RTS which is coupled to the CTS on the esb and I read a 0.
00078    * Maybe RTS is defined active LOW on the PC? //Kalle
00079    */
00080   return RS232CTS_READ() ? 0 : 1;
00081 }
00082 /*---------------------------------------------------------------------------*/
00083 static int
00084 configure(int type, int value)
00085 {
00086   switch(type) {
00087   case SENSORS_HW_INIT:
00088     RS232RTS_SELECT();
00089     RS232RTS_MAKE_OUTPUT();
00090     RS232RTS_CLEAR();
00091     RS232CTS_SELECT();
00092     RS232CTS_MAKE_INPUT();
00093     return 1;
00094   case SENSORS_ACTIVE:
00095     if(value) {
00096       if(!RS232CTS_IRQ_ENABLED()) {
00097 
00098         /*
00099          * Check current status on the CTS pin and set IRQ flank so we
00100          * will detect a shift.
00101          */
00102         if(RS232CTS_READ()) {
00103           RS232CTS_IRQ_EDGE_SELECTD();
00104         } else {
00105           RS232CTS_IRQ_EDGE_SELECTU();
00106         }
00107 
00108         irq_port1_activate(RS232CTS_IRQ(), irq);
00109         RS232CTS_ENABLE_IRQ();
00110       }
00111     } else {
00112       RS232CTS_DISABLE_IRQ();
00113       irq_port1_deactivate(RS232CTS_IRQ());
00114     }
00115     return 1;
00116   }
00117   return 0;
00118 }
00119 /*---------------------------------------------------------------------------*/
00120 static int
00121 status(int type)
00122 {
00123   switch(type) {
00124   case SENSORS_ACTIVE:
00125   case SENSORS_READY:
00126     return RS232CTS_IRQ_ENABLED();
00127   }
00128   return 0;
00129 }
00130 /*---------------------------------------------------------------------------*/
00131 /**
00132  * Indicate to host/client we are NOT ready to receive data. Sets the RTS pin
00133  * to low.
00134  */
00135 void ctsrts_rts_clear(void) {
00136   RS232RTS_CLEAR();
00137 }
00138 /*---------------------------------------------------------------------------*/
00139 /**
00140  * Request host/client to send data. Sets the RTS pin to high.
00141  */
00142 void ctsrts_rts_set(void) {
00143   RS232RTS_SET();
00144 }
00145 /*---------------------------------------------------------------------------*/
00146 SENSORS_SENSOR(ctsrts_sensor, CTSRTS_SENSOR,
00147                value, configure, status);

Generated on Mon Apr 11 14:23:43 2011 for Contiki 2.5 by  doxygen 1.6.1