Merged in raiden00/nuttx (pull request #299)

Add COMP character driver

Approved-by: Gregory Nutt <gnutt@nuttx.org>
This commit is contained in:
Mateusz Szafoni 2017-03-25 16:29:02 +00:00 committed by Gregory Nutt
commit c174074dd8
7 changed files with 731 additions and 211 deletions

View File

@ -45,6 +45,8 @@
#include <debug.h>
#include <arch/board/board.h>
#include <nuttx/analog/comp.h>
#include <nuttx/analog/ioctl.h>
#include "chip.h"
#include "stm32_gpio.h"
@ -60,6 +62,10 @@
defined(CONFIG_STM32_COMP5) || defined(CONFIG_STM32_COMP6) || \
defined(CONFIG_STM32_COMP7)
#ifndef CONFIG_STM32_SYSCFG
# error "SYSCFG clock enable must be set"
#endif
/* @TODO: support for STM32F30XX and STM32F37XX comparators */
#if defined(CONFIG_STM32_STM32F30XX) || defined(CONFIG_STM32_STM32F33XX) || \
@ -145,6 +151,61 @@
* Private Types
****************************************************************************/
/* This structure describes the configuration of one COMP device */
struct stm32_comp_s
{
uint8_t blanking; /* Blanking source */
uint8_t pol; /* Output polarity */
uint8_t inm; /* Inverting input selection */
uint8_t out; /* Comparator output */
uint8_t lock; /* Comparator Lock */
uint32_t csr; /* Control and status register */
#ifndef CONFIG_STM32_STM32F33XX
uint8_t mode; /* Comparator mode */
uint8_t hyst; /* Comparator hysteresis */
/* @TODO: Window mode + INP selection */
#endif
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* COMP Register access */
static inline void comp_modify_csr(FAR struct stm32_comp_s *priv,
uint32_t clearbits, uint32_t setbits);
static inline uint32_t comp_getreg_csr(FAR struct stm32_comp_s *priv);
static inline void comp_putreg_csr(FAR struct stm32_comp_s *priv,
uint32_t value);
static bool stm32_complock_get(FAR struct stm32_comp_s *priv);
static int stm32_complock(FAR struct stm32_comp_s *priv, bool lock);
/* COMP Driver Methods */
static void comp_shutdown(FAR struct comp_dev_s *dev);
static int comp_setup(FAR struct comp_dev_s *dev);
static int comp_read(FAR struct comp_dev_s *dev);
static int comp_ioctl(FAR struct comp_dev_s *dev, int cmd, unsigned long arg);
/* Initialization */
static int stm32_compconfig(FAR struct stm32_comp_s *priv);
static int stm32_compenable(FAR struct stm32_comp_s *priv, bool enable);
/****************************************************************************
* Private Types
****************************************************************************/
static const struct comp_ops_s g_compops =
{
.ao_shutdown = comp_shutdown,
.ao_setup = comp_setup,
.ao_read = comp_read,
.ao_ioctl = comp_ioctl,
};
#ifdef CONFIG_STM32_COMP1
static struct stm32_comp_s g_comp1priv =
{
@ -159,6 +220,12 @@ static struct stm32_comp_s g_comp1priv =
.hyst = COMP1_HYST,
#endif
};
static struct comp_dev_s g_comp1dev =
{
.ad_ops = &g_compops,
.ad_priv = &g_comp1priv,
};
#endif
#ifdef CONFIG_STM32_COMP2
@ -175,6 +242,12 @@ static struct stm32_comp_s g_comp2priv =
.hyst = COMP2_HYST,
#endif
};
static struct comp_dev_s g_comp2dev =
{
.ad_ops = &g_compops,
.ad_priv = &g_comp2priv,
};
#endif
#ifdef CONFIG_STM32_COMP3
@ -187,10 +260,16 @@ static struct stm32_comp_s g_comp3priv =
.lock = COMP3_LOCK,
.csr = STM32_COMP3_CSR,
#ifndef CONFIG_STM32_STM32F33XX
.mode = COMP3_MODE,
.hyst = COMP3_HYST,
.mode = COMP3_MODE,
.hyst = COMP3_HYST,
#endif
};
static struct comp_dev_s g_comp3dev =
{
.ad_ops = &g_compops,
.ad_priv = &g_comp3priv,
};
#endif
#ifdef CONFIG_STM32_COMP4
@ -207,6 +286,12 @@ static struct stm32_comp_s g_comp4priv =
.hyst = COMP4_HYST,
#endif
};
static struct comp_dev_s g_comp4dev =
{
.ad_ops = &g_compops,
.ad_priv = &g_comp4priv,
};
#endif
#ifdef CONFIG_STM32_COMP5
@ -223,6 +308,12 @@ static struct stm32_comp_s g_comp5priv =
.hyst = COMP5_HYST,
#endif
};
static struct comp_dev_s g_comp5dev =
{
.ad_ops = &g_compops,
.ad_priv = &g_comp5priv,
};
#endif
#ifdef CONFIG_STM32_COMP6
@ -239,6 +330,12 @@ static struct stm32_comp_s g_comp6priv =
.hyst = COMP6_HYST,
#endif
};
static struct comp_dev_s g_comp6dev =
{
.ad_ops = &g_compops,
.ad_priv = &g_comp6priv,
};
#endif
#ifdef CONFIG_STM32_COMP7
@ -255,19 +352,14 @@ static struct stm32_comp_s g_comp7priv =
.hyst = COMP7_HYST,
#endif
};
static struct comp_dev_s g_comp7dev =
{
.ad_ops = &g_compops,
.ad_priv = &g_comp7priv,
};
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static inline void comp_modify_csr(FAR struct stm32_comp_s *priv,
uint32_t clearbits, uint32_t setbits);
static inline uint32_t comp_getreg_csr(FAR struct stm32_comp_s *priv);
static inline void comp_putreg_csr(FAR struct stm32_comp_s *priv,
uint32_t value);
static bool stm32_complock_get(FAR struct stm32_comp_s *priv);
/****************************************************************************
* Private Functions
****************************************************************************/
@ -360,13 +452,52 @@ static bool stm32_complock_get(FAR struct stm32_comp_s *priv)
regval = comp_getreg_csr(priv);
return ((regval & COMP_CSR_LOCK == 0) ? false : true);
return (((regval & COMP_CSR_LOCK) == 0) ? false : true);
}
/****************************************************************************
* Public Functions
* Name: stm32_complock
*
* Description:
* Lock comparator CSR register
*
* Input Parameters:
* priv - A reference to the COMP structure
* enable - lock flag
*
* Returned Value:
* 0 on success, a negated errno value on failure
*
****************************************************************************/
static int stm32_complock(FAR struct stm32_comp_s *priv, bool lock)
{
bool current;
current = stm32_complock_get(priv);
if (current)
{
if (lock == false)
{
aerr("ERROR: COMP LOCK can be cleared only by a system reset\n");
return -EPERM;
}
}
else
{
if (lock == true)
{
comp_modify_csr(priv, 0, COMP_CSR_LOCK);
priv->lock = COMP_LOCK_RO;
}
}
return OK;
}
/****************************************************************************
* Name: stm32_compconfig
*
@ -383,9 +514,9 @@ static bool stm32_complock_get(FAR struct stm32_comp_s *priv)
*
****************************************************************************/
int stm32_compconfig(FAR struct stm32_comp_s *priv)
static int stm32_compconfig(FAR struct stm32_comp_s *priv)
{
uint32_t regval;
uint32_t regval = 0;
int index;
/* Get comparator index */
@ -665,98 +796,6 @@ int stm32_compconfig(FAR struct stm32_comp_s *priv)
return OK;
}
/****************************************************************************
* Name: stm32_compinitialize
*
* Description:
* Initialize the COMP.
*
* Input Parameters:
* intf - The COMP interface number.
*
* Returned Value:
* Valid COMP device structure reference on succcess; a NULL on failure.
*
* Assumptions:
* 1. Clock to the COMP block has enabled,
* 2. Board-specific logic has already configured
*
****************************************************************************/
FAR struct stm32_comp_s* stm32_compinitialize(int intf)
{
FAR struct stm32_comp_s *priv;
int ret;
switch (intf)
{
#ifdef CONFIG_STM32_COMP1
case 1:
ainfo("COMP1 selected\n");
priv = &g_comp1priv;
break;
#endif
#ifdef CONFIG_STM32_COMP2
case 2:
ainfo("COMP2 selected\n");
priv = &g_comp2priv;
break;
#endif
#ifdef CONFIG_STM32_COMP3
case 3:
ainfo("COMP3 selected\n");
priv = &g_comp3priv;
break;
#endif
#ifdef CONFIG_STM32_COMP4
case 4:
ainfo("COMP4 selected\n");
priv = &g_comp4priv;
break;
#endif
#ifdef CONFIG_STM32_COMP5
case 5:
ainfo("COMP5 selected\n");
priv = &g_comp5priv;
break;
#endif
#ifdef CONFIG_STM32_COMP6
case 6:
ainfo("COMP6 selected\n");
priv = &g_comp6priv;
break;
#endif
#ifdef CONFIG_STM32_COMP7
case 7:
ainfo("COMP7 selected\n");
priv = &g_comp7priv;
break;
#endif
default:
aerr("ERROR: No COMP interface defined\n");
return NULL;
}
/* Configure selected comparator */
ret = stm32_compconfig(priv);
if (ret < 0)
{
aerr("ERROR: Failed to initialize COMP%d: %d\n", intf, ret);
errno = -ret;
return NULL;
}
return priv;
}
/****************************************************************************
* Name: stm32_compenable
*
@ -772,7 +811,7 @@ FAR struct stm32_comp_s* stm32_compinitialize(int intf)
*
****************************************************************************/
int stm32_compenable(FAR struct stm32_comp_s *priv, bool enable)
static int stm32_compenable(FAR struct stm32_comp_s *priv, bool enable)
{
bool lock;
@ -792,13 +831,13 @@ int stm32_compenable(FAR struct stm32_comp_s *priv, bool enable)
{
/* Enable the COMP */
comp_modify_csr(priv, COMP_CSR_COMPEN, 0);
comp_modify_csr(priv, 0, COMP_CSR_COMPEN);
}
else
{
/* Disable the COMP */
comp_modify_csr(priv, 0, COMP_CSR_COMPEN);
comp_modify_csr(priv, COMP_CSR_COMPEN, 0);
}
}
@ -806,46 +845,190 @@ int stm32_compenable(FAR struct stm32_comp_s *priv, bool enable)
}
/****************************************************************************
* Name: stm32_complock
* Name: adc_setup
*
* Description:
* Lock comparator CSR register
* Configure the COMP. This method is called the first time that the COMP
* device is opened. This will occur when the port is first opened.
* This setup includes configuring and attaching COMP interrupts.
* Interrupts are all disabled upon return.
*
* Input Parameters:
* priv - A reference to the COMP structure
* enable - lock flag
*
* Returned Value:
* 0 on success, a negated errno value on failure
*
****************************************************************************/
int stm32_complock(FAR struct stm32_comp_s *priv, bool lock)
static int comp_setup(FAR struct comp_dev_s *dev)
{
bool current;
#warning "Missing logic"
return OK;
}
current = stm32_complock_get(priv);
/****************************************************************************
* Name: comp_shutdown
*
* Description:
* Disable the COMP. This method is called when the COMP device is closed.
* This method reverses the operation the setup method.
* Works only if COMP device is not locked.
*
* Input Parameters:
*
* Returned Value:
* None
*
****************************************************************************/
if (current)
static void comp_shutdown(FAR struct comp_dev_s *dev)
{
#warning "Missing logic"
}
/****************************************************************************
* Name: comp_read
*
* Description:
* Get the COMP output state.
*
* Input Parameters:
*
* Returned Value:
* 0 if output is low (non-inverting input below inverting input),
* 1 if output is high (non inverting input above inverting input).
*
****************************************************************************/
static int comp_read(FAR struct comp_dev_s *dev)
{
FAR struct stm32_comp_s *priv;
uint32_t regval;
priv = dev->ad_priv;
regval = comp_getreg_csr(priv);
return (((regval & COMP_CSR_OUT) == 0) ? 0 : 1);
}
/****************************************************************************
* Name: comp_ioctl
*
* Description:
* All ioctl calls will be routed through this method.
*
* Input Parameters:
* dev - pointer to device structure used by the driver
* cmd - command
* arg - arguments passed with command
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int comp_ioctl(FAR struct comp_dev_s *dev, int cmd, unsigned long arg)
{
#warning "Missing logic"
return -ENOTTY;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_compinitialize
*
* Description:
* Initialize the COMP.
*
* Input Parameters:
* intf - The COMP interface number.
*
* Returned Value:
* Valid COMP device structure reference on succcess; a NULL on failure.
*
* Assumptions:
* 1. Clock to the COMP block has enabled,
* 2. Board-specific logic has already configured
*
****************************************************************************/
FAR struct comp_dev_s* stm32_compinitialize(int intf)
{
FAR struct comp_dev_s *dev;
FAR struct stm32_comp_s *comp;
int ret;
switch (intf)
{
if (lock == false)
{
aerr("ERROR: COMP LOCK can be cleared only by a system reset\n");
#ifdef CONFIG_STM32_COMP1
case 1:
ainfo("COMP1 selected\n");
dev = &g_comp1dev;
break;
#endif
return -EPERM;
}
}
else
{
if (lock == true)
{
comp_modify_csr(priv, COMP_CSR_LOCK, 0);
#ifdef CONFIG_STM32_COMP2
case 2:
ainfo("COMP2 selected\n");
dev = &g_comp2dev;
break;
#endif
priv->lock = COMP_LOCK_RO;
}
#ifdef CONFIG_STM32_COMP3
case 3:
ainfo("COMP3 selected\n");
dev = &g_comp3dev;
break;
#endif
#ifdef CONFIG_STM32_COMP4
case 4:
ainfo("COMP4 selected\n");
dev = &g_comp4dev;
break;
#endif
#ifdef CONFIG_STM32_COMP5
case 5:
ainfo("COMP5 selected\n");
dev = &g_comp5dev;
break;
#endif
#ifdef CONFIG_STM32_COMP6
case 6:
ainfo("COMP6 selected\n");
dev = &g_comp6dev;
break;
#endif
#ifdef CONFIG_STM32_COMP7
case 7:
ainfo("COMP7 selected\n");
dev = &g_comp7dev;
break;
#endif
default:
aerr("ERROR: No COMP interface defined\n");
return NULL;
}
return OK;
/* Configure selected comparator */
comp = dev->ad_priv;
ret = stm32_compconfig(comp);
if (ret < 0)
{
aerr("ERROR: Failed to initialize COMP%d: %d\n", intf, ret);
errno = -ret;
return NULL;
}
return dev;
}
#endif /* CONFIG_STM32_STM32F30XX || CONFIG_STM32_STM32F33XX ||

