d6dcd30356ea9f510bdd7fe587d9c4c7f2d2aa1f
[platform/core/api/peripheral-io.git] / include / peripheral_io.h
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __PERIPHERAL_IO_H__
18 #define __PERIPHERAL_IO_H__
19
20 #include <stdint.h>
21 #include <tizen.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /**
28  * @file peripheral_io.h
29  * @brief This file contains the peripheral-io API
30  */
31
32 /**
33  * @brief Enumeration for peripheral-io error.
34  * @since_tizen
35  */
36 typedef enum {
37         PERIPHERAL_ERROR_NONE                = TIZEN_ERROR_NONE,                /**< Successful */
38         PERIPHERAL_ERROR_IO_ERROR            = TIZEN_ERROR_IO_ERROR,            /**< I/O error */
39         PERIPHERAL_ERROR_OUT_OF_MEMORY       = TIZEN_ERROR_OUT_OF_MEMORY,       /**< Out of memory */
40         PERIPHERAL_ERROR_PERMISSON_DENIED    = TIZEN_ERROR_PERMISSION_DENIED,   /**< Permission denied */
41         PERIPHERAL_ERROR_RESOURCE_BUSY       = TIZEN_ERROR_RESOURCE_BUSY,       /**< Device or resource busy */
42         PERIPHERAL_ERROR_INVALID_PARAMETER   = TIZEN_ERROR_INVALID_PARAMETER,   /**< Invalid parameter */
43         PERIPHERAL_ERROR_NO_DATA             = TIZEN_ERROR_NO_DATA,             /**< No data available */
44         PERIPHERAL_ERROR_INVALID_OPERATION   = TIZEN_ERROR_INVALID_OPERATION,   /**< Function not implemented */
45         PERIPHERAL_ERROR_TIMED_OUT           = TIZEN_ERROR_TIMED_OUT,           /**< Time out */
46         PERIPHERAL_ERROR_NOT_SUPPORTED       = TIZEN_ERROR_NOT_SUPPORTED,       /**< Not supported */
47         PERIPHERAL_ERROR_UNKNOWN             = TIZEN_ERROR_UNKNOWN,             /**< Unknown error */
48         PERIPHERAL_ERROR_NO_DEVICE           = -ENODEV,                         /**< No such device */
49 } peripheral_error_e;
50
51 /**
52  * @addtogroup CAPI_SYSTEM_PERPHERAL_GPIO_MODULE
53  * @{
54  */
55
56 /**
57  * @brief Enumeration of gpio direction
58  */
59 typedef enum {
60         PERIPHERAL_GPIO_DIRECTION_IN = 0,    /**< Input Mode */
61         PERIPHERAL_GPIO_DIRECTION_OUT,       /**< Output mode and this implies "low" output value */
62         PERIPHERAL_GPIO_DIRECTION_OUT_HIGH,  /**< Output mode and value also be written as "high" */
63 } peripheral_gpio_direction_e;
64
65 /**
66  * @brief Enumeration of edge type for gpio interrupt
67  */
68 typedef enum {
69         PERIPHERAL_GPIO_EDGE_NONE = 0,  /**< No interrupt on Gpio */
70         PERIPHERAL_GPIO_EDGE_RISING,    /**< Interrupt on rising only */
71         PERIPHERAL_GPIO_EDGE_FALLING,   /**< Interrupt on falling only */
72         PERIPHERAL_GPIO_EDGE_BOTH,      /**< Interrupt on rising & falling */
73 } peripheral_gpio_edge_e;
74
75 /**
76  * @brief The handle to the gpio pin
77  * @since_tizen 4.0
78  */
79 typedef struct _peripheral_gpio_s* peripheral_gpio_h;
80
81 /**
82  * @brief Called when the gpio interrupt is triggered.
83  * @since_tizen 4.0
84  *
85  * @param[in] user_data The user data passed from the callback registration function
86  *
87  * @see peripheral_gpio_register_cb()
88  * @see peripheral_gpio_unregister_cb()
89  */
90 typedef void(*gpio_isr_cb)(void *user_data);
91
92 /**
93  * @brief Initilizes(export) gpio pin and creates gpio handle.
94  * @since_tizen 4.0
95  *
96  * @param[in] gpio_pin The gpio pin number what you want to use
97  * @param[out] gpio The gpio handle is created on success
98  *
99  * @return 0 on success, otherwise a negative error value
100  * @retval #PERIPHERAL_ERROR_NONE Successful
101  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
102  * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed
103  * @retval #PERIPHERAL_ERROR_PERMISSON_DENIED Permission denied
104  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
105  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
106  *
107  * @see peripheral_gpio_close()
108  */
109 int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio);
110
111 /**
112  * @brief Release the gpio handle and finalize(unexport) the gpio pin.
113  * @since_tizen 4.0
114  *
115  * @param[in] gpio The handle to the gpio pin to release
116  *
117  * @return 0 on success, otherwise a negative error value
118  * @retval #PERIPHERAL_ERROR_NONE Successful
119  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
120  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
121  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
122  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
123  *
124  * @see peripheral_gpio_open()
125  */
126 int peripheral_gpio_close(peripheral_gpio_h gpio);
127
128 /**
129  * @brief Sets direction of the gpio pin.
130  * @since_tizen 4.0
131  *
132  * @param[in] gpio The handle to the gpio pin to set
133  * @param[in] direction The direction type of the gpio pin
134  *
135  * @return 0 on success, otherwise a negative error value
136  * @retval #PERIPHERAL_ERROR_NONE Successful
137  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
138  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
139  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
140  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
141  */
142 int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction);
143
144 /**
145  * @brief Sets the edge mode of the gpio pin.
146  * @since_tizen 4.0
147  *
148  * @param[in] gpio The handle to the gpio pin to set
149  * @param[in] edge The edge type of the gpio pin
150  *
151  * @return 0 on success, otherwise a negative error value
152  * @retval #PERIPHERAL_ERROR_NONE Successful
153  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
154  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
155  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
156  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
157  */
158 int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge);
159
160 /**
161  * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
162  * @since_tizen 4.0
163  *
164  * @param[in] gpio The handle to the gpio pin to set
165  * @param[in] edge The edge type of the gpio pin
166  * @param[in] callback The callback function to register
167  * @param[in] user_data The user data to be passed to the callback function
168  *
169  * @return 0 on success, otherwise a negative error value
170  * @retval #PERIPHERAL_ERROR_NONE Successfu
171  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parametera
172  *
173  * @see peripheral_gpio_set_edge_mode()
174  * @see peripheral_gpio_unregister_cb()
175  */
176 int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data);
177
178 /**
179  * @brief Unregisters the callback function for the gpio handler.
180  * @since_tizen 4.0
181  *
182  * @param[in] gpio The handle to the gpio pin
183  *
184  * @return 0 on success, otherwise a negative error value
185  * @retval #PERIPHERAL_ERROR_NONE Successful
186  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
187  *
188  * @see peripheral_gpio_register_cb()
189  */
190 int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio);
191
192 /**
193  * @brief Reads the gpio value.
194  * @since_tizen 4.0
195  *
196  * @param[in] gpio The handle to the gpio pin
197  * @param[out] value The result of the gpio
198  *
199  * @return 0 on success, otherwise a negative error value
200  * @retval #PERIPHERAL_ERROR_NONE Successful
201  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
202  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
203  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
204  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
205  */
206 int peripheral_gpio_read(peripheral_gpio_h gpio, int *value);
207
208 /**
209  * @brief Writes the gpio value.
210  * @since_tizen 4.0
211  *
212  * @param[in] gpio The handle to the gpio pin
213  * @param[in] value The value to be written to the gpio
214  *
215  * @return 0 on success, otherwise a negative error value
216  * @retval #PERIPHERAL_ERROR_NONE Successful
217  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
218  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
219  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
220  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
221  */
222 int peripheral_gpio_write(peripheral_gpio_h gpio, int value);
223
224 /**
225  * @brief Gets direction of the gpio.
226  * @since_tizen 4.0
227  *
228  * @param[in] gpio The handle to the gpio pin
229  * @param[out] value The value to be written to the gpio
230  *
231  * @return 0 on success, otherwise a negative error value
232  * @retval #PERIPHERAL_ERROR_NONE Successful
233  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
234  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
235  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
236  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
237  */
238 int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction);
239
240 /**
241  * @brief Gets pin number of the gpio.
242  * @since_tizen 4.0
243  *
244  * @param[in] gpio The handle to the gpio pin
245  * @param[out] gpio_pin The number of the gpio
246  *
247  * @return 0 on success, otherwise a negative error value
248  * @retval #PERIPHERAL_ERROR_NONE Successful
249  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
250  */
251 int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin);
252
253 /**
254  * @brief Gets edge mode of the gpio.
255  * @since_tizen 4.0
256  *
257  * @param[in] gpio The handle to the gpio pin
258  * @param[out] gpio_pin The number of the gpio
259  *
260  * @return 0 on success, otherwise a negative error value
261  * @retval #PERIPHERAL_ERROR_NONE Successful
262  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
263  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
264  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
265  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
266  */
267 int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge);
268
269 /**
270 * @}
271 */
272
273 /**
274  * @addtogroup CAPI_SYSTEM_PERPHERAL_I2C_MODULE
275  * @{
276  */
277
278 /**
279  * @brief Struct for peripheral_gpio_s
280  */
281 struct _peripheral_i2c_s {
282         int fd;
283 };
284 typedef struct _peripheral_i2c_s *peripheral_i2c_context_h;
285
286 typedef enum {
287         PERIPHERAL_I2C_STD = 0,
288         PERIPHERAL_I2C_FAST = 1,
289         PERIPHERAL_I2C_HIGH = 2
290 } peripheral_i2c_mode_e;
291
292 peripheral_i2c_context_h peripheral_i2c_init(int bus);
293
294 int peripheral_i2c_stop(peripheral_i2c_context_h hnd);
295
296 int peripheral_i2c_set_frequency(peripheral_i2c_context_h hnd, peripheral_i2c_mode_e mode);
297
298 int peripheral_i2c_set_address(peripheral_i2c_context_h hnd, int address);
299
300 int peripheral_i2c_read(peripheral_i2c_context_h hnd, uint8_t *data, int length);
301
302 int peripheral_i2c_write(peripheral_i2c_context_h hnd, uint8_t *data, int length);
303
304
305 /**
306 * @}
307 */
308
309 /**
310  * @addtogroup CAPI_SYSTEM_PERPHERAL_PWM_MODULE
311  * @{
312  */
313
314 struct _peripheral_pwm_s {
315         int device;
316         int channel;
317         int period;
318         int duty_cycle;
319         int enabled;
320 };
321 typedef struct _peripheral_pwm_s *peripheral_pwm_context_h;
322
323 typedef enum {
324         PERIPHERAL_PWM_DISABLE = 0,
325         PERIPHERAL_PWM_ENABLE,
326 } peripheral_pwm_state_e;
327
328 peripheral_pwm_context_h peripheral_pwm_open(int device, int channel);
329
330 int peripheral_pwm_close(peripheral_pwm_context_h pwm);
331
332 int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle);
333
334 int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period);
335
336 int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable);
337
338 int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm);
339
340 int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle);
341
342 int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period);
343
344
345 /**
346 * @}
347 */
348
349 /**
350  * @addtogroup CAPI_SYSTEM_PERPHERAL_ADC_MODULE
351  * @{
352  */
353
354 /**
355  * @brief Struct for peripheral_gpio_s
356  */
357
358 #define DEVICE_NAME_SIZE        20
359
360 struct _peripheral_adc_s {
361         char device_name[DEVICE_NAME_SIZE];
362         int channel;
363 };
364
365 /**
366  * @brief Pointer definition to the internal struct peripheral_adc_s
367  */
368 typedef struct _peripheral_adc_s* peripheral_adc_context_h;
369
370 peripheral_adc_context_h peripheral_adc_open(int channel);
371
372 int peripheral_adc_read(peripheral_adc_context_h dev, int *data);
373
374 int peripheral_adc_close(peripheral_adc_context_h dev);
375
376 /**
377 * @}
378 */
379
380 /**
381  * @addtogroup CAPI_SYSTEM_PERPHERAL_UART_MODULE
382  * @{
383  */
384 struct _peripheral_uart_s {
385         int fd;
386 };
387
388
389 typedef struct _peripheral_uart_s* peripheral_uart_context_h;
390
391 typedef enum {
392         PERIPHERAL_UART_PARITY_NONE = 0,
393         PERIPHERAL_UART_PARITY_EVEN,
394         PERIPHERAL_UART_PARITY_ODD,
395 } peripheral_uart_parity_e;
396
397 peripheral_uart_context_h peripheral_uart_init(const char *path);
398
399 int peripheral_uart_stop(peripheral_uart_context_h hnd);
400
401 int peripheral_uart_flush(peripheral_uart_context_h hnd);
402
403 int peripheral_uart_set_baudrate(peripheral_uart_context_h hnd, unsigned int baud);
404
405 int peripheral_uart_set_mode(peripheral_uart_context_h hnd, int bytesize, peripheral_uart_parity_e parity, int stopbits);
406
407 int peripheral_uart_set_flowcontrol(peripheral_uart_context_h hnd, int xonxoff, int rtscts);
408
409 int peripheral_uart_read(peripheral_uart_context_h hnd, char *buf, unsigned int length);
410
411 int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsigned int length);
412
413 /**
414 * @}
415 */
416
417 /**
418  * @addtogroup CAPI_SYSTEM_PERPHERAL_SPI_MODULE
419  * @{
420  */
421
422 typedef enum {
423         PERIPHERAL_SPI_MODE0 = 0,
424         PERIPHERAL_SPI_MODE1,
425         PERIPHERAL_SPI_MODE2,
426         PERIPHERAL_SPI_MODE3
427 } peripheral_spi_mode_e;
428
429 struct peripheral_spi_config_s {
430         int fd;
431         char bits_per_word;
432         int lsb;
433         unsigned int chip_select;
434         unsigned int frequency;
435         peripheral_spi_mode_e mode;
436 };
437
438 typedef struct peripheral_spi_config_s * peripheral_spi_context_h;
439
440 peripheral_spi_context_h peripheral_spi_open(unsigned int bus, peripheral_spi_context_h config);
441
442 int     peripheral_spi_write(peripheral_spi_context_h hnd, char *txbuf, int length);
443
444 int     peripheral_spi_recv(peripheral_spi_context_h hnd, char *rxbuf, int length);
445
446 int peripheral_spi_transfer_buf(peripheral_spi_context_h hnd, char *txbuf, char *rxbuf, int length);
447
448 int     peripheral_spi_close(peripheral_spi_context_h hnd);
449
450 /**
451 * @}
452 */
453
454 #ifdef __cplusplus
455 }
456 #endif
457
458 #endif /* __PERIPHERAL_IO_H__ */