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 __xdata struct ao_ms5607_prom ao_ms5607_prom;
25 static __xdata uint8_t ms5607_configured;
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);
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);
38 ao_ms5607_reset(void) {
41 cmd = AO_MS5607_RESET;
43 ao_spi_send(DATA_TO_XDATA(&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];
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(__xdata struct ao_ms5607_prom *prom)
78 r = (__xdata uint16_t *) prom;
79 for (addr = 0; addr < 8; addr++) {
80 uint8_t cmd = AO_MS5607_PROM_READ(addr);
82 ao_spi_send(DATA_TO_XDATA(&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)) {
90 printf ("MS5607 PROM CRC error (computed %x actual %x)\n",
91 crc, (((uint8_t *) prom)[15] & 0xf));
94 ao_panic(AO_PANIC_SELF_TEST_MS5607);
97 #if __BYTE_ORDER == __LITTLE_ENDIAN
99 r = (uint16_t *) prom;
100 for (addr = 0; addr < 8; addr++) {
102 *r++ = (t << 8) | (t >> 8);
108 ao_ms5607_setup(void)
110 if (ms5607_configured)
112 ms5607_configured = 1;
114 ao_ms5607_prom_read(&ao_ms5607_prom);
117 static __xdata volatile uint8_t ao_ms5607_done;
122 ao_exti_disable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
124 ao_wakeup((__xdata void *) &ao_ms5607_done);
128 ao_ms5607_get_sample(uint8_t cmd) {
129 __xdata uint8_t reply[3];
130 __xdata uint8_t read;
135 ao_spi_send(DATA_TO_XDATA(&cmd), 1, AO_MS5607_SPI_INDEX);
137 ao_exti_enable(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN);
139 #if AO_MS5607_PRIVATE_PINS
140 ao_spi_put(AO_MS5607_SPI_INDEX);
142 ao_arch_block_interrupts();
143 while (!ao_gpio_get(AO_MS5607_MISO_PORT, AO_MS5607_MISO_PIN, AO_MS5607_MISO) &&
145 ao_sleep((void *) &ao_ms5607_done);
146 ao_arch_release_interrupts();
147 #if AO_MS5607_PRIVATE_PINS
148 stm_gpio_set(AO_MS5607_CS_PORT, AO_MS5607_CS_PIN, 1);
154 read = AO_MS5607_ADC_READ;
155 ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
156 ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
159 return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
162 #ifndef AO_MS5607_BARO_OVERSAMPLE
163 #define AO_MS5607_BARO_OVERSAMPLE 2048
166 #ifndef AO_MS5607_TEMP_OVERSAMPLE
167 #define AO_MS5607_TEMP_OVERSAMPLE AO_MS5607_BARO_OVERSAMPLE
170 #define token_paster(x,y) x ## y
171 #define token_evaluator(x,y) token_paster(x,y)
173 #define AO_CONVERT_D1 token_evaluator(AO_MS5607_CONVERT_D1_, AO_MS5607_BARO_OVERSAMPLE)
174 #define AO_CONVERT_D2 token_evaluator(AO_MS5607_CONVERT_D2_, AO_MS5607_TEMP_OVERSAMPLE)
177 ao_ms5607_sample(__xdata struct ao_ms5607_sample *sample)
179 sample->pres = ao_ms5607_get_sample(AO_CONVERT_D1);
180 sample->temp = ao_ms5607_get_sample(AO_CONVERT_D2);
184 #include "ao_ms5607_convert_8051.c"
186 #include "ao_ms5607_convert.c"
190 __xdata struct ao_ms5607_sample ao_ms5607_current;
198 ao_ms5607_sample(&ao_ms5607_current);
199 ao_arch_block_interrupts();
200 AO_DATA_PRESENT(AO_DATA_MS5607);
202 ao_arch_release_interrupts();
206 __xdata struct ao_task ao_ms5607_task;
211 printf ("ms5607 reserved: %u\n", ao_ms5607_prom.reserved);
212 printf ("ms5607 sens: %u\n", ao_ms5607_prom.sens);
213 printf ("ms5607 off: %u\n", ao_ms5607_prom.off);
214 printf ("ms5607 tcs: %u\n", ao_ms5607_prom.tcs);
215 printf ("ms5607 tco: %u\n", ao_ms5607_prom.tco);
216 printf ("ms5607 tref: %u\n", ao_ms5607_prom.tref);
217 printf ("ms5607 tempsens: %u\n", ao_ms5607_prom.tempsens);
218 printf ("ms5607 crc: %u\n", ao_ms5607_prom.crc);
224 __xdata struct ao_ms5607_value value;
226 ao_ms5607_convert(&ao_ms5607_current, &value);
227 printf ("Pressure: %8lu %8ld\n", ao_ms5607_current.pres, value.pres);
228 printf ("Temperature: %8lu %8ld\n", ao_ms5607_current.temp, value.temp);
229 printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres));
232 __code struct ao_cmds ao_ms5607_cmds[] = {
233 { ao_ms5607_dump, "B\0Display MS5607 data" },
236 #endif /* HAS_TASK */
241 ms5607_configured = 0;
242 ao_spi_init_cs(AO_MS5607_CS_PORT, (1 << AO_MS5607_CS_PIN));
245 ao_cmd_register(&ao_ms5607_cmds[0]);
246 ao_add_task(&ao_ms5607_task, ao_ms5607, "ms5607");
249 /* Configure the MISO pin as an interrupt; when the
250 * conversion is complete, the MS5607 will raise this
253 ao_exti_setup(AO_MS5607_MISO_PORT,
256 AO_EXTI_PIN_NOCONFIGURE,