Skip to content

Commit f304555

Browse files
Minh Tangquytranpzz
authored andcommitted
samples: boards: renesas: Add sample for CTSU input driver
Add sample for CTSU input driver using QE config files on RSK-RX130-512KB Signed-off-by: Minh Tang <[email protected]>
1 parent f90465b commit f304555

File tree

7 files changed

+604
-0
lines changed

7 files changed

+604
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
6+
project(renesas_ctsu)
7+
8+
target_sources(app PRIVATE src/main.c)
9+
10+
if(CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG)
11+
zephyr_include_directories(src/qe_generated_files)
12+
zephyr_sources(src/qe_generated_files/qe_touch_config.c)
13+
endif()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&ctsu {
8+
status = "okay";
9+
};
10+
11+
&onboard_slider {
12+
zephyr,code = <1>;
13+
};
14+
15+
&onboard_button_1 {
16+
zephyr,code = <10>;
17+
};
18+
19+
&onboard_button_2 {
20+
zephyr,code = <11>;
21+
};
22+
23+
/ {
24+
leds {
25+
testled0: testled0 {
26+
gpios = <&ioportd 3 GPIO_ACTIVE_LOW>;
27+
label = "LED0";
28+
};
29+
30+
testled1: testled1 {
31+
gpios = <&ioportd 4 GPIO_ACTIVE_LOW>;
32+
label = "LED1";
33+
};
34+
35+
testled2: testled2 {
36+
gpios = <&ioporte 6 GPIO_ACTIVE_LOW>;
37+
label = "LED2";
38+
};
39+
40+
testled3: testled3 {
41+
gpios = <&ioporte 7 GPIO_ACTIVE_LOW>;
42+
label = "LED3";
43+
};
44+
};
45+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_GPIO=y
2+
CONFIG_INPUT=y
3+
CONFIG_INPUT_MODE_SYNCHRONOUS=y
4+
CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG=y
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/sys/util.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/gpio/gpio_emul.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/input/input.h>
12+
#include <zephyr/input/input_renesas_rx_ctsu.h>
13+
14+
#ifdef CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG
15+
#include "qe_touch_config.h"
16+
#endif /* CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG */
17+
18+
static const struct device *const test_touch_dev = DEVICE_DT_GET(DT_NODELABEL(ctsu));
19+
#define BUTTON_1_IDX DT_NODE_CHILD_IDX(DT_NODELABEL(onboard_button_1))
20+
#define BUTTON_2_IDX DT_NODE_CHILD_IDX(DT_NODELABEL(onboard_button_2))
21+
#define SLIDER_IDX DT_NODE_CHILD_IDX(DT_NODELABEL(onboard_slider))
22+
23+
#define STACKSIZE 1024
24+
#define PRIORITY 7
25+
#define LED0_NODE DT_NODELABEL(testled0)
26+
#define LED1_NODE DT_NODELABEL(testled1)
27+
#define LED2_NODE DT_NODELABEL(testled2)
28+
#define LED3_NODE DT_NODELABEL(testled3)
29+
#define NUM_TEST_LED 4
30+
31+
#define LED_ON 1
32+
#define LED_OFF 0
33+
34+
#define DEBOUNCE_TIMES 3
35+
#define DEBOUNCE_INTERVAL_MS (500 / DEBOUNCE_TIMES)
36+
37+
enum {
38+
TOUCH_BUTTON_1 = 10,
39+
TOUCH_BUTTON_2 = 11,
40+
TOUCH_SLIDER = 1,
41+
};
42+
43+
static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
44+
static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios);
45+
static const struct gpio_dt_spec led2 = GPIO_DT_SPEC_GET(LED2_NODE, gpios);
46+
static const struct gpio_dt_spec led3 = GPIO_DT_SPEC_GET(LED3_NODE, gpios);
47+
static const struct gpio_dt_spec *leds[] = {&led0, &led1, &led2, &led3};
48+
static volatile int led_state = 0;
49+
static volatile int leds_on_off[NUM_TEST_LED] = {0};
50+
static volatile int current_main_led = 0;
51+
52+
static volatile int event_count[DT_CHILD_NUM_STATUS_OKAY(DT_NODELABEL(ctsu))] = {0};
53+
static uint16_t last_code;
54+
static volatile int32_t last_val[DT_CHILD_NUM_STATUS_OKAY(DT_NODELABEL(ctsu))];
55+
static struct k_sem btn1_sem;
56+
static struct k_sem btn2_sem;
57+
static struct k_sem sldr_sem;
58+
static struct k_sem render_sem;
59+
60+
static void sample_setup(void)
61+
{
62+
gpio_pin_configure_dt(&led0, GPIO_OUTPUT_INACTIVE);
63+
gpio_pin_configure_dt(&led1, GPIO_OUTPUT_INACTIVE);
64+
gpio_pin_configure_dt(&led2, GPIO_OUTPUT_INACTIVE);
65+
gpio_pin_configure_dt(&led3, GPIO_OUTPUT_INACTIVE);
66+
67+
k_sem_init(&btn1_sem, 0, 1);
68+
k_sem_init(&btn2_sem, 0, 1);
69+
k_sem_init(&sldr_sem, 0, 1);
70+
k_sem_init(&render_sem, 0, 1);
71+
72+
#ifdef CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG
73+
int ret = renesas_rx_ctsu_group_configure(test_touch_dev, (struct renesas_rx_ctsu_touch_cfg *)&g_qe_touch_instance_config01);
74+
75+
if(ret < 0) {
76+
printk("Failed to configure QE Touch: %d\n", ret);
77+
return;
78+
}
79+
#endif /* CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG */
80+
}
81+
82+
void button1_task(void) {
83+
84+
while (1) {
85+
k_sem_take(&btn1_sem, K_FOREVER);
86+
if (led_state == 0) {
87+
/** Current leds are OFF, do nothing */
88+
} else {
89+
if(led_state >= 15) {
90+
led_state = 1;
91+
} else {
92+
led_state++;
93+
}
94+
}
95+
k_sem_give(&render_sem);
96+
}
97+
}
98+
99+
void button2_task(void) {
100+
int prev_state = 0;
101+
102+
while (1) {
103+
k_sem_take(&btn2_sem, K_FOREVER);
104+
if (led_state == 0) {
105+
/** Current led state is OFF */
106+
if (prev_state == 0) {
107+
/** First time power on */
108+
led_state = 1;
109+
} else {
110+
/** Restore prev_state */
111+
led_state = prev_state;
112+
}
113+
} else {
114+
prev_state = led_state;
115+
led_state = 0;
116+
}
117+
k_sem_give(&render_sem);
118+
}
119+
}
120+
121+
void slider_task(void) {
122+
while (1) {
123+
k_sem_take(&sldr_sem, K_FOREVER);
124+
if (led_state == 0) continue;
125+
int curr_val = last_val[SLIDER_IDX];
126+
if(curr_val != TOUCH_OFF_VALUE) {
127+
if(curr_val == 100) {
128+
curr_val = 99;
129+
}
130+
current_main_led = 3 - (curr_val/25);
131+
}
132+
k_sem_give(&render_sem);
133+
}
134+
}
135+
136+
void render_task(void) {
137+
while(1) {
138+
k_sem_take(&render_sem, K_FOREVER);
139+
for(int i = 0; i < NUM_TEST_LED; i++) {
140+
leds_on_off[(current_main_led + i) % 4] = ((led_state >> i) & 0x1)? LED_ON : LED_OFF;
141+
}
142+
143+
for(int i = 0; i < NUM_TEST_LED; i++) {
144+
gpio_pin_set_dt(leds[i], leds_on_off[i]);
145+
}
146+
}
147+
}
148+
149+
static void test_touch_keys_cb_handler(struct input_event *evt, void *user_data)
150+
{
151+
switch (evt->code) {
152+
case TOUCH_BUTTON_1:
153+
if (evt->value == 0) {
154+
printk("Button 1 released\n");
155+
if (last_val[BUTTON_1_IDX] != 0) {
156+
k_sem_give(&btn1_sem);
157+
}
158+
} else {
159+
event_count[BUTTON_1_IDX]++;
160+
printk("Button 1 pressed %d time(s)\n", event_count[BUTTON_1_IDX]);
161+
}
162+
last_val[BUTTON_1_IDX] = evt->value;
163+
break;
164+
165+
case TOUCH_BUTTON_2:
166+
if (evt->value == 0) {
167+
printk("Button 2 released\n");
168+
if (last_val[BUTTON_2_IDX] != 0) {
169+
k_sem_give(&btn2_sem);
170+
}
171+
} else {
172+
event_count[BUTTON_2_IDX]++;
173+
printk("Button 2 pressed %d time(s)\n", event_count[BUTTON_2_IDX]);
174+
}
175+
last_val[BUTTON_2_IDX] = evt->value;
176+
break;
177+
178+
case TOUCH_SLIDER:
179+
if (evt->value == TOUCH_OFF_VALUE) {
180+
printk("Slider released\n");
181+
if (last_val[SLIDER_IDX] != TOUCH_OFF_VALUE) {
182+
}
183+
} else {
184+
if (last_val[SLIDER_IDX] == TOUCH_OFF_VALUE) {
185+
event_count[SLIDER_IDX]++;
186+
printk("Slider pressed\n");
187+
}
188+
printk("Position: %d\n", evt->value);
189+
}
190+
k_sem_give(&sldr_sem);
191+
last_val[SLIDER_IDX] = evt->value;
192+
break;
193+
194+
default:
195+
break;
196+
}
197+
last_code = evt->code;
198+
}
199+
INPUT_CALLBACK_DEFINE(test_touch_dev, test_touch_keys_cb_handler, NULL);
200+
201+
K_THREAD_DEFINE(button1_id, STACKSIZE, button1_task, NULL, NULL, NULL,
202+
PRIORITY, 0, 0);
203+
K_THREAD_DEFINE(button2_id, STACKSIZE, button2_task, NULL, NULL, NULL,
204+
PRIORITY, 0, 0);
205+
K_THREAD_DEFINE(slider_id, STACKSIZE, slider_task, NULL, NULL, NULL,
206+
PRIORITY, 0, 0);
207+
K_THREAD_DEFINE(render_id, STACKSIZE, render_task, NULL, NULL, NULL,
208+
PRIORITY, 0, 0);
209+
210+
int main(void)
211+
{
212+
sample_setup();
213+
printk("On board CTSU components sample started!\n");
214+
printk("Press on touch nodes for sample\n");
215+
return 0;
216+
}

0 commit comments

Comments
 (0)