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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
21 #include <ao_ms5607.h>
23 #if HAS_MS5607 || HAS_MS5611
25 struct ao_ms5607_prom ao_ms5607_prom;
26 static uint8_t ms5607_configured;
28 #define AO_MS5607_SPI_SPEED ao_spi_speed(20000000)
31 ao_ms5607_start(void) {
32 ao_spi_get_bit(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_SPI_INDEX, AO_MS5607_SPI_SPEED);
36 ao_ms5607_stop(void) {
37 ao_spi_put_bit(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, AO_MS5607_SPI_INDEX);
41 ao_ms5607_reset(void) {
44 cmd = AO_MS5607_RESET;
46 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
47 ao_delay(AO_MS_TO_TICKS(10));
52 ao_ms5607_crc(uint8_t *prom)
54 uint8_t crc_byte = prom[15];
61 for (cnt = 0; cnt < 16; cnt++) {
62 n_rem ^= (uint16_t) *p++;
63 for (n_bit = 8; n_bit > 0; n_bit--) {
65 n_rem = (uint16_t) ((n_rem << 1) ^ 0x3000U);
70 n_rem = (n_rem >> 12) & 0xf;
72 return (uint8_t) n_rem;
76 ao_ms5607_prom_valid(uint8_t *prom)
82 /* Look for a value other than 0x0000 or 0xffff */
84 for (i = 0; i < 16; i++)
90 crc = ao_ms5607_crc(prom);
91 if (crc != (prom[15] & 0xf))
98 ao_ms5607_prom_read(struct ao_ms5607_prom *prom)
103 r = (uint16_t *) prom;
104 for (addr = 0; addr < 8; addr++) {
105 uint8_t cmd = AO_MS5607_PROM_READ(addr);
107 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
108 ao_spi_recv(r, 2, AO_MS5607_SPI_INDEX);
114 if (!ao_ms5607_prom_valid((uint8_t *) prom)) {
115 #if HAS_SENSOR_ERRORS
116 AO_SENSOR_ERROR(AO_DATA_MS5607);
118 ao_panic(AO_PANIC_SELF_TEST_MS5607);
122 #if __BYTE_ORDER == __LITTLE_ENDIAN
124 r = (uint16_t *) prom;
125 for (addr = 0; addr < 8; addr++) {
126 uint8_t *t = (uint8_t *) r;
136 ao_ms5607_setup(void)
138 if (ms5607_configured)
140 ms5607_configured = 1;
142 ao_ms5607_prom_read(&ao_ms5607_prom);
145 static volatile uint8_t ao_ms5607_done;
150 ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
151 ao_wakeup((void *) &ao_ms5607_done);
155 ao_ms5607_get_sample(uint8_t cmd) {
160 ao_exti_enable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
162 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
164 #if AO_MS5607_PRIVATE_PINS
165 ao_spi_put_pins(AO_MS5607_SPI_INDEX);
167 ao_arch_block_interrupts();
169 #define ao_sleep_for(a,t) ao_sleep(a)
171 while (!ao_gpio_get(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN)) {
172 if (ao_sleep_for((void *) &ao_ms5607_done, AO_MS_TO_TICKS(10)))
175 ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
176 ao_arch_release_interrupts();
177 #if AO_MS5607_PRIVATE_PINS
178 ao_spi_clr_cs(AO_MS5607_CS_PORT, 1 << (AO_MS5607_CS_PIN));
184 reply[0] = AO_MS5607_ADC_READ;
185 #if defined(AO_SPI_DUPLEX) && AO_SPI_DUPLEX == 0
186 ao_spi_send(reply, 1, AO_MS5607_SPI_INDEX);
187 ao_spi_recv(reply+1, 3, AO_MS5607_SPI_INDEX);
189 ao_spi_duplex(&reply, &reply, 4, AO_MS5607_SPI_INDEX);
193 return ((uint32_t) reply[1] << 16) | ((uint32_t) reply[2] << 8) | (uint32_t) reply[3];
196 #ifndef AO_MS5607_BARO_OVERSAMPLE
197 #define AO_MS5607_BARO_OVERSAMPLE 2048
200 #ifndef AO_MS5607_TEMP_OVERSAMPLE
201 #define AO_MS5607_TEMP_OVERSAMPLE AO_MS5607_BARO_OVERSAMPLE
204 #define token_paster(x,y) x ## y
205 #define token_evaluator(x,y) token_paster(x,y)
207 #define AO_CONVERT_D1 token_evaluator(AO_MS5607_CONVERT_D1_, AO_MS5607_BARO_OVERSAMPLE)
208 #define AO_CONVERT_D2 token_evaluator(AO_MS5607_CONVERT_D2_, AO_MS5607_TEMP_OVERSAMPLE)
211 ao_ms5607_sample(struct ao_ms5607_sample *sample)
213 sample->pres = ao_ms5607_get_sample(AO_CONVERT_D1);
214 sample->temp = ao_ms5607_get_sample(AO_CONVERT_D2);
218 #include "ao_ms5607_convert_8051.c"
220 #include "ao_ms5607_convert.c"
223 #ifndef HAS_MS5607_TASK
224 #define HAS_MS5607_TASK HAS_TASK
227 struct ao_ms5607_sample ao_ms5607_current;
233 struct ao_ms5607_sample sample;
237 ao_ms5607_sample(&sample);
238 ao_arch_block_interrupts();
239 ao_ms5607_current = sample;
240 AO_DATA_PRESENT(AO_DATA_MS5607);
242 ao_arch_release_interrupts();
246 struct ao_task ao_ms5607_task;
256 printf ("ms5607 reserved: %u\n", ao_ms5607_prom.reserved);
257 printf ("ms5607 sens: %u\n", ao_ms5607_prom.sens);
258 printf ("ms5607 off: %u\n", ao_ms5607_prom.off);
259 printf ("ms5607 tcs: %u\n", ao_ms5607_prom.tcs);
260 printf ("ms5607 tco: %u\n", ao_ms5607_prom.tco);
261 printf ("ms5607 tref: %u\n", ao_ms5607_prom.tref);
262 printf ("ms5607 tempsens: %u\n", ao_ms5607_prom.tempsens);
263 printf ("ms5607 crc: %u\n", ao_ms5607_prom.crc);
269 struct ao_ms5607_value value;
272 ao_ms5607_sample(&ao_ms5607_current);
274 ao_ms5607_convert(&ao_ms5607_current, &value);
275 printf ("Pressure: %8lu %8ld\n", ao_ms5607_current.pres, value.pres);
276 printf ("Temperature: %8lu %8ld\n", ao_ms5607_current.temp, value.temp);
277 printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
280 const struct ao_cmds ao_ms5607_cmds[] = {
281 { ao_ms5607_dump, "B\0Display MS5607 data" },
284 #endif /* HAS_TASK */
289 ms5607_configured = 0;
290 ao_spi_init_cs(AO_MS5607_CS_PORT, (1 << AO_MS5607_CS_PIN));
293 ao_cmd_register(&ao_ms5607_cmds[0]);
296 ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
299 /* Configure the MISO pin as an interrupt; when the
300 * conversion is complete, the MS5607 will raise this
303 ao_exti_setup(AO_MS5607_MISO_PORT,
306 AO_EXTI_PIN_NOCONFIGURE,