altos/detherm: Add ms5607 to detherm
[fw/altos] / src / drivers / ao_ms5607.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_exti.h>
20 #include <ao_ms5607.h>
21
22 #if HAS_MS5607 || HAS_MS5611
23
24 __xdata struct ao_ms5607_prom   ao_ms5607_prom;
25 static __xdata uint8_t          ms5607_configured;
26
27 #ifndef AO_MS5607_SPI_SPEED
28 #define AO_MS5607_SPI_SPEED     AO_SPI_SPEED_FAST
29 #endif
30
31 static void
32 ao_ms5607_start(void) {
33         ao_spi_get_bit(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, AO_MS5607_SPI_INDEX, AO_MS5607_SPI_SPEED);
34 }
35
36 static void
37 ao_ms5607_stop(void) {
38         ao_spi_put_bit(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, AO_MS5607_SPI_INDEX);
39 }
40
41 static void
42 ao_ms5607_reset(void) {
43         uint8_t cmd;
44
45         cmd = AO_MS5607_RESET;
46         ao_ms5607_start();
47         ao_spi_send(DATA_TO_XDATA(&cmd), 1, AO_MS5607_SPI_INDEX);
48         ao_delay(AO_MS_TO_TICKS(10));
49         ao_ms5607_stop();
50 }
51
52 static uint8_t
53 ao_ms5607_crc(uint8_t *prom)
54 {
55         uint8_t         crc_byte = prom[15];
56         uint8_t         cnt;
57         uint16_t        n_rem = 0;
58         uint8_t         n_bit;
59
60         prom[15] = 0;
61         for (cnt = 0; cnt < 16; cnt++) {
62                 n_rem ^= prom[cnt];
63                 for (n_bit = 8; n_bit > 0; n_bit--) {
64                         if (n_rem & 0x8000)
65                                 n_rem = (n_rem << 1) ^ 0x3000;
66                         else
67                                 n_rem = (n_rem << 1);
68                 }
69         }
70         n_rem = (n_rem >> 12) & 0xf;
71         prom[15] = crc_byte;
72         return n_rem;
73 }
74
75 static void
76 ao_ms5607_prom_read(__xdata struct ao_ms5607_prom *prom)
77 {
78         uint8_t                 addr;
79         uint8_t                 crc;
80         __xdata uint16_t        *r;
81
82         r = (__xdata uint16_t *) prom;
83         for (addr = 0; addr < 8; addr++) {
84                 uint8_t cmd = AO_MS5607_PROM_READ(addr);
85                 ao_ms5607_start();
86                 ao_spi_send(DATA_TO_XDATA(&cmd), 1, AO_MS5607_SPI_INDEX);
87                 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
88                 ao_ms5607_stop();
89                 r++;
90         }
91         crc = ao_ms5607_crc((uint8_t *) prom);
92         if (crc != (((uint8_t *) prom)[15] & 0xf)) {
93 #if HAS_TASK
94                 printf ("MS5607 PROM CRC error (computed %x actual %x)\n",
95                         crc, (((uint8_t *) prom)[15] & 0xf));
96                 flush();
97 #endif
98                 ao_panic(AO_PANIC_SELF_TEST_MS5607);
99         }
100
101 #if __BYTE_ORDER == __LITTLE_ENDIAN
102         /* Byte swap */
103         r = (uint16_t *) prom;
104         for (addr = 0; addr < 8; addr++) {
105                 uint16_t        t = *r;
106                 *r++ = (t << 8) | (t >> 8);
107         }
108 #endif
109 }
110
111 void
112 ao_ms5607_setup(void)
113 {
114         if (ms5607_configured)
115                 return;
116         ms5607_configured = 1;
117         ao_ms5607_reset();
118         ao_ms5607_prom_read(&ao_ms5607_prom);
119 }
120
121 static __xdata volatile uint8_t ao_ms5607_done;
122
123 static void
124 ao_ms5607_isr(void)
125 {
126         ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
127         ao_ms5607_done = 1;
128         ao_wakeup((__xdata void *) &ao_ms5607_done);
129 }
130
131 static uint32_t
132 ao_ms5607_get_sample(uint8_t cmd) {
133         __xdata uint8_t reply[3];
134         __xdata uint8_t read;
135
136         ao_ms5607_done = 0;
137
138         ao_ms5607_start();
139         ao_spi_send(DATA_TO_XDATA(&cmd), 1, AO_MS5607_SPI_INDEX);
140
141         ao_exti_enable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
142
143 #if AO_MS5607_PRIVATE_PINS
144         ao_spi_put(AO_MS5607_SPI_INDEX);
145 #endif
146         ao_arch_block_interrupts();
147         while (!ao_gpio_get(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN, AO_MS5607_MISO) &&
148                !ao_ms5607_done)
149                 ao_sleep((void *) &ao_ms5607_done);
150         ao_arch_release_interrupts();
151 #if AO_MS5607_PRIVATE_PINS
152         stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 1);
153 #else
154         ao_ms5607_stop();
155 #endif
156
157         ao_ms5607_start();
158         read = AO_MS5607_ADC_READ;
159         ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
160         ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
161         ao_ms5607_stop();
162
163         return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
164 }
165
166 #ifndef AO_MS5607_BARO_OVERSAMPLE
167 #define AO_MS5607_BARO_OVERSAMPLE       2048
168 #endif
169
170 #ifndef AO_MS5607_TEMP_OVERSAMPLE
171 #define AO_MS5607_TEMP_OVERSAMPLE       AO_MS5607_BARO_OVERSAMPLE
172 #endif
173
174 #define token_paster(x,y)       x ## y
175 #define token_evaluator(x,y)    token_paster(x,y)
176
177 #define AO_CONVERT_D1   token_evaluator(AO_MS5607_CONVERT_D1_, AO_MS5607_BARO_OVERSAMPLE)
178 #define AO_CONVERT_D2   token_evaluator(AO_MS5607_CONVERT_D2_, AO_MS5607_TEMP_OVERSAMPLE)
179
180 void
181 ao_ms5607_sample(__xdata struct ao_ms5607_sample *sample)
182 {
183         sample->pres = ao_ms5607_get_sample(AO_CONVERT_D1);
184         sample->temp = ao_ms5607_get_sample(AO_CONVERT_D2);
185 }
186
187 #ifdef _CC1111_H_
188 #include "ao_ms5607_convert_8051.c"
189 #else
190 #include "ao_ms5607_convert.c"
191 #endif
192
193 #if HAS_TASK
194 __xdata struct ao_ms5607_sample ao_ms5607_current;
195
196 static void
197 ao_ms5607(void)
198 {
199         ao_ms5607_setup();
200         for (;;)
201         {
202                 ao_ms5607_sample(&ao_ms5607_current);
203                 ao_arch_block_interrupts();
204                 AO_DATA_PRESENT(AO_DATA_MS5607);
205                 AO_DATA_WAIT();
206                 ao_arch_release_interrupts();
207         }
208 }
209
210 __xdata struct ao_task ao_ms5607_task;
211
212 void
213 ao_ms5607_info(void)
214 {
215         printf ("ms5607 reserved: %u\n", ao_ms5607_prom.reserved);
216         printf ("ms5607 sens: %u\n", ao_ms5607_prom.sens);
217         printf ("ms5607 off: %u\n", ao_ms5607_prom.off);
218         printf ("ms5607 tcs: %u\n", ao_ms5607_prom.tcs);
219         printf ("ms5607 tco: %u\n", ao_ms5607_prom.tco);
220         printf ("ms5607 tref: %u\n", ao_ms5607_prom.tref);
221         printf ("ms5607 tempsens: %u\n", ao_ms5607_prom.tempsens);
222         printf ("ms5607 crc: %u\n", ao_ms5607_prom.crc);
223 }
224
225 static void
226 ao_ms5607_dump(void)
227 {
228         __xdata struct ao_ms5607_value value;
229
230         ao_ms5607_convert(&ao_ms5607_current, &value);
231         printf ("Pressure:    %8lu %8ld\n", ao_ms5607_current.pres, value.pres);
232         printf ("Temperature: %8lu %8ld\n", ao_ms5607_current.temp, value.temp);
233         printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
234 }
235
236 __code struct ao_cmds ao_ms5607_cmds[] = {
237         { ao_ms5607_dump,       "B\0Display MS5607 data" },
238         { 0, NULL },
239 };
240 #endif /* HAS_TASK */
241
242 void
243 ao_ms5607_init(void)
244 {
245         ms5607_configured = 0;
246         ao_spi_init_cs(AO_MS5607_CS_PORT, (1 << AO_MS5607_CS_PIN));
247
248 #if HAS_TASK
249         ao_cmd_register(&ao_ms5607_cmds[0]);
250         ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
251 #endif
252
253         /* Configure the MISO pin as an interrupt; when the
254          * conversion is complete, the MS5607 will raise this
255          * pin as a signal
256          */
257         ao_exti_setup(AO_MS5607_MISO_PORT,
258                       AO_MS5607_MISO_PIN,
259                       AO_EXTI_MODE_RISING|
260                       AO_EXTI_PIN_NOCONFIGURE,
261                       ao_ms5607_isr);
262 }
263
264 #endif