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 #define AO_MS5607_CS_GPIO stm_gpioc
22 #define AO_MS5607_CS 4
23 #define AO_MS5607_CS_MASK (1 << AO_MS5607_CS)
24 #define AO_MS5607_SPI_INDEX (STM_SPI_INDEX(1))
37 static struct ms5607_prom ms5607_prom;
40 ao_ms5607_start(void) {
41 ao_spi_get(AO_MS5607_SPI_INDEX);
42 stm_gpio_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, 0);
46 ao_ms5607_stop(void) {
47 stm_gpio_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, 1);
48 ao_spi_put(AO_MS5607_SPI_INDEX);
52 ao_ms5607_reset(void) {
55 cmd = AO_MS5607_RESET;
57 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
58 ao_delay(AO_MS_TO_TICKS(100));
63 ao_ms5607_prom_read(uint8_t addr)
65 uint8_t cmd = AO_MS5607_PROM_READ(addr);
70 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
71 ao_spi_recv(d, 2, AO_MS5607_SPI_INDEX);
73 v = ((uint16_t) d[0] << 8) | (uint16_t) d[1];
74 // printf ("ms5607_prom_read recv %02x %02x -> %04x\n", d[0], d[1], v);
79 ao_ms5607_init_chip(void) {
83 prom = &ms5607_prom.reserved;
85 for (addr = 0; addr <= 7; addr++)
86 prom[addr] = ao_ms5607_prom_read(addr);
87 printf ("reserved: %d\n", ms5607_prom.reserved);
88 printf ("sens: %d\n", ms5607_prom.sens);
89 printf ("off: %d\n", ms5607_prom.off);
90 printf ("tcs: %d\n", ms5607_prom.tcs);
91 printf ("tco: %d\n", ms5607_prom.tco);
92 printf ("tref: %d\n", ms5607_prom.tref);
93 printf ("tempsens: %d\n", ms5607_prom.tempsens);
94 printf ("crc: %d\n", ms5607_prom.crc);
98 ao_ms5607_convert(uint8_t cmd) {
103 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
106 ao_delay(AO_MS_TO_TICKS(10));
109 read = AO_MS5607_ADC_READ;
110 ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
111 ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
114 return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
128 D2 = ao_ms5607_convert(AO_MS5607_CONVERT_D2_4096);
129 printf ("Conversion D2: %d\n", D2);
130 D1 = ao_ms5607_convert(AO_MS5607_CONVERT_D1_4096);
131 printf ("Conversion D1: %d\n", D1);
133 dT = D2 - ((int32_t) ms5607_prom.tref << 8);
135 TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
137 OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
139 SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
142 int32_t T2 = ((int64_t) dT * (int64_t) dT) >> 31;
143 int32_t TEMPM = TEMP - 2000;
144 int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4;
145 int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM;
148 P = ((((int64_t) D1 * SENS) >> 21) - OFF) >> 15;
150 printf ("Temperature: %d\n", TEMP);
151 printf ("Pressure %d\n", P);
154 __code struct ao_cmds ao_ms5607_cmds[] = {
155 { ao_ms5607_init_chip, "i\0Init MS5607" },
156 { ao_ms5607_dump, "p\0Display MS5607 data" },
163 ao_cmd_register(&ao_ms5607_cmds[0]);
165 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN);
166 stm_gpio_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, 1);
167 stm_moder_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, STM_MODER_OUTPUT);