altos: Flush the on-board mega log after every sample interval.
[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 static struct ao_ms5607_prom    ms5607_prom;
25 static uint8_t                  ms5607_configured;
26
27 static void
28 ao_ms5607_start(void) {
29         ao_spi_get(AO_MS5607_SPI_INDEX,AO_SPI_SPEED_FAST);
30         ao_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, 0);
31 }
32
33 static void
34 ao_ms5607_stop(void) {
35         ao_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, 1);
36         ao_spi_put(AO_MS5607_SPI_INDEX);
37 }
38
39 static void
40 ao_ms5607_reset(void) {
41         uint8_t cmd;
42
43         cmd = AO_MS5607_RESET;
44         ao_ms5607_start();
45         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
46         ao_delay(AO_MS_TO_TICKS(10));
47         ao_ms5607_stop();
48 }
49
50 static uint8_t
51 ao_ms5607_crc(uint8_t *prom)
52 {
53         uint8_t         crc_byte = prom[15];
54         uint8_t         cnt;
55         uint16_t        n_rem = 0;
56         uint8_t         n_bit;
57
58         prom[15] = 0;
59         for (cnt = 0; cnt < 16; cnt++) {
60                 n_rem ^= prom[cnt];
61                 for (n_bit = 8; n_bit > 0; n_bit--) {
62                         if (n_rem & 0x8000)
63                                 n_rem = (n_rem << 1) ^ 0x3000;
64                         else
65                                 n_rem = (n_rem << 1);
66                 }
67         }
68         n_rem = (n_rem >> 12) & 0xf;
69         prom[15] = crc_byte;
70         return n_rem;
71 }
72
73 static void
74 ao_ms5607_prom_read(struct ao_ms5607_prom *prom)
75 {
76         uint8_t         addr;
77         uint8_t         crc;
78         uint16_t        *r;
79
80         r = (uint16_t *) prom;
81         for (addr = 0; addr < 8; addr++) {
82                 uint8_t cmd = AO_MS5607_PROM_READ(addr);
83                 ao_ms5607_start();
84                 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
85                 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
86                 ao_ms5607_stop();
87                 r++;
88         }
89         crc = ao_ms5607_crc((uint8_t *) prom);
90         if (crc != (((uint8_t *) prom)[15] & 0xf)) {
91 #if HAS_TASK
92                 printf ("MS5607 PROM CRC error (computed %x actual %x)\n",
93                         crc, (((uint8_t *) prom)[15] & 0xf));
94                 flush();
95 #endif
96                 ao_panic(AO_PANIC_SELF_TEST_MS5607);
97         }
98
99 #if __BYTE_ORDER == __LITTLE_ENDIAN
100         /* Byte swap */
101         r = (uint16_t *) prom;
102         for (addr = 0; addr < 8; addr++) {
103                 uint16_t        t = *r;
104                 *r++ = (t << 8) | (t >> 8);
105         }
106 #endif
107 }
108
109 void
110 ao_ms5607_setup(void)
111 {
112         if (ms5607_configured)
113                 return;
114         ms5607_configured = 1;
115         ao_ms5607_reset();
116         ao_ms5607_prom_read(&ms5607_prom);
117 }
118
119 static volatile uint8_t ao_ms5607_done;
120
121 static void
122 ao_ms5607_isr(void)
123 {
124         ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
125         ao_ms5607_done = 1;
126         ao_wakeup((void *) &ao_ms5607_done);
127 }
128
129 static uint32_t
130 ao_ms5607_get_sample(uint8_t cmd) {
131         uint8_t reply[3];
132         uint8_t read;
133
134         ao_ms5607_done = 0;
135
136         ao_ms5607_start();
137         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
138
139         ao_exti_enable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
140
141 #if AO_MS5607_PRIVATE_PINS
142         ao_spi_put(AO_MS5607_SPI_INDEX);
143 #endif
144         ao_arch_block_interrupts();
145         while (!ao_ms5607_done)
146                 ao_sleep((void *) &ao_ms5607_done);
147         ao_arch_release_interrupts();
148 #if AO_MS5607_PRIVATE_PINS
149         stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 1);
150 #else
151         ao_ms5607_stop();
152 #endif
153
154         ao_ms5607_start();
155         read = AO_MS5607_ADC_READ;
156         ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
157         ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
158         ao_ms5607_stop();
159
160         return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
161 }
162
163 #ifndef AO_MS5607_BARO_OVERSAMPLE
164 #define AO_MS5607_BARO_OVERSAMPLE       2048
165 #endif
166
167 #ifndef AO_MS5607_TEMP_OVERSAMPLE
168 #define AO_MS5607_TEMP_OVERSAMPLE       AO_MS5607_BARO_OVERSAMPLE
169 #endif
170
171 #define token_paster(x,y)       x ## y
172 #define token_evaluator(x,y)    token_paster(x,y)
173
174 #define AO_CONVERT_D1   token_evaluator(AO_MS5607_CONVERT_D1_, AO_MS5607_BARO_OVERSAMPLE)
175 #define AO_CONVERT_D2   token_evaluator(AO_MS5607_CONVERT_D2_, AO_MS5607_TEMP_OVERSAMPLE)
176
177 void
178 ao_ms5607_sample(struct ao_ms5607_sample *sample)
179 {
180         sample->pres = ao_ms5607_get_sample(AO_CONVERT_D1);
181         sample->temp = ao_ms5607_get_sample(AO_CONVERT_D2);
182 }
183
184 #include "ao_ms5607_convert.c"
185
186 #if HAS_TASK
187 struct ao_ms5607_sample ao_ms5607_current;
188
189 static void
190 ao_ms5607(void)
191 {
192         ao_ms5607_setup();
193         for (;;)
194         {
195                 ao_ms5607_sample(&ao_ms5607_current);
196                 ao_arch_critical(
197                         AO_DATA_PRESENT(AO_DATA_MS5607);
198                         AO_DATA_WAIT();
199                         );
200         }
201 }
202
203 __xdata struct ao_task ao_ms5607_task;
204
205 void
206 ao_ms5607_info(void)
207 {
208         printf ("ms5607 reserved: %u\n", ms5607_prom.reserved);
209         printf ("ms5607 sens: %u\n", ms5607_prom.sens);
210         printf ("ms5607 off: %u\n", ms5607_prom.off);
211         printf ("ms5607 tcs: %u\n", ms5607_prom.tcs);
212         printf ("ms5607 tco: %u\n", ms5607_prom.tco);
213         printf ("ms5607 tref: %u\n", ms5607_prom.tref);
214         printf ("ms5607 tempsens: %u\n", ms5607_prom.tempsens);
215         printf ("ms5607 crc: %u\n", ms5607_prom.crc);
216 }
217
218 static void
219 ao_ms5607_dump(void)
220 {
221         struct ao_ms5607_value value;
222
223         ao_ms5607_convert(&ao_ms5607_current, &value);
224         printf ("Pressure:    %8u %8d\n", ao_ms5607_current.pres, value.pres);
225         printf ("Temperature: %8u %8d\n", ao_ms5607_current.temp, value.temp);
226         printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
227 }
228
229 __code struct ao_cmds ao_ms5607_cmds[] = {
230         { ao_ms5607_dump,       "B\0Display MS5607 data" },
231         { 0, NULL },
232 };
233 #endif /* HAS_TASK */
234
235 void
236 ao_ms5607_init(void)
237 {
238         ms5607_configured = 0;
239         ao_spi_init_cs(AO_MS5607_CS_PORT, (1 << AO_MS5607_CS_PIN));
240
241 #if HAS_TASK
242         ao_cmd_register(&ao_ms5607_cmds[0]);
243         ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
244 #endif
245
246         /* Configure the MISO pin as an interrupt; when the
247          * conversion is complete, the MS5607 will raise this
248          * pin as a signal
249          */
250         ao_exti_setup(AO_MS5607_MISO_PORT,
251                       AO_MS5607_MISO_PIN,
252                       AO_EXTI_MODE_RISING,
253                       ao_ms5607_isr);
254
255 #ifdef STM_MODER_ALTERNATE
256         /* Reset the pin from INPUT to ALTERNATE so that SPI works
257          * This needs an abstraction at some point...
258          */
259         stm_moder_set(AO_MS5607_MISO_PORT,
260                       AO_MS5607_MISO_PIN,
261                       STM_MODER_ALTERNATE);
262 #endif
263 }
264
265 #endif