Skip to content

Commit 699f329

Browse files
authored
Feature/p3t1755 driver (#349)
Co-authored-by: Tavi <tavishi_bhatia@outlook.com>
1 parent a893274 commit 699f329

2 files changed

Lines changed: 289 additions & 0 deletions

File tree

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) (((int16_t)(raw)) * p3t1755_TEMP_RESOLUTION)
29+
#define p3t1755_CELSIUS_TO_RAW(temp) \
30+
((uint16_t)((int16_t)((temp) / p3t1755_TEMP_RESOLUTION)))
31+
32+
// CONFIGURATION MASKS
33+
#define p3t1755_SHUTDOWN_MODE_MASK 0x01 // Bit 0
34+
#define p3t1755_THERMOSTAT_MODE_MASK 0x02 // Bit 1
35+
#define p3t1755_POLARITY_MASK 0x04 // Bit 2
36+
#define p3t1755_FAULT_QUEUE_MASK 0x18 // Bits 4-3
37+
#define p3t1755_CONVERSION_TIME_MASK 0x60 // Bits 6-5
38+
#define p3t1755_ONE_SHOT_MASK 0x80 // Bit 7
39+
40+
// FAULT QUEUE SETTINGS
41+
#define p3t1755_1_CONSECUTIVE_FAULT 0x00
42+
#define p3t1755_2_CONSECUTIVE_FAULTS 0x08 // default
43+
#define p3t1755_4_CONSECUTIVE_FAULTS 0x10
44+
#define p3t1755_6_CONSECUTIVE_FAULTS 0x18
45+
46+
// CONVERSION TIME SETTINGS
47+
#define p3t1755_27_5MS_CONVERSION_TIME 0x00
48+
#define p3t1755_55MS_CONVERSION_TIME 0x20 // default
49+
#define p3t1755_110MS_CONVERSION_TIME 0x40
50+
#define p3t1755_220MS_CONVERSION_TIME 0x60
51+
52+
// Function Pointers
53+
typedef int (*WritePtr)(uint16_t dev_addr, uint16_t reg, uint16_t *data);
54+
typedef int (*ReadPtr)(uint16_t dev_addr, uint16_t reg, uint16_t *data);
55+
56+
typedef struct {
57+
uint16_t dev_addr;
58+
WritePtr write;
59+
ReadPtr read;
60+
} p3t1755_t;
61+
62+
void p3t1755_init(p3t1755_t *p3t, WritePtr write, ReadPtr read,
63+
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,
74+
uint8_t conversion_time);
75+
int p3t1755_set_shutdown_mode(p3t1755_t *p3t, uint8_t enable);
76+
int p3t1755_set_thermostat_mode(p3t1755_t *p3t, uint8_t enable);
77+
int p3t1755_set_one_shot_mode(p3t1755_t *p3t, uint8_t enable);
78+
int p3t1755_set_polarity(p3t1755_t *p3t, uint8_t setting);
79+
int p3t1755_set_fault_queue(p3t1755_t *p3t, uint8_t data);
80+
int p3t1755_set_conversion_time(p3t1755_t *p3t, uint8_t data);
81+
82+
// Temperature high / low functions
83+
int p3t1755_read_high_temp(p3t1755_t *p3t, float *temp_c);
84+
int p3t1755_read_low_temp(p3t1755_t *p3t, float *temp_c);
85+
86+
int p3t1755_set_high_temp(p3t1755_t *p3t, float temp_c);
87+
int p3t1755_set_low_temp(p3t1755_t *p3t, float temp_c);
88+
89+
#endif

general/src/p3t1755.c

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

0 commit comments

Comments
 (0)