libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::LinuxGPIO Class Reference

基于 Linux GPIO character device uAPI 的 Linux GPIO 实现 Linux GPIO implementation based on the Linux GPIO character-device uAPI More...

#include <linux_gpio.hpp>

Inheritance diagram for LibXR::LinuxGPIO:
[legend]
Collaboration diagram for LibXR::LinuxGPIO:
[legend]

Public Member Functions

 LinuxGPIO (const std::string &chip_path, unsigned int line_offset)
 
 LinuxGPIO (const LinuxGPIO &)=delete
 
LinuxGPIO & operator= (const LinuxGPIO &)=delete
 
bool Read () override
 读取 GPIO 引脚状态 / Reads the GPIO pin state.
 
void Write (bool value) override
 写入 GPIO 引脚状态 / Writes the GPIO pin state.
 
ErrorCode EnableInterrupt () override
 使能 GPIO 引脚中断 / Enables the GPIO pin interrupt.
 
ErrorCode DisableInterrupt () override
 禁用 GPIO 引脚中断 / Disables the GPIO pin interrupt.
 
ErrorCode SetConfig (Configuration config) override
 配置 GPIO 引脚参数 / Configures the GPIO pin settings.
 
- Public Member Functions inherited from LibXR::GPIO
 GPIO ()
 默认构造函数 / Default constructor.
 
virtual ~GPIO ()=default
 虚析构函数 / Virtual destructor.
 
ErrorCode RegisterCallback (Callback callback)
 注册 GPIO 事件回调函数 / Registers a callback function for GPIO events.
 

Additional Inherited Members

- Public Types inherited from LibXR::GPIO
enum class  Direction : uint8_t {
  INPUT , OUTPUT_PUSH_PULL , OUTPUT_OPEN_DRAIN , FALL_INTERRUPT ,
  RISING_INTERRUPT , FALL_RISING_INTERRUPT
}
 定义 GPIO 引脚的方向类型 / Defines the direction types for GPIO pins. More...
 
enum class  Pull : uint8_t { NONE , UP , DOWN }
 定义 GPIO 引脚的上拉/下拉模式。 Defines the pull-up/pull-down configurations for GPIO pins. More...
 
using Callback = LibXR::Callback<>
 
- Public Attributes inherited from LibXR::GPIO
Callback callback_
 GPIO 事件的回调函数 / Callback function for GPIO events.
 

Detailed Description

基于 Linux GPIO character device uAPI 的 Linux GPIO 实现 Linux GPIO implementation based on the Linux GPIO character-device uAPI

Note
优先使用 GPIO chardev v2;旧内核仅在需要时回退到 v1。
Prefer GPIO chardev v2; fall back to v1 only on older kernels.
当前运行级验证聚焦在 chardev v2;v1 fallback 目前仅作为兼容路径保留。
Runtime validation currently focuses on chardev v2; the v1 fallback is kept as a compatibility path.

Definition at line 25 of file linux_gpio.hpp.

Constructor & Destructor Documentation

◆ LinuxGPIO()

LibXR::LinuxGPIO::LinuxGPIO ( const std::string & chip_path,
unsigned int line_offset )

Definition at line 295 of file linux_gpio.cpp.

296 : chip_path_(chip_path), line_offset_(line_offset)
297{
298 UNUSED(InitInterruptWakePipe());
299 UNUSED(OpenChip());
300}

◆ ~LinuxGPIO()

LibXR::LinuxGPIO::~LinuxGPIO ( )
override

Definition at line 302 of file linux_gpio.cpp.

303{
304 interrupt_thread_exit_.store(true);
305 request_kind_.store(RequestKind::NONE);
306 interrupt_enabled_.store(false);
307 NotifyInterruptThread();
308 WaitForInterruptThreadStopped();
309 CloseRequest();
310 CloseChip();
311 CloseInterruptWakePipe();
312}

Member Function Documentation

◆ DisableInterrupt()

ErrorCode LibXR::LinuxGPIO::DisableInterrupt ( )
overridevirtual

禁用 GPIO 引脚中断 / Disables the GPIO pin interrupt.

Returns
操作结果的错误码 / Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 421 of file linux_gpio.cpp.

422{
423 if (EnsureConfigured() != ErrorCode::OK)
424 {
426 }
427
428 if (!IsInterruptDirection(current_config_.direction))
429 {
430 return ErrorCode::ARG_ERR;
431 }
432
433 if (!interrupt_enabled_.load())
434 {
435 return ErrorCode::OK;
436 }
437
438 const Configuration inactive_config = ResolveRequestConfig(current_config_, false);
440 if (NeedsRequestReopen(inactive_config))
441 {
442 ec = ReopenRequest(inactive_config);
443 }
444 else if (abi_version_.load() == AbiVersion::V2)
445 {
446 ec = ReconfigureRequestV2(inactive_config);
447 }
448 else
449 {
450 ec = ReconfigureRequestV1(inactive_config);
451 }
452 if (ec != ErrorCode::OK)
453 {
454 return ec;
455 }
456
457 has_config_ = true;
458 interrupt_enabled_.store(false);
459 return ErrorCode::OK;
460}
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ STATE_ERR
状态错误 / State error
@ OK
操作成功 / Operation successful
@ ARG_ERR
参数错误 / Argument error
Direction direction
GPIO 引脚方向 / GPIO pin direction.
Definition gpio.hpp:48

◆ EnableInterrupt()

