Merge remote-tracking branch 'mjb/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; 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
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         stm_gpio_set(AO_MS5607_CS_GPIO, AO_MS5607_CS, 0);
31 }
32
33 static void
34 ao_ms5607_stop(void) {
35         stm_gpio_set(AO_MS5607_CS_GPIO, 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         uint16_t        crc_read;
57         uint8_t         n_bit;
58
59         prom[15] = 0;
60         for (cnt = 0; cnt < 16; cnt++) {
61                 n_rem ^= prom[cnt];
62                 for (n_bit = 8; n_bit > 0; n_bit--) {
63                         if (n_rem & 0x8000)
64                                 n_rem = (n_rem << 1) ^ 0x3000;
65                         else
66                                 n_rem = (n_rem << 1);
67                 }
68         }
69         n_rem = (n_rem >> 12) & 0xf;
70         prom[15] = crc_byte;
71         return n_rem;
72 }
73
74 static void
75 ao_ms5607_prom_read(struct ao_ms5607_prom *prom)
76 {
77         uint8_t         addr;
78         uint8_t         crc;
79         uint16_t        *r;
80
81         r = (uint16_t *) prom;
82         for (addr = 0; addr < 8; addr++) {
83                 uint8_t cmd = AO_MS5607_PROM_READ(addr);
84                 ao_ms5607_start();
85                 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
86                 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
87                 ao_ms5607_stop();
88                 r++;
89         }
90         crc = ao_ms5607_crc((uint8_t *) prom);
91         if (crc != (((uint8_t *) prom)[15] & 0xf)) {
92                 printf ("MS5607 PROM CRC error (computed %x actual %x)\n",
93                         crc, (((uint8_t *) prom)[15] & 0xf));
94                 flush();
95                 ao_panic(AO_PANIC_SELF_TEST);
96         }
97
98 #if __BYTE_ORDER == __LITTLE_ENDIAN
99         /* Byte swap */
100         r = (uint16_t *) prom;
101         for (addr = 0; addr < 8; addr++) {
102                 uint16_t        t = *r;
103                 *r++ = (t << 8) | (t >> 8);
104         }
105 #endif
106 }
107
108 static void
109 ao_ms5607_setup(void)
110 {
111         if (ms5607_configured)
112                 return;
113         ms5607_configured = 1;
114         ao_ms5607_reset();
115         ao_ms5607_prom_read(&ms5607_prom);
116 }
117
118 static uint8_t  ao_ms5607_done;
119
120 static void
121 ao_ms5607_isr(void)
122 {
123         ao_exti_disable(AO_MS5607_MISO_GPIO, AO_MS5607_MISO);
124         ao_ms5607_done = 1;
125         ao_wakeup(&ao_ms5607_done);
126 }
127
128 static uint32_t
129 ao_ms5607_get_sample(uint8_t cmd) {
130         uint8_t reply[3];
131         uint8_t read;
132         uint16_t now;
133
134         ao_ms5607_done = 0;
135
136         ao_ms5607_start();
137         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
138         ao_exti_enable(AO_MS5607_MISO_GPIO, AO_MS5607_MISO);
139 #if AO_MS5607_PRIVATE_PINS
140         ao_spi_put(AO_MS5607_SPI_INDEX);
141 #endif
142         cli();
143         while (!ao_ms5607_done)
144                 ao_sleep(&ao_ms5607_done);
145         sei();
146 #if AO_MS5607_PRIVATE_PINS
147         stm_gpio_set(AO_MS5607_CS_GPIO, AO_MS5607_CS, 1);
148 #else
149         ao_ms5607_stop();
150 #endif
151
152         ao_ms5607_start();
153         read = AO_MS5607_ADC_READ;
154         ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
155         ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
156         ao_ms5607_stop();
157
158         return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
159 }
160
161 void
162 ao_ms5607_sample(struct ao_ms5607_sample *sample)
163 {
164         sample->pres = ao_ms5607_get_sample(AO_MS5607_CONVERT_D1_2048);
165         sample->temp = ao_ms5607_get_sample(AO_MS5607_CONVERT_D2_2048);
166 }
167
168 void
169 ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value)
170 {
171         uint8_t addr;
172         int32_t dT;
173         int32_t TEMP;
174         int64_t OFF;
175         int64_t SENS;
176         int32_t P;
177
178         dT = sample->temp - ((int32_t) ms5607_prom.tref << 8);
179         
180         TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
181
182         OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
183
184         SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
185
186         if (TEMP < 2000) {
187                 int32_t T2 = ((int64_t) dT * (int64_t) dT) >> 31;
188                 int32_t TEMPM = TEMP - 2000;
189                 int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4;
190                 int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM;
191                 if (TEMP < 1500) {
192                         int32_t TEMPP = TEMP + 1500;
193                         int64_t TEMPP2 = TEMPP * TEMPP;
194                         OFF2 = OFF2 + 15 * TEMPP2;
195                         SENS2 = SENS2 + 8 * TEMPP2;
196                 }
197                 TEMP -= T2;
198                 OFF -= OFF2;
199                 SENS -= SENS2;
200         }
201
202         value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15;
203         value->temp = TEMP;
204 }
205
206 static void
207 ao_ms5607(void)
208 {
209         ao_ms5607_setup();
210         for (;;)
211         {
212                 ao_ms5607_sample((struct ao_ms5607_sample *) &ao_data_ring[ao_data_head].ms5607_raw);
213                 ao_arch_critical(
214                         AO_DATA_PRESENT(AO_DATA_MS5607);
215                         AO_DATA_WAIT();
216                         );
217         }
218 }
219
220 __xdata struct ao_task ao_ms5607_task;
221
222 void
223 ao_ms5607_info(void)
224 {
225         printf ("ms5607 reserved: %u\n", ms5607_prom.reserved);
226         printf ("ms5607 sens: %u\n", ms5607_prom.sens);
227         printf ("ms5607 off: %u\n", ms5607_prom.off);
228         printf ("ms5607 tcs: %u\n", ms5607_prom.tcs);
229         printf ("ms5607 tco: %u\n", ms5607_prom.tco);
230         printf ("ms5607 tref: %u\n", ms5607_prom.tref);
231         printf ("ms5607 tempsens: %u\n", ms5607_prom.tempsens);
232         printf ("ms5607 crc: %u\n", ms5607_prom.crc);
233 }
234
235 static void
236 ao_ms5607_dump(void)
237 {
238         struct ao_data  sample;
239         struct ao_ms5607_value value;
240
241         ao_data_get(&sample);
242         ao_ms5607_convert(&sample.ms5607_raw, &value);
243         printf ("Pressure:    %8u %8d\n", sample.ms5607_raw.pres, value.pres);
244         printf ("Temperature: %8u %8d\n", sample.ms5607_raw.temp, value.temp);
245         printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
246 }
247
248 __code struct ao_cmds ao_ms5607_cmds[] = {
249         { ao_ms5607_dump,       "B\0Display MS5607 data" },
250         { 0, NULL },
251 };
252
253 void
254 ao_ms5607_init(void)
255 {
256         ms5607_configured = 0;
257         ao_cmd_register(&ao_ms5607_cmds[0]);
258         ao_spi_init_cs(AO_MS5607_CS_GPIO, (1 << AO_MS5607_CS));
259
260         ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
261
262         /* Configure the MISO pin as an interrupt; when the
263          * conversion is complete, the MS5607 will raise this
264          * pin as a signal
265          */
266         ao_exti_setup(AO_MS5607_MISO_GPIO,
267                       AO_MS5607_MISO,
268                       AO_EXTI_MODE_RISING,
269                       ao_ms5607_isr);
270
271         /* Reset the pin from INPUT to ALTERNATE so that SPI works
272          * This needs an abstraction at some point...
273          */
274         stm_moder_set(AO_MS5607_MISO_GPIO,
275                       AO_MS5607_MISO,
276                       STM_MODER_ALTERNATE);
277 }
278
279 #endif