1 /*
2  * Copyright (c) 2020 arithmetics.io
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/sys/util.h>
10 #include <zephyr/drivers/sensor.h>
11 #include <zephyr/drivers/sensor/fdc2x1x.h>
12 #include <stdio.h>
13 
14 #define CH_BUF_INIT(m)          {},
15 
16 K_SEM_DEFINE(sem, 0, 1);
17 
18 #ifdef CONFIG_FDC2X1X_TRIGGER
trigger_handler(const struct device * dev,const struct sensor_trigger * trigger)19 static void trigger_handler(const struct device *dev,
20 			    const struct sensor_trigger *trigger)
21 {
22 	switch (trigger->type) {
23 	case SENSOR_TRIG_DATA_READY:
24 		if (sensor_sample_fetch(dev)) {
25 			printk("Sample fetch error\n");
26 			return;
27 		}
28 		k_sem_give(&sem);
29 		break;
30 	default:
31 		printk("Unknown trigger\n");
32 	}
33 }
34 #endif
35 
36 #ifdef CONFIG_PM_DEVICE
pm_info(enum pm_device_action action,int status)37 static void pm_info(enum pm_device_action action, int status)
38 {
39 	switch (action) {
40 	case PM_DEVICE_ACTION_RESUME:
41 		printk("Enter ACTIVE_STATE ");
42 		break;
43 	case PM_DEVICE_ACTION_SUSPEND:
44 		printk("Enter SUSPEND_STATE ");
45 		break;
46 	case PM_DEVICE_ACTION_TURN_OFF:
47 		printk("Enter OFF_STATE ");
48 		break;
49 	default:
50 		printk("Unknown power state");
51 	}
52 
53 	if (status) {
54 		printk("Fail\n");
55 	} else {
56 		printk("Success\n");
57 	}
58 }
59 #endif
60 
61 #define DEVICE_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(ti_fdc2x1x)
62 
main(void)63 int main(void)
64 {
65 	struct sensor_value ch_buf[] = {
66 		DT_FOREACH_CHILD(DEVICE_NODE, CH_BUF_INIT)
67 	};
68 
69 	uint8_t num_ch = ARRAY_SIZE(ch_buf);
70 	enum sensor_channel base;
71 	int i;
72 
73 	const struct device *const dev = DEVICE_DT_GET(DEVICE_NODE);
74 
75 	if (!device_is_ready(dev)) {
76 		printk("Device %s is not ready\n", dev->name);
77 		return 0;
78 	}
79 
80 #ifdef CONFIG_FDC2X1X_TRIGGER
81 	struct sensor_trigger trig = {
82 		.type = SENSOR_TRIG_DATA_READY,
83 		.chan = SENSOR_CHAN_ALL,
84 	};
85 
86 	if (sensor_trigger_set(dev, &trig, trigger_handler)) {
87 		printk("Could not set trigger\n");
88 		return 0;
89 	}
90 #endif
91 
92 #ifdef CONFIG_PM_DEVICE
93 	/* Testing the power modes */
94 	enum pm_device_action p_action;
95 	int ret;
96 
97 	p_action = PM_DEVICE_ACTION_SUSPEND;
98 	ret = pm_device_action_run(dev, p_action);
99 	pm_info(p_action, ret);
100 
101 	p_action = PM_DEVICE_ACTION_TURN_OFF;
102 	ret = pm_device_action_run(dev, p_action);
103 	pm_info(p_action, ret);
104 
105 	p_action = PM_DEVICE_ACTION_RESUME;
106 	ret = pm_device_action_run(dev, p_action);
107 	pm_info(p_action, ret);
108 #endif
109 
110 	while (1) {
111 #ifdef CONFIG_FDC2X1X_TRIGGER
112 		k_sem_take(&sem, K_FOREVER);
113 #else
114 		if (sensor_sample_fetch(dev) < 0) {
115 			printk("Sample fetch failed\n");
116 			return 0;
117 		}
118 #endif
119 		base = SENSOR_CHAN_FDC2X1X_FREQ_CH0;
120 		for (i = 0; i < num_ch; i++) {
121 			sensor_channel_get(dev, base++, &ch_buf[i]);
122 			printf("ch%d: %f MHz ", i, sensor_value_to_double(&ch_buf[i]));
123 		}
124 		printf("\n");
125 
126 		base = SENSOR_CHAN_FDC2X1X_CAPACITANCE_CH0;
127 		for (i = 0; i < num_ch; i++) {
128 			sensor_channel_get(dev, base++, &ch_buf[i]);
129 			printf("ch%d: %f pF ", i, sensor_value_to_double(&ch_buf[i]));
130 		}
131 		printf("\n\n");
132 
133 
134 #ifdef CONFIG_PM_DEVICE
135 		p_action = PM_DEVICE_ACTION_TURN_OFF;
136 		ret = pm_device_action_run(dev, p_action);
137 		pm_info(p_action, ret);
138 		k_sleep(K_MSEC(2000));
139 		p_action = PM_DEVICE_ACTION_RESUME;
140 		ret = pm_device_action_run(dev, p_action);
141 		pm_info(p_action, ret);
142 #elif CONFIG_FDC2X1X_TRIGGER_NONE
143 		k_sleep(K_MSEC(100));
144 #endif
145 	}
146 }
147