*
****************************************************************************/
-/*******************************************************************************
+/****************************************************************************
* Included Files
- ******************************************************************************/
+ ****************************************************************************/
+#include <tinyara/config.h>
#include <stdio.h>
#include <string.h>
#include <tinyara/gpio.h>
-/*******************************************************************************
+/****************************************************************************
* Pre-processor Definitions
- ******************************************************************************/
+ ****************************************************************************/
#define GPIO_PREVENT_MULTIPLE_OPEN 0
-/******************************************************************************
+/****************************************************************************
* Private Types
- ******************************************************************************/
+ ****************************************************************************/
-/******************************************************************************
+/****************************************************************************
* Private Function Prototypes
- ******************************************************************************/
-
-static int gpio_fopen(FAR struct file *filep);
-static int gpio_fclose(FAR struct file *filep);
-static ssize_t gpio_read(FAR struct file *filep, FAR char *buffer, size_t buflen);
-static ssize_t gpio_write(FAR struct file *filep, FAR const char *buffer, size_t buflen);
-static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
+ ****************************************************************************/
+static int gpio_fopen(FAR struct file *filep);
+static int gpio_fclose(FAR struct file *filep);
+static ssize_t gpio_read(FAR struct file *filep, FAR char *buffer,
+ size_t buflen);
+static ssize_t gpio_write(FAR struct file *filep, FAR const char *buffer,
+ size_t buflen);
+static int gpio_ioctl(FAR struct file *filep, int cmd,
+ unsigned long arg);
#ifndef CONFIG_DISABLE_POLL
-static int gpio_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup);
+static int gpio_poll(FAR struct file *filep, FAR struct pollfd *fds,
+ bool setup);
#endif
-/******************************************************************************
+/****************************************************************************
* Private Variables
- ******************************************************************************/
-
+ ****************************************************************************/
static const struct file_operations g_gpioops = {
- gpio_fopen, /* open */
- gpio_fclose, /* close */
- gpio_read, /* read */
- gpio_write, /* write */
- NULL, /* seek */
- gpio_ioctl, /* ioctl */
+ gpio_fopen, /* open */
+ gpio_fclose, /* close */
+ gpio_read, /* read */
+ gpio_write, /* write */
+ NULL, /* seek */
+ gpio_ioctl, /* ioctl */
#ifndef CONFIG_DISABLE_POLL
- gpio_poll /* poll */
+ gpio_poll /* poll */
#endif
};
-/******************************************************************************
+/****************************************************************************
* Private Functions
- ******************************************************************************/
+ ****************************************************************************/
-/******************************************************************************
+/****************************************************************************
* Name: sem_reinit
*
* Description:
* Reinitialize semaphore
*
- ******************************************************************************/
-
+ ****************************************************************************/
#ifndef CONFIG_DISABLE_POLL
static int sem_reinit(FAR sem_t *sem, int pshared, unsigned int value)
{
sem_destroy(sem);
+
return sem_init(sem, pshared, value);
}
#endif
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_takesem
*
* Description:
* Take the lock, waiting as necessary
*
- ******************************************************************************/
-
+ ****************************************************************************/
static int gpio_takesem(FAR sem_t *sem, bool errout)
{
- /* Loop, ignoring interrupts, until we have successfully acquired the lock */
+ /*
+ * Loop, ignoring interrupts, until we have successfully acquired
+ * the lock
+ */
while (sem_wait(sem) != OK) {
- /* The only case that an error should occur here is if the wait
- * was awakened by a signal.
+ /*
+ * The only case that an error should occur here is if the
+ * wait was awakened by a signal.
*/
-
ASSERT(get_errno() == EINTR);
- /* When the signal is received, should we errout? Or should we just
- * continue waiting until we have the semaphore?
+ /*
+ * When the signal is received, should we errout? Or should
+ * we just continue waiting until we have the semaphore?
*/
-
if (errout) {
return -EINTR;
}
return OK;
}
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_givesem
*
* Description:
* release the lock
*
- ******************************************************************************/
-
+ ****************************************************************************/
static inline void gpio_givesem(sem_t *sem)
{
sem_post(sem);
}
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_pollnotify
*
* Description:
* notify file descriptor. It is necessary to handle async I/O.
*
- ******************************************************************************/
-
+ ****************************************************************************/
#ifndef CONFIG_DISABLE_POLL
static void gpio_pollnotify(FAR struct gpio_dev_s *dev, pollevent_t eventset)
{
int i;
+
for (i = 0; i < CONFIG_GPIO_NPOLLWAITERS; i++) {
struct pollfd *fds = dev->fds[i];
if (fds) {
#define gpio_pollnotify(dev, event)
#endif
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_write
*
* Description:
* This function is called when you handle the write() API in
* File system.
*
- ******************************************************************************/
-
-static ssize_t gpio_write(FAR struct file *filep, FAR const char *buffer, size_t buflen)
+ ****************************************************************************/
+static ssize_t gpio_write(FAR struct file *filep, FAR const char *buffer,
+ size_t buflen)
{
int32_t value;
FAR struct inode *inode = filep->f_inode;
}
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_read
*
* Description:
* This function is automatically called when you handle the read() API in
* File system.
*
- ******************************************************************************/
-
-static ssize_t gpio_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
+ ****************************************************************************/
+static ssize_t gpio_read(FAR struct file *filep, FAR char *buffer,
+ size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct gpio_dev_s *dev = inode->i_private;
int ret;
int value;
- /* if you read gpio with 'cat', you have to return 0,
- * Otherwise cat will be cleaned up properly.
- * So If it is the first time, then return size, and resize fpos.
- * next time return zero.
+ /*
+ * if you read gpio with 'cat', you have to return 0, Otherwise cat
+ * will be cleaned up properly. So If it is the first time, then
+ * return size, and resize fpos. next time return zero.
*/
-
if (filep->f_pos == 0) {
value = gpio_get(dev);
}
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_ioctl
*
* Description:
* This function is allow you to control the event.
*
- ******************************************************************************/
-
+ ****************************************************************************/
static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
int ret;
return ret;
}
-/******************************************************************************
+/****************************************************************************
* Name: gpio_poll
*
* Description:
* This function is called when you want to wait for events.
* You don't know when the event will be occured.
*
- ******************************************************************************/
-
+ ****************************************************************************/
#ifndef CONFIG_DISABLE_POLL
-static int gpio_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
+static int gpio_poll(FAR struct file *filep, FAR struct pollfd *fds,
+ bool setup)
{
FAR struct inode *inode = filep->f_inode;
FAR struct gpio_dev_s *dev = inode->i_private;
int i;
/* Some sanity checking */
-
if (!dev || !fds) {
return -ENODEV;
}
/* Are we setting up the poll? Or tearing it down? */
-
ret = gpio_takesem(&dev->pollsem, true);
if (ret < 0) {
- /* A signal received while waiting for access to the poll data
+ /*
+ * A signal received while waiting for access to the poll data
* will abort the operation.
*/
-
return ret;
}
if (setup) {
- /* This is a request to set up the poll. Find an available
+ /*
+ * This is a request to set up the poll. Find an available
* slot for the poll structure reference
*/
-
for (i = 0; i < CONFIG_GPIO_NPOLLWAITERS; i++) {
/* Find an available slot */
-
if (!dev->fds[i]) {
/* Bind the poll structure and this slot */
-
dev->fds[i] = fds;
fds->priv = &dev->fds[i];
break;
}
} else if (fds->priv) {
/* This is a request to tear down the poll. */
-
struct pollfd **slot = (struct pollfd **)fds->priv;
if (!slot) {
}
/* Remove all memory of the poll setup */
-
*slot = NULL;
fds->priv = NULL;
}
gpio_givesem(&dev->pollsem);
return ret;
}
-#endif /* CONFIG_DISABLE_POLL */
+#endif /* CONFIG_DISABLE_POLL */
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_fclose
*
* Description:
* This routine is called when the gpio gets closed.
*
- ******************************************************************************/
-
+ ****************************************************************************/
static int gpio_fclose(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
return OK;
}
-/*******************************************************************************
+/*****************************************************************************
* Name: gpio_fopen
*
* Description:
* This routine is called whenever gpio is opened.
*
- ******************************************************************************/
-
+ ****************************************************************************/
static int gpio_fopen(FAR struct file *filep)
{
int ret;
FAR struct gpio_dev_s *dev = inode->i_private;
uint8_t tmp;
- /* If the port is the middle of closing, wait until the close is finished.
- * If a signal is received while we are waiting, then return EINTR.
+ /*
+ * If the port is the middle of closing, wait until the close is
+ * finished. If a signal is received while we are waiting, then
+ * return EINTR.
*/
ret = gpio_takesem(&dev->closesem, true);
if (ret < 0) {
- /* A signal received while waiting for the last close operation. */
+ /*
+ * A signal received while waiting for the last close
+ * operation.
+ */
return ret;
}
return ret;
}
-/*******************************************************************************
+/*****************************************************************************
* Name: gpio_notify
*
* Description:
* This routine is called from irq handler. If you want to handle with
* async call or event, you have to register your fd with poll().
*
- ******************************************************************************/
-
+ ****************************************************************************/
void gpio_notify(FAR struct gpio_dev_s *dev)
{
gpio_pollnotify(dev, POLLIN);
return;
}
-/******************************************************************************
+/****************************************************************************
* Public Functions
- ******************************************************************************/
+ ****************************************************************************/
-/******************************************************************************
+/****************************************************************************
* Name: gpio_register
*
* Description:
* Register GPIO device.
*
- ******************************************************************************/
-
+ ****************************************************************************/
int gpio_register(FAR const char *path, FAR struct gpio_dev_s *dev)
{
sem_init(&dev->closesem, 0, 1);
return register_driver(path, &g_gpioops, 0666, dev);
}
-/******************************************************************************
+/****************************************************************************
* Name: gpio_unregister
*
* Description:
* unregister GPIO device.
*
- ******************************************************************************/
-
+ ****************************************************************************/
int gpio_unregister(FAR const char *path)
{
return unregister_driver(path);
* language governing permissions and limitations under the License.
*
****************************************************************************/
-/************************************************************************************
+/****************************************************************************
* include/tinyara/gpio.h
*
* Copyright (C) 2016 SAMSUNG ELECTRONICS CO., LTD. All rights reserved.
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
- ******************************************************************************/
+ ****************************************************************************/
#ifndef __INCLUDE_TINYARA_GPIO_H
#define __INCLUDE_TINYARA_GPIO_H
-/******************************************************************************
+/****************************************************************************
* Included Files
- ******************************************************************************/
+ ****************************************************************************/
#include <tinyara/config.h>
+
#include <stdint.h>
#include <semaphore.h>
+
#include <tinyara/wdog.h>
#include <tinyara/wqueue.h>
#ifndef CONFIG_DISABLE_POLL
-#include<tinyara/serial/serial.h>
+#include <tinyara/serial/serial.h>
#endif
-/******************************************************************************
+
+/****************************************************************************
* Pre-processor Definitions
- ******************************************************************************/
+ ****************************************************************************/
/* Maximum number of threads than can be waiting for POLL events */
#define gpio_set(dev, s) dev->ops->set(dev, s)
#define gpio_ctrl(dev, s, v) dev->ops->ctrl(dev, s, v)
-/******************************************************************************
+/****************************************************************************
* Public Types
- ******************************************************************************/
+ ****************************************************************************/
typedef void (*GPIO_CB_FUNC)(void);
typedef enum {
- GPIO_EDGE_NONE, /* No interrupt on GPIO */
- GPIO_EDGE_BOTH, /* Interrupt occures on rising and falling edge */
- GPIO_EDGE_RISING, /* Interrupt occurs on rising edge */
- GPIO_EDGE_FALLING, /* Interrupt occurs on falling edge */
+ GPIO_EDGE_NONE, /* No interrupt on GPIO */
+ GPIO_EDGE_BOTH, /* Interrupt occures on rising and falling edge */
+ GPIO_EDGE_RISING, /* Interrupt occurs on rising edge */
+ GPIO_EDGE_FALLING, /* Interrupt occurs on falling edge */
- GPIO_LEVEL_LOW, /* Not support in SystemIO */
- GPIO_LEVEL_HIGH, /* Not support in SystemIO */
+ GPIO_LEVEL_LOW, /* Not support in SystemIO */
+ GPIO_LEVEL_HIGH, /* Not support in SystemIO */
} gpio_edge_t;
typedef enum {
#endif
};
-/******************************************************************************
+/****************************************************************************
* Public Data
- ******************************************************************************/
+ ****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern
#endif
-/******************************************************************************
+/****************************************************************************
* Public Functions
- ******************************************************************************/
+ ****************************************************************************/
-/*******************************************************************************
+/****************************************************************************
* Name: gpio_notify
*
* Description:
* This routine is called from irq handler. If you want to handle with
* async call or event, you have to register your fd with poll().
*
- ******************************************************************************/
+ ****************************************************************************/
void gpio_notify(FAR struct gpio_dev_s *dev);
-/******************************************************************************
+/****************************************************************************
* Name: gpio_register
*
* Description:
* Register GPIO device.
*
- ******************************************************************************/
+ ****************************************************************************/
int gpio_register(FAR const char *path, FAR struct gpio_dev_s *dev);
-/******************************************************************************
+/****************************************************************************
* Name: gpio_export_init
*
* Description:
* register export-file in filesystem
*
- ******************************************************************************/
+ ****************************************************************************/
#ifdef CONFIG_GPIO_EXPORT
int gpio_export_init();
#endif
-/******************************************************************************
+/****************************************************************************
* Name: gpio_unexport_init
*
* Description:
* register unexport-file in filesystem
*
- ******************************************************************************/
+ ****************************************************************************/
#ifdef CONFIG_GPIO_EXPORT
int gpio_unexport_init();
#endif
-/*******************************************************************************
+/****************************************************************************
* Name: up_destroy_gpio
*
* Description:
- * Destroy device.
+ * Destroy device.
*
- ******************************************************************************/
+ ****************************************************************************/
int up_destroy_gpio(int32_t idx);
-/*******************************************************************************
+/****************************************************************************
* Name: up_create_gpio
*
* Description:
- * Create device. Userspace may ask the kernel to export control of
- * a GPIO to userspace by writing its number to file (gpio_export).
+ * Create device. Userspace may ask the kernel to export control of
+ * a GPIO to userspace by writing its number to file (gpio_export).
*
- ******************************************************************************/
+ ****************************************************************************/
int up_create_gpio(int32_t idx);
}
#endif
-#endif /* __INCLUDE_TINYARA_GPIO_H */
+#endif /* __INCLUDE_TINYARA_GPIO_H */