Rename peripheral_i2c_context_h to peripheral_i2c_h
[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 with low value */
62         PERIPHERAL_GPIO_DIRECTION_OUT_LOW = PERIPHERAL_GPIO_DIRECTION_OUT, /**< Same as above */
63         PERIPHERAL_GPIO_DIRECTION_OUT_HIGH,  /**< Output mode with high value */
64 } peripheral_gpio_direction_e;
65
66 /**
67  * @brief Enumeration of edge type for gpio interrupt
68  */
69 typedef enum {
70         PERIPHERAL_GPIO_EDGE_NONE = 0,  /**< No interrupt on Gpio */
71         PERIPHERAL_GPIO_EDGE_RISING,    /**< Interrupt on rising only */
72         PERIPHERAL_GPIO_EDGE_FALLING,   /**< Interrupt on falling only */
73         PERIPHERAL_GPIO_EDGE_BOTH,      /**< Interrupt on rising & falling */
74 } peripheral_gpio_edge_e;
75
76 /**
77  * @brief The handle to the gpio pin
78  * @since_tizen 4.0
79  */
80 typedef struct _peripheral_gpio_s* peripheral_gpio_h;
81
82 /**
83  * @brief Initializes(export) gpio pin and creates gpio handle.
84  * @since_tizen 4.0
85  *
86  * @param[in] gpio_pin The gpio pin number
87  * @param[out] gpio The gpio handle is created on success
88  *
89  * @return 0 on success, otherwise a negative error value
90  * @retval #PERIPHERAL_ERROR_NONE Successful
91  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
92  * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed
93  * @retval #PERIPHERAL_ERROR_PERMISSON_DENIED Permission denied
94  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
95  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
96  *
97  * @see peripheral_gpio_close()
98  */
99 int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio);
100
101 /**
102  * @brief Releases the gpio handle and finalize(unexport) the gpio pin.
103  * @since_tizen 4.0
104  *
105  * @param[in] gpio The handle to the gpio pin to release
106  *
107  * @return 0 on success, otherwise a negative error value
108  * @retval #PERIPHERAL_ERROR_NONE Successful
109  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
110  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
111  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
112  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
113  *
114  * @see peripheral_gpio_open()
115  */
116 int peripheral_gpio_close(peripheral_gpio_h gpio);
117
118 /**
119  * @brief Gets direction of the gpio.
120  * @since_tizen 4.0
121  *
122  * @param[in] gpio The handle to the gpio pin
123  * @param[out] value The direction(value) type of the gpio
124  *
125  * @return 0 on success, otherwise a negative error value
126  * @retval #PERIPHERAL_ERROR_NONE Successful
127  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
128  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
129  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
130  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
131  *
132  * @see peripheral_gpio_set_direction()
133  */
134 int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction);
135
136 /**
137  * @brief Sets direction of the gpio pin.
138  * @since_tizen 4.0
139  *
140  * @param[in] gpio The handle to the gpio pin to set
141  * @param[in] direction Direction(value) type of the gpio pin
142  *
143  * @return 0 on success, otherwise a negative error value
144  * @retval #PERIPHERAL_ERROR_NONE Successful
145  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
146  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
147  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
148  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
149  */
150 int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction);
151
152 /**
153  * @brief Reads value of the gpio.
154  * @since_tizen 4.0
155  *
156  * @param[in] gpio The handle to the gpio pin
157  * @param[out] value The value of the gpio (zero or non-zero)
158  *
159  * @return 0 on success, otherwise a negative error value
160  * @retval #PERIPHERAL_ERROR_NONE Successful
161  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
162  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
163  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
164  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
165  *
166  * @see peripheral_gpio_write()
167  */
168 int peripheral_gpio_read(peripheral_gpio_h gpio, int *value);
169
170 /**
171  * @brief Writes value to the gpio.
172  * @since_tizen 4.0
173  *
174  * @param[in] gpio The handle to the gpio pin
175  * @param[in] value Value to be written to the gpio (muse be zero or non-zero)
176  *
177  * @return 0 on success, otherwise a negative error value
178  * @retval #PERIPHERAL_ERROR_NONE Successful
179  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
180  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
181  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
182  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
183  *
184  * @see peripheral_gpio_read()
185  */
186 int peripheral_gpio_write(peripheral_gpio_h gpio, int value);
187
188 /**
189  * @brief Gets the edge mode of the gpio.
190  * @since_tizen 4.0
191  *
192  * @param[in] gpio The handle to the gpio pin
193  * @param[out] gpio_pin The edge mode of the gpio
194  *
195  * @return 0 on success, otherwise a negative error value
196  * @retval #PERIPHERAL_ERROR_NONE Successful
197  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
198  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
199  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
200  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
201  *
202  * @see peripheral_gpio_set_edge_mode()
203  */
204 int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge);
205
206 /**
207  * @brief Sets the edge mode of the gpio pin.
208  * @since_tizen 4.0
209  *
210  * @param[in] gpio The handle to the gpio pin to set
211  * @param[in] edge The edge mode of the gpio pin
212  *
213  * @return 0 on success, otherwise a negative error value
214  * @retval #PERIPHERAL_ERROR_NONE Successful
215  * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
216  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
217  * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
218  * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
219  *
220  * @see peripheral_gpio_get_edge_mode()
221  */
222 int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge);
223
224 /**
225  * @brief Called when the gpio interrupt is triggered.
226  * @since_tizen 4.0
227  *
228  * @param[in] user_data The user data passed from the callback registration function
229  *
230  * @see peripheral_gpio_register_cb()
231  * @see peripheral_gpio_unregister_cb()
232  */
233 typedef void(*gpio_isr_cb)(void *user_data);
234
235 /**
236  * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
237  * @since_tizen 4.0
238  *
239  * @param[in] gpio The handle to the gpio pin to set
240  * @param[in] edge The edge type of the gpio pin
241  * @param[in] callback The callback function to register
242  * @param[in] user_data The user data to be passed to the callback function
243  *
244  * @return 0 on success, otherwise a negative error value
245  * @retval #PERIPHERAL_ERROR_NONE Successful
246  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
247  *
248  * @see peripheral_gpio_set_edge_mode()
249  * @see peripheral_gpio_unregister_cb()
250  */
251 int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data);
252
253 /**
254  * @brief Unregisters the callback function for the gpio handler.
255  * @since_tizen 4.0
256  *
257  * @param[in] gpio The handle to the gpio pin
258  *
259  * @return 0 on success, otherwise a negative error value
260  * @retval #PERIPHERAL_ERROR_NONE Successful
261  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
262  *
263  * @see peripheral_gpio_register_cb()
264  */
265 int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio);
266
267 /**
268  * @brief Gets pin number of the gpio handle.
269  * @since_tizen 4.0
270  *
271  * @param[in] gpio The handle to the gpio pin
272  * @param[out] gpio_pin The pin number of the gpio
273  *
274  * @return 0 on success, otherwise a negative error value
275  * @retval #PERIPHERAL_ERROR_NONE Successful
276  * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
277  */
278 int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin);
279
280 /**
281 * @}
282 */
283
284 /**
285  * @addtogroup CAPI_SYSTEM_PERPHERAL_I2C_MODULE
286  * @{
287  */
288
289 typedef struct _peripheral_i2c_s *peripheral_i2c_h;
290
291 peripheral_i2c_h peripheral_i2c_init(int bus);
292
293 int peripheral_i2c_stop(peripheral_i2c_h i2c);
294
295 int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address);
296
297 int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length);
298
299 int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length);
300
301
302 /**
303 * @}
304 */
305
306 /**
307  * @addtogroup CAPI_SYSTEM_PERPHERAL_PWM_MODULE
308  * @{
309  */
310
311 struct _peripheral_pwm_s {
312         int device;
313         int channel;
314         int period;
315         int duty_cycle;
316         int enabled;
317 };
318 typedef struct _peripheral_pwm_s *peripheral_pwm_context_h;
319
320 typedef enum {
321         PERIPHERAL_PWM_DISABLE = 0,
322         PERIPHERAL_PWM_ENABLE,
323 } peripheral_pwm_state_e;
324
325 peripheral_pwm_context_h peripheral_pwm_open(int device, int channel);
326
327 int peripheral_pwm_close(peripheral_pwm_context_h pwm);
328
329 int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle);
330
331 int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period);
332
333 int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable);
334
335 int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm);
336
337 int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle);
338
339 int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period);
340
341
342 /**
343 * @}
344 */
345
346 /**
347  * @addtogroup CAPI_SYSTEM_PERPHERAL_ADC_MODULE
348  * @{
349  */
350
351 /**
352  * @brief Struct for peripheral_gpio_s
353  */
354
355 #define DEVICE_NAME_SIZE        20
356
357 struct _peripheral_adc_s {
358         char device_name[DEVICE_NAME_SIZE];
359         int channel;
360 };
361
362 /**
363  * @brief Pointer definition to the internal struct peripheral_adc_s
364  */
365 typedef struct _peripheral_adc_s* peripheral_adc_context_h;
366
367 peripheral_adc_context_h peripheral_adc_open(int channel);
368
369 int peripheral_adc_read(peripheral_adc_context_h dev, int *data);
370
371 int peripheral_adc_close(peripheral_adc_context_h dev);
372
373 /**
374 * @}
375 */
376
377 /**
378  * @addtogroup CAPI_SYSTEM_PERPHERAL_UART_MODULE
379  * @{
380  */
381 struct _peripheral_uart_s {
382         int fd;
383 };
384
385
386 typedef struct _peripheral_uart_s* peripheral_uart_context_h;
387
388 typedef enum {
389         PERIPHERAL_UART_PARITY_NONE = 0,
390         PERIPHERAL_UART_PARITY_EVEN,
391         PERIPHERAL_UART_PARITY_ODD,
392 } peripheral_uart_parity_e;
393
394 peripheral_uart_context_h peripheral_uart_init(const char *path);
395
396 int peripheral_uart_stop(peripheral_uart_context_h hnd);
397
398 int peripheral_uart_flush(peripheral_uart_context_h hnd);
399
400 int peripheral_uart_set_baudrate(peripheral_uart_context_h hnd, unsigned int baud);
401
402 int peripheral_uart_set_mode(peripheral_uart_context_h hnd, int bytesize, peripheral_uart_parity_e parity, int stopbits);
403
404 int peripheral_uart_set_flowcontrol(peripheral_uart_context_h hnd, int xonxoff, int rtscts);
405
406 int peripheral_uart_read(peripheral_uart_context_h hnd, char *buf, unsigned int length);
407
408 int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsigned int length);
409
410 /**
411 * @}
412 */
413
414 /**
415  * @addtogroup CAPI_SYSTEM_PERPHERAL_SPI_MODULE
416  * @{
417  */
418
419 typedef enum {
420         PERIPHERAL_SPI_MODE0 = 0,
421         PERIPHERAL_SPI_MODE1,
422         PERIPHERAL_SPI_MODE2,
423         PERIPHERAL_SPI_MODE3
424 } peripheral_spi_mode_e;
425
426 struct peripheral_spi_config_s {
427         int fd;
428         char bits_per_word;
429         int lsb;
430         unsigned int chip_select;
431         unsigned int frequency;
432         peripheral_spi_mode_e mode;
433 };
434
435 typedef struct peripheral_spi_config_s * peripheral_spi_context_h;
436
437 peripheral_spi_context_h peripheral_spi_open(unsigned int bus, peripheral_spi_context_h config);
438
439 int     peripheral_spi_write(peripheral_spi_context_h hnd, char *txbuf, int length);
440
441 int     peripheral_spi_recv(peripheral_spi_context_h hnd, char *rxbuf, int length);
442
443 int peripheral_spi_transfer_buf(peripheral_spi_context_h hnd, char *txbuf, char *rxbuf, int length);
444
445 int     peripheral_spi_close(peripheral_spi_context_h hnd);
446
447 /**
448 * @}
449 */
450
451 #ifdef __cplusplus
452 }
453 #endif
454
455 #endif /* __PERIPHERAL_IO_H__ */