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