View File

@ -60,7 +60,7 @@
#define COMP_POL_DEFAULT COMP_POL_NONINVERT /* Output is not inverted */
#define COMP_INM_DEFAULT COMP_INMSEL_1P4VREF /* 1/4 of Vrefint as INM */
#define COMP_OUTSEL_DEFAULT COMP_OUTSEL_NOSEL /* Output not selected */
#define COMP_LOCK_DEFAULT COMP_LOCK_RO /* Do not lock CSR register */
#define COMP_LOCK_DEFAULT COMP_LOCK_RW /* Do not lock CSR register */
#ifndef CONFIG_STM32_STM32F33XX
#define COMP_MODE_DEFAULT
@ -172,23 +172,6 @@ enum stm32_comp_winmode_e
#endif
/* Comparator configuration ***********************************************************/
struct stm32_comp_s
{
uint8_t blanking; /* Blanking source */
uint8_t pol; /* Output polarity */
uint8_t inm; /* Inverting input selection */
uint8_t out; /* Comparator output */
uint8_t lock; /* Comparator Lock */
uint32_t csr; /* Control and status register */
#ifndef CONFIG_STM32_STM32F33XX
uint8_t mode; /* Comparator mode */
uint8_t hyst; /* Comparator hysteresis */
/* @TODO: Window mode + INP selection */
#endif
};
/************************************************************************************
* Public Function Prototypes
************************************************************************************/
@ -202,22 +185,6 @@ extern "C"
#define EXTERN extern
#endif
/****************************************************************************
* Name: stm32_compconfig
*
* Description:
* Configure comparator and used I/Os
*
* Input Parameters:
* priv - A reference to the COMP structure
*
* Returned Value:
* 0 on success, a negated errno value on failure
*
****************************************************************************/
int stm32_compconfig(FAR struct stm32_comp_s *priv);
/****************************************************************************
* Name: stm32_compinitialize
*
@ -236,41 +203,7 @@ int stm32_compconfig(FAR struct stm32_comp_s *priv);
*
****************************************************************************/
FAR struct stm32_comp_s* stm32_compinitialize(int intf);
/****************************************************************************
* Name: stm32_compenable
*
* Description:
* Enable/disable comparator
*
* Input Parameters:
* priv - A reference to the COMP structure
* enable - enable/disable flag
*
* Returned Value:
* 0 on success, a negated errno value on failure
*
****************************************************************************/
int stm32_compenable(FAR struct stm32_comp_s *priv, bool enable);
/****************************************************************************
* Name: stm32_complock
*
* Description:
* Lock comparator CSR register
*
* Input Parameters:
* priv - A reference to the COMP structure
* enable - lock flag
*
* Returned Value:
* 0 on success, a negated errno value on failure
*
****************************************************************************/
int stm32_complock(FAR struct stm32_comp_s *priv, bool lock);
FAR struct comp_dev_s* stm32_compinitialize(int intf);
#undef EXTERN
#ifdef __cplusplus

