Contiki 2.5
ds1629.c
1 /*
2 Copyright 2005, Freie Universitaet Berlin. All rights reserved.
3 
4 These sources were developed at the Freie Universität Berlin, Computer
5 Systems and Telematics group.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10 
11 - Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 
18 - Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
19 contributors may be used to endorse or promote products derived from
20 this software without specific prior written permission.
21 
22 This software is provided by FUB and the contributors on an "as is"
23 basis, without any representations or warranties of any kind, express
24 or implied including, but not limited to, representations or
25 warranties of non-infringement, merchantability or fitness for a
26 particular purpose. In no event shall FUB or contributors be liable
27 for any direct, indirect, incidental, special, exemplary, or
28 consequential damages (including, but not limited to, procurement of
29 substitute goods or services; loss of use, data, or profits; or
30 business interruption) however caused and on any theory of liability,
31 whether in contract, strict liability, or tort (including negligence
32 or otherwise) arising in any way out of the use of this software, even
33 if advised of the possibility of such damage.
34 
35 This implementation was developed by the CST group at the FUB.
36 
37 For documentation and questions please use the web site
38 http://scatterweb.mi.fu-berlin.de and the mailinglist
39 scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
40 Berlin, 2005
41 */
42 
43 /**
44  * Part of the source code from ScatterWeb 2.2 (ScatterWeb.{Data,System}.c)
45  * released by Freie Universitaet Berlin has been reworked and
46  * reformatted to fit the Contiki ESB port.
47  */
48 
49 #include "contiki.h"
50 #include "dev/ds1629.h"
51 
52 #define SDA_HIGH (P5OUT |= 0x01) /* RTC data line high */
53 #define SDA_LOW (P5OUT &= 0xFE) /* RTC data line low */
54 #define SCL_HIGH (P5OUT |= 0x02) /* RTC clock line high */
55 #define SCL_LOW (P5OUT &= 0xFD) /* RTC clock line low */
56 #define BUS_READ 0x9F
57 #define BUS_WRITE 0x9E
58 #define ACC_CSR 0xAC /* Access Configuration/Control Register */
59 #define ACC_CLOCK 0xC0 /* Access Clock Register */
60 #define ACC_CLOCK_ALARM 0xC7 /* Access Clock Alarm Register */
61 #define ACC_TH 0xA1 /* Access Thermostat Setpoint High */
62 #define ACC_TL 0xA2 /* Access Thermostat Setpoint Low */
63 #define ACC_CSRAM 0x17 /* Access Clock 32 byte SRAM */
64 #define ACC_RT 0xAA /* Access Read Temperatur Register */
65 #define CSR_OS1 (0x80)
66 #define CSR_OS0 (0x40)
67 #define CSR_A1 (0x20)
68 #define CSR_A0 (0x10)
69 #define CSR_CNV (0x04)
70 #define CSR_POL (0x02)
71 #define CSR_1SH (0x01)
72 #define CSR_DEFAULT (CSR_OS1 + CSR_OS0 + CSR_A1 + CSR_CNV + CSR_1SH + CSR_POL)
73 
74 /**
75  * Temperature type (built on a signed int). It's a signed (twos complement)
76  * fixed point value with 8 bits before comma and 7 bits after. So Bit 15 is
77  * sign, Bit14-7 is before comma and Bit 6-0 after comma.
78  *
79  * @since 2.0
80  */
81 typedef union { unsigned int u; signed int s; } temp_t;
82 
83 /*--------------------------------------------------------------------------*/
84 /* Puts the start condition on bus. */
85 static void
86 cl_start(void)
87 {
88  P5DIR |= 0x03; /* ensure: P50(SDA), P51(SCL) output */
89  SCL_LOW; _NOP(); _NOP();
90  SDA_HIGH; _NOP(); _NOP();
91  SCL_HIGH; _NOP(); _NOP();
92  SDA_LOW; _NOP(); _NOP();
93  SCL_LOW; _NOP(); _NOP();
94 }
95 /*--------------------------------------------------------------------------*/
96 /* Puts the stop condition on bus. */
97 static void
98 cl_stop()
99 {
100  SCL_LOW; _NOP(); _NOP();
101  SDA_LOW; _NOP(); _NOP();
102  SCL_HIGH; _NOP(); _NOP();
103  SDA_HIGH; _NOP(); _NOP();
104  SCL_LOW; _NOP(); _NOP();
105  P5DIR &= ~0x03;
106 }
107 /*--------------------------------------------------------------------------*/
108 /* Writes a byte on the bus, returns the acknowledge bit. */
109 static u16_t
110 cl_writeOnBus(u8_t byte)
111 {
112  u16_t i, ack;
113  for(i=0;i<8;i++) {
114  if(byte & 0x80) SDA_HIGH; else SDA_LOW;
115  SCL_HIGH;
116  byte = byte << 1; _NOP();
117  SCL_LOW; _NOP();
118  }
119  /* check ack */
120  P5DIR &= 0xFE; /* P50(SDA) input */
121  SCL_HIGH;
122  if(P5IN & 0x01) ack = 0; else ack = 1; /* test if ack=0, else error */
123  _NOP();
124  SCL_LOW;
125  P5DIR |= 0x01; /* P50(SDA) output */
126  return ack;
127 }
128 /*--------------------------------------------------------------------------*/
129 static u8_t
130 cl_readFromBus(u16_t ack)
131 {
132  u16_t i;
133  u8_t byte = 0;
134  P5DIR &= 0xFE; /* P50(SDA) input */
135  for(i=0;i<8;i++) {
136  byte = byte << 1;
137  SCL_HIGH;
138  if(P5IN & 0x01) byte |= 0x01; else byte &= 0xFE;
139  SCL_LOW;
140  }
141  P5DIR |= 0x01; /* P50(SDA) output */
142  if(ack) SDA_LOW; else SDA_HIGH;
143  SCL_HIGH;
144  SCL_LOW;
145  return byte;
146 }
147 /*--------------------------------------------------------------------------*/
148 static u16_t
149 getReg16bit(u8_t acc, u16_t bitmask)
150 {
151  u16_t config = 0;
152  do cl_start();
153  while(!cl_writeOnBus(BUS_WRITE));
154  cl_writeOnBus(acc);
155  cl_start();
156  cl_writeOnBus(BUS_READ);
157  config = cl_readFromBus(1);
158  config = config << 8;
159  config += cl_readFromBus(0);
160  cl_stop();
161  config &= bitmask;
162  _NOP();
163  _NOP();
164  return config;
165 }
166 /*--------------------------------------------------------------------------*/
167 /* Only first 8 bit of Configuration Status Register can be set */
168 static void
169 setCSReg(u8_t setting)
170 {
171  do cl_start();
172  while(!cl_writeOnBus(BUS_WRITE));
173  cl_writeOnBus(ACC_CSR);
174  cl_writeOnBus(setting);
175  cl_stop();
176  _NOP();
177  _NOP();
178  _NOP();
179  _NOP();
180 }
181 /*--------------------------------------------------------------------------*/
182 static void
183 System_startConversion(void)
184 {
185  do cl_start(); /* do start until BUS_WRITE is acked */
186  while(!cl_writeOnBus(BUS_WRITE)); /* control byte */
187  cl_writeOnBus(0xEE); /* start conversion */
188  cl_stop();
189 }
190 /*--------------------------------------------------------------------------*/
191 /* RTC initialization. Initializes RTC with ::CSR_DEFAULT. */
192 static void
193 initClock(void)
194 {
195  u8_t csr = getReg16bit(ACC_CSR,0xFF00) >> 8;
196  if(csr!=CSR_DEFAULT) setCSReg(CSR_DEFAULT); /* if desired config isnt in clock => set it */
197  /* IMPORTANT: Ensure quartz is generating 32768 Hz */
198  /* (sometimes CH bit gets set when clock is read while reset) */
199  do cl_start(); /* Do start until BUS_WRITE is acked. */
200  while(!cl_writeOnBus(BUS_WRITE)); /* Send control byte */
201  cl_writeOnBus(ACC_CLOCK); /* Send command byte ::ACC_CLOCK. */
202  cl_writeOnBus(0x00); /* Send starting address 0x00. */
203  cl_writeOnBus(0x00); /* Set CH to 0, tseconds and seconds will also be reset! */
204  cl_stop(); /* Stop condition. */
205 }
206 /*--------------------------------------------------------------------------*/
207 void
208 ds1629_init()
209 {
210  initClock();
211 }
212 /*--------------------------------------------------------------------------*/
213 void
214 ds1629_start()
215 {
216  System_startConversion();
217 }
218 /*--------------------------------------------------------------------------*/
219 signed int
220 ds1629_temperature()
221 {
222  temp_t temperature;
223 
224  ds1629_start();
225 
226  temperature.u = getReg16bit(ACC_RT,0xFFFF);
227  return temperature.s;
228 }