Contiki 3.x
n740.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Loughborough University - 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 
32 /**
33  * \file
34  * This file provides functions to control various chips on the
35  * Sensinode N740s:
36  *
37  * - The 74HC595D is an 8-bit serial in-parallel out shift register.
38  * LEDs are connected to this chip. It also serves other functions such as
39  * enabling/disabling the Accelerometer (see n740.h).
40  * - The 74HC4053D is a triple, 2-channel analog mux/de-mux.
41  * It switches I/O between the USB and the D-Connector.
42  * It also controls P0_0 input source (Light Sensor / External I/O)
43  *
44  * Mux/De-mux: Connected to P0_3 (set to output in models.c
45  * Changing the state of the mux/demux can have catastrophic (tm) results
46  * on our software. If we are not careful, we risk entering a state where
47  * UART1 RX interrupts are being generated non-stop. Only change its state
48  * via the function in this file.
49  *
50  * Shift Register:
51  * For the shift register we can:
52  * - write a new instruction
53  * - remember and retrieve the last instruction sent
54  *
55  * The chip is connected to CPU pins as follows:
56  * - P0_2: Serial Data Input
57  * - P1_3: Shift Register Clock Input
58  * - P1_1: Storage Register Clock
59  *
60  * This file can be placed in any bank.
61  *
62  * \author
63  * George Oikonomou - <oikonomou@users.sourceforge.net>
64  */
65 
66 #include "dev/n740.h"
67 #include "dev/uart1.h"
68 #include "8051def.h"
69 
70 /*
71  * This variable stores the most recent instruction sent to the ser-par chip.
72  * We declare it as static and return its value through n740_ser_par_get().
73  */
74 static CC_AT_DATA uint8_t ser_par_status;
75 
76 /*---------------------------------------------------------------------------*/
77 /* Init the serial-parallel chip:
78  * - Set I/O direction for all 3 pins (P0_2, P1_1 and P1_3) to output
79  */
80 void
81 n740_ser_par_init()
82 {
83  /* bus_init and uart1_init also touch the I/O direction for those pins */
84  P1DIR |= 0x0A;
85  P0DIR |= 0x04;
86 }
87 /*---------------------------------------------------------------------------*/
88 /*
89  * Send a command to the N740 serial-parallel chip. Each command is a single
90  * byte, each bit controls a different feature on the sensor.
91  */
92 void
93 n740_ser_par_set(uint8_t data)
94 {
95  uint8_t i;
96  uint8_t mask = 1;
97  uint8_t temp = 0;
98 
100  /* bit-by-bit */
101  for(i = 0; i < 8; i++) {
102  temp = (data & mask);
103  /* Is the bit set? */
104  if(i && temp) {
105  /* If it was set, we want to send 1 */
106  temp >>= i;
107  }
108  /* Send the bit */
109  P0_2 = temp;
110  /* Shift */
111  P1_3 = 1;
112  P1_3 = 0;
113  mask <<= 1;
114  }
115  /* Move to Par-Out */
116  P1_1 = 1;
117  P1_1 = 0;
118  ENABLE_INTERRUPTS();
119 
120  /* Right, we're done. Save the new status in ser_par_status */
121  ser_par_status = data;
122 }
123 /*---------------------------------------------------------------------------*/
124 /* This function returns the last value sent to the ser-par chip on the N740.
125  *
126  * The caveat here is that we must always use n740_set_ser_par() to send
127  * commands to the ser-par chip, never write directly.
128  *
129  * If any other function sends a command directly, ser_par_status and the
130  * actual status will end up out of sync.
131  */
132 uint8_t
133 n740_ser_par_get()
134 {
135  return ser_par_status;
136 }
137 /*---------------------------------------------------------------------------*/
138 void
139 n740_analog_switch(uint8_t state)
140 {
141  /* Turn off the UART RX interrupt before switching */
143  UART1_RX_INT(0);
144 
145  /* Switch */
146  P0_3 = state;
147 
148  /* If P0_3 now points to the USB and nothing is flowing down P1_7,
149  * enable the interrupt again */
150  if(P1_7 == 1 && P0_3 == N740_ANALOG_SWITCH_USB) {
151  UART1_RX_INT(1);
152  }
153  ENABLE_INTERRUPTS();
154 }
155 /*---------------------------------------------------------------------------*/
156 /*
157  * Activate the the 74HC4053D analog switch U5 on the N740 and at the same
158  * time set relevant pins to Peripheral I/O mode. This stops communications
159  * with the serial flash and enables UART1 I/O
160  */
161 void
162 n740_analog_activate()
163 {
164  uint8_t ser_par = n740_ser_par_get();
165  ser_par &= ~N740_SER_PAR_U5_ENABLE; /* Turn on */
166 
167  N740_PINS_PER_IO();
168 
169  n740_ser_par_set(ser_par);
170 }
171 /*---------------------------------------------------------------------------*/
172 /*
173  * De-Activate the the 74HC4053D analog switch U5 on the N740 and at the same
174  * time set relevant pins to GP I/O mode. This effectively prepares us to
175  * start talking with the serial flash chip
176  */
177 void
178 n740_analog_deactivate()
179 {
180  uint8_t ser_par = n740_ser_par_get();
181  ser_par |= N740_SER_PAR_U5_ENABLE; /* Turn off */
182 
183  n740_ser_par_set(ser_par);
184 
185  N740_PINS_GPIO();
186 }
187 /*---------------------------------------------------------------------------*/
Header File for the module which controls the Sensinode N740 8-bit serial-in/serial or parall...
#define DISABLE_INTERRUPTS()
Disable interrupts, saving the previous state so it can be later restored with RESTORE_INTERRUPTS().
Definition: gnu.h:405