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 #if defined(__NUTTX__)
20 #include <nuttx/analog/adc.h>
23 #include "iotjs_def.h"
24 #include "iotjs_systemio-arm-nuttx.h"
25 #include "module/iotjs_module_adc.h"
26 #include "module/iotjs_module_stm32f4dis.h"
29 #define ADC_DEVICE_PATH_FORMAT "/dev/adc%d"
30 #define ADC_DEVICE_PATH_BUFFER_SIZE 12
33 static void iotjs_adc_get_path(char* buffer, int32_t number) {
34 // Create ADC device path
35 snprintf(buffer, ADC_DEVICE_PATH_BUFFER_SIZE - 1, ADC_DEVICE_PATH_FORMAT,
40 static bool iotjs_adc_read_data(int32_t pin, struct adc_msg_s* msg) {
41 int32_t adc_number = ADC_GET_NUMBER(pin);
42 char path[ADC_DEVICE_PATH_BUFFER_SIZE] = { 0 };
43 iotjs_adc_get_path(path, adc_number);
45 const iotjs_environment_t* env = iotjs_environment_get();
46 uv_loop_t* loop = iotjs_environment_loop(env);
47 int result, close_result;
51 result = uv_fs_open(loop, &open_req, path, O_RDONLY, 0666, NULL);
52 uv_fs_req_cleanup(&open_req);
59 uv_buf_t uvbuf = uv_buf_init((char*)msg, sizeof(*msg));
60 result = uv_fs_read(loop, &read_req, open_req.result, &uvbuf, 1, 0, NULL);
61 uv_fs_req_cleanup(&read_req);
65 close_result = uv_fs_close(loop, &close_req, open_req.result, NULL);
66 uv_fs_req_cleanup(&close_req);
67 if (result < 0 || close_result < 0) {
71 DDLOG("ADC Read - path: %s, value: %d", path, msg->am_data);
77 int32_t iotjs_adc_read(iotjs_adc_t* adc) {
78 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_adc_t, adc);
82 if (!iotjs_adc_read_data(_this->pin, &msg)) {
90 bool iotjs_adc_close(iotjs_adc_t* adc) {
91 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_adc_t, adc);
93 int32_t pin = _this->pin;
94 int32_t adc_number = ADC_GET_NUMBER(pin);
96 char path[ADC_DEVICE_PATH_BUFFER_SIZE] = { 0 };
97 iotjs_adc_get_path(path, adc_number);
100 if (unregister_driver(path) < 0) {
104 iotjs_gpio_unconfig_nuttx(pin);
110 void iotjs_adc_open_worker(uv_work_t* work_req) {
112 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_adc_t, adc);
114 int32_t pin = _this->pin;
115 int32_t adc_number = ADC_GET_NUMBER(pin);
116 int32_t timer = SYSIO_GET_TIMER(pin);
117 struct adc_dev_s* adc_dev = iotjs_adc_config_nuttx(adc_number, timer, pin);
119 char path[ADC_DEVICE_PATH_BUFFER_SIZE] = { 0 };
120 iotjs_adc_get_path(path, adc_number);
122 if (adc_register(path, adc_dev) != 0) {
123 req_data->result = false;
127 DDLOG("%s - path: %s, number: %d, timer: %d", __func__, path, adc_number,
130 req_data->result = true;