Skip to content
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

Add gpio control and related codes. #113

Merged
merged 1 commit into from
Jul 2, 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
1 change: 1 addition & 0 deletions cmake/config/arm-nuttx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CMAKE_FORCE_CXX_COMPILER(${EXTERNAL_CMAKE_CXX_COMPILER} GNU)

set(NO_PTHREAD YES)
set(BUILD_TO_LIB YES)
set(DEVICE_DEPENDS "nuttx-stm32f4disco")

set(FLAGS_COMMON -mcpu=cortex-m4
-mthumb
Expand Down
2 changes: 2 additions & 0 deletions cmake/config/i686-linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ include(CMakeForceCompiler)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR x86)

set(DEVICE_DEPENDS "linux-x86")

set(FLAGS_COMMON -D__LINUX__
-D__i686__
-fno-builtin)
Expand Down
2 changes: 2 additions & 0 deletions cmake/config/x86_64-linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ include(CMakeForceCompiler)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR x86_64)

set(DEVICE_DEPENDS "linux-x86")

set(FLAGS_COMMON -D__LINUX__
-D__x86_64__
-fno-builtin)
Expand Down
3 changes: 2 additions & 1 deletion cmake/iotjs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

cmake_minimum_required(VERSION 2.8)

file(GLOB LIB_IOTJS_SRC ${SRC_ROOT}/*.cpp)
file(GLOB LIB_IOTJS_SRC ${SRC_ROOT}/*.cpp
${SRC_ROOT}/device/${DEVICE_DEPENDS}/*.cpp)

set(LIB_IOTJS_CFLAGS ${CFLAGS})
set(LIB_IOTJS_INCDIR ${TARGET_INC}
Expand Down
37 changes: 37 additions & 0 deletions src/device/linux-x86/gpioctl_linux_x86.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* 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.
*/

#if defined(__LINUX__)

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


namespace iotjs {

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

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


} // namespace iotjs

#endif // __NUTTX__
111 changes: 111 additions & 0 deletions src/device/nuttx-stm32f4disco/gpioctl_nuttx_stm32f4disco.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* 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.
*/

#if defined(__NUTTX__)

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

#include <unistd.h>
#include <fcntl.h>
#include <nuttx/gpio.h>
#include <sys/ioctl.h>


namespace iotjs {


class GpioControlInst : public GpioControl {
public:
explicit GpioControlInst(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);
};

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

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


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


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

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

void GpioControlInst::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 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;
}


} // namespace iotjs

#endif // __NUTTX__
2 changes: 2 additions & 0 deletions src/iotjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ int Start(char* src) {

JObject* process = InitModules();

SetProcessIotjs(process);

// FIXME: this should be moved to seperate function
{
JObject argv;
Expand Down
2 changes: 2 additions & 0 deletions src/iotjs_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#ifndef IOTJS_MAX_READ_BUFFER_SIZE
#ifdef __NUTTX__
#define IOTJS_MAX_READ_BUFFER_SIZE 1024
#define IOTJS_MAX_PATH_SIZE 120
#else
#define IOTJS_MAX_READ_BUFFER_SIZE 65535
#define IOTJS_MAX_PATH_SIZE PATH_MAX
#endif
#endif

Expand Down
1 change: 1 addition & 0 deletions src/iotjs_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "iotjs_module_stream.h"
#include "iotjs_module_tcp.h"
#include "iotjs_module_timer.h"
#include "iotjs_module_gpioctl.h"


namespace iotjs {
Expand Down
3 changes: 2 additions & 1 deletion src/iotjs_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ typedef JObject* (*register_func)();
F(PROCESS, Process, process) \
F(STREAM, Stream, stream) \
F(TCP, Tcp, tcp) \
F(TIMER, Timer, timer)
F(TIMER, Timer, timer) \
F(GPIOCTL, GpioCtl, gpioctl)


#define ENUMDEF_MODULE_LIST(upper, Camel, lower) \
Expand Down
Loading