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