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