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
30typedef struct {
31 int fd;
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
50{
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) */
79
80 /* Turn on the pressure sensor analog front end in single shot mode */
82
83 return FE_SUCCESS;
84}
85
86/*----------------------------------------------------------------------------*/
87
89{
90 close(info->fd);
91
92 /* free local variables */
93 free(info);
94
95 return FE_SUCCESS;
96}
97
98/*----------------------------------------------------------------------------*/
99
101{
102 uint8_t status = 0;
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) */
110
111 /* Wait until the measurement is complete */
112 do {
113 usleep(25*1000); /* 25 milliseconds */
115 }
116 while (status != 0);
117
118 /* Read the temperature measurement (2 bytes to read) */
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
134INT rpi_temp(INT cmd, ...)
135{
137 HNDLE hKey;
139 float *pvalue;
140 void *info;
141
142 va_start(argptr, cmd);
144
145 switch (cmd) {
146 case CMD_INIT:
148 info = va_arg(argptr, void *);
150 break;
151
152 case CMD_EXIT:
153 info = va_arg(argptr, void *);
155 break;
156
157 case CMD_GET:
158 info = va_arg(argptr, void *);
160 pvalue = va_arg(argptr, float *);
162 break;
163
164 default:
165 break;
166 }
167
168 va_end(argptr);
169
170 return status;
171}
172
173/*------------------------------------------------------------------*/
#define FE_SUCCESS
Definition midas.h:717
#define CMD_INIT
Definition midas.h:762
#define CMD_GET
Definition midas.h:787
#define CMD_EXIT
Definition midas.h:763
void ** info
Definition fesimdaq.cxx:41
INT channel
HNDLE hKey
INT HNDLE
Definition midas.h:132
int INT
Definition midas.h:129
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
INT rpi_temp(INT cmd,...)
Definition rpi_temp.c:134
INT rpi_temp_get(RPI_TEMP_INFO *info, INT channel, float *pvalue)
Definition rpi_temp.c:100
INT rpi_temp_init(HNDLE hkey, void **pinfo)
Definition rpi_temp.c:49
#define TEMP_OUT_L
Definition rpi_temp.c:41
#define WHO_AM_I
Definition rpi_temp.c:35
INT rpi_temp_exit(RPI_TEMP_INFO *info)
Definition rpi_temp.c:88
#define CTRL_REG1
Definition rpi_temp.c:36
#define DEV_ID
Definition rpi_temp.c:34
#define TEMP_OUT_H
Definition rpi_temp.c:42
#define CTRL_REG2
Definition rpi_temp.c:37