Skip to content

Commit 473cbd0

Browse files
author
Tavi
committed
Coded the P3T1755DPZ Temp Sensor driver
1 parent 2babe03 commit 473cbd0

File tree

2 files changed

+287
-0
lines changed

2 files changed

+287
-0
lines changed

general/include/p3t1755.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
P3T1755DPZ (Temperature Sensor) Driver
3+
Datasheet: https://www.nxp.com/docs/en/data-sheet/P3T1755.pdf
4+
*/
5+
6+
#ifndef p3t1755_H
7+
#define p3t1755_H
8+
#include <stdint.h>
9+
#include <math.h>
10+
11+
// REGISTERS
12+
// Temperature register: contains two 8-bit data bytes; to store the measured Temp data.
13+
#define p3t1755_TEMPERATURE 0x00 // read only
14+
// Configuration register: contains a single 8-bit data byte; to set the device operating condition
15+
#define p3t1755_CONFIGURATION 0x01
16+
// T_low register: Hysteresis register, it contains two 8-bit data bytes to store the hysteresis T_low limit; default = 75 °C.
17+
#define p3t1755_T_LOW 0x02
18+
// T_high register: Overtemperature shut down threshold register, it contains two 8-bit data bytes to
19+
// store the overtemperature shutdown T_high limit; default = 80 °C.
20+
#define p3t1755_T_HIGH 0x03
21+
22+
// TEMPERATURE REGISTER FORMAT
23+
#define p3t1755_TEMP_RESOLUTION 0.0625f
24+
// temperature range
25+
#define p3t1755_TEMP_MIN -40.0f
26+
#define p3t1755_TEMP_MAX 125.0f
27+
28+
#define p3t1755_RAW_TO_CELSIUS(raw) \
29+
(((int16_t)(raw)) * p3t1755_TEMP_RESOLUTION)
30+
#define p3t1755_CELSIUS_TO_RAW(temp) \
31+
((uint16_t)((int16_t)((temp) / p3t1755_TEMP_RESOLUTION)))
32+
33+
// CONFIGURATION MASKS
34+
#define p3t1755_SHUTDOWN_MODE_MASK 0x01 // Bit 0
35+
#define p3t1755_THERMOSTAT_MODE_MASK 0x02 // Bit 1
36+
#define p3t1755_POLARITY_MASK 0x04 // Bit 2
37+
#define p3t1755_FAULT_QUEUE_MASK 0x18 // Bits 4-3
38+
#define p3t1755_CONVERSION_TIME_MASK 0x60 // Bits 6-5
39+
#define p3t1755_ONE_SHOT_MASK 0x80 // Bit 7
40+
41+
// FAULT QUEUE SETTINGS
42+
#define p3t1755_1_CONSECUTIVE_FAULT 0x00
43+
#define p3t1755_2_CONSECUTIVE_FAULTS 0x08 // default
44+
#define p3t1755_4_CONSECUTIVE_FAULTS 0x10
45+
#define p3t1755_6_CONSECUTIVE_FAULTS 0x18
46+
47+
// CONVERSION TIME SETTINGS
48+
#define p3t1755_27_5MS_CONVERSION_TIME 0x00
49+
#define p3t1755_55MS_CONVERSION_TIME 0x20 // default
50+
#define p3t1755_110MS_CONVERSION_TIME 0x40
51+
#define p3t1755_220MS_CONVERSION_TIME 0x60
52+
53+
// Function Pointers
54+
typedef int (*WritePtr)(uint16_t dev_addr, uint16_t reg, uint16_t *data);
55+
typedef int (*ReadPtr)(uint16_t dev_addr, uint16_t reg, uint16_t *data);
56+
57+
typedef struct {
58+
uint16_t dev_addr;
59+
WritePtr write;
60+
ReadPtr read;
61+
} p3t1755_t;
62+
63+
void p3t1755_init(p3t1755_t *p3t, WritePtr write, ReadPtr read, uint16_t dev_addr);
64+
65+
int p3t1755_read_reg(p3t1755_t *p3t, uint16_t reg, uint16_t *data);
66+
int p3t1755_write_reg(p3t1755_t *p3t, uint16_t reg, uint16_t *data);
67+
68+
// Reads current temp in celcius
69+
int p3t1755_read_temperature(p3t1755_t *p3t, float *temp_c);
70+
71+
// Config functions
72+
int p3t1755_configure(p3t1755_t *p3t, uint8_t shutdown, uint8_t thermostat,
73+
uint8_t polarity, uint8_t fault_queue, uint8_t conversion_time);
74+
int p3t1755_set_shutdown_mode(p3t1755_t *p3t, uint8_t enable);
75+
int p3t1755_set_thermostat_mode(p3t1755_t *p3t, uint8_t enable);
76+
int p3t1755_set_one_shot_mode(p3t1755_t *p3t, uint8_t enable);
77+
int p3t1755_set_polarity(p3t1755_t *p3t, uint8_t setting);
78+
int p3t1755_set_fault_queue(p3t1755_t *p3t, uint8_t data);
79+
int p3t1755_set_conversion_time(p3t1755_t *p3t, uint8_t data);
80+
81+
// Temperature high / low functions
82+
int p3t1755_read_high_temp(p3t1755_t *p3t, float *temp_c);
83+
int p3t1755_read_low_temp(p3t1755_t *p3t, float *temp_c);
84+
85+
int p3t1755_set_high_temp(p3t1755_t *p3t, float temp_c);
86+
int p3t1755_set_low_temp(p3t1755_t *p3t, float temp_c);
87+
88+
89+
#endif