View File

@ -44,7 +44,7 @@
#include <debug.h>
#include <nuttx/board.h>
/* #include <nuttx/analog/comp.h> */
#include <nuttx/analog/comp.h>
#include "stm32.h"
@ -52,6 +52,20 @@
defined(CONFIG_STM32_COMP4) || \
defined(CONFIG_STM32_COMP6))
#ifdef CONFIG_STM32_COMP2
# if defined(CONFIG_STM32_COMP4) || defined(CONFIG_STM32_COMP6)
# error "Currently only one COMP device supported"
# endif
#elif CONFIG_STM32_COMP4
# if defined(CONFIG_STM32_COMP2) || defined(CONFIG_STM32_COMP6)
# error "Currently only one COMP device supported"
# endif
#elif CONFIG_STM32_COMP6
# if defined(CONFIG_STM32_COMP2) || defined(CONFIG_STM32_COMP4)
# error "Currently only one COMP device supported"
# endif
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -67,9 +81,8 @@
int stm32_comp_setup(void)
{
static bool initialized = false;
struct stm32_comp_s* comp = NULL;
struct comp_dev_s* comp = NULL;
int ret;
UNUSED(ret);
if (!initialized)
{
@ -100,16 +113,12 @@ int stm32_comp_setup(void)
}
#endif
#if 0
/* COMP driver not implemented yet */
ret = comp_register("/dev/comp0", comp);
if (ret < 0)
{
aerr("ERROR: comp_register failed: %d\n", ret);
return ret;
}
#endif
initialized = true;
}

