2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __PERIPHERAL_IO_H__
18 #define __PERIPHERAL_IO_H__
28 * @file peripheral_io.h
29 * @brief This file contains the peripheral-io API
33 * @brief Enumeration for peripheral-io error.
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 */
52 * @addtogroup CAPI_SYSTEM_PERPHERAL_GPIO_MODULE
57 * @brief Enumeration of gpio direction
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;
67 * @brief Enumeration of edge type for gpio interrupt
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;
77 * @brief The handle to the gpio pin
80 typedef struct _peripheral_gpio_s* peripheral_gpio_h;
83 * @brief Initializes(export) gpio pin and creates gpio handle.
86 * @param[in] gpio_pin The gpio pin number
87 * @param[out] gpio The gpio handle is created on success
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
97 * @see peripheral_gpio_close()
99 int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio);
102 * @brief Releases the gpio handle and finalize(unexport) the gpio pin.
105 * @param[in] gpio The handle to the gpio pin to release
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
114 * @see peripheral_gpio_open()
116 int peripheral_gpio_close(peripheral_gpio_h gpio);
119 * @brief Gets direction of the gpio.
122 * @param[in] gpio The handle to the gpio pin
123 * @param[out] value The direction(value) type of the gpio
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
132 * @see peripheral_gpio_set_direction()
134 int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction);
137 * @brief Sets direction of the gpio pin.
140 * @param[in] gpio The handle to the gpio pin to set
141 * @param[in] direction Direction(value) type of the gpio pin
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
150 int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction);
153 * @brief Reads value of the gpio.
156 * @param[in] gpio The handle to the gpio pin
157 * @param[out] value The value of the gpio (zero or non-zero)
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
166 * @see peripheral_gpio_write()
168 int peripheral_gpio_read(peripheral_gpio_h gpio, int *value);
171 * @brief Writes value to the gpio.
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)
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
184 * @see peripheral_gpio_read()
186 int peripheral_gpio_write(peripheral_gpio_h gpio, int value);
189 * @brief Gets the edge mode of the gpio.
192 * @param[in] gpio The handle to the gpio pin
193 * @param[out] gpio_pin The edge mode of the gpio
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
202 * @see peripheral_gpio_set_edge_mode()
204 int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge);
207 * @brief Sets the edge mode of the gpio pin.
210 * @param[in] gpio The handle to the gpio pin to set
211 * @param[in] edge The edge mode of the gpio pin
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
220 * @see peripheral_gpio_get_edge_mode()
222 int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge);
225 * @brief Called when the gpio interrupt is triggered.
228 * @param[in] user_data The user data passed from the callback registration function
230 * @see peripheral_gpio_register_cb()
231 * @see peripheral_gpio_unregister_cb()
233 typedef void(*gpio_isr_cb)(void *user_data);
236 * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
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
244 * @return 0 on success, otherwise a negative error value
245 * @retval #PERIPHERAL_ERROR_NONE Successful
246 * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
248 * @see peripheral_gpio_set_edge_mode()
249 * @see peripheral_gpio_unregister_cb()
251 int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data);
254 * @brief Unregisters the callback function for the gpio handler.
257 * @param[in] gpio The handle to the gpio pin
259 * @return 0 on success, otherwise a negative error value
260 * @retval #PERIPHERAL_ERROR_NONE Successful
261 * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
263 * @see peripheral_gpio_register_cb()
265 int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio);
268 * @brief Gets pin number of the gpio handle.
271 * @param[in] gpio The handle to the gpio pin
272 * @param[out] gpio_pin The pin number of the gpio
274 * @return 0 on success, otherwise a negative error value
275 * @retval #PERIPHERAL_ERROR_NONE Successful
276 * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
278 int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin);
285 * @addtogroup CAPI_SYSTEM_PERPHERAL_I2C_MODULE
289 typedef struct _peripheral_i2c_s *peripheral_i2c_h;
291 int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c);
293 int peripheral_i2c_stop(peripheral_i2c_h i2c);
295 int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address);
297 int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length);
299 int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length);
307 * @addtogroup CAPI_SYSTEM_PERPHERAL_PWM_MODULE
311 struct _peripheral_pwm_s {
318 typedef struct _peripheral_pwm_s *peripheral_pwm_context_h;
321 PERIPHERAL_PWM_DISABLE = 0,
322 PERIPHERAL_PWM_ENABLE,
323 } peripheral_pwm_state_e;
325 peripheral_pwm_context_h peripheral_pwm_open(int device, int channel);
327 int peripheral_pwm_close(peripheral_pwm_context_h pwm);
329 int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle);
331 int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period);
333 int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable);
335 int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm);
337 int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle);
339 int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period);
347 * @addtogroup CAPI_SYSTEM_PERPHERAL_ADC_MODULE
352 * @brief Struct for peripheral_gpio_s
355 #define DEVICE_NAME_SIZE 20
357 struct _peripheral_adc_s {
358 char device_name[DEVICE_NAME_SIZE];
363 * @brief Pointer definition to the internal struct peripheral_adc_s
365 typedef struct _peripheral_adc_s* peripheral_adc_context_h;
367 peripheral_adc_context_h peripheral_adc_open(int channel);
369 int peripheral_adc_read(peripheral_adc_context_h dev, int *data);
371 int peripheral_adc_close(peripheral_adc_context_h dev);
378 * @addtogroup CAPI_SYSTEM_PERPHERAL_UART_MODULE
381 struct _peripheral_uart_s {
386 typedef struct _peripheral_uart_s* peripheral_uart_context_h;
389 PERIPHERAL_UART_PARITY_NONE = 0,
390 PERIPHERAL_UART_PARITY_EVEN,
391 PERIPHERAL_UART_PARITY_ODD,
392 } peripheral_uart_parity_e;
394 peripheral_uart_context_h peripheral_uart_init(const char *path);
396 int peripheral_uart_stop(peripheral_uart_context_h hnd);
398 int peripheral_uart_flush(peripheral_uart_context_h hnd);
400 int peripheral_uart_set_baudrate(peripheral_uart_context_h hnd, unsigned int baud);
402 int peripheral_uart_set_mode(peripheral_uart_context_h hnd, int bytesize, peripheral_uart_parity_e parity, int stopbits);
404 int peripheral_uart_set_flowcontrol(peripheral_uart_context_h hnd, int xonxoff, int rtscts);
406 int peripheral_uart_read(peripheral_uart_context_h hnd, char *buf, unsigned int length);
408 int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsigned int length);
415 * @addtogroup CAPI_SYSTEM_PERPHERAL_SPI_MODULE
420 PERIPHERAL_SPI_MODE0 = 0,
421 PERIPHERAL_SPI_MODE1,
422 PERIPHERAL_SPI_MODE2,
424 } peripheral_spi_mode_e;
426 struct peripheral_spi_config_s {
430 unsigned int chip_select;
431 unsigned int frequency;
432 peripheral_spi_mode_e mode;
435 typedef struct peripheral_spi_config_s * peripheral_spi_context_h;
437 peripheral_spi_context_h peripheral_spi_open(unsigned int bus, peripheral_spi_context_h config);
439 int peripheral_spi_write(peripheral_spi_context_h hnd, char *txbuf, int length);
441 int peripheral_spi_recv(peripheral_spi_context_h hnd, char *rxbuf, int length);
443 int peripheral_spi_transfer_buf(peripheral_spi_context_h hnd, char *txbuf, char *rxbuf, int length);
445 int peripheral_spi_close(peripheral_spi_context_h hnd);
455 #endif /* __PERIPHERAL_IO_H__ */