altos: Add DMA, SPI and MS5607 drivers
[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_ms5607_stop();
59 }
60
61 static uint16_t
62 ao_ms5607_prom_read(uint8_t addr)
63 {
64         uint8_t cmd = AO_MS5607_PROM_READ(addr);
65         uint8_t d[2];
66
67         ao_ms5607_start();
68         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
69         ao_spi_recv(d, 2, AO_MS5607_SPI_INDEX);
70         ao_ms5607_stop();
71         return ((uint16_t) d[0] << 8) | (uint16_t) d[1];
72 }
73
74 static void
75 ao_ms5607_init_chip(void) {
76         uint8_t         addr;
77         uint16_t        *prom;
78         ao_ms5607_reset();
79         prom = &ms5607_prom.reserved;
80
81         for (addr = 0; addr <= 7; addr++)
82                 prom[addr] = ao_ms5607_prom_read(addr);
83 }
84
85 static uint32_t
86 ao_ms5607_convert(uint8_t cmd) {
87         uint8_t reply[3];
88         uint8_t read;
89
90         ao_ms5607_start();
91         ao_spi_send(&cmd, 1, AO_MS5607_SPI_INDEX);
92         ao_ms5607_stop();
93
94         ao_delay(AO_MS_TO_TICKS(200));
95
96         ao_ms5607_start();
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);
100         ao_ms5607_stop();
101
102         return ((uint32_t) reply[0] << 16) | ((uint32_t) reply[1] << 8) | (uint32_t) reply[2];
103 }
104
105 static void
106 ao_ms5607_dump(void)
107 {
108         uint8_t addr;
109         uint32_t d1, d2;
110         int32_t dT;
111         int32_t TEMP;
112         int64_t OFF;
113         int64_t SENS;
114         int32_t P;
115
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);
129
130         dT = d2 - ((int32_t) ms5607_prom.tref << 8);
131         
132         TEMP = 2000 + (((int64_t) dT * ms5607_prom.tempsens) >> 23);
133
134         OFF = ((int64_t) ms5607_prom.off << 17) + (((int64_t) ms5607_prom.tco * dT) >> 6);
135
136         SENS = ((int64_t) ms5607_prom.sens << 16) + (((int64_t) ms5607_prom.tcs * dT) >> 7);
137
138         if (TEMP < 2000) {
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;
143         }
144
145         P = ((((int64_t) d1 * SENS) >> 21) - OFF) >> 15;
146         
147         printf ("Temperature: %d", TEMP);
148         printf ("Pressure %d\n", P);
149 }
150
151 __code struct ao_cmds ao_ms5607_cmds[] = {
152         { ao_ms5607_dump,       "p\0Display MS5607 data" },
153         { 0, NULL },
154 };
155
156 void
157 ao_ms5607_init(void)
158 {
159         ao_cmd_register(&ao_ms5607_cmds[0]);
160
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);
164 }