View File

@ -109,6 +109,13 @@ config PGA11X_MULTIPLE
endif # if ADC_PGA11X
endif # ADC
config COMP
bool "Analog Comparator"
default n
---help---
Select to enable support for Analog Comparators (COMPs).
config DAC
bool "Digital-to-Analog Conversion"
default n

View File

@ -50,6 +50,16 @@ ifeq ($(CONFIG_DAC_AD5410),y)
endif
endif
# Check for COMP devices
ifeq ($(CONFIG_COMP),y)
# Include the common COMP character driver
CSRCS += comp.c
endif
# Check for ADC devices
ifeq ($(CONFIG_ADC),y)
@ -86,6 +96,12 @@ ifeq ($(CONFIG_ADC),y)
DEPPATH += --dep-path analog
VPATH += :analog
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)drivers$(DELIM)analog}
else
ifeq ($(CONFIG_COMP),y)
DEPPATH += --dep-path analog
VPATH += :analog
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)drivers$(DELIM)analog}
endif
endif
endif

259
drivers/analog/comp.c Normal file
View File

@ -0,0 +1,259 @@
/****************************************************************************
* drivers/analog/comp.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Mateusz Szafoni <raiden00@railab.me>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/semaphore.h>
#include <nuttx/fs/fs.h>
#include <nuttx/analog/comp.h>
#include <nuttx/irq.h>
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int comp_open(FAR struct file *filep);
static int comp_close(FAR struct file *filep);
static ssize_t comp_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static int comp_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations comp_fops =
{
comp_open, /* open */
comp_close, /* close */
comp_read, /* read */
NULL, /* write */
NULL, /* seek */
comp_ioctl /* ioctl */
#ifndef CONFIG_DISABLE_POLL
, 0
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: comp_open
*
* Description:
* This function is called whenever the COMP device is opened.
*
****************************************************************************/
static int comp_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
uint8_t tmp;
int ret = OK;
/* If the port is the middle of closing, wait until the close is finished */
if (sem_wait(&dev->ad_closesem) != OK)
{
ret = -errno;
}
else
{
/* Increment the count of references to the device. If this the first
* time that the driver has been opened for this device, then initialize
* the device.
*/
tmp = dev->ad_ocount + 1;
if (tmp == 0)
{
/* More than 255 opens; uint8_t overflows to zero */
ret = -EMFILE;
}
else
{
/* Check if this is the first time that the driver has been opened. */
if (tmp == 1)
{
/* Yes.. perform one time hardware initialization. */
irqstate_t flags = enter_critical_section();
ret = dev->ad_ops->ao_setup(dev);
if (ret == OK)
{
/* Save the new open count on success */
dev->ad_ocount = tmp;
}
leave_critical_section(flags);
}
}
sem_post(&dev->ad_closesem);
}
return ret;
}
/****************************************************************************
* Name: comp_close
*
* Description:
* This routine is called when the COMP device is closed.
* It waits for the last remaining data to be sent.
*
****************************************************************************/
static int comp_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
irqstate_t flags;
int ret = OK;
if (sem_wait(&dev->ad_closesem) != OK)
{
ret = -errno;
}
else
{
/* Decrement the references to the driver. If the reference count will
* decrement to 0, then uninitialize the driver.
*/
if (dev->ad_ocount > 1)
{
dev->ad_ocount--;
sem_post(&dev->ad_closesem);
}
else
{
/* There are no more references to the port */
dev->ad_ocount = 0;
/* Free the IRQ and disable the COMP device */
flags = enter_critical_section(); /* Disable interrupts */
dev->ad_ops->ao_shutdown(dev); /* Disable the COMP */
leave_critical_section(flags);
sem_post(&dev->ad_closesem);
}
}
return ret;
}
/****************************************************************************
* Name: comp_read
****************************************************************************/
static ssize_t comp_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
int ret;
ret = dev->ad_ops->ao_read(dev);
buffer[0] = (uint8_t)ret;
return 1;
}
/****************************************************************************
* Name: comp_ioctl
****************************************************************************/
static int comp_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
int ret;
ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: comp_register
****************************************************************************/
int comp_register(FAR const char *path, FAR struct comp_dev_s *dev)
{
int ret;
/* Initialize the COMP device structure */
dev->ad_ocount = 0;
/* Initialize semaphores */
sem_init(&dev->ad_closesem, 0, 1);
/* Register the COMP character driver */
ret = register_driver(path, &comp_fops, 0444, dev);
if (ret < 0)
{
sem_destroy(&dev->ad_closesem);
}
return ret;
}

