-
Notifications
You must be signed in to change notification settings - Fork 1
/
ams5915.c
executable file
·140 lines (119 loc) · 3.74 KB
/
ams5915.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
sensord - Sensor Interface for XCSoar Glide Computer - http://www.openvario.org/
Copyright (C) 2014 The openvario project
A detailed list of copyright holders can be found in the file "AUTHORS"
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "ams5915.h"
#include <time.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "def.h"
extern int g_debug;
extern FILE *fp_console;
/**
* @brief Establish connection to AMS5915 pressure sensor
* @param sensor pointer to sensor instance
* @param i2c_address
* @return result
*
* @date 24.03.2016 revised
*
*/
int ams5915_open(t_ams5915 *sensor, unsigned char i2c_address)
{
// local variables
int fd;
// try to open I2C Bus
fd = open("/dev/i2c-1", O_RDWR);
if (fd < 0) {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
return 1;
}
if (ioctl(fd, I2C_SLAVE, i2c_address) < 0) {
fprintf(stderr, "ioctl error: %s\n", strerror(errno));
return 1;
}
if (g_debug > 0) printf("Opened AMS5915 on 0x%x\n", i2c_address);
// assign file handle to sensor object
sensor->fd = fd;
sensor->address = i2c_address;
return (0);
}
/**
* @brief Initialize AMS5915 pressure sensor
* @param sensor pointer to sensor instance
* @return result
*
* @date 24.03.2016 revised
*
*/
int ams5915_init(t_ams5915 *sensor)
{
// set calibration data
sensor->digoutpmin = 1638; // from datasheet
sensor->digoutpmax = 14745; // from datasheet
sensor->pmin = 0; // from datasheet
sensor->pmax = 50; // from datasheet
sensor->sensp = (float)(sensor->digoutpmax - sensor->digoutpmin)/(sensor->pmax - sensor->pmin);
ddebug_print("%s @ 0x%x: sensp=%f\n", __func__, sensor->address, sensor->sensp);
return(0);
}
/**
* @brief Read pressure measurement from AMS5915 pressure sensor
* @param sensor pointer to sensor instance
* @return result
*
* @date 24.03.2016 revised
*
*/
int ams5915_measure(t_ams5915 *sensor)
{
//variables
uint8_t buf[10]={0x00};
if (read(sensor->fd, buf, 4) != 4) { // Read back data into buf[]
printf("Unable to read from slave\n");
return(1);
}
sensor->digoutp = ((buf[0] & (0x3F)) << 8) + buf[1];
sensor->digoutT = ((buf[2] << 8) + (buf[3] & 0xE0)) >> 5;
debug_print("%s @ 0x%x: digoutp=0x%x %d\n", __func__, sensor->address, sensor->digoutp, sensor->digoutp);
debug_print("%s @ 0x%x: digoutT=0x%x %d\n", __func__, sensor->address,sensor->digoutT, sensor->digoutT);
return(0);
}
/**
* @brief Calculate pressure from AMS5915 pressure sensor
* @param sensor pointer to sensor instance
* @return result
*
* @date 24.03.2016 revised
*
*/
int ams5915_calculate(t_ams5915 *sensor)
{
// calculate differential pressure
sensor->p = (((sensor->digoutp - sensor->digoutpmin)/sensor->sensp) + sensor->pmin);
// calculate temperature
sensor->T = ((sensor->digoutT * 200)/ (float)2048)-50;
// correct measured pressure
sensor->p = sensor->linearity * sensor->p + sensor->offset;
debug_print("%s @ 0x%x: Pressure: %f Temp: %f\n", __func__, sensor->address, sensor->p, sensor->T);
return (0);
}