general/src/p3t1755.c

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
P3T1755DPZ (Temperature Sensor) Driver
3+
Datasheet: https://www.nxp.com/docs/en/data-sheet/P3T1755.pdf
4+
*/
5+
6+
#include "p3t1755.h"
7+
8+
void p3t1755_init(p3t1755_t *p3t, WritePtr write, ReadPtr read, uint16_t dev_addr)
9+
{
10+
p3t->write = write;
11+
p3t->read = read;
12+
p3t->dev_addr = dev_addr;
13+
}
14+
15+
int p3t1755_write_reg(p3t1755_t *p3t, uint16_t reg, uint16_t *data)
16+
{
17+
return p3t->write(p3t->dev_addr, reg, data);
18+
}
19+
20+
int p3t1755_read_reg(p3t1755_t *p3t, uint16_t reg, uint16_t *data)
21+
{
22+
return p3t->read(p3t->dev_addr, reg, data);
23+
}
24+
25+
int p3t1755_read_temperature(p3t1755_t *p3t, float *temp_c)
26+
{
27+
uint16_t temp_reg;
28+
29+
int status = p3t1755_read_reg(p3t, p3t1755_TEMPERATURE, &temp_reg);
30+
if (status != 0) {
31+
return status;
32+
}
33+
34+
*temp_c = p3t1755_RAW_TO_CELSIUS(temp_reg);
35+
return status;
36+
}
37+
38+
int p3t1755_configure(p3t1755_t *p3t, uint8_t shutdown, uint8_t thermostat,
39+
uint8_t polarity, uint8_t fault_queue, uint8_t conversion_time)
40+
{
41+
uint16_t config = 0;
42+
43+
if (shutdown) {
44+
config |= p3t1755_SHUTDOWN_MODE_MASK;
45+
}
46+
if (thermostat) {
47+
config |= p3t1755_THERMOSTAT_MODE_MASK;
48+
}
49+
if (polarity) {
50+
config |= p3t1755_POLARITY_MASK;
51+
}
52+
config |= (fault_queue & p3t1755_FAULT_QUEUE_MASK);
53+
config |= (conversion_time & p3t1755_CONVERSION_TIME_MASK);
54+
55+
return p3t1755_write_reg(p3t, p3t1755_CONFIGURATION, &config);
56+
}
57+
58+
int p3t1755_set_shutdown_mode(p3t1755_t *p3t, uint8_t enable)
59+
{
60+
uint16_t config;
61+
62+
int status = p3t1755_read_reg(p3t, p3t1755_CONFIGURATION, &config);
63+
if (status != 0) {
64+
return status;
65+
}
66+
67+
if (enable != 0) {
68+
config |= p3t1755_SHUTDOWN_MODE_MASK;
69+
} else {
70+
config &= ~p3t1755_SHUTDOWN_MODE_MASK;
71+
}
72+
73+
return p3t1755_write_reg(p3t, p3t1755_CONFIGURATION, &config);
74+
}
75+
76+
int p3t1755_set_thermostat_mode(p3t1755_t *p3t, uint8_t enable)
77+
{
78+
uint16_t config;
79+
80+
int status = p3t1755_read_reg(p3t, p3t1755_CONFIGURATION, &config);
81+
if (status != 0) {
82+
return status;
83+
}
84+
85+
if (enable != 0) {
86+
config |= p3t1755_THERMOSTAT_MODE_MASK;
87+
} else {
88+
config &= ~p3t1755_THERMOSTAT_MODE_MASK;
89+
}
90+
91+
return p3t1755_write_reg(p3t, p3t1755_CONFIGURATION, &config);
92+
}
93+
94+
int p3t1755_set_one_shot_mode(p3t1755_t *p3t, uint8_t enable)
95+
{
96+
uint16_t config;
97+
98+
int status = p3t1755_read_reg(p3t, p3t1755_CONFIGURATION, &config);
99+
if (status != 0) {
100+
return status;
101+
}
102+
103+
if (enable != 0) {
104+
config |= p3t1755_ONE_SHOT_MASK;
105+
} else {
106+
config &= ~p3t1755_ONE_SHOT_MASK;
107+
}
108+
109+
return p3t1755_write_reg(p3t, p3t1755_CONFIGURATION, &config);
110+
}
111+
112+
int p3t1755_set_polarity(p3t1755_t *p3t, uint8_t setting)
113+
{
114+
uint16_t config;
115+
116+
int status = p3t1755_read_reg(p3t, p3t1755_CONFIGURATION, &config);
117+
if (status != 0) {
118+
return status;
119+
}
120+
121+
if (setting != 0) {
122+
config |= p3t1755_POLARITY_MASK;
123+
} else {
124+
config &= ~p3t1755_POLARITY_MASK;
125+
}
126+
127+
return p3t1755_write_reg(p3t, p3t1755_CONFIGURATION, &config);
128+
}
129+
130+
int p3t1755_set_fault_queue(p3t1755_t *p3t, uint8_t data)
131+
{
132+
uint16_t config;
133+
134+
int status = p3t1755_read_reg(p3t, p3t1755_CONFIGURATION, &config);
135+
if (status != 0) {
136+
return status;
137+
}
138+
139+
config &= ~p3t1755_FAULT_QUEUE_MASK;
140+
config |= (data & p3t1755_FAULT_QUEUE_MASK);
141+
142+
return p3t1755_write_reg(p3t, p3t1755_CONFIGURATION, &config);
143+
}
144+
145+
int p3t1755_set_conversion_time(p3t1755_t *p3t, uint8_t data)
146+
{
147+
uint16_t config;
148+
149+
int status = p3t1755_read_reg(p3t, p3t1755_CONFIGURATION, &config);
150+
if (status != 0) {
151+
return status;
152+
}
153+
154+
config &= ~p3t1755_CONVERSION_TIME_MASK;
155+
config |= (data & p3t1755_CONVERSION_TIME_MASK);
156+
157+
return p3t1755_write_reg(p3t, p3t1755_CONFIGURATION, &config);
158+
}
159+
160+
int p3t1755_read_high_temp(p3t1755_t *p3t, float *temp_c)
161+
{
162+
uint16_t high_temp_reg;
163+
164+
int status = p3t1755_read_reg(p3t, p3t1755_T_HIGH, &high_temp_reg);
165+
if (status != 0) {
166+
return status;
167+
}
168+
169+
*temp_c = p3t1755_RAW_TO_CELSIUS(high_temp_reg);
170+
return status;
171+
}
172+
173+
int p3t1755_read_low_temp(p3t1755_t *p3t, float *temp_c)
174+
{
175+
uint16_t low_temp_reg;
176+
177+
int status = p3t1755_read_reg(p3t, p3t1755_T_LOW, &low_temp_reg);
178+
if (status != 0) {
179+
return status;
180+
}
181+
182+
*temp_c = p3t1755_RAW_TO_CELSIUS(low_temp_reg);
183+
return status;
184+
}
185+
186+
int p3t1755_set_high_temp(p3t1755_t *p3t, float temp_c)
187+
{
188+
uint16_t raw_temp = p3t1755_CELSIUS_TO_RAW(temp_c);
189+
190+
return p3t1755_write_reg(p3t, p3t1755_T_HIGH, &raw_temp);
191+
}
192+
193+
int p3t1755_set_low_temp(p3t1755_t *p3t, float temp_c)
194+
{
195+
uint16_t raw_temp = p3t1755_CELSIUS_TO_RAW(temp_c);
196+
197+
return p3t1755_write_reg(p3t, p3t1755_T_LOW, &raw_temp);
198+
}

0 commit comments

Comments
 (0)