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.
19 #include "ao_ms5607.h"
21 static struct ms5607_prom ms5607_prom;
22 static uint8_t ms5607_configured;
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);
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);
37 ao_ms5607_reset(void) {
40 cmd = AO_MS5607_RESET;
42 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
43 ao_delay(AO_MS_TO_TICKS(10));
48 ao_ms5607_crc(uint8_t *prom)
50 uint8_t crc_byte = prom[15];
57 for (cnt = 0; cnt < 16; cnt++) {
59 for (n_bit = 8; n_bit > 0; n_bit--) {
61 n_rem = (n_rem << 1) ^ 0x3000;
66 n_rem = (n_rem >> 12) & 0xf;
72 ao_ms5607_prom_read(struct ms5607_prom *prom)
78 r = (uint16_t *) prom;
79 for (addr = 0; addr < 8; addr++) {
80 uint8_t cmd = AO_MS5607_PROM_READ(addr);
82 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
83 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
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));
91 #if __BYTE_ORDER == __LITTLE_ENDIAN
93 r = (uint16_t *) prom;
94 for (addr = 0; addr < 8; addr++) {
96 *r++ = (t << 8) | (t >> 8);
102 ao_ms5607_setup(void)
104 if (ms5607_configured)
106 ms5607_configured = 1;
108 ao_ms5607_prom_read(&ms5607_prom);
112 ao_ms5607_convert(uint8_t cmd) {
117 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
120 ao_delay(AO_MS_TO_TICKS(20));
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);
128 return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
132 ao_ms5607_sample(struct ao_ms5607_sample *sample)
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);
149 dT = D2 - ((int32_t) ms5607_prom.tref << 8);
151 TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
153 OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
155 SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
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;
163 int32_t TEMPP = TEMP + 1500;
164 int64_t TEMPP2 = TEMPP * TEMPP;
165 OFF2 = OFF2 + 15 * TEMPP2;
166 SENS2 = SENS2 + 8 * TEMPP2;
173 P = ((((int64_t) D1 * SENS) >> 21) - OFF) >> 15;
181 struct ao_ms5607_sample sample;
183 ao_ms5607_sample(&sample);
184 printf ("Temperature: %d\n", sample.temp);
185 printf ("Pressure %d\n", sample.pres);
188 __code struct ao_cmds ao_ms5607_cmds[] = {
189 { ao_ms5607_dump, "p\0Display MS5607 data" },
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));