MIDAS
Loading...
Searching...
No Matches
rpi_temp.c
Go to the documentation of this file.
1
/******************************************************************** \
2
3
Name: rpi_temp.c
4
Created by: Stefan Ritt
5
6
Contents: Rapberry Pi sense hat temperature driver
7
8
Prerequisite: $ sudo apt-get install libi2c-dev i2c-tools
9
10
\********************************************************************/
11
12
#include <stdio.h>
13
#include <stdint.h>
14
#include <stdlib.h>
15
#include <stdarg.h>
16
#include <unistd.h>
17
#include <fcntl.h>
18
#include <string.h>
19
#include <linux/i2c-dev.h>
20
#include "
midas.h
"
21
22
/*---- globals -----------------------------------------------------*/
23
24
/* following structure contains private variables to the device
25
driver. It is necessary to store it here in case the device
26
driver is used for more than one device in one frontend. If it
27
would be stored in a global variable, one device could over-
28
write the other device's variables. */
29
30
typedef
struct
{
31
int
fd
;
32
}
RPI_TEMP_INFO
;
33
34
#define DEV_ID 0x5c
35
#define WHO_AM_I 0x0F
36
#define CTRL_REG1 0x20
37
#define CTRL_REG2 0x21
38
#define PRESS_OUT_XL 0x28
39
#define PRESS_OUT_L 0x29
40
#define PRESS_OUT_H 0x2A
41
#define TEMP_OUT_L 0x2B
42
#define TEMP_OUT_H 0x2C
43
44
/*---- device driver routines --------------------------------------*/
45
46
/* the init function creates a ODB record which contains the
47
settings and initialized it variables as well as the bus driver */
48
49
INT
rpi_temp_init
(
HNDLE
hkey
,
void
**
pinfo
)
50
{
51
RPI_TEMP_INFO
*
info
;
52
53
/* allocate info structure */
54
info
=
calloc
(1,
sizeof
(
RPI_TEMP_INFO
));
55
*
pinfo
=
info
;
56
57
/* open i2c comms */
58
if
((
info
->fd = open(
"/dev/i2c-1"
,
O_RDWR
)) < 0) {
59
perror
(
"Unable to open i2c device"
);
60
exit(1);
61
}
62
63
/* configure i2c slave */
64
if
(
ioctl
(
info
->fd,
I2C_SLAVE
,
DEV_ID
) < 0) {
65
perror
(
"Unable to configure i2c slave device"
);
66
close(
info
->fd);
67
exit(1);
68
}
69
70
/* check we are who we should be */
71
if
(
i2c_smbus_read_byte_data
(
info
->fd,
WHO_AM_I
) != 0xBD) {
72
printf
(
"%s\n"
,
"who_am_i error"
);
73
close(
info
->fd);
74
exit(1);
75
}
76
77
/* Power down the device (clean start) */
78
i2c_smbus_write_byte_data
(
info
->fd,
CTRL_REG1
, 0x00);
79
80
/* Turn on the pressure sensor analog front end in single shot mode */
81
i2c_smbus_write_byte_data
(
info
->fd,
CTRL_REG1
, 0x84);
82
83
return
FE_SUCCESS
;
84
}
85
86
/*----------------------------------------------------------------------------*/
87
88
INT
rpi_temp_exit
(
RPI_TEMP_INFO
*
info
)
89
{
90
close(
info
->fd);
91
92
/* free local variables */
93
free(
info
);
94
95
return
FE_SUCCESS
;
96
}
97
98
/*----------------------------------------------------------------------------*/
99
100
INT
rpi_temp_get
(
RPI_TEMP_INFO
*
info
,
INT
channel
,
float
*
pvalue
)
101
{
102
uint8_t
status
= 0;
103
uint8_t
temp_l
,
temp_h
;
104
int16_t
temp
;
105
float
deg
;
106
107
/* Run one-shot measurement (temperature and pressure), the set bit will be reset by the
108
* sensor itself after execution (self-clearing bit) */
109
i2c_smbus_write_byte_data
(
info
->fd,
CTRL_REG2
, 0x01);
110
111
/* Wait until the measurement is complete */
112
do
{
113
usleep
(25*1000);
/* 25 milliseconds */
114
status
=
i2c_smbus_read_byte_data
(
info
->fd,
CTRL_REG2
);
115
}
116
while
(
status
!= 0);
117
118
/* Read the temperature measurement (2 bytes to read) */
119
temp_l
=
i2c_smbus_read_byte_data
(
info
->fd,
TEMP_OUT_L
);
120
temp_h
=
i2c_smbus_read_byte_data
(
info
->fd,
TEMP_OUT_H
);
121
temp
=
temp_h
<< 8 |
temp_l
;
122
123
/* calculate temperature */
124
deg
= (42.5 + (
temp
/ 480.0));
125
*
pvalue
= ((
int
)(
deg
*100+0.5))/100.0;;
126
127
// printf("Temp = %.2f°C\n", *pvalue);
128
129
return
FE_SUCCESS
;
130
}
131
132
/*---- device driver entry point -----------------------------------*/
133
134
INT
rpi_temp
(
INT
cmd, ...)
135
{
136
va_list
argptr
;
137
HNDLE
hKey
;
138
INT
status
,
channel
;
139
float
*
pvalue
;
140
void
*
info
;
141
142
va_start(
argptr
, cmd);
143
status
=
FE_SUCCESS
;
144
145
switch
(cmd) {
146
case
CMD_INIT
:
147
hKey
=
va_arg
(
argptr
,
HNDLE
);
148
info
=
va_arg
(
argptr
,
void
*);
149
status
=
rpi_temp_init
(
hKey
,
info
);
150
break
;
151
152
case
CMD_EXIT
:
153
info
=
va_arg
(
argptr
,
void
*);
154
status
=
rpi_temp_exit
(
info
);
155
break
;
156
157
case
CMD_GET
:
158
info
=
va_arg
(
argptr
,
void
*);
159
channel
=
va_arg
(
argptr
,
INT
);
160
pvalue
=
va_arg
(
argptr
,
float
*);
161
status
=
rpi_temp_get
(
info
,
channel
,
pvalue
);
162
break
;
163
164
default
:
165
break
;
166
}
167
168
va_end(
argptr
);
169
170
return
status
;
171
}
172
173
/*------------------------------------------------------------------*/
FE_SUCCESS
#define FE_SUCCESS
Definition
midas.h:717
CMD_INIT
#define CMD_INIT
Definition
midas.h:762
CMD_GET
#define CMD_GET
Definition
midas.h:787
CMD_EXIT
#define CMD_EXIT
Definition
midas.h:763
info
void ** info
Definition
fesimdaq.cxx:41
channel
INT channel
Definition
lazylogger.cxx:198
hKey
HNDLE hKey
Definition
lazylogger.cxx:202
midas.h
HNDLE
INT HNDLE
Definition
midas.h:132
INT
int INT
Definition
midas.h:129
status
DWORD status
Definition
odbhist.cxx:39
h1_book
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition
rmidas.h:24
rpi_temp
INT rpi_temp(INT cmd,...)
Definition
rpi_temp.c:134
rpi_temp_get
INT rpi_temp_get(RPI_TEMP_INFO *info, INT channel, float *pvalue)
Definition
rpi_temp.c:100
rpi_temp_init
INT rpi_temp_init(HNDLE hkey, void **pinfo)
Definition
rpi_temp.c:49
TEMP_OUT_L
#define TEMP_OUT_L
Definition
rpi_temp.c:41
WHO_AM_I
#define WHO_AM_I
Definition
rpi_temp.c:35
rpi_temp_exit
INT rpi_temp_exit(RPI_TEMP_INFO *info)
Definition
rpi_temp.c:88
CTRL_REG1
#define CTRL_REG1
Definition
rpi_temp.c:36
DEV_ID
#define DEV_ID
Definition
rpi_temp.c:34
TEMP_OUT_H
#define TEMP_OUT_H
Definition
rpi_temp.c:42
CTRL_REG2
#define CTRL_REG2
Definition
rpi_temp.c:37
RPI_TEMP_INFO
Definition
rpi_temp.c:30
RPI_TEMP_INFO::fd
int fd
Definition
rpi_temp.c:31
examples
raspberrypi
rpi_temp.c
Generated on Sun May 4 2025 05:01:04 for MIDAS by
1.9.8