MIDAS
Loading...
Searching...
No Matches
rpi_temp.c File Reference
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <linux/i2c-dev.h>
#include "midas.h"
Include dependency graph for rpi_temp.c:

Go to the source code of this file.

Classes

struct  RPI_TEMP_INFO
 

Macros

#define DEV_ID   0x5c
 
#define WHO_AM_I   0x0F
 
#define CTRL_REG1   0x20
 
#define CTRL_REG2   0x21
 
#define PRESS_OUT_XL   0x28
 
#define PRESS_OUT_L   0x29
 
#define PRESS_OUT_H   0x2A
 
#define TEMP_OUT_L   0x2B
 
#define TEMP_OUT_H   0x2C
 

Functions

INT rpi_temp_init (HNDLE hkey, void **pinfo)
 
INT rpi_temp_exit (RPI_TEMP_INFO *info)
 
INT rpi_temp_get (RPI_TEMP_INFO *info, INT channel, float *pvalue)
 
INT rpi_temp (INT cmd,...)
 

Macro Definition Documentation

◆ CTRL_REG1

#define CTRL_REG1   0x20

Definition at line 36 of file rpi_temp.c.

◆ CTRL_REG2

#define CTRL_REG2   0x21

Definition at line 37 of file rpi_temp.c.

◆ DEV_ID

#define DEV_ID   0x5c

Definition at line 34 of file rpi_temp.c.

◆ PRESS_OUT_H

#define PRESS_OUT_H   0x2A

Definition at line 40 of file rpi_temp.c.

◆ PRESS_OUT_L

#define PRESS_OUT_L   0x29

Definition at line 39 of file rpi_temp.c.

◆ PRESS_OUT_XL

#define PRESS_OUT_XL   0x28

Definition at line 38 of file rpi_temp.c.

◆ TEMP_OUT_H

#define TEMP_OUT_H   0x2C

Definition at line 42 of file rpi_temp.c.

◆ TEMP_OUT_L

#define TEMP_OUT_L   0x2B

Definition at line 41 of file rpi_temp.c.

◆ WHO_AM_I

#define WHO_AM_I   0x0F

Definition at line 35 of file rpi_temp.c.

Function Documentation

◆ rpi_temp()

INT rpi_temp ( INT  cmd,
  ... 
)

Definition at line 134 of file rpi_temp.c.

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}
#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_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
INT rpi_temp_exit(RPI_TEMP_INFO *info)
Definition rpi_temp.c:88
Here is the call graph for this function:

◆ rpi_temp_exit()

INT rpi_temp_exit ( RPI_TEMP_INFO info)

Definition at line 88 of file rpi_temp.c.

89{
90 close(info->fd);
91
92 /* free local variables */
93 free(info);
94
95 return FE_SUCCESS;
96}
Here is the caller graph for this function:

◆ rpi_temp_get()

INT rpi_temp_get ( RPI_TEMP_INFO info,
INT  channel,
float pvalue 
)

Definition at line 100 of file rpi_temp.c.

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}
#define TEMP_OUT_L
Definition rpi_temp.c:41
#define TEMP_OUT_H
Definition rpi_temp.c:42
#define CTRL_REG2
Definition rpi_temp.c:37
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rpi_temp_init()

INT rpi_temp_init ( HNDLE  hkey,
void **  pinfo 
)

Definition at line 49 of file rpi_temp.c.

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}
#define WHO_AM_I
Definition rpi_temp.c:35
#define CTRL_REG1
Definition rpi_temp.c:36
#define DEV_ID
Definition rpi_temp.c:34
Here is the call graph for this function:
Here is the caller graph for this function: