Skip to content

Start of GPIO implementation based on GPIO API #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions src/device/arm-linux/giopctl-arm-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,50 @@
namespace iotjs {


class GpioControlInst : public GpioControl {
class GpioControlImpl : public GpioControl {
public:
explicit GpioControlInst(JObject& jgpioctl);
explicit GpioControlImpl(JObject& jgpioctl);

virtual int Initialize(void);
virtual void Release(void);
virtual int PinMode(uint32_t portpin);
virtual int WritePin(uint32_t portpin, uint8_t data);
virtual int ReadPin(uint32_t portpin, uint8_t* pdata);
virtual int SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb);
virtual int SetPin(int32_t pin, int32_t dir, int32_t mode);
};


//-----------------------------------------------------------------------------

GpioControl* GpioControl::Create(JObject& jgpioctl)
{
return new GpioControlInst(jgpioctl);
return new GpioControlImpl(jgpioctl);
}


GpioControlInst::GpioControlInst(JObject& jgpioctl)
GpioControlImpl::GpioControlImpl(JObject& jgpioctl)
: GpioControl(jgpioctl) {
}


int GpioControlInst::Initialize(void) {
int GpioControlImpl::Initialize(void) {
if (_fd > 0 )
return IOTJS_GPIO_INUSE;
return GPIO_ERR_INITALIZE;

_fd = 1;
return _fd;
}


void GpioControlInst::Release(void) {
void GpioControlImpl::Release(void) {
_fd = 0;
}


int GpioControlInst::PinMode(uint32_t portpin) {
if (_fd <= 0)
return IOTJS_GPIO_NOTINITED;
return 0;
}


int GpioControlInst::WritePin(uint32_t portpin, uint8_t data) {
if (_fd <= 0)
return IOTJS_GPIO_NOTINITED;
int GpioControlImpl::SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb) {
return 0;
}


int GpioControlInst::ReadPin(uint32_t portpin, uint8_t* pdata) {
if (_fd <= 0)
return IOTJS_GPIO_NOTINITED;
int GpioControlImpl::SetPin(int32_t pin, int32_t dir, int32_t mode) {
return 0;
}

Expand Down
55 changes: 15 additions & 40 deletions src/device/arm-nuttx/gpioctl_arm_nuttx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,81 +28,56 @@
namespace iotjs {


class GpioControlInst : public GpioControl {
class GpioControlImpl : public GpioControl {
public:
explicit GpioControlInst(JObject& jgpioctl);
explicit GpioControlImpl(JObject& jgpioctl);

virtual int Initialize(void);
virtual void Release(void);
virtual int PinMode(uint32_t portpin);
virtual int WritePin(uint32_t portpin, uint8_t data);
virtual int ReadPin(uint32_t portpin, uint8_t* pdata);
virtual int SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb);
virtual int SetPin(int32_t pin, int32_t dir, int32_t mode);
};

//-----------------------------------------------------------------------------

GpioControl* GpioControl::Create(JObject& jgpioctl)
{
return new GpioControlInst(jgpioctl);
return new GpioControlImpl(jgpioctl);
}


GpioControlInst::GpioControlInst(JObject& jgpioctl)
GpioControlImpl::GpioControlImpl(JObject& jgpioctl)
: GpioControl(jgpioctl) {
}


int GpioControlInst::Initialize(void) {
int GpioControlImpl::Initialize(void) {
if (_fd > 0 )
return IOTJS_GPIO_INUSE;
return GPIO_ERR_INITALIZE;

const char* devfilepath = "/dev/gpio";
_fd = open(devfilepath, O_RDWR);
DDDLOG("gpio> %s : fd(%d)", devfilepath, _fd);
return _fd;
}

void GpioControlInst::Release(void) {

void GpioControlImpl::Release(void) {
if (_fd > 0) {
close(_fd);
}
_fd = 0;
}


int GpioControlInst::PinMode(uint32_t portpin) {
if (_fd > 0) {
struct gpioioctl_config_s cdata;
cdata.port = portpin;
return ioctl(_fd, GPIOIOCTL_CONFIG, (long unsigned int)&cdata);
}
return IOTJS_GPIO_NOTINITED;
}


int GpioControlInst::WritePin(uint32_t portpin, uint8_t data) {
if (_fd > 0) {
struct gpioioctl_write_s wdata;
wdata.port = portpin;
wdata.data = data;
return ioctl(_fd, GPIOIOCTL_WRITE, (long unsigned int)&wdata);
}
return IOTJS_GPIO_NOTINITED;
int GpioControlImpl::SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb) {
return 0;
}


int GpioControlInst::ReadPin(uint32_t portpin, uint8_t* pdata) {
if (_fd > 0) {
struct gpioioctl_write_s wdata;
int ret;
wdata.port = portpin;
wdata.port = *pdata = 0;
ret = ioctl(_fd, GPIOIOCTL_READ, (long unsigned int)&wdata);
if (ret >= 0) {
*pdata = wdata.data;
}
return ret;
}
return IOTJS_GPIO_NOTINITED;
int GpioControlImpl::SetPin(int32_t pin, int32_t dir, int32_t mode) {
return 0;
}


Expand Down
124 changes: 116 additions & 8 deletions src/device/x86-linux/gpioctl_x86_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,133 @@
* limitations under the License.
*/

#if defined(__LINUX__)

#include "iotjs_def.h"
#include "iotjs_objectwrap.h"
#include "iotjs_module_gpioctl.h"

//
// this is a dummy emulator code for x86.
// please add new 'device' file for your purpose.
// use timer with 1msec delay to implement async callback.
// real gpio implementation would be better to use threading
//

namespace iotjs {

/*
* Use default dummy for linux-x86
*/

// type for delayed callback
typedef enum {
DELAYEDCALL_SETPIN = 1,
} DelayedCallType;


// base class for delayed callback
struct DelayedCallBase {
DelayedCallType type;
uv_timer_t timer;
};


// setspin delayed callback class
struct DelayedCallSetpin : public DelayedCallBase {
GpioCbDataSetpin* data;
GpioSetpinCb aftersetpin;
int result;
};


static void timerHandleTimeout(uv_timer_t* handle) {
uv_timer_stop(handle);

DelayedCallBase* base;
base = reinterpret_cast<DelayedCallBase*>(handle->data);
switch (base->type) {
case DELAYEDCALL_SETPIN : {
DelayedCallSetpin* dcsp =
reinterpret_cast<DelayedCallSetpin*>(base);
GpioCbDataSetpin* setpin =
reinterpret_cast<GpioCbDataSetpin*>(dcsp->data);

DDDLOG("x86 linux gpio dummy setpin w/cb pin(%d), dir(%d), mode(%d)",
setpin->pin, setpin->dir, setpin->mode);

dcsp->result = (setpin->pin >= 0) ? 0 : GPIO_ERR_INVALIDPARAM;
dcsp->aftersetpin(dcsp->data, dcsp->result);
delete dcsp;
break;
}
default:
IOTJS_ASSERT(false);
break;
}
}


//-----------------------------------------------------------------------------

class GpioControlImpl : public GpioControl {
public:
explicit GpioControlImpl(JObject& jgpioctl);

virtual int Initialize(void);
virtual void Release(void);
virtual int SetPin(GpioCbDataSetpin* setpin, GpioSetpinCb cb);
virtual int SetPin(int32_t pin, int32_t dir, int32_t mode);
};


GpioControl* GpioControl::Create(JObject& jgpioctl)
{
return new GpioControl(jgpioctl);
return new GpioControlImpl(jgpioctl);
}


} // namespace iotjs
GpioControlImpl::GpioControlImpl(JObject& jgpioctl)
: GpioControl(jgpioctl) {
}


int GpioControlImpl::Initialize(void) {
if (_fd > 0 )
return GPIO_ERR_INITALIZE;

DDDLOG("x86 linux gpio dummy initalize");
_fd = 1;
return _fd;
}


void GpioControlImpl::Release(void) {
DDDLOG("x86 linux gpio dummy release");
_fd = 0;
}


// callback is provided for SetPin
int GpioControlImpl::SetPin(GpioCbDataSetpin* setpin_data, GpioSetpinCb cb) {
Environment* env = Environment::GetEnv();
DelayedCallSetpin* delay = new DelayedCallSetpin;
delay->type = DELAYEDCALL_SETPIN;
delay->data = setpin_data;
delay->aftersetpin = cb;
delay->result = 0;

#endif // __NUTTX__
uv_timer_init(env->loop(), &delay->timer);
delay->timer.data = delay;
uv_timer_start(&delay->timer, timerHandleTimeout, 1, 0);

return 0;
}


// callback is omiited for SetPin
int GpioControlImpl::SetPin(int32_t pin, int32_t dir, int32_t mode) {
DDDLOG("x86 linux gpio dummy setpin pin(%d), dir(%d), mode(%d)",
pin, dir, mode);

// return result of SetPin
return (pin >= 0) ? 0 : GPIO_ERR_INVALIDPARAM;
}


} // namespace iotjs
54 changes: 54 additions & 0 deletions src/iotjs_gpiocbwrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Copyright 2015 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "iotjs_gpiocbwrap.h"


namespace iotjs {


GpioCbWrap::GpioCbWrap(JObject& jcallback, GpioCbData* cbdata)
: __cbdata(cbdata)
, _jcallback(NULL) {
if (!jcallback.IsNull()) {
_jcallback = new JObject(jcallback);
}
}


GpioCbWrap::~GpioCbWrap() {
if (_jcallback != NULL) {
delete _jcallback;
}
}


JObject& GpioCbWrap::jcallback() {
IOTJS_ASSERT(_jcallback != NULL);
return *_jcallback;
}


GpioCbData* GpioCbWrap::cbdata() {
return __cbdata;
}


void GpioCbWrap::Dispatched() {
cbdata()->data = this;
}


} // namespace iotjs
Loading