forked from NachiketKelkar/AESD-Project_1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp_i2c.h
125 lines (104 loc) · 4.63 KB
/
temp_i2c.h
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
/************************************************************************************************
* File name : temp_i2c.h *
* Authors : Nachiket Kelkar and Puneet Bansal *
* Description : The functions used for reading and configuring temperature sensor TMP102 for *
* getting the temperature values through i2c interface. *
* Tools used : GNU make, gcc, arm-linux-gcc. *
************************************************************************************************/
#include <stdint.h>
/* Format of meassge that is sent to temperature thread */
// typedef struct{
// int temperature_unit;
// char* source;
// }temp_msg;
/* Enumeration for temperature units available */
typedef enum {
Celsius,
Fahrenheit,
Kelvin,
}temperature_unit;
#define DEFAULT_UNIT Celsius;
/* Default slave address */
#define DEFAULT_SLAVE_ADDRESS 0X48
/* The defines for addresses of the registers */
#define TEMP_REG_ADDR 0x00
#define CONFIG_REG_ADDR 0x01
#define TLOW_REG_ADDR 0x02
#define THIGH_REG_ADDR 0x03
/* The macros for configuration register */
#define DEFAULT_CONFIG 0x60A0
#define MODE_12BIT (0 << 4)
#define MODE_13BIT (1 << 4)
#define REFRESH_025HZ (0 << 6)
#define REFRESH_1HZ (1 << 6)
#define REFRESH_4HZ (2 << 6)
#define REFRESH_8HZ (3 << 6)
#define SHUTDOWN_MODE_ENABLE (1 << 8)
#define SHUTDOWN_MODE_DISABLE (0 << 8)
#define INTERRUPT_MODE (1 << 9)
#define COMPARATOR_MODE (0 << 9)
#define ALERT_ACTIVE_HIGH (1 << 10)
#define ALERT_ACTIVE_LOW (0 << 10)
#define ALERT_ON_1_FAULT (0 << 11)
#define ALERT_ON_2_FAULT (1 << 11)
#define ALERT_ON_4_FAULT (2 << 11)
#define ALERT_ON_6_FAULT (3 << 11)
#define START_CONVERSION (1 << 15)
/* Default values for configuring the registers */
#define TLOW_REG_DEFAULT 25
#define THIGH_REG_DEFAULT 35
/* Macros for queue setup */
//#define TEMP_SENS_QUEUE "/temp_sens"
//#define TEMP_QUEUE_SIZE 10
/* Macros for timer setup */
#define CLOCK_TO_USE CLOCK_REALTIME
#define SIGNAL_NOTIFICATION_METHOD SIGEV_SIGNAL
#define SIGNAL_NO SIGRTMIN
#define TIME_IN_NANOSEC 100000000 //100msec
/* The functions that are used to communicate to the i2c temperature sensor TMP102 */
/*
* Function name:- temp_i2c_init
* Description:- This function opens the i2c file for i2c transactions. It then sets
* the slave address for the transactions according to the parameter.
* @param:- uint8_t (slave address)
* @return:- int (file descriptor)
*/
int temp_i2c_init(uint8_t);
/*
* Function name:- temp_i2c_write_to_reg
* Description:- This function takes the file descriptor as parameter which is used to
* write to a file. It writes the data to the temperature sensor register
* which is described in parameter. For writing the data to THIGH or TLOW
* regiser the data to write should be in Celsius.
* @param:- int (file descriptor), uint8_t (temperature sensor register address),
* int16_t (data to write)
* @return:- int (return error)
*/
int temp_i2c_write_to_reg(int, uint8_t, int16_t);
/*
* Function name:- temp_i2c_read_from_reg
* Description:- This function takes the file descriptor as parameter which is used to
* write to a file. It reads the received i2c data from register passed
* and return the buffer value in uint16_t format.
* @param:- int (file descriptor), uint8_t (temperature sensor register address),
* @return:- uint16_t (contents of the register)
*/
uint16_t temp_i2c_read_from_reg(int, uint8_t);
/*
* Function name:- read_temperature
* Description:- This function takes the file descriptor as parameter which is used to
* write to a file. It formats the data of the register passed in the format
* of Celcius unit. As configuration register does not contain
* temperature passing config register address will cause an error.
* @param:- int (file descriptor), uint8_t (temperature sensor register address),
* @return:- float (temperature in Celsius)
*/
float read_temperature(int, uint8_t);
/*
* Function name:- convert_to_unit
* Description:- This function takes temperature and unit to conver to and then converts
* the value in required temperature unit and returns the value.
* @param:- float (value to be converted), int (temperature unit to convert to),
* @return:- float (temperature in requested unit)
*/
float convert_to_unit(float, int);