Contiki 2.5
ctsrts-sensor.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: ctsrts-sensor.c,v 1.4 2010/02/08 00:00:45 nifi Exp $
32  */
33 
34 /**
35  * RTS/CTS (Request to Send/Clear to Send) are the signals used for hardware
36  * flow control. By setting the RTS line to "ON" the host tells the connected
37  * device that it is ready to receive data. Hardware flow control is not
38  * implemented yet. This implementation is just so some application can use
39  * the pins, it would also be possible for rs232.c to use it for hardware
40  * handshake but as said, that is not implemented yet.
41  */
42 
43 #include "dev/ctsrts-sensor.h"
44 #include "dev/irq.h"
45 #include "dev/hwconf.h"
46 #include <signal.h>
47 
48 const struct sensors_sensor ctsrts_sensor;
49 
50 HWCONF_PIN(RS232RTS, 1, 7);
51 
52 #define RS232CTS_IRQ() 6
53 HWCONF_PIN(RS232CTS, 1, RS232CTS_IRQ());
54 HWCONF_IRQ(RS232CTS, 1, RS232CTS_IRQ());
55 
56 /*---------------------------------------------------------------------------*/
57 static int
58 irq(void)
59 {
60  /* Change the flank triggering for the irq so we will detect next shift. */
61  if(RS232CTS_READ()) {
62  RS232CTS_IRQ_EDGE_SELECTD();
63  } else {
64  RS232CTS_IRQ_EDGE_SELECTU();
65  }
66  sensors_changed(&ctsrts_sensor);
67  return 1;
68 }
69 /*---------------------------------------------------------------------------*/
70 static int
71 value(int type)
72 {
73  /*
74  * Invert the bit and return.
75  * This is strange, accordingly to the MSP430 manual section 9.2.1, Input
76  * Register PxIN the bit should be low when input is low. In RealTerm on
77  * the PC I set RTS which is coupled to the CTS on the esb and I read a 0.
78  * Maybe RTS is defined active LOW on the PC? //Kalle
79  */
80  return RS232CTS_READ() ? 0 : 1;
81 }
82 /*---------------------------------------------------------------------------*/
83 static int
84 configure(int type, int value)
85 {
86  switch(type) {
87  case SENSORS_HW_INIT:
88  RS232RTS_SELECT();
89  RS232RTS_MAKE_OUTPUT();
90  RS232RTS_CLEAR();
91  RS232CTS_SELECT();
92  RS232CTS_MAKE_INPUT();
93  return 1;
94  case SENSORS_ACTIVE:
95  if(value) {
96  if(!RS232CTS_IRQ_ENABLED()) {
97 
98  /*
99  * Check current status on the CTS pin and set IRQ flank so we
100  * will detect a shift.
101  */
102  if(RS232CTS_READ()) {
103  RS232CTS_IRQ_EDGE_SELECTD();
104  } else {
105  RS232CTS_IRQ_EDGE_SELECTU();
106  }
107 
108  irq_port1_activate(RS232CTS_IRQ(), irq);
109  RS232CTS_ENABLE_IRQ();
110  }
111  } else {
112  RS232CTS_DISABLE_IRQ();
113  irq_port1_deactivate(RS232CTS_IRQ());
114  }
115  return 1;
116  }
117  return 0;
118 }
119 /*---------------------------------------------------------------------------*/
120 static int
121 status(int type)
122 {
123  switch(type) {
124  case SENSORS_ACTIVE:
125  case SENSORS_READY:
126  return RS232CTS_IRQ_ENABLED();
127  }
128  return 0;
129 }
130 /*---------------------------------------------------------------------------*/
131 /**
132  * Indicate to host/client we are NOT ready to receive data. Sets the RTS pin
133  * to low.
134  */
135 void ctsrts_rts_clear(void) {
136  RS232RTS_CLEAR();
137 }
138 /*---------------------------------------------------------------------------*/
139 /**
140  * Request host/client to send data. Sets the RTS pin to high.
141  */
142 void ctsrts_rts_set(void) {
143  RS232RTS_SET();
144 }
145 /*---------------------------------------------------------------------------*/
146 SENSORS_SENSOR(ctsrts_sensor, CTSRTS_SENSOR,
147  value, configure, status);