Skip to content

Commit

Permalink
compolete pinmap module
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed May 14, 2024
1 parent 88c8280 commit f257baa
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 37 deletions.
1 change: 1 addition & 0 deletions components/peripheral/include/maix_gpio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ namespace maix::peripheral::gpio
gpio::Pull _pull;
int _fd;
int _line;
bool _special;
};
}; // namespace maix::peripheral::gpio
7 changes: 7 additions & 0 deletions components/peripheral/include/maix_pinmap.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @author neucrack@sipeed
* @copyright Sipeed Ltd 2024-
* @license Apache 2.0
* @update 2024.5.13: create this file.
*/

#pragma once

#include "maix_basic.hpp"
Expand Down
161 changes: 161 additions & 0 deletions components/peripheral/port/linux/maix_gpio.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @author neucrack@sipeed
* @copyright Sipeed Ltd 2024-
* @license Apache 2.0
* @update 2024.5.13: update this file.
*/


#include "maix_gpio.hpp"
#include "maix_basic.hpp"
#include <fcntl.h>
Expand All @@ -6,9 +14,136 @@
#include <linux/gpio.h>
#include <algorithm>
#include <sys/ioctl.h>
#include <errno.h>

namespace maix::peripheral::gpio
{
#if PLATFORM_MAIXCAM
static int led_init(int default_value)
{
int fd;
const char *brightness_path = "/sys/class/leds/led-user/brightness";
const char *trigger_path = "/sys/class/leds/led-user/trigger";
char value_str[2] = {default_value == 0 ? '0' : '1', '\0'};
char none_str[] = "none";

// Step 1: Open brightness file and write default value
fd = open(brightness_path, O_RDWR);
if (fd < 0)
{
perror("Failed to open brightness file");
return -1;
}

if (write(fd, value_str, sizeof(value_str)) < 0)
{
perror("Failed to write to brightness file");
close(fd);
return -1;
}

// Step 2: Write "none" to trigger file
int trigger_fd = open(trigger_path, O_WRONLY);
if (trigger_fd < 0)
{
perror("Failed to open trigger file");
close(fd);
return -1;
}

if (write(trigger_fd, none_str, strlen(none_str)) < 0)
{
perror("Failed to write to trigger file");
close(fd);
close(trigger_fd);
return -1;
}

close(trigger_fd);

// Step 3: Return fd (keep brightness file open)
return fd;
}

static int led_set(int fd, int value)
{
char value_str[2] = {value == 0 ? '0' : '1', '\0'};

// Write value to the open file descriptor
if (write(fd, value_str, sizeof(value_str)) < 0)
{
perror("Failed to write to brightness file descriptor");
return -1;
}

return 0;
}
static int led_get(int fd)
{
const char *brightness_path = "/sys/class/leds/led-user/brightness";
char value_str[2] = {0};

// Open the brightness file for reading
int brightness_fd = open(brightness_path, O_RDONLY);
if (brightness_fd < 0)
{
perror("Failed to open brightness file");
return -1;
}

// Read the current value from the brightness file
if (read(brightness_fd, value_str, sizeof(value_str) - 1) < 0)
{
perror("Failed to read from brightness file");
close(brightness_fd);
return -1;
}

// Close the brightness file
close(brightness_fd);

// Convert the read value to an integer (assuming '0' or '1')
if (value_str[0] == '0')
{
return 0;
}
else if (value_str[0] == '1')
{
return 1;
}
log::error("Unexpected value read from brightness file: %s\n", value_str);
return -1;
}

static int led_deinit(int fd)
{
const char *trigger_path = "/sys/class/leds/led-user/trigger";
const char activity_str[] = "activity";
int trigger_fd;

// Step 1: Close the file descriptor, ignoring any error
close(fd);

// Step 2: Write "activity" to the trigger file
trigger_fd = open(trigger_path, O_WRONLY);
if (trigger_fd < 0)
{
perror("Failed to open trigger file");
return -1;
}

if (write(trigger_fd, activity_str, strlen(activity_str)) < 0)
{
perror("Failed to write to trigger file");
close(trigger_fd);
return -1;
}

close(trigger_fd);
return 0;
}
#endif

GPIO::GPIO(std::string pin, gpio::Mode mode, gpio::Pull pull)
{
this->_pin = pin;
Expand Down Expand Up @@ -46,7 +181,19 @@ namespace maix::peripheral::gpio
#if PLATFORM_MAIXCAM
if (chip_id == 'P' - 'A') // GPIOP is /dev/gpiochip4
chip_id = 4;
// special for A14
if (pin == "A14")
{
this->_fd = led_init(pull == gpio::Pull::PULL_UP ? 1 : 0);
if (this->_fd <= 0)
{
throw err::Exception(err::Err::ERR_IO, "open A14 failed");
}
_special = true;
return;
}
#endif
_special = false;
// open gpiochip
std::string chip_path = "/dev/gpiochip" + std::to_string(chip_id);
int fd = open(chip_path.c_str(), O_RDWR);
Expand Down Expand Up @@ -79,6 +226,11 @@ namespace maix::peripheral::gpio

GPIO::~GPIO()
{
if (_special)
{
led_deinit(_fd);
return;
}
if (this->_line > 0)
close(this->_line);
if (this->_fd > 0)
Expand All @@ -87,6 +239,15 @@ namespace maix::peripheral::gpio

int GPIO::value(int value)
{
if (_special)
{
if (value >= 0)
led_set(_fd, value);
else
return led_get(_fd);
return value;
}

struct gpiohandle_data data;
memset(&data, 0, sizeof(data));
if (value >= 0)
Expand Down
Loading

0 comments on commit f257baa

Please sign in to comment.