altos: Use ao_spi_get/put_bit in MS5607 driver
[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_bit(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, AO_MS5607_SPI_INDEX, AO_SPI_SPEED_FAST);
30 }
31
32 static void
33 ao_ms5607_stop(void) {
34         ao_spi_put_bit(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, 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         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 ao_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 #if HAS_TASK
90                 printf ("MS5607 PROM CRC error (computed %x actual %x)\n",
91                         crc, (((uint8_t *) prom)[15] & 0xf));
92                 flush();
93 #endif
94                 ao_panic(AO_PANIC_SELF_TEST_MS5607);
95         }
96
97 #if __BYTE_ORDER == __LITTLE_ENDIAN
98         /* Byte swap */
99         r = (uint16_t *) prom;
100         for (addr = 0; addr < 8; addr++) {
101                 uint16_t        t = *r;
102                 *r++ = (t << 8) | (t >> 8);
103         }
104 #endif
105 }
106
107 void
108 ao_ms5607_setup(void)
109 {
110         if (ms5607_configured)
111                 return;
112         ms5607_configured = 1;
113         ao_ms5607_reset();
114         ao_ms5607_prom_read(&ms5607_prom);
115 }
116
117 static volatile uint8_t ao_ms5607_done;
118
119 static void
120 ao_ms5607_isr(void)
121 {
122         ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
123         ao_ms5607_done = 1;
124         ao_wakeup((void *) &ao_ms5607_done);
125 }
126
127 static uint32_t
128 ao_ms5607_get_sample(uint8_t cmd) {
129         uint8_t reply[3];
130         uint8_t read;
131
132         ao_ms5607_done = 0;
133
134         ao_ms5607_start();
135         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
136
137         ao_exti_enable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
138
139 #if AO_MS5607_PRIVATE_PINS
140         ao_spi_put(AO_MS5607_SPI_INDEX);
141 #endif
142         ao_arch_block_interrupts();
143         while (!ao_ms5607_done)
144                 ao_sleep((void *) &ao_ms5607_done);
145         ao_arch_release_interrupts();
146 #if AO_MS5607_PRIVATE_PINS
147         stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 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 #ifndef AO_MS5607_BARO_OVERSAMPLE
162 #define AO_MS5607_BARO_OVERSAMPLE       2048
163 #endif
164
165 #ifndef AO_MS5607_TEMP_OVERSAMPLE
166 #define AO_MS5607_TEMP_OVERSAMPLE       AO_MS5607_BARO_OVERSAMPLE
167 #endif
168
169 #define token_paster(x,y)       x ## y
170 #define token_evaluator(x,y)    token_paster(x,y)
171
172 #define AO_CONVERT_D1   token_evaluator(AO_MS5607_CONVERT_D1_, AO_MS5607_BARO_OVERSAMPLE)
173 #define AO_CONVERT_D2   token_evaluator(AO_MS5607_CONVERT_D2_, AO_MS5607_TEMP_OVERSAMPLE)
174
175 void
176 ao_ms5607_sample(struct ao_ms5607_sample *sample)
177 {
178         sample->pres = ao_ms5607_get_sample(AO_CONVERT_D1);
179         sample->temp = ao_ms5607_get_sample(AO_CONVERT_D2);
180 }
181
182 #include "ao_ms5607_convert.c"
183
184 #if HAS_TASK
185 struct ao_ms5607_sample ao_ms5607_current;
186
187 static void
188 ao_ms5607(void)
189 {
190         ao_ms5607_setup();
191         for (;;)
192         {
193                 ao_ms5607_sample(&ao_ms5607_current);
194                 ao_arch_critical(
195                         AO_DATA_PRESENT(AO_DATA_MS5607);
196                         AO_DATA_WAIT();
197                         );
198         }
199 }
200
201 __xdata struct ao_task ao_ms5607_task;
202
203 void
204 ao_ms5607_info(void)
205 {
206         printf ("ms5607 reserved: %u\n", ms5607_prom.reserved);
207         printf ("ms5607 sens: %u\n", ms5607_prom.sens);
208         printf ("ms5607 off: %u\n", ms5607_prom.off);
209         printf ("ms5607 tcs: %u\n", ms5607_prom.tcs);
210         printf ("ms5607 tco: %u\n", ms5607_prom.tco);
211         printf ("ms5607 tref: %u\n", ms5607_prom.tref);
212         printf ("ms5607 tempsens: %u\n", ms5607_prom.tempsens);
213         printf ("ms5607 crc: %u\n", ms5607_prom.crc);
214 }
215
216 static void
217 ao_ms5607_dump(void)
218 {
219         struct ao_ms5607_value value;
220
221         ao_ms5607_convert(&ao_ms5607_current, &value);
222         printf ("Pressure:    %8u %8d\n", ao_ms5607_current.pres, value.pres);
223         printf ("Temperature: %8u %8d\n", ao_ms5607_current.temp, value.temp);
224         printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
225 }
226
227 __code struct ao_cmds ao_ms5607_cmds[] = {
228         { ao_ms5607_dump,       "B\0Display MS5607 data" },
229         { 0, NULL },
230 };
231 #endif /* HAS_TASK */
232
233 void
234 ao_ms5607_init(void)
235 {
236         ms5607_configured = 0;
237         ao_spi_init_cs(AO_MS5607_CS_PORT, (1 << AO_MS5607_CS_PIN));
238
239 #if HAS_TASK
240         ao_cmd_register(&ao_ms5607_cmds[0]);
241         ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
242 #endif
243
244         /* Configure the MISO pin as an interrupt; when the
245          * conversion is complete, the MS5607 will raise this
246          * pin as a signal
247          */
248         ao_exti_setup(AO_MS5607_MISO_PORT,
249                       AO_MS5607_MISO_PIN,
250                       AO_EXTI_MODE_RISING,
251                       ao_ms5607_isr);
252
253 #ifdef STM_MODER_ALTERNATE
254         /* Reset the pin from INPUT to ALTERNATE so that SPI works
255          * This needs an abstraction at some point...
256          */
257         stm_moder_set(AO_MS5607_MISO_PORT,
258                       AO_MS5607_MISO_PIN,
259                       STM_MODER_ALTERNATE);
260 #endif
261 }
262
263 #endif