LibDriver TTP229
Loading...
Searching...
No Matches
driver_ttp229.c
Go to the documentation of this file.
1
36
37#include "driver_ttp229.h"
38
42#define CHIP_NAME "Tontek TTP229"
43#define MANUFACTURER_NAME "Tontek"
44#define SUPPLY_VOLTAGE_MIN 2.4f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 8.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define TTP229_ADDRESS (0x57 << 1)
55
66static uint8_t a_ttp229_iic_spi_read(ttp229_handle_t *handle, uint8_t *buf, uint16_t len)
67{
68 if (handle->iic_spi == (uint8_t)TTP229_INTERFACE_IIC) /* iic interface */
69 {
70 if (handle->iic_read_cmd(TTP229_ADDRESS, buf, len) != 0) /* iic read */
71 {
72 return 1; /* return error */
73 }
74
75 return 0; /* success return 0 */
76 }
77 else /* spi interface */
78 {
79 if (handle->spi_read_cmd(buf, len) != 0) /* spi read */
80 {
81 return 1; /* return error */
82 }
83
84 return 0; /* success return 0 */
85 }
86}
87
98{
99 if (handle == NULL) /* check handle */
100 {
101 return 2; /* return error */
102 }
103
104 handle->iic_spi = (uint8_t)interface; /* set interface */
105
106 return 0; /* success return 0 */
107}
108
119{
120 if (handle == NULL) /* check handle */
121 {
122 return 2; /* return error */
123 }
124
125 *interface = (ttp229_interface_t)(handle->iic_spi); /* get interface */
126
127 return 0; /* success return 0 */
128}
129
141{
142 if (handle == NULL) /* check handle */
143 {
144 return 2; /* return error */
145 }
146 if (handle->inited != 1) /* check handle initialization */
147 {
148 return 3; /* return error */
149 }
150
151 handle->active = (uint8_t)active_level; /* set active level */
152
153 return 0; /* success return 0 */
154}
155
166uint8_t ttp229_get_active(ttp229_handle_t *handle, ttp229_active_t *active_level)
167{
168 if (handle == NULL) /* check handle */
169 {
170 return 2; /* return error */
171 }
172 if (handle->inited != 1) /* check handle initialization */
173 {
174 return 3; /* return error */
175 }
176
177 *active_level = (ttp229_active_t)(handle->active); /* set active level */
178
179 return 0; /* success return 0 */
180}
181
193{
194 if (handle == NULL) /* check handle */
195 {
196 return 2; /* return error */
197 }
198 if (handle->debug_print == NULL) /* check debug_print */
199 {
200 return 3; /* return error */
201 }
202 if (handle->iic_init == NULL) /* check iic_init */
203 {
204 handle->debug_print("ttp229: iic_init is null.\n"); /* iic_init is nul */
205
206 return 3; /* return error */
207 }
208 if (handle->iic_deinit == NULL) /* check iic_deinit */
209 {
210 handle->debug_print("ttp229: iic_deinit is null.\n"); /* iic_deinit is null */
211
212 return 3; /* return error */
213 }
214 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
215 {
216 handle->debug_print("ttp229: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
217
218 return 3; /* return error */
219 }
220 if (handle->spi_init == NULL) /* check spi_init */
221 {
222 handle->debug_print("ttp229: spi_init is null.\n"); /* spi_init is nul */
223
224 return 3; /* return error */
225 }
226 if (handle->spi_deinit == NULL) /* check spi_deinit */
227 {
228 handle->debug_print("ttp229: spi_deinit is null.\n"); /* spi_deinit is nul */
229
230 return 3; /* return error */
231 }
232 if (handle->spi_read_cmd == NULL) /* check spi_read_cmd */
233 {
234 handle->debug_print("ttp229: spi_read_cmd is null.\n"); /* spi_read_cmd is nul */
235
236 return 3; /* return error */
237 }
238 if (handle->delay_ms == NULL) /* check delay_ms */
239 {
240 handle->debug_print("ttp229: delay_ms is null.\n"); /* delay_ms is null */
241
242 return 3; /* return error */
243 }
244
245 if (handle->iic_spi == (uint8_t)TTP229_INTERFACE_IIC) /* iic interface */
246 {
247 if (handle->iic_init() != 0) /* iic init */
248 {
249 handle->debug_print("ttp229: iic init failed.\n"); /* iic init failed */
250
251 return 1; /* return error */
252 }
253 }
254 else /* spi interface */
255 {
256 if (handle->spi_init() != 0) /* spi init */
257 {
258 handle->debug_print("ttp229: spi init failed.\n"); /* spi init failed */
259
260 return 1; /* return error */
261 }
262 }
263
264 handle->inited = 1; /* flag finish initialization */
265
266 return 0; /* success return 0 */
267}
268
280{
281 if (handle == NULL) /* check handle */
282 {
283 return 2; /* return error */
284 }
285 if (handle->inited != 1) /* check handle initialization */
286 {
287 return 3; /* return error */
288 }
289
290 if (handle->iic_spi == (uint8_t)TTP229_INTERFACE_IIC) /* iic interface */
291 {
292 if (handle->iic_init() != 0) /* iic init */
293 {
294 handle->debug_print("ttp229: iic init failed.\n"); /* iic init failed */
295
296 return 1; /* return error */
297 }
298 }
299 else /* spi interface */
300 {
301 if (handle->spi_init() != 0) /* spi init */
302 {
303 handle->debug_print("ttp229: spi init failed.\n"); /* spi init failed */
304
305 return 1; /* return error */
306 }
307 }
308 handle->inited = 0; /* flag close */
309
310 return 0; /* success return 0 */
311}
312
324uint8_t ttp229_read_16_keys(ttp229_handle_t *handle, uint8_t keys[16])
325{
326 uint8_t i;
327 uint8_t buf[2];
328
329 if (handle == NULL) /* check handle */
330 {
331 return 2; /* return error */
332 }
333 if (handle->inited != 1) /* check handle initialization */
334 {
335 return 3; /* return error */
336 }
337
338 if (a_ttp229_iic_spi_read(handle, buf, 2) != 0) /* read data */
339 {
340 handle->debug_print("ttp229: read failed.\n"); /* read failed */
341
342 return 1; /* return error */
343 }
344 for (i = 0; i < 16; i++) /* loop all */
345 {
346 keys[i] = (buf[i / 8] >> (7 - i % 8)) & 0x01; /* get one key */
347 if (handle->active == (uint8_t)(TTP229_ACTIVE_LOW)) /* if active low */
348 {
349 keys[i] = !keys[i]; /* revert */
350 }
351 }
352
353 return 0; /* success return 0 */
354}
355
367uint8_t ttp229_read_8_keys(ttp229_handle_t *handle, uint8_t keys[8])
368{
369 uint8_t i;
370 uint8_t buf[1];
371
372 if (handle == NULL) /* check handle */
373 {
374 return 2; /* return error */
375 }
376 if (handle->inited != 1) /* check handle initialization */
377 {
378 return 3; /* return error */
379 }
380
381 if (a_ttp229_iic_spi_read(handle, buf, 1) != 0) /* read data */
382 {
383 handle->debug_print("ttp229: read failed.\n"); /* read failed */
384
385 return 1; /* return error */
386 }
387 for (i = 0; i < 8; i++) /* loop all */
388 {
389 keys[i] = (buf[i / 8] >> (7 - i % 8)) & 0x01; /* get one key */
390 if (handle->active == (uint8_t)(TTP229_ACTIVE_LOW)) /* if active low */
391 {
392 keys[i] = !keys[i]; /* revert */
393 }
394 }
395
396 return 0; /* success return 0 */
397}
398
411uint8_t ttp229_get_reg(ttp229_handle_t *handle, uint8_t *buf, uint16_t len)
412{
413 if (handle == NULL) /* check handle */
414 {
415 return 2; /* return error */
416 }
417 if (handle->inited != 1) /* check handle initialization */
418 {
419 return 3; /* return error */
420 }
421
422 return a_ttp229_iic_spi_read(handle, buf, len); /* read register */
423}
424
434{
435 if (info == NULL) /* check handle */
436 {
437 return 2; /* return error */
438 }
439
440 memset(info, 0, sizeof(ttp229_info_t)); /* initialize ttp229 info structure */
441 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
442 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
443 strncpy(info->interface, "IIC SPI", 8); /* copy interface name */
444 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
445 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
446 info->max_current_ma = MAX_CURRENT; /* set maximum current */
447 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
448 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
449 info->driver_version = DRIVER_VERSION; /* set driver version */
450
451 return 0; /* success return 0 */
452}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define TTP229_ADDRESS
chip address definition
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver ttp229 header file
ttp229_active_t
ttp229 active enumeration definition
uint8_t ttp229_init(ttp229_handle_t *handle)
initialize the chip
struct ttp229_info_s ttp229_info_t
ttp229 information structure definition
ttp229_interface_t
ttp229 interface enumeration definition
uint8_t ttp229_set_active(ttp229_handle_t *handle, ttp229_active_t active_level)
set the active level
uint8_t ttp229_get_interface(ttp229_handle_t *handle, ttp229_interface_t *interface)
get the interface
uint8_t ttp229_deinit(ttp229_handle_t *handle)
close the chip
uint8_t ttp229_read_8_keys(ttp229_handle_t *handle, uint8_t keys[8])
read 8 keys
uint8_t ttp229_info(ttp229_info_t *info)
get chip's information
uint8_t ttp229_get_active(ttp229_handle_t *handle, ttp229_active_t *active_level)
get the active level
uint8_t ttp229_read_16_keys(ttp229_handle_t *handle, uint8_t keys[16])
read 16 keys
uint8_t ttp229_set_interface(ttp229_handle_t *handle, ttp229_interface_t interface)
set the interface
struct ttp229_handle_s ttp229_handle_t
ttp229 handle structure definition
@ TTP229_ACTIVE_LOW
@ TTP229_INTERFACE_IIC
uint8_t ttp229_get_reg(ttp229_handle_t *handle, uint8_t *buf, uint16_t len)
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(* iic_init)(void)
uint8_t(* spi_deinit)(void)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]