Merged in raiden00/nuttx (pull request #338)
OPAMP support for STM32F33XX
This commit is contained in:
commit
1feaae7222
@ -221,6 +221,10 @@ ifeq ($(CONFIG_COMP),y)
|
||||
CHIP_CSRCS += stm32_comp.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_OPAMP),y)
|
||||
CHIP_CSRCS += stm32_opamp.c
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_STM32_1WIREDRIVER),y)
|
||||
CHIP_CSRCS += stm32_1wire.c
|
||||
endif
|
||||
|
@ -54,7 +54,7 @@
|
||||
|
||||
/* Register Addresses *******************************************************************************/
|
||||
|
||||
#define STM32_OPAMP2_CSR (STM32_OPAMP2_BASE+STM32_OPAMP2_CSR_OFFSET)
|
||||
#define STM32_OPAMP2_CSR (STM32_OPAMP_BASE+STM32_OPAMP2_CSR_OFFSET)
|
||||
|
||||
/* Register Bitfield Definitions ****************************************************/
|
||||
|
||||
@ -66,7 +66,7 @@
|
||||
#define OPAMP_CSR_VPSEL_SHIFT (3) /* Bits 2-3: OPAMP non inverting input selection */
|
||||
#define OPAMP_CSR_VPSEL_MASK (3 << OPAMP_CSR_VPSEL_SHIFT)
|
||||
/* 00: Reserved */
|
||||
# define OPAMP_CSR_VPSEL_PB13 (1 << OPAMP_CSR_VPSEL_SHIFT) /* 01: PB14 */
|
||||
# define OPAMP_CSR_VPSEL_PB14 (1 << OPAMP_CSR_VPSEL_SHIFT) /* 01: PB14 */
|
||||
# define OPAMP_CSR_VPSEL_PB0 (2 << OPAMP_CSR_VPSEL_SHIFT) /* 10: PB0 */
|
||||
# define OPAMP_CSR_VPSEL_PA7 (3 << OPAMP_CSR_VPSEL_SHIFT) /* 11: PA7 */
|
||||
/* Bit 4: Reserved */
|
||||
@ -74,7 +74,7 @@
|
||||
#define OPAMP_CSR_VMSEL_MASK (3 << OPAMP_CSR_VMSEL_SHIFT)
|
||||
# define OPAMP_CSR_VMSEL_PC5 (0 << OPAMP_CSR_VMSEL_SHIFT) /* 00: PC5 */
|
||||
# define OPAMP_CSR_VMSEL_PA5 (1 << OPAMP_CSR_VMSEL_SHIFT) /* 01: PA5 */
|
||||
# define OPAMP_CSR_VMSEL_RESISTOR (2 << OPAMP_CSR_VMSEL_SHIFT) /* 10: Resistor feedback output */
|
||||
# define OPAMP_CSR_VMSEL_PGA (2 << OPAMP_CSR_VMSEL_SHIFT) /* 10: Resistor feedback output (PGA mode)*/
|
||||
# define OPAMP_CSR_VMSEL_FOLLOWER (3 << OPAMP_CSR_VMSEL_SHIFT) /* 11: Follower mode */
|
||||
#define OPAMP_CSR_TCMEN (1 << 7) /* Bit 7: Timer controlled Mux mode enable */
|
||||
#define OPAMP_CSR_VMSSEL (1 << 8) /* Bit 8: OPAMP inverting input secondary selection */
|
||||
|
@ -337,9 +337,10 @@
|
||||
#define GPIO_OPAMP2_VINM_1 (GPIO_ANALOG|GPIO_PORTA|GPIO_PIN5)
|
||||
#define GPIO_OPAMP2_VINM_2 (GPIO_ANALOG|GPIO_PORTC|GPIO_PIN5)
|
||||
#define GPIO_OPAMP2_VOUT (GPIO_ANALOG|GPIO_PORTA|GPIO_PIN6)
|
||||
#define GPIO_OPAMP2_VINP_1 (GPIO_ANALOG|GPIO_PORTA|GPIO_PIN7)
|
||||
#define GPIO_OPAMP2_VINP_2 (GPIO_ANALOG|GPIO_PORTB|GPIO_PIN0)
|
||||
#define GPIO_OPAMP2_VINP_3 (GPIO_ANALOG|GPIO_PORTB|GPIO_PIN14)
|
||||
#undef GPIO_OPAMP2_VINP_1 /* not supported in F33XX */
|
||||
#define GPIO_OPAMP2_VINP_2 (GPIO_ANALOG|GPIO_PORTA|GPIO_PIN7)
|
||||
#define GPIO_OPAMP2_VINP_3 (GPIO_ANALOG|GPIO_PORTB|GPIO_PIN0)
|
||||
#define GPIO_OPAMP2_VINP_4 (GPIO_ANALOG|GPIO_PORTB|GPIO_PIN14)
|
||||
|
||||
/* TSC */
|
||||
|
||||
|
@ -69,6 +69,7 @@
|
||||
#include "stm32_gpio.h"
|
||||
#include "stm32_i2c.h"
|
||||
#include "stm32_ltdc.h"
|
||||
#include "stm32_opamp.h"
|
||||
#include "stm32_pwr.h"
|
||||
#include "stm32_rcc.h"
|
||||
#include "stm32_rtc.h"
|
||||
|
1418
arch/arm/src/stm32/stm32_opamp.c
Normal file
1418
arch/arm/src/stm32/stm32_opamp.c
Normal file
File diff suppressed because it is too large
Load Diff
231
arch/arm/src/stm32/stm32_opamp.h
Normal file
231
arch/arm/src/stm32/stm32_opamp.h
Normal file
@ -0,0 +1,231 @@
|
||||
/************************************************************************************
|
||||
* arch/arm/src/stm32/stm32_opamp.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 __ARCH_ARM_SRC_STM32_STM32_OPAMP_H
|
||||
#define __ARCH_ARM_SRC_STM32_STM32_OPAMP_H
|
||||
|
||||
/************************************************************************************
|
||||
* Included Files
|
||||
************************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include "chip.h"
|
||||
|
||||
#ifdef CONFIG_STM32_OPAMP
|
||||
|
||||
#if defined(CONFIG_STM32_STM32F30XX)
|
||||
# error "OPAMP support for STM32F30XX not implemented yet"
|
||||
#elif defined(CONFIG_STM32_STM32F33XX)
|
||||
# include "chip/stm32f33xxx_opamp.h"
|
||||
#endif
|
||||
|
||||
#include <nuttx/analog/opamp.h>
|
||||
|
||||
/************************************************************************************
|
||||
* Pre-processor definitions
|
||||
************************************************************************************/
|
||||
|
||||
/* OPAMP operation mode */
|
||||
|
||||
#define OPAMP_MODE_STANDALONE 0
|
||||
#define OPAMP_MODE_FOLLOWER 1
|
||||
#define OPAMP_MODE_PGA 2
|
||||
|
||||
/* Timer controlled Mux mode */
|
||||
|
||||
#define OPAMP_MUX_DISABLE 0
|
||||
#define OPAMP_MUX_ENABLE 1
|
||||
|
||||
/* User callibration */
|
||||
|
||||
#define OPAMP_USERCAL_DISABLE 0
|
||||
#define OPAMP_USERCAL_ENABLE 1
|
||||
|
||||
/* Default configuration */
|
||||
|
||||
#define OPAMP_MODE_DEFAULT OPAMP_MODE_STANDALONE /* Standalone mode */
|
||||
#define OPAMP_MUX_DEFAULT OPAMP_MUX_DISABLE /* MUX disabled */
|
||||
#define OPAMP_USERCAL_DEFAULT OPAMP_USERCAL_DISABLE /* User calibration disabled */
|
||||
#define OPAMP_GAIN_DEFAULT OPAMP_GAIN_2 /* Gain in PGA mode = 2 */
|
||||
#define OPAMP_LOCK_DEFAULT OPAMP_LOCK_RW /* Do not lock CSR register */
|
||||
|
||||
/************************************************************************************
|
||||
* Public Types
|
||||
************************************************************************************/
|
||||
|
||||
/* CSR register lock state */
|
||||
|
||||
enum stm32_opamp_lock_e
|
||||
{
|
||||
OPAMP_LOCK_RW,
|
||||
OPAMP_LOCK_RO
|
||||
};
|
||||
|
||||
/* Gain in PGA mode */
|
||||
|
||||
enum stm32_opamp_gain_e
|
||||
{
|
||||
OPAMP_GAIN_2,
|
||||
OPAMP_GAIN_4,
|
||||
OPAMP_GAIN_8,
|
||||
OPAMP_GAIN_2_VM0,
|
||||
OPAMP_GAIN_4_VM0,
|
||||
OPAMP_GAIN_8_VM0,
|
||||
OPAMP_GAIN_16_VM0,
|
||||
OPAMP_GAIN_2_VM1,
|
||||
OPAMP_GAIN_4_VM1,
|
||||
OPAMP_GAIN_8_VM1,
|
||||
OPAMP_GAIN_16_VM1
|
||||
};
|
||||
|
||||
/* Input selection and secondary input selection use the same GPIOs */
|
||||
|
||||
#ifdef CONFIG_STM32_OPAMP1
|
||||
enum stm32_opamp1_vpsel_e
|
||||
{
|
||||
OPAMP1_VPSEL_PA7,
|
||||
OPAMP1_VPSEL_PA5,
|
||||
OPAMP1_VPSEL_PA3,
|
||||
OPAMP1_VPSEL_PA1
|
||||
};
|
||||
|
||||
enum stm32_opamp1_vmsel_e
|
||||
{
|
||||
OPAMP1_VMSEL_PC5,
|
||||
OPAMP1_VMSEL_PA3,
|
||||
OPAMP1_VMSEL_PGAMODE,
|
||||
OPAMP1_VMSEL_FOLLOWER,
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_STM32_OPAMP2
|
||||
enum stm32_opamp2_vpsel_e
|
||||
{
|
||||
#ifndef CONFIG_STM32_STM32F33XX
|
||||
/* TODO: STM32F303xB/C and STM32F358C devices only */
|
||||
OPAMP2_VPSEL_PD14,
|
||||
#endif
|
||||
OPAMP2_VPSEL_PB14,
|
||||
OPAMP2_VPSEL_PB0,
|
||||
OPAMP2_VPSEL_PA7
|
||||
};
|
||||
|
||||
enum stm32_opamp2_vmsel_e
|
||||
{
|
||||
OPAMP2_VMSEL_PC5,
|
||||
OPAMP2_VMSEL_PA5,
|
||||
OPAMP2_VMSEL_PGAMODE,
|
||||
OPAMP2_VMSEL_FOLLOWER
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_STM32_OPAMP3
|
||||
enum stm32_opamp3_vpsel_e
|
||||
{
|
||||
OPAMP3_VPSEL_PB13,
|
||||
OPAMP3_VPSEL_PA5,
|
||||
OPAMP3_VPSEL_PA1,
|
||||
OPAMP3_VPSEL_PB0
|
||||
};
|
||||
|
||||
enum stm32_opamp3_vmsel_e
|
||||
{
|
||||
OPAMP3_VMSEL_PB10,
|
||||
OPAMP3_VMSEL_PB2,
|
||||
OPAMP3_VMSEL_PGAMODE,
|
||||
OPAMP3_VMSEL_FOLLOWER
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_STM32_OPAMP4
|
||||
enum stm32_opamp4_vpsel_e
|
||||
{
|
||||
OPAMP4_VPSEL_PD11,
|
||||
OPAMP4_VPSEL_PB11,
|
||||
OPAMP4_VPSEL_PA4,
|
||||
OPAMP4_VPSEL_PB13
|
||||
};
|
||||
|
||||
enum stm32_opamp4_vmsel_e
|
||||
{
|
||||
OPAMP4_VMSEL_PB10,
|
||||
OPAMP4_VMSEL_PD8,
|
||||
OPAMP4_VMSEL_PGAMODE,
|
||||
OPAMP4_VMSEL_FOLLOWER
|
||||
};
|
||||
#endif
|
||||
|
||||
/************************************************************************************
|
||||
* Public Function Prototypes
|
||||
************************************************************************************/
|
||||
|
||||
#ifndef __ASSEMBLY__
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stm32_opampinitialize
|
||||
*
|
||||
* Description:
|
||||
* Initialize the OPAMP.
|
||||
*
|
||||
* Input Parameters:
|
||||
* intf - The OPAMP interface number.
|
||||
*
|
||||
* Returned Value:
|
||||
* Valid OPAMP device structure reference on succcess; a NULL on failure.
|
||||
*
|
||||
* Assumptions:
|
||||
* 1. Clock to the OPAMP block has enabled,
|
||||
* 2. Board-specific logic has already configured
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct opamp_dev_s* stm32_opampinitialize(int intf);
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* __ASSEMBLY__ */
|
||||
|
||||
#endif /* CONFIG_STM32_OPAMP */
|
||||
#endif /* __ARCH_ARM_SRC_STM32_STM32_OPAMP_H */
|
@ -237,8 +237,14 @@
|
||||
#define GPIO_USART1_RX GPIO_USART1_RX_1 /* PA10 */
|
||||
#define GPIO_USART1_TX GPIO_USART1_TX_1 /* PA9 */
|
||||
|
||||
/* HRTIM */
|
||||
/* COMP */
|
||||
|
||||
/* OPAMP */
|
||||
|
||||
#define OPAMP2_VMSEL OPAMP2_VMSEL_PC5
|
||||
#define OPAMP2_VPSEL OPAMP2_VPSEL_PB14
|
||||
|
||||
/* HRTIM */
|
||||
|
||||
/* DMA channels *************************************************************/
|
||||
/* ADC */
|
||||
|
@ -202,4 +202,16 @@ int stm32_can_setup(void);
|
||||
int stm32_comp_setup(void);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stm32_opamp_setup
|
||||
*
|
||||
* Description:
|
||||
* Initialize OPAMP peripheral for the board.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_OPAMP
|
||||
int stm32_opamp_setup(void);
|
||||
#endif
|
||||
|
||||
#endif /* __CONFIGS_NUCLEO_F334R8_SRC_NUCLEO_F334R8_H */
|
||||
|
@ -137,6 +137,16 @@ int board_app_initialize(uintptr_t arg)
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_OPAMP
|
||||
/* Initialize OPAMP and register the OPAMP driver. */
|
||||
|
||||
ret = stm32_opamp_setup();
|
||||
if (ret < 0)
|
||||
{
|
||||
syslog(LOG_ERR, "ERROR: stm32_opamp_setup failed: %d\n", ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
UNUSED(ret);
|
||||
return OK;
|
||||
}
|
||||
|
@ -0,0 +1,99 @@
|
||||
/****************************************************************************
|
||||
* configs/nucleo-f334r8/src/stm32_opamp.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 <stdbool.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/board.h>
|
||||
#include <nuttx/analog/opamp.h>
|
||||
|
||||
#include "stm32.h"
|
||||
|
||||
#if defined(CONFIG_OPAMP) && defined(CONFIG_STM32_OPAMP2)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: stm32_opamp_setup
|
||||
*
|
||||
* Description:
|
||||
* Initialize OPAMP
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int stm32_opamp_setup(void)
|
||||
{
|
||||
static bool initialized = false;
|
||||
struct opamp_dev_s* opamp = NULL;
|
||||
int ret;
|
||||
|
||||
if (!initialized)
|
||||
{
|
||||
/* Get the OPAMP interface */
|
||||
|
||||
#ifdef CONFIG_STM32_OPAMP2
|
||||
opamp = stm32_opampinitialize(2);
|
||||
if (opamp == NULL)
|
||||
{
|
||||
aerr("ERROR: Failed to get OPAMP%d interface\n", 2);
|
||||
return -ENODEV;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Register the OPAMP character driver at /dev/opamp0 */
|
||||
|
||||
ret = opamp_register("/dev/opamp0", opamp);
|
||||
if (ret < 0)
|
||||
{
|
||||
aerr("ERROR: opamp_register failed: %d\n", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_OPAMP && CONFIG_STM32_OPAMP2 */
|
@ -141,3 +141,9 @@ config DAC_AD5410
|
||||
select SPI
|
||||
|
||||
endif # DAC
|
||||
|
||||
config OPAMP
|
||||
bool "Operational Amplifier"
|
||||
default n
|
||||
---help---
|
||||
Select to enable support for Operational Amplifiers (OPAMPs).
|
||||
|
@ -60,6 +60,16 @@ CSRCS += comp.c
|
||||
|
||||
endif
|
||||
|
||||
# Check for OPAMP devices
|
||||
|
||||
ifeq ($(CONFIG_OPAMP),y)
|
||||
|
||||
# Include the common OPAMP character driver
|
||||
|
||||
CSRCS += opamp.c
|
||||
|
||||
endif
|
||||
|
||||
# Check for ADC devices
|
||||
|
||||
ifeq ($(CONFIG_ADC),y)
|
||||
@ -105,6 +115,13 @@ ifeq ($(CONFIG_COMP),y)
|
||||
DEPPATH += --dep-path analog
|
||||
VPATH += :analog
|
||||
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)drivers$(DELIM)analog}
|
||||
else
|
||||
ifeq ($(CONFIG_OPAMP),y)
|
||||
DEPPATH += --dep-path analog
|
||||
VPATH += :analog
|
||||
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)drivers$(DELIM)analog}
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
|
243
drivers/analog/opamp.c
Normal file
243
drivers/analog/opamp.c
Normal file
@ -0,0 +1,243 @@
|
||||
/****************************************************************************
|
||||
* drivers/analog/opamp.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/opamp.h>
|
||||
|
||||
#include <nuttx/irq.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static int opamp_open(FAR struct file *filep);
|
||||
static int opamp_close(FAR struct file *filep);
|
||||
static int opamp_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const struct file_operations opamp_fops =
|
||||
{
|
||||
opamp_open, /* open */
|
||||
opamp_close, /* close */
|
||||
NULL, /* read */
|
||||
NULL, /* write */
|
||||
NULL, /* seek */
|
||||
opamp_ioctl /* ioctl */
|
||||
#ifndef CONFIG_DISABLE_POLL
|
||||
, NULL /* poll */
|
||||
#endif
|
||||
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
||||
, NULL /* unlink */
|
||||
#endif
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
/****************************************************************************
|
||||
* Name: opamp_open
|
||||
*
|
||||
* Description:
|
||||
* This function is called whenever the OPAMP device is opened.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int opamp_open(FAR struct file *filep)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct opamp_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: opamp_close
|
||||
*
|
||||
* Description:
|
||||
* This routine is called when the OPAMP device is closed.
|
||||
* It waits for the last remaining data to be sent.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int opamp_close(FAR struct file *filep)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct opamp_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 OPAMP device */
|
||||
|
||||
flags = enter_critical_section(); /* Disable interrupts */
|
||||
dev->ad_ops->ao_shutdown(dev); /* Disable the OPAMP */
|
||||
leave_critical_section(flags);
|
||||
|
||||
sem_post(&dev->ad_closesem);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: opamp_ioctl
|
||||
****************************************************************************/
|
||||
|
||||
static int opamp_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
||||
{
|
||||
FAR struct inode *inode = filep->f_inode;
|
||||
FAR struct opamp_dev_s *dev = inode->i_private;
|
||||
int ret;
|
||||
|
||||
ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: opamp_register
|
||||
****************************************************************************/
|
||||
|
||||
int opamp_register(FAR const char *path, FAR struct opamp_dev_s *dev)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* Initialize the OPAMP device structure */
|
||||
|
||||
dev->ad_ocount = 0;
|
||||
|
||||
/* Initialize semaphores */
|
||||
|
||||
sem_init(&dev->ad_closesem, 0, 1);
|
||||
|
||||
/* Register the OPAMP character driver */
|
||||
|
||||
ret = register_driver(path, &opamp_fops, 0444, dev);
|
||||
if (ret < 0)
|
||||
{
|
||||
sem_destroy(&dev->ad_closesem);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
109
include/nuttx/analog/opamp.h
Normal file
109
include/nuttx/analog/opamp.h
Normal file
@ -0,0 +1,109 @@
|
||||
/************************************************************************************
|
||||
* include/nuttx/analog/opamp.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_OPAMP_H
|
||||
#define __INCLUDE_NUTTX_ANALOG_OPAMP_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 opamp_dev_s;
|
||||
struct opamp_ops_s
|
||||
{
|
||||
/* Configure the OPAMP. This method is called the first time that the OPAMP
|
||||
* device is opened. This will occur when the port is first opened.
|
||||
* This setup includes configuring and attaching OPAMP interrupts. Interrupts
|
||||
* are all disabled upon return.
|
||||
*/
|
||||
|
||||
CODE int (*ao_setup)(FAR struct opamp_dev_s *dev);
|
||||
|
||||
/* Disable the OPAMP. This method is called when the OPAMP device is closed.
|
||||
* This method reverses the operation the setup method.
|
||||
* Works only if OPAMP device is not locked.
|
||||
*/
|
||||
|
||||
CODE void (*ao_shutdown)(FAR struct opamp_dev_s *dev);
|
||||
|
||||
/* All ioctl calls will be routed through this method */
|
||||
|
||||
CODE int (*ao_ioctl)(FAR struct opamp_dev_s *dev, int cmd, unsigned long arg);
|
||||
};
|
||||
|
||||
struct opamp_dev_s
|
||||
{
|
||||
#ifdef CONFIG_OPAMP
|
||||
/* Fields managed by common upper half OPAMP 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 OPAMP logic */
|
||||
|
||||
FAR const struct opamp_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 opamp_register(FAR const char *path, FAR struct opamp_dev_s *dev);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __INCLUDE_NUTTX_ANALOG_OPAMP_H */
|
Loading…
Reference in New Issue
Block a user