Skip to content

Commit

Permalink
Merge pull request #9 from MK16kawai/dev
Browse files Browse the repository at this point in the history
add gpio.reset Mode&Pull
  • Loading branch information
Neutree authored Jun 20, 2024
2 parents 71d5b39 + 9a318a4 commit efe8a86
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 10 deletions.
28 changes: 27 additions & 1 deletion components/peripheral/include/maix_gpio.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @author neucrack@sipeed, lxowalle@sipeed
* @author neucrack@sipeed, lxowalle@sipeed, iawak9lkm@sipeed
* @copyright Sipeed Ltd 2023-
* @license Apache 2.0
* @update 2023.9.8: Add framework, create this file.
Expand All @@ -8,6 +8,7 @@
#pragma once

#include <string>
#include "maix_basic.hpp"

namespace maix::peripheral::gpio
{
Expand Down Expand Up @@ -86,11 +87,36 @@ namespace maix::peripheral::gpio
*/
void toggle();

/**
* @brief gpio get mode
* @maixpy maix.peripheral.gpio.GPIO.get_mode
*/
gpio::Mode get_mode();

/**
* @brief get gpio pull
* @return gpio::Pull type
* @maixpy maix.peripheral.gpio.GPIO.get_pull
*/
gpio::Pull get_pull();

/**
* @brief reset gpio
* @param[in] mode gpio mode. gpio.Mode type
* @param[in] pull gpio pull. gpio.Pull type
* For input mode, this will set gpio default status(value), if set to gpio.Pull.PULL_NONE, gpio value will be floating.
* For output mode, this will set gpio default status(value), if set to gpio.Pull.PULL_UP, gpio value will be 1, else 0.
* @return err::Err type
* @maixpy maix.peripheral.gpio.GPIO.reset
*/
err::Err reset(gpio::Mode mode, gpio::Pull pull);

private:
std::string _pin;
gpio::Mode _mode;
gpio::Pull _pull;
int _fd;
int _offset;
int _line;
bool _special;
};
Expand Down
46 changes: 44 additions & 2 deletions components/peripheral/port/linux/maix_gpio.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @author neucrack@sipeed
* @author neucrack@sipeed, iawak9lkm@sipeed
* @copyright Sipeed Ltd 2024-
* @license Apache 2.0
* @update 2024.5.13: update this file.
Expand Down Expand Up @@ -39,7 +39,6 @@ namespace maix::peripheral::gpio

GPIO::GPIO(std::string pin, gpio::Mode mode, gpio::Pull pull)
{
this->_pin = pin;
this->_mode = mode;
this->_pull = pull;
this->_fd = 0;
Expand Down Expand Up @@ -161,4 +160,47 @@ namespace maix::peripheral::gpio
else
low();
}

gpio::Mode GPIO::get_mode()
{
return _mode;
}

gpio::Pull GPIO::get_pull()
{
return _pull;
}

err::Err GPIO::reset(gpio::Mode mode, gpio::Pull pull)
{
if (mode == _mode && pull == _pull)
return err::ERR_NONE;

if (this->_line > 0) {
::close(this->_line);
this->_line = -1;
}

struct gpiohandle_request req;
::memset(&req, 0, sizeof(req));
req.lineoffsets[0] = _offset;
req.lines = 1;
if (mode == gpio::Mode::IN)
req.flags = GPIOHANDLE_REQUEST_INPUT;
else if (mode == gpio::Mode::OUT)
req.flags = GPIOHANDLE_REQUEST_OUTPUT;
else if (mode == gpio::Mode::OUT_OD)
req.flags = GPIOHANDLE_REQUEST_OUTPUT | GPIOHANDLE_REQUEST_OPEN_DRAIN;
req.default_values[0] = (pull == gpio::Pull::PULL_UP ? 1 : 0);
::strncpy(req.consumer_label, "maix_gpio", sizeof(req.consumer_label));
if (::ioctl(_fd, GPIO_GET_LINEHANDLE_IOCTL, &req) < 0) {
log::error("gpio set mode err: %s", ::strerror(errno));
return err::ERR_IO;
}

this->_line = req.fd;
this->_mode = mode;
this->_pull = pull;
return err::ERR_NONE;
}
}; // namespace maix::peripheral::gpio
56 changes: 49 additions & 7 deletions components/peripheral/port/maixcam/maix_gpio.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* @author neucrack@sipeed
* @author neucrack@sipeed, iawak9lkm@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>
#include <unistd.h>
#include <string.h>
Expand Down Expand Up @@ -145,17 +144,16 @@ namespace maix::peripheral::gpio

GPIO::GPIO(std::string pin, gpio::Mode mode, gpio::Pull pull)
{
this->_pin = pin;
this->_mode = mode;
this->_pull = pull;
this->_mode = mode;
this->_fd = 0;
this->_line = 0;

// to upper case first
// convert B14/GPIOB14 to chip_id and offset, B can be any letter
// if pin is number, throw not implemented error
int chip_id = 0;
int offset = 0;
_offset = 0;
std::transform(pin.begin(), pin.end(), pin.begin(), ::toupper);
if (pin.find("GPIO") != std::string::npos)
{
Expand All @@ -166,7 +164,7 @@ namespace maix::peripheral::gpio
chip_id = pin[0] - 'A';
try
{
offset = std::stoi(pin.substr(1));
_offset = std::stoi(pin.substr(1));
}
catch (const std::exception &e)
{
Expand Down Expand Up @@ -201,7 +199,7 @@ namespace maix::peripheral::gpio
// get gpio line
struct gpiohandle_request req;
memset(&req, 0, sizeof(req));
req.lineoffsets[0] = offset;
req.lineoffsets[0] = _offset;
req.lines = 1;
if (mode == gpio::Mode::IN)
req.flags = GPIOHANDLE_REQUEST_INPUT;
Expand Down Expand Up @@ -280,4 +278,48 @@ namespace maix::peripheral::gpio
else
low();
}

gpio::Mode GPIO::get_mode()
{
return _mode;
}

gpio::Pull GPIO::get_pull()
{
return _pull;
}

err::Err GPIO::reset(gpio::Mode mode, gpio::Pull pull)
{
if (mode == _mode && pull == _pull)
return err::ERR_NONE;

if (this->_line > 0) {
::close(this->_line);
this->_line = -1;
}

struct gpiohandle_request req;
::memset(&req, 0, sizeof(req));
req.lineoffsets[0] = _offset;
req.lines = 1;
if (mode == gpio::Mode::IN)
req.flags = GPIOHANDLE_REQUEST_INPUT;
else if (mode == gpio::Mode::OUT)
req.flags = GPIOHANDLE_REQUEST_OUTPUT;
else if (mode == gpio::Mode::OUT_OD)
req.flags = GPIOHANDLE_REQUEST_OUTPUT | GPIOHANDLE_REQUEST_OPEN_DRAIN;
req.default_values[0] = (pull == gpio::Pull::PULL_UP ? 1 : 0);
::strncpy(req.consumer_label, "maix_gpio", sizeof(req.consumer_label));
if (::ioctl(_fd, GPIO_GET_LINEHANDLE_IOCTL, &req) < 0) {
log::error("gpio set mode err: %s", ::strerror(errno));
return err::ERR_IO;
}

this->_line = req.fd;
this->_mode = mode;
this->_pull = pull;
return err::ERR_NONE;
}

}; // namespace maix::peripheral::gpio

0 comments on commit efe8a86

Please sign in to comment.