1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*
* Copyright (c) 2006-2026 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2026-06-08 CYFS add generic pin loopback utest
*/
/**
* Test Case Name: Pin Two-wire Loopback Test
*
* Test Objectives:
* - Validate the generic RT-Thread pin APIs with two externally shorted pins.
* - Verify APIs: rt_device_find, rt_pin_get, rt_pin_mode, rt_pin_write,
* rt_pin_read, rt_pin_attach_irq, rt_pin_irq_enable, rt_pin_detach_irq.
*
* Test Scenarios:
* - **Scenario 1 (GPIO level loopback / pin_loopback_level):**
* 1. Resolve the configured output/input pins.
* 2. Configure one pin as output and the shorted peer as input.
* 3. Drive low and high levels from the output pin, then confirm the input
* pin follows each level.
* 4. Reverse direction and repeat the level check to cover both pins.
* - **Scenario 2 (GPIO IRQ loopback / pin_loopback_irq):**
* 1. Configure the output pin low and attach an interrupt on the input pin.
* 2. Toggle the output pin to generate rising, falling, and both-edge events.
* 3. Wait for the interrupt callback after each generated edge.
*
* Dependencies:
* - Requires `RT_UTEST_PIN` and two physical GPIO pins shorted together.
* - Set `RT_UTEST_PIN_OUT_NAME`/`RT_UTEST_PIN_IN_NAME` or numeric
* `RT_UTEST_PIN_OUT_NUM`/`RT_UTEST_PIN_IN_NUM` for the connected pins.
* - IRQ scenario requires `RT_UTEST_PIN_IRQ` and pin IRQ support in the BSP.
*
* Expected Results:
* - Input level always matches the driven output level.
* - If IRQ testing is enabled and supported, all configured edge callbacks
* arrive before timeout.
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "utest.h"
#ifdef RT_UTEST_PIN
struct pin_loopback_ctx
{
rt_base_t out_pin;
rt_base_t in_pin;
};
static struct pin_loopback_ctx loopback_ctx;
#ifdef RT_UTEST_PIN_IRQ
static volatile rt_uint32_t irq_count;
#endif
static rt_bool_t pin_name_configured(const char *name)
{
return (name != RT_NULL) && (name[0] != '\0');
}
static rt_base_t pin_resolve(const char *name, rt_base_t number)
{
if (pin_name_configured(name))
{
return rt_pin_get(name);
}
return number;
}
static void pin_wait_settle(void)
{
rt_thread_mdelay(RT_UTEST_PIN_SETTLE_MS);
}
static void pin_prepare_loopback(rt_base_t drive_pin, rt_base_t sense_pin)
{
rt_pin_mode(sense_pin, PIN_MODE_INPUT);
rt_pin_mode(drive_pin, PIN_MODE_OUTPUT);
rt_pin_write(drive_pin, PIN_LOW);
pin_wait_settle();
}
static void pin_drive_and_check(rt_base_t drive_pin, rt_base_t sense_pin, rt_uint8_t level)
{
rt_pin_write(drive_pin, level);
pin_wait_settle();
uassert_int_equal((int)rt_pin_read(sense_pin), (int)level);
}
static void pin_loopback_level(void)
{
pin_prepare_loopback(loopback_ctx.out_pin, loopback_ctx.in_pin);
pin_drive_and_check(loopback_ctx.out_pin, loopback_ctx.in_pin, PIN_LOW);
pin_drive_and_check(loopback_ctx.out_pin, loopback_ctx.in_pin, PIN_HIGH);
pin_prepare_loopback(loopback_ctx.in_pin, loopback_ctx.out_pin);
pin_drive_and_check(loopback_ctx.in_pin, loopback_ctx.out_pin, PIN_LOW);
pin_drive_and_check(loopback_ctx.in_pin, loopback_ctx.out_pin, PIN_HIGH);
rt_pin_write(loopback_ctx.in_pin, PIN_LOW);
}
#ifdef RT_UTEST_PIN_IRQ
static rt_tick_t pin_irq_timeout_ticks(void)
{
rt_tick_t ticks;
ticks = rt_tick_from_millisecond(RT_UTEST_PIN_IRQ_TIMEOUT_MS);
if (ticks == 0)
{
ticks = 1;
}
return ticks;
}
static void pin_irq_handler(void *args)
{
RT_UNUSED(args);
irq_count++;
}
static rt_err_t pin_irq_wait_count(rt_uint32_t expected_count)
{
rt_tick_t tick_start;
rt_tick_t timeout;
tick_start = rt_tick_get();
timeout = pin_irq_timeout_ticks();
while (irq_count < expected_count)
{
if (rt_tick_get_delta(tick_start) >= timeout)
{
return -RT_ETIMEOUT;
}
rt_thread_mdelay(1);
}
return RT_EOK;
}
static rt_err_t pin_irq_cleanup(rt_base_t pin)
{
rt_err_t ret;
ret = rt_pin_irq_enable(pin, PIN_IRQ_DISABLE);
if (ret != RT_EOK)
{
return ret;
}
return rt_pin_detach_irq(pin);
}
static rt_err_t pin_irq_attach_enable(rt_uint8_t mode)
{
rt_err_t ret;
ret = rt_pin_attach_irq(loopback_ctx.in_pin, mode, pin_irq_handler, RT_NULL);
if (ret != RT_EOK)
{
return ret;
}
ret = rt_pin_irq_enable(loopback_ctx.in_pin, PIN_IRQ_ENABLE);
if (ret != RT_EOK)
{
rt_pin_detach_irq(loopback_ctx.in_pin);
}
return ret;
}
static void pin_trigger_rising(void)
{
rt_pin_write(loopback_ctx.out_pin, PIN_LOW);
pin_wait_settle();
rt_pin_write(loopback_ctx.out_pin, PIN_HIGH);
}
static void pin_trigger_falling(void)
{
rt_pin_write(loopback_ctx.out_pin, PIN_HIGH);
pin_wait_settle();
rt_pin_write(loopback_ctx.out_pin, PIN_LOW);
}
static void pin_loopback_irq_rising(void)
{
rt_err_t ret;
pin_prepare_loopback(loopback_ctx.out_pin, loopback_ctx.in_pin);
irq_count = 0;
ret = pin_irq_attach_enable(PIN_IRQ_MODE_RISING);
if (ret == -RT_ENOSYS)
{
LOG_W("pin irq is not supported, skip rising-edge irq check");
return;
}
uassert_int_equal(ret, RT_EOK);
if (ret != RT_EOK)
{
return;
}
pin_trigger_rising();
uassert_int_equal(pin_irq_wait_count(1), RT_EOK);
uassert_int_equal((int)irq_count, 1);
uassert_int_equal(pin_irq_cleanup(loopback_ctx.in_pin), RT_EOK);
}
static void pin_loopback_irq_falling(void)
{
rt_err_t ret;
pin_prepare_loopback(loopback_ctx.out_pin, loopback_ctx.in_pin);
irq_count = 0;
ret = pin_irq_attach_enable(PIN_IRQ_MODE_FALLING);
if (ret == -RT_ENOSYS)
{
LOG_W("pin irq is not supported, skip falling-edge irq check");
return;
}
uassert_int_equal(ret, RT_EOK);
if (ret != RT_EOK)
{
return;
}
pin_trigger_falling();
uassert_int_equal(pin_irq_wait_count(1), RT_EOK);
uassert_int_equal((int)irq_count, 1);
uassert_int_equal(pin_irq_cleanup(loopback_ctx.in_pin), RT_EOK);
}
static void pin_loopback_irq_both_edges(void)
{
rt_err_t ret;
pin_prepare_loopback(loopback_ctx.out_pin, loopback_ctx.in_pin);
irq_count = 0;
ret = pin_irq_attach_enable(PIN_IRQ_MODE_RISING_FALLING);
if (ret == -RT_ENOSYS)
{
LOG_W("pin irq is not supported, skip both-edge irq check");
return;
}
uassert_int_equal(ret, RT_EOK);
if (ret != RT_EOK)
{
return;
}
pin_trigger_rising();
uassert_int_equal(pin_irq_wait_count(1), RT_EOK);
pin_trigger_falling();
uassert_int_equal(pin_irq_wait_count(2), RT_EOK);
uassert_int_equal((int)irq_count, 2);
uassert_int_equal(pin_irq_cleanup(loopback_ctx.in_pin), RT_EOK);
}
#endif /* RT_UTEST_PIN_IRQ */
static rt_err_t utest_tc_init(void)
{
if ((rt_device_find("pin") == RT_NULL) && (rt_device_find("gpio") == RT_NULL))
{
LOG_E("pin/gpio device is not registered");
return -RT_ERROR;
}
loopback_ctx.out_pin = pin_resolve(RT_UTEST_PIN_OUT_NAME, RT_UTEST_PIN_OUT_NUM);
loopback_ctx.in_pin = pin_resolve(RT_UTEST_PIN_IN_NAME, RT_UTEST_PIN_IN_NUM);
if ((loopback_ctx.out_pin < 0) || (loopback_ctx.in_pin < 0))
{
LOG_E("invalid pin test config: out=%d in=%d", loopback_ctx.out_pin, loopback_ctx.in_pin);
return -RT_ERROR;
}
if (loopback_ctx.out_pin == loopback_ctx.in_pin)
{
LOG_E("pin test output and input pins must be different");
return -RT_ERROR;
}
LOG_I("pin loopback test uses output pin %d and input pin %d", loopback_ctx.out_pin, loopback_ctx.in_pin);
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
#ifdef RT_UTEST_PIN_IRQ
rt_pin_irq_enable(loopback_ctx.in_pin, PIN_IRQ_DISABLE);
rt_pin_detach_irq(loopback_ctx.in_pin);
#endif
rt_pin_mode(loopback_ctx.in_pin, PIN_MODE_INPUT);
rt_pin_mode(loopback_ctx.out_pin, PIN_MODE_OUTPUT);
rt_pin_write(loopback_ctx.out_pin, PIN_LOW);
rt_pin_mode(loopback_ctx.out_pin, PIN_MODE_INPUT);
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(pin_loopback_level);
#ifdef RT_UTEST_PIN_IRQ
UTEST_UNIT_RUN(pin_loopback_irq_rising);
UTEST_UNIT_RUN(pin_loopback_irq_falling);
UTEST_UNIT_RUN(pin_loopback_irq_both_edges);
#endif
}
UTEST_TC_EXPORT(testcase, "components.drivers.pin.loopback", utest_tc_init, utest_tc_cleanup, 20);
#endif /* RT_UTEST_PIN */
|