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);
62 ao_ms5607_prom_read(uint8_t addr)
64 uint8_t cmd = AO_MS5607_PROM_READ(addr);
68 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
69 ao_spi_recv(d, 2, AO_MS5607_SPI_INDEX);
71 return ((uint16_t) d[0] << 8) | (uint16_t) d[1];
75 ao_ms5607_init_chip(void) {
79 prom = &ms5607_prom.reserved;
81 for (addr = 0; addr <= 7; addr++)
82 prom[addr] = ao_ms5607_prom_read(addr);
86 ao_ms5607_convert(uint8_t cmd) {
91 ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
94 ao_delay(AO_MS_TO_TICKS(200));
97 read = AO_MS5607_ADC_READ;
98 ao_spi_send(&read, 1, AO_MS5607_SPI_INDEX);
99 ao_spi_recv(&reply, 3, AO_MS5607_SPI_INDEX);
102 return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
116 ao_ms5607_init_chip();
117 printf ("reserved: %d\n", ms5607_prom.reserved);
118 printf ("sens: %d\n", ms5607_prom.sens);
119 printf ("off: %d\n", ms5607_prom.off);
120 printf ("tcs: %d\n", ms5607_prom.tcs);
121 printf ("tco: %d\n", ms5607_prom.tco);
122 printf ("tref: %d\n", ms5607_prom.tref);
123 printf ("tempsens: %d\n", ms5607_prom.tempsens);
124 printf ("crc: %d\n", ms5607_prom.crc);
125 d1 = ao_ms5607_convert(AO_MS5607_CONVERT_D1_4096);
126 printf ("Conversion D1: %d\n", d1);
127 d2 = ao_ms5607_convert(AO_MS5607_CONVERT_D2_4096);
128 printf ("Conversion D2: %d\n", d2);
130 dT = d2 - ((int32_t) ms5607_prom.tref << 8);
132 TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
134 OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
136 SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
139 int32_t T2 = ((int64_t) dT * (int64_t) dT) >> 31;
140 int32_t TEMPM = TEMP - 2000;
141 int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4;
142 int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM;
145 P = ((((int64_t) d1 * SENS) >> 21) - OFF) >> 15;
147 printf ("Temperature: %d", TEMP);
148 printf ("Pressure %d\n", P);
151 __code struct ao_cmds ao_ms5607_cmds[] = {
152 { ao_ms5607_dump, "p\0Display MS5607 data" },
159 ao_cmd_register(&ao_ms5607_cmds[0]);
161 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN);
162 stm_gpio_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, 1);
163 stm_moder_set(&AO_MS5607_CS_GPIO, AO_MS5607_CS, STM_MODER_OUTPUT);