altos: Check MS5607 CRC. Clean up MS5607 API
[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_ms5607.h"
20
21 static struct ms5607_prom ms5607_prom;
22 static uint8_t            ms5607_configured;
23
24 static void
25 ao_ms5607_start(void) {
26         ao_spi_get(AO_MS5607_SPI_INDEX);
27         stm_gpio_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, 0);
28 }
29
30 static void
31 ao_ms5607_stop(void) {
32         stm_gpio_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, 1);
33         ao_spi_put(AO_MS5607_SPI_INDEX);
34 }
35
36 static void
37 ao_ms5607_reset(void) {
38         uint8_t cmd;
39
40         cmd = AO_MS5607_RESET;
41         ao_ms5607_start();
42         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
43         ao_delay(AO_MS_TO_TICKS(10));
44         ao_ms5607_stop();
45 }
46
47 static uint8_t
48 ao_ms5607_crc(uint8_t *prom)
49 {
50         uint8_t         crc_byte = prom[15];
51         uint8_t         cnt;
52         uint16_t        n_rem = 0;
53         uint16_t        crc_read;
54         uint8_t         n_bit;
55
56         prom[15] = 0;
57         for (cnt = 0; cnt < 16; cnt++) {
58                 n_rem ^= prom[cnt];
59                 for (n_bit = 8; n_bit > 0; n_bit--) {
60                         if (n_rem & 0x8000)
61                                 n_rem = (n_rem << 1) ^ 0x3000;
62                         else
63                                 n_rem = (n_rem << 1);
64                 }
65         }
66         n_rem = (n_rem >> 12) & 0xf;
67         prom[15] = crc_byte;
68         return n_rem;
69 }
70
71 static void
72 ao_ms5607_prom_read(struct ms5607_prom *prom)
73 {
74         uint8_t         addr;
75         uint8_t         crc;
76         uint16_t        *r;
77
78         r = (uint16_t *) prom;
79         for (addr = 0; addr < 8; addr++) {
80                 uint8_t cmd = AO_MS5607_PROM_READ(addr);
81                 ao_ms5607_start();
82                 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
83                 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
84                 ao_ms5607_stop();
85                 r++;
86         }
87         crc = ao_ms5607_crc((uint8_t *) prom);
88         if (crc != (((uint8_t *) prom)[15] & 0xf))
89                 printf ("MS5607 PROM CRC error (computed %x actual %x)\n", crc, (((uint8_t *) prom)[15] & 0xf));
90
91 #if __BYTE_ORDER == __LITTLE_ENDIAN
92         /* Byte swap */
93         r = (uint16_t *) prom;
94         for (addr = 0; addr < 8; addr++) {
95                 uint16_t        t = *r;
96                 *r++ = (t << 8) | (t >> 8);
97         }
98 #endif
99 }
100
101 static void
102 ao_ms5607_setup(void)
103 {
104         if (ms5607_configured)
105                 return;
106         ms5607_configured = 1;
107         ao_ms5607_reset();
108         ao_ms5607_prom_read(&ms5607_prom);
109 }
110
111 static uint32_t
112 ao_ms5607_convert(uint8_t cmd) {
113         uint8_t reply[3];
114         uint8_t read;
115
116         ao_ms5607_start();
117         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
118         ao_ms5607_stop();
119
120         ao_delay(AO_MS_TO_TICKS(20));
121
122         ao_ms5607_start();
123         read = AO_MS5607_ADC_READ;
124         ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
125         ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
126         ao_ms5607_stop();
127
128         return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
129 }
130
131 void
132 ao_ms5607_sample(struct ao_ms5607_sample *sample)
133 {
134         uint8_t addr;
135         uint32_t D1, D2;
136         int32_t dT;
137         int32_t TEMP;
138         int64_t OFF;
139         int64_t SENS;
140         int32_t P;
141
142         ao_ms5607_setup();
143
144         D2 =  ao_ms5607_convert(AO_MS5607_CONVERT_D2_4096);
145         printf ("Conversion D2: %d\n", D2);
146         D1 =  ao_ms5607_convert(AO_MS5607_CONVERT_D1_4096);
147         printf ("Conversion D1: %d\n", D1);
148
149         dT = D2 - ((int32_t) ms5607_prom.tref << 8);
150         
151         TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
152
153         OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
154
155         SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
156
157         if (TEMP < 2000) {
158                 int32_t T2 = ((int64_t) dT * (int64_t) dT) >> 31;
159                 int32_t TEMPM = TEMP - 2000;
160                 int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4;
161                 int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM;
162                 if (TEMP < 1500) {
163                         int32_t TEMPP = TEMP + 1500;
164                         int64_t TEMPP2 = TEMPP * TEMPP;
165                         OFF2 = OFF2 + 15 * TEMPP2;
166                         SENS2 = SENS2 + 8 * TEMPP2;
167                 }
168                 TEMP -= T2;
169                 OFF -= OFF2;
170                 SENS -= SENS2;
171         }
172
173         P = ((((int64_t) D1 * SENS) >> 21) - OFF) >> 15;
174         sample->temp = TEMP;
175         sample->pres = P;
176 }
177
178 static void
179 ao_ms5607_dump(void)
180 {
181         struct ao_ms5607_sample sample;
182
183         ao_ms5607_sample(&sample);
184         printf ("Temperature: %d\n", sample.temp);
185         printf ("Pressure %d\n", sample.pres);
186 }
187
188 __code struct ao_cmds ao_ms5607_cmds[] = {
189         { ao_ms5607_dump,       "p\0Display MS5607 data" },
190         { 0, NULL },
191 };
192
193 void
194 ao_ms5607_init(void)
195 {
196         ms5607_configured = 0;
197         ao_cmd_register(&ao_ms5607_cmds[0]);
198         ao_spi_init_cs(AO_MS5607_CS_GPIO, (1 << AO_MS5607_CS));
199 }