Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / drv / ntc.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005 Develer S.r.l. (http://www.de+veler.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief Driver for NTC (reads a temperature through an ADC)
34  *
35  * \author Giovanni Bajo <rasky@develer.com>
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  *
39  * This module handles an external NTC bound to an AD converter. As usual,
40  * it relies on a low-level API (ntc_hw_*) (see below):
41  *
42  */
43
44 #include "hw/hw_ntc.h"
45 #include "hw/ntc_map.h"
46
47 #include "cfg/cfg_ntc.h"
48
49 // Define logging setting (for cfg/log.h module).
50 #define LOG_LEVEL         CONFIG_NTC_LOG_LEVEL
51 #define LOG_VERBOSITY     CONFIG_NTC_LOG_FORMAT
52 #include <cfg/log.h>
53 #include <cfg/debug.h>
54
55 #include <drv/ntc.h>
56
57 DB(bool ntc_initialized;)
58
59 /**
60  * Find in a table of values \a orig_table of size \a size, the index which
61  * value is less or equal to \a val.
62  *
63  * \retval 0 When \a val is higher than the first table entry.
64  * \retval size When \a val is lower than the last table entry.
65  * \retval 1..size-1 When \a val is within the table.
66  */
67 static size_t upper_bound(const res_t *orig_table, size_t size, res_t val)
68 {
69         const res_t *table = orig_table;
70
71         while (size)
72         {
73                 size_t pos = size / 2;
74                 if (val > table[pos])
75                         size = pos;
76                 else
77                 {
78                         table += pos+1;
79                         size -= pos+1;
80                 }
81         }
82
83         return table - orig_table;
84 }
85
86
87 /**
88  * Read the temperature for the NTC channel \a dev.
89  * First read the resistence of the NTC through ntc_hw_read(), then,
90  * for the conversion from resistance to temperature, since the formula
91  * varies from device to device, we implemented a generic system using
92  * a table of data which maps temperature (index) to resistance (data).
93  * The range of the table (min/max temperature) and the step
94  * (temperature difference between two consecutive elements of the table)
95  * is variable and can be specified. Notice that values inbetween the
96  * table elements are still possible as the library does a linear
97  * interpolation using the actual calculated resistance to find out
98  * the exact temperature.
99  *
100  * The low-level API provides a function to get access to a description
101  * of the NTC (ntc_hw_getInfo()), including the resistance table.
102  *
103  */
104 deg_t ntc_read(NtcDev dev)
105 {
106         const NtcHwInfo* hw = ntc_hw_getInfo(dev);
107         const res_t* r = hw->resistances;
108
109         res_t rx;
110         size_t i;
111         deg_t degrees = 0;
112
113         rx = ntc_hw_read(dev);
114
115
116         i = upper_bound(r, hw->num_resistances, rx);
117         ASSERT(i <= hw->num_resistances);
118
119         if (i >= hw->num_resistances)
120                 return NTC_SHORT_CIRCUIT;
121         else if (i == 0)
122                 return NTC_OPEN_CIRCUIT;
123
124         /*
125          * Interpolated value in 0.1 degrees multiplied by 10:
126          *   delta t          step t
127          * ----------  = ----------------
128          * (rx - r[i])   (r[i-1] - r [i])
129          */
130         float tmp;
131         tmp = 10 * hw->degrees_step * (rx - r[i]) / (r[i - 1] - r[i]);
132
133         /*
134          * degrees = integer part corresponding to the superior index
135          *           in the table multiplied by 10
136          *           - decimal part interpolated (already multiplied by 10)
137          */
138         degrees = (i * hw->degrees_step + hw->degrees_min) * 10 - (int)(tmp);
139
140         //kprintf("dev= %d, I=%d, degrees = %d\n", dev, i , degrees);
141
142         return degrees;
143 }
144
145
146 /**
147  * Init NTC hardware.
148  */
149 void ntc_init(void)
150 {
151         NTC_HW_INIT;
152         DB(ntc_initialized = true;)
153 }
154