LPC2: add LPC2378 example project.
[bertos.git] / examples / lpc2378 / hw / hw_ntc.h
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 2008 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief NTC hardware-specific definition
34  *
35  * \version $Id: hw_ntc.h 1359 2008-05-26 09:42:37Z asterix $
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  * A NTC acts as a variable resistor, whose resistance changes as a
39  * function of the temperature it measures. To sample it correctly, it is
40  * usually parallelized and serialized with two fixed resistor. The following diagram shows
41  * what is the exact disposition of the components, as handled by this
42  * library:
43  *
44  *<pre>
45  *                 o Vref
46  *                 |
47  *                 |                               o Vref
48  *                 |                               |
49  *               -----                             |
50  *              |     |                        ---------
51  *              | Rser|                       |         |
52  *              |     |                       |         |
53  *               -----     -----              |   ADC   |
54  *                 | Vp   |     |             |         |
55  *      -----------|------| Amp |-------------|         |
56  *     |           |      |     |      Vadc   |         |
57  *   -----       -----     -----               ---------
58  *  |     |     |     |
59  *  | NTC |     | Rpar|
60  *  |     |     |     |
61  *   -----       -----
62  *     |           |
63  *     |           |
64  *   -----       -----
65  *    ---         ---
66  *</pre>
67  * Amp is an amplifier that amplify of AMP times the signal.
68  * If we indicate Rp as the parallel of NTC with Rpar, ADCBITS as the bits of the ad converter
69  * and ADCVAL as the result from the adc convertion (Not Vadc but just the value read
70  * from the adc register), after various calculation, the expression of Rp is:
71  *
72  *<pre>
73  *
74  *            ADCVAL * Rser
75  * Rp = ------------------------
76  *         ADCBITS
77  *      2         * AMP - ADCVAL
78  *</pre>
79  *
80  * And after that NTC obvisiously is:
81  *<pre>
82  *        Rpar * Rp
83  * NTC = ----------
84  *        Rpar - Rp
85  *</pre>
86  *
87  *
88  * The function ntc_hw_read() compute the resistence using these formulas above.
89  */
90
91 #ifndef HW_NTC_H
92 #define HW_NTC_H
93
94 #include "ntc_map.h"
95
96 #include <cfg/debug.h>
97
98 #include <drv/ntc.h>
99 #include <drv/adc.h>
100
101 #warning TODO:This is an example implementation, you must implement it!
102
103 extern const res_t      NTC_RSER[NTC_CNT];
104 extern const res_t      NTC_RPAR[NTC_CNT];
105 extern const amp_t      NTC_AMP[NTC_CNT];
106 extern const NtcHwInfo* NTC_INFO[NTC_CNT];
107
108
109 /*!
110  * Read the resistence of ntc device \a dev.
111  * Return the result in res_t type.
112  */
113 INLINE res_t ntc_hw_read(NtcDev dev)
114 {
115         ASSERT(dev < NTC_CNT);
116         // See above for formula explanation.
117         adcread_t adcval = adc_read((uint16_t)dev);
118         float rp = (adcval * NTC_RSER[dev] ) / ((1 << adc_bits()) * NTC_AMP[dev] - adcval);
119
120         //kprintf("Rp[%f], Rntc[%f]\n", rp/100, ((NTC_RPAR[dev] * rp) / (NTC_RPAR[dev] - rp)) / 100.0);
121
122         return ( (NTC_RPAR[dev] * rp) / (NTC_RPAR[dev] - rp) );
123 }
124
125
126 /*!
127  * Return the info (aka the table) associated with ntc device \a dev.
128  */
129 INLINE const NtcHwInfo* ntc_hw_getInfo(NtcDev dev)
130 {
131         return NTC_INFO[dev];
132 }
133
134 #define NTC_HW_INIT     do  { /* Implement me! */ } while(0)
135
136 #endif /* HW_NTC_H */