LibDriver TPL0501
Loading...
Searching...
No Matches
driver_tpl0501.c
Go to the documentation of this file.
1
36
37#include "driver_tpl0501.h"
38
42#define CHIP_NAME "Texas Instruments TPL0501"
43#define MANUFACTURER_NAME "Texas Instruments"
44#define SUPPLY_VOLTAGE_MIN 2.7f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 5.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
60static uint8_t a_tpl0501_spi_write(tpl0501_handle_t *handle, uint8_t data)
61{
62 uint8_t buf[1];
63
64 buf[0] = data; /* set data */
65 if (handle->spi_write_cmd(buf, 1) != 0) /* spi write */
66 {
67 return 1; /* return error */
68 }
69
70 return 0; /* success return 0 */
71}
72
84{
85 if (handle == NULL) /* check handle */
86 {
87 return 2; /* return error */
88 }
89 if (handle->debug_print == NULL) /* check debug_print */
90 {
91 return 3; /* return error */
92 }
93 if (handle->spi_init == NULL) /* check spi_init */
94 {
95 handle->debug_print("tpl0501: spi_init is null.\n"); /* spi_init is null */
96
97 return 3; /* return error */
98 }
99 if (handle->spi_deinit == NULL) /* check spi_deinit */
100 {
101 handle->debug_print("tpl0501: spi_deinit is null.\n"); /* spi_deinit is null */
102
103 return 3; /* return error */
104 }
105 if (handle->spi_write_cmd == NULL) /* check spi_write_cmd */
106 {
107 handle->debug_print("tpl0501: spi_write_cmd is null.\n"); /* spi_read_cmd is null */
108
109 return 3; /* return error */
110 }
111 if (handle->delay_ms == NULL) /* check delay_ms */
112 {
113 handle->debug_print("tpl0501: delay_ms is null.\n"); /* delay_ms is null */
114
115 return 3; /* return error */
116 }
117
118 if (handle->spi_init() != 0) /* spi init */
119 {
120 handle->debug_print("tpl0501: spi init failed.\n"); /* spi init failed */
121
122 return 1; /* return error */
123 }
124 handle->inited = 1; /* flag finish initialization */
125
126 return 0; /* success return 0 */
127}
128
140{
141 uint8_t res;
142
143 if (handle == NULL) /* check handle */
144 {
145 return 2; /* return error */
146 }
147 if (handle->inited != 1) /* check handle initialization */
148 {
149 return 3; /* return error */
150 }
151
152 res = handle->spi_deinit(); /* spi deinit */
153 if (res != 0) /* check result */
154 {
155 handle->debug_print("tpl0501: spi deinit failed.\n"); /* spi deinit failed */
156
157 return 1; /* return error */
158 }
159 handle->inited = 0; /* flag close */
160
161 return 0; /* success return 0 */
162}
163
175uint8_t tpl0501_write(tpl0501_handle_t *handle, uint8_t raw)
176{
177 uint8_t res;
178
179 if (handle == NULL) /* check handle */
180 {
181 return 2; /* return error */
182 }
183 if (handle->inited != 1) /* check handle initialization */
184 {
185 return 3; /* return error */
186 }
187
188 res =a_tpl0501_spi_write(handle, raw); /* set data */
189 if (res != 0) /* check result */
190 {
191 handle->debug_print("tpl0501: read data failed.\n"); /* read data failed */
192
193 return 1; /* return error */
194 }
195
196 return 0; /* success return 0 */
197}
198
213 float percentage, uint8_t *reg,
214 float *wl_ohm, float *hw_ohm)
215{
216 if (handle == NULL) /* check handle */
217 {
218 return 2; /* return error */
219 }
220 if (handle->inited != 1) /* check handle initialization */
221 {
222 return 3; /* return error */
223 }
224
225 *reg = (uint8_t)(percentage / 100.0f * 256); /* convert real data to register data */
226 *wl_ohm = 100 * 1000 * (percentage / 100.0f); /* set wl ohm */
227 *hw_ohm = 100 * 1000 - (*wl_ohm); /* set hw ohm */
228
229 return 0; /* success return 0 */
230}
231
246 uint8_t reg, float *percentage,
247 float *wl_ohm, float *hw_ohm)
248{
249 if (handle == NULL) /* check handle */
250 {
251 return 2; /* return error */
252 }
253 if (handle->inited != 1) /* check handle initialization */
254 {
255 return 3; /* return error */
256 }
257
258 *percentage = (float)((float)(reg) / 256.0f) * 100.0f; /* convert register data to real data */
259 *wl_ohm = 100 * 1000 * ((float)reg / 256.0f); /* set wl ohm */
260 *hw_ohm = 100 * 1000 - (*wl_ohm); /* set hw ohm */
261
262 return 0; /* success return 0 */
263}
264
276uint8_t tpl0501_set_reg(tpl0501_handle_t *handle, uint8_t raw)
277{
278 if (handle == NULL) /* check handle */
279 {
280 return 2; /* return error */
281 }
282 if (handle->inited != 1) /* check handle initialization */
283 {
284 return 3; /* return error */
285 }
286
287 return a_tpl0501_spi_write(handle, raw); /* write data */
288}
289
299{
300 if (info == NULL) /* check handle */
301 {
302 return 2; /* return error */
303 }
304
305 memset(info, 0, sizeof(tpl0501_info_t)); /* initialize tpl0501 info structure */
306 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
307 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
308 strncpy(info->interface, "SPI", 8); /* copy interface name */
309 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
310 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
311 info->max_current_ma = MAX_CURRENT; /* set maximum current */
312 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
313 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
314 info->driver_version = DRIVER_VERSION; /* set driver version */
315
316 return 0; /* success return 0 */
317}
#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 tpl0501 header file
uint8_t tpl0501_percentage_convert_to_data(tpl0501_handle_t *handle, uint8_t reg, float *percentage, float *wl_ohm, float *hw_ohm)
convert the register raw data to percentage
uint8_t tpl0501_info(tpl0501_info_t *info)
get chip's information
uint8_t tpl0501_deinit(tpl0501_handle_t *handle)
close the chip
uint8_t tpl0501_percentage_convert_to_register(tpl0501_handle_t *handle, float percentage, uint8_t *reg, float *wl_ohm, float *hw_ohm)
convert the percentage to the register raw data
uint8_t tpl0501_init(tpl0501_handle_t *handle)
initialize the chip
struct tpl0501_info_s tpl0501_info_t
tpl0501 information structure definition
uint8_t tpl0501_write(tpl0501_handle_t *handle, uint8_t raw)
write data
struct tpl0501_handle_s tpl0501_handle_t
tpl0501 handle structure definition
uint8_t tpl0501_set_reg(tpl0501_handle_t *handle, uint8_t raw)
set the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* spi_write_cmd)(uint8_t *buf, uint16_t len)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v