1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef IOTJS_MODULE_GPIO_ARM_NUTTX_STM32_INL_H
17 #define IOTJS_MODULE_GPIO_ARM_NUTTX_STM32_INL_H
19 #include "module/iotjs_module_gpio.h"
20 #include "stm32_gpio.h"
22 #define GPIO_FREQUENCY_DEFAULT GPIO_SPEED_25MHz
23 #define GPIO_PINCNT_IN_NUTTXPORT 16
25 #define GPIO_CONFIG_OK 0
28 uint32_t gpioDirection[] = {
29 GPIO_INPUT, GPIO_OUTPUT | GPIO_OUTPUT_SET | GPIO_FREQUENCY_DEFAULT,
33 uint32_t gpioMode[] = {
35 GPIO_PULLUP, GPIO_PULLDOWN, GPIO_FLOAT, GPIO_PUSHPULL, GPIO_OPENDRAIN,
39 bool iotjs_gpio_write(int32_t pin, bool value) {
40 DDDLOG("%s - pin: %d, value: %d", __func__, pin, value);
41 stm32_gpiowrite(pin, value);
47 int iotjs_gpio_read(int32_t pin) {
48 DDDLOG("%s - pin: %d", __func__, pin);
49 return stm32_gpioread(pin);
53 bool iotjs_gpio_close(int32_t pin) {
54 DDDLOG("%s - pin: %d", __func__, pin);
55 iotjs_gpio_write(pin, 0);
61 void iotjs_gpio_open_worker(uv_work_t* work_req) {
63 DDDLOG("%s - pin: %d, dir: %d, mode: %d", __func__, _this->pin,
64 _this->direction, _this->mode);
68 // Set pin direction and mode
69 cfgset = gpioDirection[_this->direction] | gpioMode[_this->mode] | _this->pin;
71 if (stm32_configgpio(cfgset) != GPIO_CONFIG_OK) {
72 req_data->result = -1;
80 void iotjs_gpio_write_worker(uv_work_t* work_req) {
82 DDDLOG("%s - pin: %d, value: %d", __func__, _this->pin, req_data->value);
84 iotjs_gpio_write(_this->pin, req_data->value);
90 void iotjs_gpio_read_worker(uv_work_t* work_req) {
92 DDDLOG("%s - pin: %d", __func__, _this->pin);
95 req_data->value = iotjs_gpio_read(_this->pin);
99 void iotjs_gpio_close_worker(uv_work_t* work_req) {
101 DDDLOG("%s - pin: %d", __func__, _this->pin);
103 iotjs_gpio_close(_this->pin);
105 req_data->result = 0;
108 #endif /* IOTJS_MODULE_GPIO_ARM_NUTTX_STM32_INL_H */