Update Iot.js
[platform/upstream/iotjs.git] / src / module / iotjs_module_spi.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
17 #ifndef IOTJS_MODULE_SPI_H
18 #define IOTJS_MODULE_SPI_H
19
20 #include "iotjs_def.h"
21 #include "iotjs_module_buffer.h"
22 #include "iotjs_objectwrap.h"
23 #include "iotjs_reqwrap.h"
24
25
26 typedef enum {
27   kSpiOpOpen,
28   kSpiOpTransfer,
29   kSpiOpClose,
30 } SpiOp;
31
32 typedef enum {
33   kSpiMode_0,
34   kSpiMode_1,
35   kSpiMode_2,
36   kSpiMode_3,
37 } SpiMode;
38
39 typedef enum {
40   kSpiCsNone,
41   kSpiCsHigh,
42 } SpiChipSelect;
43
44 typedef enum { kSpiOrderMsb, kSpiOrderLsb } SpiOrder;
45
46
47 typedef struct {
48   iotjs_jobjectwrap_t jobjectwrap;
49   iotjs_string_t device;
50   int32_t device_fd;
51
52   SpiMode mode;
53   SpiChipSelect chip_select;
54   SpiOrder bit_order;
55   uint8_t bits_per_word;
56   uint16_t delay;
57   uint32_t max_speed;
58   bool loopback;
59
60   // SPI buffer
61   char* tx_buf_data;
62   char* rx_buf_data;
63   uint8_t buf_len;
64
65 } IOTJS_VALIDATED_STRUCT(iotjs_spi_t);
66
67
68 typedef struct {
69   bool result;
70   SpiOp op;
71 } iotjs_spi_reqdata_t;
72
73
74 typedef struct {
75   iotjs_reqwrap_t reqwrap;
76   uv_work_t req;
77   iotjs_spi_reqdata_t req_data;
78   iotjs_spi_t* spi_instance;
79 } IOTJS_VALIDATED_STRUCT(iotjs_spi_reqwrap_t);
80
81
82 #define THIS iotjs_spi_reqwrap_t* spi_reqwrap
83
84 iotjs_spi_reqwrap_t* iotjs_spi_reqwrap_from_request(uv_work_t* req);
85 iotjs_spi_reqdata_t* iotjs_spi_reqwrap_data(THIS);
86
87 iotjs_spi_t* iotjs_spi_instance_from_reqwrap(THIS);
88
89 #undef THIS
90
91
92 #define SPI_WORKER_INIT                                                     \
93   iotjs_spi_reqwrap_t* req_wrap = iotjs_spi_reqwrap_from_request(work_req); \
94   iotjs_spi_reqdata_t* req_data = iotjs_spi_reqwrap_data(req_wrap);         \
95   iotjs_spi_t* spi = iotjs_spi_instance_from_reqwrap(req_wrap);
96
97
98 bool iotjs_spi_transfer(iotjs_spi_t* spi);
99 bool iotjs_spi_close(iotjs_spi_t* spi);
100
101 void iotjs_spi_open_worker(uv_work_t* work_req);
102
103
104 #endif /* IOTJS_MODULE_SPI_H */