LibDriver MAX31855
Loading...
Searching...
No Matches
driver_max31855.c
Go to the documentation of this file.
1
36
37#include "driver_max31855.h"
38
42#define CHIP_NAME "Maxim Integrated max31855"
43#define MANUFACTURER_NAME "Maxim Integrated"
44#define SUPPLY_VOLTAGE_MIN 3.0f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 1.5f
47#define TEMPERATURE_MIN -20.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
60static uint8_t a_max31855_spi_read(max31855_handle_t *handle, uint32_t *data)
61{
62 uint8_t buf[4];
63
64 if (handle->spi_read_cmd(buf, 4) != 0) /* spi read */
65 {
66 return 1; /* return error */
67 }
68 else
69 {
70 *data = (((uint32_t)buf[0]) << 24) |
71 (((uint32_t)buf[1]) << 16) |
72 (((uint32_t)buf[2]) << 8) |
73 (((uint32_t)buf[3]) << 0); /* get the data */
74
75 return 0; /* success return 0 */
76 }
77}
78
90{
91 uint32_t data;
92
93 if (handle == NULL) /* check handle */
94 {
95 return 2; /* return error */
96 }
97 if (handle->debug_print == NULL) /* check debug_print */
98 {
99 return 3; /* return error */
100 }
101 if (handle->spi_init == NULL) /* check spi_init */
102 {
103 handle->debug_print("max31855: spi_init is null.\n"); /* spi_init is null */
104
105 return 3; /* return error */
106 }
107 if (handle->spi_deinit == NULL) /* check spi_deinit */
108 {
109 handle->debug_print("max31855: spi_deinit is null.\n"); /* spi_deinit is null */
110
111 return 3; /* return error */
112 }
113 if (handle->spi_read_cmd == NULL) /* check spi_read_cmd */
114 {
115 handle->debug_print("max31855: spi_read_cmd is null.\n"); /* spi_read_cmd is null */
116
117 return 3; /* return error */
118 }
119 if (handle->delay_ms == NULL) /* check delay_ms */
120 {
121 handle->debug_print("max31855: delay_ms is null.\n"); /* delay_ms is null */
122
123 return 3; /* return error */
124 }
125
126 if (handle->spi_init() != 0) /* spi init */
127 {
128 handle->debug_print("max31855: spi init failed.\n"); /* spi init failed */
129
130 return 1; /* return error */
131 }
132 if (a_max31855_spi_read(handle, &data) != 0) /* read data */
133 {
134 handle->debug_print("max31855: read data failed.\n"); /* read data failed */
135
136 return 1; /* return error */
137 }
138
139 handle->fault = 0x00; /* init none */
140 handle->inited = 1; /* flag finish initialization */
141
142 return 0; /* success return 0 */
143}
144
156{
157 uint8_t res;
158
159 if (handle == NULL) /* check handle */
160 {
161 return 2; /* return error */
162 }
163 if (handle->inited != 1) /* check handle initialization */
164 {
165 return 3; /* return error */
166 }
167
168 res = handle->spi_deinit(); /* spi deinit */
169 if (res != 0) /* check result */
170 {
171 handle->debug_print("max31855: spi deinit failed.\n"); /* spi deinit failed */
172
173 return 1; /* return error */
174 }
175 handle->inited = 0; /* flag close */
176
177 return 0; /* success return 0 */
178}
179
191{
192 if (handle == NULL) /* check handle */
193 {
194 return 2; /* return error */
195 }
196 if (handle->inited != 1) /* check handle initialization */
197 {
198 return 3; /* return error */
199 }
200
201 *fault = (max31855_fault_t)(handle->fault); /* set last fault */
202
203 return 0; /* success return 0 */
204}
205
221uint8_t max31855_read(max31855_handle_t *handle, int16_t *thermocouple_raw, float *thermocouple_temp,
222 int16_t *reference_junction_raw, float *reference_junction_temp)
223{
224 uint8_t res;
225 uint32_t data;
226 uint16_t d1;
227 uint16_t d0;
228
229 if (handle == NULL) /* check handle */
230 {
231 return 2; /* return error */
232 }
233 if (handle->inited != 1) /* check handle initialization */
234 {
235 return 3; /* return error */
236 }
237
238 res = a_max31855_spi_read(handle, &data); /* read data */
239 if (res != 0) /* check result */
240 {
241 handle->debug_print("max31855: read data failed.\n"); /* read data failed */
242
243 return 1; /* return error */
244 }
245 d1 = (data >> 16) & 0xFFFFU; /* get d1 */
246 d0 = (data >> 0) & 0xFFFFU; /* get d0 */
247 if ((d1 & 0x01) != 0) /* check fault */
248 {
249 handle->debug_print("max31855: find fault.\n"); /* find fault */
250 handle->fault = d0 & 0x07; /* save fault */
251
252 return 4; /* return error */
253 }
254 if ((d1 >> 15) != 0) /* check sign bit */
255 {
256 *thermocouple_raw = (int16_t)((d1 >> 2) | 0xC000U); /* set raw data */
257 }
258 else
259 {
260 *thermocouple_raw = (int16_t)(d1 >> 2); /* set raw data */
261 }
262 *thermocouple_temp = (float)(*thermocouple_raw) * 0.25f; /* convert data */
263 if ((d0 >> 15) != 0) /* check sign bit */
264 {
265 *reference_junction_raw = (int16_t)((d1 >> 4) | 0xF000U); /* set raw data */
266 }
267 else
268 {
269 *reference_junction_raw = (int16_t)(d0 >> 4); /* set raw data */
270 }
271 *reference_junction_temp = (float)(*reference_junction_raw) * 0.0625f; /* convert data */
272
273 return 0; /* success return 0 */
274}
275
287uint8_t max31855_get_reg(max31855_handle_t *handle, uint32_t *data)
288{
289 if (handle == NULL) /* check handle */
290 {
291 return 2; /* return error */
292 }
293 if (handle->inited != 1) /* check handle initialization */
294 {
295 return 3; /* return error */
296 }
297
298 return a_max31855_spi_read(handle, data); /* read data */
299}
300
310{
311 if (info == NULL) /* check handle */
312 {
313 return 2; /* return error */
314 }
315
316 memset(info, 0, sizeof(max31855_info_t)); /* initialize max31855 info structure */
317 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
318 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
319 strncpy(info->interface, "SPI", 8); /* copy interface name */
320 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
321 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
322 info->max_current_ma = MAX_CURRENT; /* set maximum current */
323 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
324 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
325 info->driver_version = DRIVER_VERSION; /* set driver version */
326
327 return 0; /* success return 0 */
328}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver max31855 header file
max31855_fault_t
max31855 fault enumeration definition
uint8_t max31855_deinit(max31855_handle_t *handle)
close the chip
uint8_t max31855_get_last_fault(max31855_handle_t *handle, max31855_fault_t *fault)
get the last fault
uint8_t max31855_info(max31855_info_t *info)
get chip's information
uint8_t max31855_read(max31855_handle_t *handle, int16_t *thermocouple_raw, float *thermocouple_temp, int16_t *reference_junction_raw, float *reference_junction_temp)
read the temperature
struct max31855_info_s max31855_info_t
max31855 information structure definition
struct max31855_handle_s max31855_handle_t
max31855 handle structure definition
uint8_t max31855_init(max31855_handle_t *handle)
initialize the chip
uint8_t max31855_get_reg(max31855_handle_t *handle, uint32_t *data)
get the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_read_cmd)(uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint32_t driver_version
char manufacturer_name[32]