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 #if HAS_MS5607 || HAS_MS5611
24 static struct ao_ms5607_prom ms5607_prom;
25 static uint8_t ms5607_configured;
28 ao_ms5607_start(void) {
29 ao_spi_get(AO_MS5607_SPI_INDEX,AO_SPI_SPEED_FAST);
30 ao_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, 0);
34 ao_ms5607_stop(void) {
35 ao_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_CS, 1);
36 ao_spi_put(AO_MS5607_SPI_INDEX);
40 ao_ms5607_reset(void) {
43 cmd = AO_MS5607_RESET;
45 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
46 ao_delay(AO_MS_TO_TICKS(10));
51 ao_ms5607_crc(uint8_t *prom)
53 uint8_t crc_byte = prom[15];
59 for (cnt = 0; cnt < 16; cnt++) {
61 for (n_bit = 8; n_bit > 0; n_bit--) {
63 n_rem = (n_rem << 1) ^ 0x3000;
68 n_rem = (n_rem >> 12) & 0xf;
74 ao_ms5607_prom_read(struct ao_ms5607_prom *prom)
80 r = (uint16_t *) prom;
81 for (addr = 0; addr < 8; addr++) {
82 uint8_t cmd = AO_MS5607_PROM_READ(addr);
84 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
85 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
89 crc = ao_ms5607_crc((uint8_t *) prom);
90 if (crc != (((uint8_t *) prom)[15] & 0xf)) {
92 printf ("MS5607 PROM CRC error (computed %x actual %x)\n",
93 crc, (((uint8_t *) prom)[15] & 0xf));
96 ao_panic(AO_PANIC_SELF_TEST_MS5607);
99 #if __BYTE_ORDER == __LITTLE_ENDIAN
101 r = (uint16_t *) prom;
102 for (addr = 0; addr < 8; addr++) {
104 *r++ = (t << 8) | (t >> 8);
110 ao_ms5607_setup(void)
112 if (ms5607_configured)
114 ms5607_configured = 1;
116 ao_ms5607_prom_read(&ms5607_prom);
119 static volatile uint8_t ao_ms5607_done;
124 ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
126 ao_wakeup((void *) &ao_ms5607_done);
130 ao_ms5607_get_sample(uint8_t cmd) {
137 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
139 ao_exti_enable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
141 #if AO_MS5607_PRIVATE_PINS
142 ao_spi_put(AO_MS5607_SPI_INDEX);
145 while (!ao_ms5607_done)
146 ao_sleep((void *) &ao_ms5607_done);
148 #if AO_MS5607_PRIVATE_PINS
149 stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 1);
155 read = AO_MS5607_ADC_READ;
156 ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
157 ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
160 return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
164 ao_ms5607_sample(struct ao_ms5607_sample *sample)
166 sample->pres = ao_ms5607_get_sample(AO_MS5607_CONVERT_D1_2048);
167 sample->temp = ao_ms5607_get_sample(AO_MS5607_CONVERT_D2_2048);
170 #include "ao_ms5607_convert.c"
173 struct ao_ms5607_sample ao_ms5607_current;
181 ao_ms5607_sample(&ao_ms5607_current);
183 AO_DATA_PRESENT(AO_DATA_MS5607);
189 __xdata struct ao_task ao_ms5607_task;
194 printf ("ms5607 reserved: %u\n", ms5607_prom.reserved);
195 printf ("ms5607 sens: %u\n", ms5607_prom.sens);
196 printf ("ms5607 off: %u\n", ms5607_prom.off);
197 printf ("ms5607 tcs: %u\n", ms5607_prom.tcs);
198 printf ("ms5607 tco: %u\n", ms5607_prom.tco);
199 printf ("ms5607 tref: %u\n", ms5607_prom.tref);
200 printf ("ms5607 tempsens: %u\n", ms5607_prom.tempsens);
201 printf ("ms5607 crc: %u\n", ms5607_prom.crc);
207 struct ao_ms5607_value value;
209 ao_ms5607_convert(&ao_ms5607_current, &value);
210 printf ("Pressure: %8u %8d\n", ao_ms5607_current.pres, value.pres);
211 printf ("Temperature: %8u %8d\n", ao_ms5607_current.temp, value.temp);
212 printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
215 __code struct ao_cmds ao_ms5607_cmds[] = {
216 { ao_ms5607_dump, "B\0Display MS5607 data" },
219 #endif /* HAS_TASK */
224 ms5607_configured = 0;
225 ao_spi_init_cs(AO_MS5607_CS_PORT, (1 << AO_MS5607_CS_PIN));
228 ao_cmd_register(&ao_ms5607_cmds[0]);
229 ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
232 /* Configure the MISO pin as an interrupt; when the
233 * conversion is complete, the MS5607 will raise this
236 ao_exti_setup(AO_MS5607_MISO_PORT,
241 #ifdef STM_MODER_ALTERNATE
242 /* Reset the pin from INPUT to ALTERNATE so that SPI works
243 * This needs an abstraction at some point...
245 stm_moder_set(AO_MS5607_MISO_PORT,
247 STM_MODER_ALTERNATE);