113
include/nuttx/analog/comp.h Normal file
View File

@ -0,0 +1,113 @@
/************************************************************************************
* include/nuttx/analog/comp.h
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Mateusz Szafoni <raiden00@railab.me>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
************************************************************************************/
#ifndef __INCLUDE_NUTTX_ANALOG_COMP_H
#define __INCLUDE_NUTTX_ANALOG_COMP_H
/************************************************************************************
* Included Files
************************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <nuttx/fs/fs.h>
/************************************************************************************
* Public Types
************************************************************************************/
struct comp_dev_s;
struct comp_ops_s
{
/* Configure the COMP. This method is called the first time that the COMP
* device is opened. This will occur when the port is first opened.
* This setup includes configuring and attaching COMP interrupts. Interrupts
* are all disabled upon return.
*/
CODE int (*ao_setup)(FAR struct comp_dev_s *dev);
/* Disable the COMP. This method is called when the COMP device is closed.
* This method reverses the operation the setup method.
* Works only if COMP device is not locked.
*/
CODE void (*ao_shutdown)(FAR struct comp_dev_s *dev);
/* Read COMP output state */
CODE int (*ao_read)(FAR struct comp_dev_s *dev);
/* All ioctl calls will be routed through this method */
CODE int (*ao_ioctl)(FAR struct comp_dev_s *dev, int cmd, unsigned long arg);
};
struct comp_dev_s
{
#ifdef CONFIG_COMP
/* Fields managed by common upper half COMP logic */
uint8_t ad_ocount; /* The number of times the device has been opened */
sem_t ad_closesem; /* Locks out new opens while close is in progress */
#endif
/* Fields provided by lower half COMP logic */
FAR const struct comp_ops_s *ad_ops; /* Arch-specific operations */
FAR void *ad_priv; /* Used by the arch-specific logic */
};
/************************************************************************************
* Public Functions
************************************************************************************/
#if defined(__cplusplus)
extern "C"
{
#endif
int comp_register(FAR const char *path, FAR struct comp_dev_s *dev);
#if defined(__cplusplus)
}
#endif
#endif /* __INCLUDE_NUTTX_ANALOG_COMP_H */