2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #include "ao_ms5607.h"
22 static struct ao_ms5607_prom ms5607_prom;
23 static uint8_t ms5607_configured;
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);
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);
38 ao_ms5607_reset(void) {
41 cmd = AO_MS5607_RESET;
43 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
44 ao_delay(AO_MS_TO_TICKS(10));
49 ao_ms5607_crc(uint8_t *prom)
51 uint8_t crc_byte = prom[15];
58 for (cnt = 0; cnt < 16; cnt++) {
60 for (n_bit = 8; n_bit > 0; n_bit--) {
62 n_rem = (n_rem << 1) ^ 0x3000;
67 n_rem = (n_rem >> 12) & 0xf;
73 ao_ms5607_prom_read(struct ao_ms5607_prom *prom)
79 r = (uint16_t *) prom;
80 for (addr = 0; addr < 8; addr++) {
81 uint8_t cmd = AO_MS5607_PROM_READ(addr);
83 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
84 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
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));
93 ao_panic(AO_PANIC_SELF_TEST);
96 #if __BYTE_ORDER == __LITTLE_ENDIAN
98 r = (uint16_t *) prom;
99 for (addr = 0; addr < 8; addr++) {
101 *r++ = (t << 8) | (t >> 8);
107 ao_ms5607_setup(void)
109 if (ms5607_configured)
111 ms5607_configured = 1;
113 ao_ms5607_prom_read(&ms5607_prom);
116 static uint8_t ao_ms5607_done;
121 ao_exti_disable(AO_MS5607_MISO_GPIO, AO_MS5607_MISO);
123 ao_wakeup(&ao_ms5607_done);
127 ao_ms5607_get_sample(uint8_t cmd) {
135 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
136 ao_exti_enable(AO_MS5607_MISO_GPIO, AO_MS5607_MISO);
138 while (!ao_ms5607_done)
139 ao_sleep(&ao_ms5607_done);
144 read = AO_MS5607_ADC_READ;
145 ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
146 ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
149 return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
153 ao_ms5607_sample(struct ao_ms5607_sample *sample)
155 sample->pres = ao_ms5607_get_sample(AO_MS5607_CONVERT_D1_2048);
156 sample->temp = ao_ms5607_get_sample(AO_MS5607_CONVERT_D2_2048);
160 ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value)
169 dT = sample->temp - ((int32_t) ms5607_prom.tref << 8);
171 TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
173 OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
175 SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
178 int32_t T2 = ((int64_t) dT * (int64_t) dT) >> 31;
179 int32_t TEMPM = TEMP - 2000;
180 int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4;
181 int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM;
183 int32_t TEMPP = TEMP + 1500;
184 int64_t TEMPP2 = TEMPP * TEMPP;
185 OFF2 = OFF2 + 15 * TEMPP2;
186 SENS2 = SENS2 + 8 * TEMPP2;
193 value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15;
197 struct ao_ms5607_sample ao_ms5607_current;
198 uint8_t ao_ms5607_valid;
206 static struct ao_ms5607_sample ao_ms5607_next;
207 ao_ms5607_sample(&ao_ms5607_next);
209 ao_ms5607_current = ao_ms5607_next;
216 __xdata struct ao_task ao_ms5607_task;
221 printf ("ms5607 reserved: %u\n", ms5607_prom.reserved);
222 printf ("ms5607 sens: %u\n", ms5607_prom.sens);
223 printf ("ms5607 off: %u\n", ms5607_prom.off);
224 printf ("ms5607 tcs: %u\n", ms5607_prom.tcs);
225 printf ("ms5607 tco: %u\n", ms5607_prom.tco);
226 printf ("ms5607 tref: %u\n", ms5607_prom.tref);
227 printf ("ms5607 tempsens: %u\n", ms5607_prom.tempsens);
228 printf ("ms5607 crc: %u\n", ms5607_prom.crc);
234 struct ao_ms5607_sample sample;
235 struct ao_ms5607_value value;
237 sample = ao_ms5607_current;
238 ao_ms5607_convert(&sample, &value);
239 printf ("Pressure: %8u %8d\n", sample.pres, value.pres);
240 printf ("Temperature: %8u %8d\n", sample.temp, value.temp);
241 printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
244 __code struct ao_cmds ao_ms5607_cmds[] = {
245 { ao_ms5607_dump, "B\0Display MS5607 data" },
252 ms5607_configured = 0;
254 ao_cmd_register(&ao_ms5607_cmds[0]);
255 ao_spi_init_cs(AO_MS5607_CS_GPIO, (1 << AO_MS5607_CS));
257 ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
259 /* Configure the MISO pin as an interrupt; when the
260 * conversion is complete, the MS5607 will raise this
263 ao_exti_setup(AO_MS5607_MISO_GPIO,
268 /* Reset the pin from INPUT to ALTERNATE so that SPI works
269 * This needs an abstraction at some point...
271 stm_moder_set(AO_MS5607_MISO_GPIO,
273 STM_MODER_ALTERNATE);