Upload packaging folder
[platform/upstream/iotjs.git] / tools / src / platform / arm-nuttx / iotjs_module_gpio-arm-nuttx-stm32.inl.h
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef IOTJS_MODULE_GPIO_ARM_NUTTX_STM32_INL_H
17 #define IOTJS_MODULE_GPIO_ARM_NUTTX_STM32_INL_H
18
19 #include "module/iotjs_module_gpio.h"
20 #include "stm32_gpio.h"
21
22 #define GPIO_FREQUENCY_DEFAULT GPIO_SPEED_25MHz
23 #define GPIO_PINCNT_IN_NUTTXPORT 16
24
25 #define GPIO_CONFIG_OK 0
26
27
28 uint32_t gpioDirection[] = {
29   GPIO_INPUT, GPIO_OUTPUT | GPIO_OUTPUT_SET | GPIO_FREQUENCY_DEFAULT,
30 };
31
32
33 uint32_t gpioMode[] = {
34   0, // none
35   GPIO_PULLUP, GPIO_PULLDOWN, GPIO_FLOAT, GPIO_PUSHPULL, GPIO_OPENDRAIN,
36 };
37
38
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);
42
43   return true;
44 }
45
46
47 int iotjs_gpio_read(int32_t pin) {
48   DDDLOG("%s - pin: %d", __func__, pin);
49   return stm32_gpioread(pin);
50 }
51
52
53 bool iotjs_gpio_close(int32_t pin) {
54   DDDLOG("%s - pin: %d", __func__, pin);
55   iotjs_gpio_write(pin, 0);
56
57   return true;
58 }
59
60
61 void iotjs_gpio_open_worker(uv_work_t* work_req) {
62   GPIO_WORKER_INIT();
63   DDDLOG("%s - pin: %d, dir: %d, mode: %d", __func__, _this->pin,
64          _this->direction, _this->mode);
65
66   uint32_t cfgset = 0;
67
68   // Set pin direction and mode
69   cfgset = gpioDirection[_this->direction] | gpioMode[_this->mode] | _this->pin;
70
71   if (stm32_configgpio(cfgset) != GPIO_CONFIG_OK) {
72     req_data->result = -1;
73     return;
74   }
75
76   req_data->result = 0;
77 }
78
79
80 void iotjs_gpio_write_worker(uv_work_t* work_req) {
81   GPIO_WORKER_INIT();
82   DDDLOG("%s - pin: %d, value: %d", __func__, _this->pin, req_data->value);
83
84   iotjs_gpio_write(_this->pin, req_data->value);
85
86   req_data->result = 0;
87 }
88
89
90 void iotjs_gpio_read_worker(uv_work_t* work_req) {
91   GPIO_WORKER_INIT();
92   DDDLOG("%s - pin: %d", __func__, _this->pin);
93
94   req_data->result = 0;
95   req_data->value = iotjs_gpio_read(_this->pin);
96 }
97
98
99 void iotjs_gpio_close_worker(uv_work_t* work_req) {
100   GPIO_WORKER_INIT();
101   DDDLOG("%s - pin: %d", __func__, _this->pin);
102
103   iotjs_gpio_close(_this->pin);
104
105   req_data->result = 0;
106 }
107
108 #endif /* IOTJS_MODULE_GPIO_ARM_NUTTX_STM32_INL_H */