altos: Make ms5607 driver do something
[fw/altos] / src / drivers / ao_ms5607.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include "ao_ms5607.h"
20
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))
25
26 struct ms5607_prom {
27         uint16_t        reserved;
28         uint16_t        sens;
29         uint16_t        off;
30         uint16_t        tcs;
31         uint16_t        tco;
32         uint16_t        tref;
33         uint16_t        tempsens;
34         uint16_t        crc;
35 };
36
37 static struct ms5607_prom ms5607_prom;
38
39 static void
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);
43 }
44
45 static void
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);
49 }
50
51 static void
52 ao_ms5607_reset(void) {
53         uint8_t cmd;
54
55         cmd = AO_MS5607_RESET;
56         ao_ms5607_start();
57         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
58         ao_delay(AO_MS_TO_TICKS(100));
59         ao_ms5607_stop();
60 }
61
62 static uint16_t
63 ao_ms5607_prom_read(uint8_t addr)
64 {
65         uint8_t cmd = AO_MS5607_PROM_READ(addr);
66         uint8_t d[2];
67         uint16_t v;
68
69         ao_ms5607_start();
70         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
71         ao_spi_recv(d, 2, AO_MS5607_SPI_INDEX);
72         ao_ms5607_stop();
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);
75         return v;
76 }
77
78 static void
79 ao_ms5607_init_chip(void) {
80         uint8_t         addr;
81         uint16_t        *prom;
82         ao_ms5607_reset();
83         prom = &ms5607_prom.reserved;
84
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);
95 }
96
97 static uint32_t
98 ao_ms5607_convert(uint8_t cmd) {
99         uint8_t reply[3];
100         uint8_t read;
101
102         ao_ms5607_start();
103         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
104         ao_ms5607_stop();
105
106         ao_delay(AO_MS_TO_TICKS(10));
107
108         ao_ms5607_start();
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);
112         ao_ms5607_stop();
113
114         return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
115 }
116
117 static void
118 ao_ms5607_dump(void)
119 {
120         uint8_t addr;
121         uint32_t D1, D2;
122         int32_t dT;
123         int32_t TEMP;
124         int64_t OFF;
125         int64_t SENS;
126         int32_t P;
127
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);
132
133         dT = D2 - ((int32_t) ms5607_prom.tref << 8);
134         
135         TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
136
137         OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
138
139         SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
140
141         if (TEMP < 2000) {
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;
146         }
147
148         P = ((((int64_t) D1 * SENS) >> 21) - OFF) >> 15;
149         
150         printf ("Temperature: %d\n", TEMP);
151         printf ("Pressure %d\n", P);
152 }
153
154 __code struct ao_cmds ao_ms5607_cmds[] = {
155         { ao_ms5607_init_chip,  "i\0Init MS5607" },
156         { ao_ms5607_dump,       "p\0Display MS5607 data" },
157         { 0, NULL },
158 };
159
160 void
161 ao_ms5607_init(void)
162 {
163         ao_cmd_register(&ao_ms5607_cmds[0]);
164
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);
168 }