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 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;
66 * @brief Enumeration of edge type for gpio interrupt
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;
76 * @brief The handle to the gpio pin
79 typedef struct _peripheral_gpio_s* peripheral_gpio_h;
82 * @brief Called when the gpio interrupt is triggered.
85 * @param[in] user_data The user data passed from the callback registration function
87 * @see peripheral_gpio_register_cb()
88 * @see peripheral_gpio_unregister_cb()
90 typedef void(*gpio_isr_cb)(void *user_data);
93 * @brief Initilizes(export) gpio pin and creates gpio handle.
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
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
107 * @see peripheral_gpio_close()
109 int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio);
112 * @brief Release the gpio handle and finalize(unexport) the gpio pin.
115 * @param[in] gpio The handle to the gpio pin to release
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
124 * @see peripheral_gpio_open()
126 int peripheral_gpio_close(peripheral_gpio_h gpio);
129 * @brief Sets direction of the gpio pin.
132 * @param[in] gpio The handle to the gpio pin to set
133 * @param[in] direction The direction type of the gpio pin
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
142 int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction);
145 * @brief Sets the edge mode of the gpio pin.
148 * @param[in] gpio The handle to the gpio pin to set
149 * @param[in] edge The edge type of the gpio pin
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
158 int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge);
161 * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
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
169 * @return 0 on success, otherwise a negative error value
170 * @retval #PERIPHERAL_ERROR_NONE Successfu
171 * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parametera
173 * @see peripheral_gpio_set_edge_mode()
174 * @see peripheral_gpio_unregister_cb()
176 int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data);
179 * @brief Unregisters the callback function for the gpio handler.
182 * @param[in] gpio The handle to the gpio pin
184 * @return 0 on success, otherwise a negative error value
185 * @retval #PERIPHERAL_ERROR_NONE Successful
186 * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
188 * @see peripheral_gpio_register_cb()
190 int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio);
193 * @brief Reads the gpio value.
196 * @param[in] gpio The handle to the gpio pin
197 * @param[out] value The result of the gpio
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
206 int peripheral_gpio_read(peripheral_gpio_h gpio, int *value);
209 * @brief Writes the gpio value.
212 * @param[in] gpio The handle to the gpio pin
213 * @param[in] value The value to be written to the gpio
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
222 int peripheral_gpio_write(peripheral_gpio_h gpio, int value);
225 * @brief Gets direction of the gpio.
228 * @param[in] gpio The handle to the gpio pin
229 * @param[out] value The value to be written to the gpio
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
238 int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction);
241 * @brief Gets pin number of the gpio.
244 * @param[in] gpio The handle to the gpio pin
245 * @param[out] gpio_pin The number of the gpio
247 * @return 0 on success, otherwise a negative error value
248 * @retval #PERIPHERAL_ERROR_NONE Successful
249 * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
251 int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin);
254 * @brief Gets edge mode of the gpio.
257 * @param[in] gpio The handle to the gpio pin
258 * @param[out] gpio_pin The number of the gpio
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
267 int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge);
274 * @addtogroup CAPI_SYSTEM_PERPHERAL_I2C_MODULE
279 * @brief Struct for peripheral_gpio_s
281 struct _peripheral_i2c_s {
284 typedef struct _peripheral_i2c_s *peripheral_i2c_context_h;
287 PERIPHERAL_I2C_STD = 0,
288 PERIPHERAL_I2C_FAST = 1,
289 PERIPHERAL_I2C_HIGH = 2
290 } peripheral_i2c_mode_e;
292 peripheral_i2c_context_h peripheral_i2c_init(int bus);
294 int peripheral_i2c_stop(peripheral_i2c_context_h hnd);
296 int peripheral_i2c_set_frequency(peripheral_i2c_context_h hnd, peripheral_i2c_mode_e mode);
298 int peripheral_i2c_set_address(peripheral_i2c_context_h hnd, int address);
300 int peripheral_i2c_read(peripheral_i2c_context_h hnd, uint8_t *data, int length);
302 int peripheral_i2c_write(peripheral_i2c_context_h hnd, uint8_t *data, int length);
310 * @addtogroup CAPI_SYSTEM_PERPHERAL_PWM_MODULE
314 struct _peripheral_pwm_s {
321 typedef struct _peripheral_pwm_s *peripheral_pwm_context_h;
324 PERIPHERAL_PWM_DISABLE = 0,
325 PERIPHERAL_PWM_ENABLE,
326 } peripheral_pwm_state_e;
328 peripheral_pwm_context_h peripheral_pwm_open(int device, int channel);
330 int peripheral_pwm_close(peripheral_pwm_context_h pwm);
332 int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle);
334 int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period);
336 int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable);
338 int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm);
340 int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle);
342 int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period);
350 * @addtogroup CAPI_SYSTEM_PERPHERAL_ADC_MODULE
355 * @brief Struct for peripheral_gpio_s
358 #define DEVICE_NAME_SIZE 20
360 struct _peripheral_adc_s {
361 char device_name[DEVICE_NAME_SIZE];
366 * @brief Pointer definition to the internal struct peripheral_adc_s
368 typedef struct _peripheral_adc_s* peripheral_adc_context_h;
370 peripheral_adc_context_h peripheral_adc_open(int channel);
372 int peripheral_adc_read(peripheral_adc_context_h dev, int *data);
374 int peripheral_adc_close(peripheral_adc_context_h dev);
381 * @addtogroup CAPI_SYSTEM_PERPHERAL_UART_MODULE
384 struct _peripheral_uart_s {
389 typedef struct _peripheral_uart_s* peripheral_uart_context_h;
392 PERIPHERAL_UART_PARITY_NONE = 0,
393 PERIPHERAL_UART_PARITY_EVEN,
394 PERIPHERAL_UART_PARITY_ODD,
395 } peripheral_uart_parity_e;
397 peripheral_uart_context_h peripheral_uart_init(const char *path);
399 int peripheral_uart_stop(peripheral_uart_context_h hnd);
401 int peripheral_uart_flush(peripheral_uart_context_h hnd);
403 int peripheral_uart_set_baudrate(peripheral_uart_context_h hnd, unsigned int baud);
405 int peripheral_uart_set_mode(peripheral_uart_context_h hnd, int bytesize, peripheral_uart_parity_e parity, int stopbits);
407 int peripheral_uart_set_flowcontrol(peripheral_uart_context_h hnd, int xonxoff, int rtscts);
409 int peripheral_uart_read(peripheral_uart_context_h hnd, char *buf, unsigned int length);
411 int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsigned int length);
418 * @addtogroup CAPI_SYSTEM_PERPHERAL_SPI_MODULE
423 PERIPHERAL_SPI_MODE0 = 0,
424 PERIPHERAL_SPI_MODE1,
425 PERIPHERAL_SPI_MODE2,
427 } peripheral_spi_mode_e;
429 struct peripheral_spi_config_s {
433 unsigned int chip_select;
434 unsigned int frequency;
435 peripheral_spi_mode_e mode;
438 typedef struct peripheral_spi_config_s * peripheral_spi_context_h;
440 peripheral_spi_context_h peripheral_spi_open(unsigned int bus, peripheral_spi_context_h config);
442 int peripheral_spi_write(peripheral_spi_context_h hnd, char *txbuf, int length);
444 int peripheral_spi_recv(peripheral_spi_context_h hnd, char *rxbuf, int length);
446 int peripheral_spi_transfer_buf(peripheral_spi_context_h hnd, char *txbuf, char *rxbuf, int length);
448 int peripheral_spi_close(peripheral_spi_context_h hnd);
458 #endif /* __PERIPHERAL_IO_H__ */