Contiki 3.x
co2_sa_kxx-sensor.c
1 /*
2  * Copyright (c) 2015, Copyright Markus Hidell, Robert Olsson
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  * Authors : Markus Hidell, Robert Olsson {mahidell, roolss} @kth.se
33  * Created : 2015-10-27
34  * Updated : $Date: 2010/01/14 20:23:02 $
35  * $Revision: 1.2 $
36  */
37 
38 #include "contiki.h"
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdint.h>
43 #include <i2c.h>
44 #include <avr/pgmspace.h>
45 #include <avr/sleep.h>
46 #include <avr/wdt.h>
47 #include <avr/boot.h>
48 #include <avr/eeprom.h>
49 #include "dev/watchdog.h"
50 #include "lib/list.h"
51 #include "lib/memb.h"
52 #include "co2_sa_kxx-sensor.h"
53 
54 static int
55 status(int type)
56 {
57  return 0;
58 }
59 static int
60 configure(int type, int c)
61 {
62  return 0;
63 }
64 static int
65 value(int var)
66 {
67  int val, status;
68  uint8_t buf[2], csum;
69  int16_t res;
70  (void) status;
71  (void) csum;
72 
73  res = 0;
74  i2c_start_wait(I2C_CO2SA_ADDR | I2C_WRITE);
75  if(res) {
76  goto err;
77  }
78 
79  i2c_write(0x22);
80  i2c_write(0x00);
81 
82  if(var == CO2_SA_KXX_CO2) {
83  i2c_write(0x08);
84  i2c_write(0x2A);
85  }
86 
87  if(var == CO2_SA_KXX_TEMP) {
88  i2c_write(0x12);
89  i2c_write(0x34);
90  }
91 
92  if(var == CO2_SA_KXX_RH) {
93  i2c_write(0x14);
94  i2c_write(0x36);
95  }
96 
97  i2c_stop();
98 
99  if(res) {
100  goto err;
101  }
102 
103  clock_delay_msec(20);
104 
105  res = 0;
106  i2c_start(I2C_CO2SA_ADDR | I2C_READ);
107 
108  if(res) {
109  goto err;
110  }
111 
112 
113  status = i2c_readAck();
114 
115  if((status & 0x01) == 0)
116  goto err;
117 
118  buf[0] = i2c_readAck();
119  buf[1] = i2c_readAck();
120  csum = i2c_readNak();
121  i2c_stop();
122 
123  val = ((int16_t)(buf[0] << 8)) | buf[1];
124 
125  return val;
126 
127 err:
128  i2c_stop();
129  return 0;
130 }
131 SENSORS_SENSOR(co2_sa_kxx_sensor, "CO2", value, configure, status);
void clock_delay_msec(uint16_t howlong)
Delay up to 65535 milliseconds.
Definition: clock.c:260
Linked list manipulation routines.
Memory block allocation routines.