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