Project

General

Profile

Can't read state of output GPIO pins » gpio_read_output_test_cpp20.cpp

C++ 20 test program demonstrating the issue - Tim Troester, 03/16/2026 05:43 PM

 
1
#include <fcntl.h>
2
#include <linux/gpio.h>
3
#include <sys/ioctl.h>
4

    
5
#include <cerrno>
6
#include <chrono>
7
#include <cstring>
8
#include <iostream>
9
#include <thread>
10

    
11
static constexpr auto s_which_gpio_chip = "/dev/gpiochip2";
12
static constexpr auto s_which_gpio_line = 10;
13

    
14
int main()
15
{
16
    int chip_fd = open(s_which_gpio_chip, O_RDWR);
17
    if (chip_fd < 0)
18
    {
19
        std::cout << "failed to open gpio chip: " << strerror(errno) << std::endl;
20
        return -1;
21
    }
22

    
23
    gpio_v2_line_request request{};
24
    request.num_lines = 1;
25
    request.offsets[0] = s_which_gpio_line;
26
    request.event_buffer_size = 16;
27
    request.config.num_attrs = 0;
28
    request.config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
29

    
30
    if (ioctl(chip_fd, GPIO_V2_GET_LINE_IOCTL, &request) != 0)
31
    {
32
        std::cout << "GPIO_V2_GET_LINE_IOCTL failed: " << strerror(errno) << std::endl;
33
        return -2;
34
    }
35

    
36
    int line_fd = request.fd;
37
    if (line_fd < 0)
38
    {
39
        std::cout << "GPIO_V2_GET_LINE_IOCTL returned unexpected fd: " << line_fd << std::endl;
40
        return -3;
41
    }
42

    
43
    for (;;)
44
    {
45
        // set pin active
46
        std::cout << "setting pin to ACTIVE!" << std::endl;
47
        gpio_v2_line_values values_1{.bits = 1u, .mask = 1u};
48
        if (ioctl(line_fd, GPIO_V2_LINE_SET_VALUES_IOCTL, &values_1) != 0)
49
        {
50
            std::cout << "GPIO_V2_LINE_SET_VALUES_IOCTL failed: " << strerror(errno) << std::endl;
51
            return -4;
52
        }
53

    
54
        // read pin state
55
        gpio_v2_line_values confirm_values_1{.bits = 0u, .mask = 1u};
56
        if (ioctl(line_fd, GPIO_V2_LINE_GET_VALUES_IOCTL, &confirm_values_1) != 0)
57
        {
58
            std::cout << "GPIO_V2_LINE_GET_VALUES_IOCTL failed: " << strerror(errno) << std::endl;
59
            return -5;
60
        }
61
        std::cout << "confirming pin state as " << ((confirm_values_1.bits & 1u) ? "ACTIVE" : "INACTIVE") << "!"
62
                  << std::endl;
63

    
64
        std::this_thread::sleep_for(std::chrono::seconds(5));
65

    
66
        // set pin inactive
67
        std::cout << "setting pin to INACTIVE!" << std::endl;
68
        gpio_v2_line_values values_2{.bits = 0u, .mask = 1u};
69
        if (ioctl(line_fd, GPIO_V2_LINE_SET_VALUES_IOCTL, &values_2) != 0)
70
        {
71
            std::cout << "GPIO_V2_LINE_SET_VALUES_IOCTL failed: " << strerror(errno) << std::endl;
72
            return -4;
73
        }
74

    
75
        // read pin state
76
        gpio_v2_line_values confirm_values_2{.bits = 0u, .mask = 1u};
77
        if (ioctl(line_fd, GPIO_V2_LINE_GET_VALUES_IOCTL, &confirm_values_2) != 0)
78
        {
79
            std::cout << "GPIO_V2_LINE_GET_VALUES_IOCTL failed: " << strerror(errno) << std::endl;
80
            return -5;
81
        }
82
        std::cout << "confirming pin state as " << ((confirm_values_2.bits & 1u) ? "ACTIVE" : "INACTIVE") << "!"
83
                  << std::endl;
84

    
85
        std::this_thread::sleep_for(std::chrono::seconds(5));
86
    }
87
}
88
 
(1-1/2) Go to top
Add picture from clipboard (Maximum size: 1 GB)