ErrorCode LibXR::LinuxGPIO::EnableInterrupt ( )
overridevirtual

使能 GPIO 引脚中断 / Enables the GPIO pin interrupt.

Returns
操作结果的错误码 / Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 380 of file linux_gpio.cpp.

381{
382 if (EnsureConfigured() != ErrorCode::OK)
383 {
385 }
386
387 if (!IsInterruptDirection(current_config_.direction))
388 {
389 return ErrorCode::ARG_ERR;
390 }
391
392 if (interrupt_enabled_.load())
393 {
394 return ErrorCode::OK;
395 }
396
398 if (NeedsRequestReopen(current_config_))
399 {
400 ec = ReopenRequest(current_config_);
401 }
402 else if (abi_version_.load() == AbiVersion::V2)
403 {
404 ec = ReconfigureRequestV2(current_config_);
405 }
406 else
407 {
408 ec = ReconfigureRequestV1(current_config_);
409 }
410 if (ec != ErrorCode::OK)
411 {
412 return ec;
413 }
414
415 has_config_ = true;
416 interrupt_enabled_.store(true);
417 StartInterruptThread();
418 return ErrorCode::OK;
419}

◆ Read()

bool LibXR::LinuxGPIO::Read ( )
overridevirtual

读取 GPIO 引脚状态 / Reads the GPIO pin state.

Returns
返回引脚状态,true 表示高电平,false 表示低电平。 Returns the pin state, true for high, false for low.

Implements LibXR::GPIO.

Definition at line 314 of file linux_gpio.cpp.

315{
316 if (EnsureConfigured() != ErrorCode::OK)
317 {
318 return false;
319 }
320
321 const int request_fd = request_fd_.load();
322 if (abi_version_.load() == AbiVersion::V2)
323 {
324#if XR_LINUX_GPIO_HAS_V2
325 gpio_v2_line_values values = {};
326 values.mask = 1ULL;
327 if (ioctl(request_fd, GPIO_V2_LINE_GET_VALUES_IOCTL, &values) < 0)
328 {
329 XR_LOG_ERROR("Failed to read GPIO value: %s", std::strerror(errno));
330 return false;
331 }
332 return (values.bits & 1ULL) != 0U;
333#else
334 return false;
335#endif
336 }
337
338 gpiohandle_data values = {};
339 if (ioctl(request_fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &values) < 0)
340 {
341 XR_LOG_ERROR("Failed to read GPIO value: %s", std::strerror(errno));
342 return false;
343 }
344
345 return values.values[0] != 0U;
346}

◆ SetConfig()

ErrorCode LibXR::LinuxGPIO::SetConfig ( Configuration config)
overridevirtual

配置 GPIO 引脚参数 / Configures the GPIO pin settings.

Parameters
config需要应用的 GPIO 配置 / The GPIO configuration to apply.
Returns
操作结果的错误码 / Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 462 of file linux_gpio.cpp.

463{
464 const ErrorCode chip_ready = OpenChip();
465 if (chip_ready != ErrorCode::OK)
466 {
467 return chip_ready;
468 }
469
470 const bool keep_interrupt_enabled =
471 interrupt_enabled_.load() && IsInterruptDirection(config.direction);
472 const Configuration request_config =
473 ResolveRequestConfig(config, keep_interrupt_enabled);
474
476 if (request_fd_.load() < 0)
477 {
478 ec = ReopenRequest(request_config);
479 }
480 else if (NeedsRequestReopen(request_config))
481 {
482 ec = ReopenRequest(request_config);
483 }
484 else if (abi_version_.load() == AbiVersion::V2)
485 {
486 ec = ReconfigureRequestV2(request_config);
487 }
488 else
489 {
490 ec = ReconfigureRequestV1(request_config);
491 }
492
493 if (ec != ErrorCode::OK)
494 {
495 return ec;
496 }
497
498 current_config_ = config;
499 has_config_ = true;
500 interrupt_enabled_.store(keep_interrupt_enabled);
501 if (keep_interrupt_enabled)
502 {
503 StartInterruptThread();
504 }
505 return ErrorCode::OK;
506}

◆ Write()

void LibXR::LinuxGPIO::Write ( bool value)
overridevirtual

写入 GPIO 引脚状态 / Writes the GPIO pin state.

Parameters
value要写入的状态,true 表示高电平,false 表示低电平。 The value to write, true for high, false for low.

Implements LibXR::GPIO.

Definition at line 348 of file linux_gpio.cpp.

349{
350 if (EnsureConfigured() != ErrorCode::OK)
351 {
352 return;
353 }
354
355 const int request_fd = request_fd_.load();
356 if (abi_version_.load() == AbiVersion::V2)
357 {
358#if XR_LINUX_GPIO_HAS_V2
359 gpio_v2_line_values values = {};
360 values.mask = 1ULL;
361 values.bits = value ? 1ULL : 0ULL;
362 if (ioctl(request_fd, GPIO_V2_LINE_SET_VALUES_IOCTL, &values) < 0)
363 {
364 XR_LOG_WARN("Failed to write GPIO value: %s", std::strerror(errno));
365 }
366 return;
367#else
368 return;
369#endif
370 }
371
372 gpiohandle_data values = {};
373 values.values[0] = value ? 1U : 0U;
374 if (ioctl(request_fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &values) < 0)
375 {
376 XR_LOG_WARN("Failed to write GPIO value: %s", std::strerror(errno));
377 }
378}

The documentation for this class was generated from the following files: