Merged in raiden00/nuttx (pull request #281)

STM32F3: Add COMP support

Approved-by: Gregory Nutt <gnutt@nuttx.org>
This commit is contained in:
Mateusz Szafoni
2017-03-19 18:55:06 +00:00
committed by Gregory Nutt
8 changed files with 1303 additions and 16 deletions
+4
View File
@@ -217,6 +217,10 @@ ifeq ($(CONFIG_DAC),y)
CHIP_CSRCS += stm32_dac.c
endif
ifeq ($(CONFIG_COMP),y)
CHIP_CSRCS += stm32_comp.c
endif
ifeq ($(CONFIG_STM32_1WIREDRIVER),y)
CHIP_CSRCS += stm32_1wire.c
endif
+17 -16
View File
@@ -72,7 +72,8 @@
# define COMP_CSR_INMSEL_1P2VREF (1 << COMP_CSR_INMSEL_SHIFT) /* 0001: 1/2 of Vrefint */
# define COMP_CSR_INMSEL_3P4VREF (2 << COMP_CSR_INMSEL_SHIFT) /* 0010: 3/4 of Vrefint */
# define COMP_CSR_INMSEL_VREF (3 << COMP_CSR_INMSEL_SHIFT) /* 0011: Vrefint */
# define COMP_CSR_INMSEL_PA4 (4 << COMP_CSR_INMSEL_SHIFT) /* 0100: PA4 or DAC1_CH output if enabled */
# define COMP_CSR_INMSEL_PA4 (4 << COMP_CSR_INMSEL_SHIFT) /* 0100: PA4 or */
# define COMP_CSR_INMSEL_DAC1CH1 (4 << COMP_CSR_INMSEL_SHIFT) /* 0100: DAC1_CH1 output if enabled */
# define COMP_CSR_INMSEL_DAC1CH2 (5 << COMP_CSR_INMSEL_SHIFT) /* 0101: DAC1_CH2 output */
# define COMP_CSR_INMSEL_PA2 (6 << COMP_CSR_INMSEL_SHIFT) /* 0110: PA2 (COMP2 only) */
# define COMP_CSR_INMSEL_PB2 (7 << COMP4_CSR_INMSEL_SHIFT) /* 0111: PB2 (COMP4 only) */
@@ -87,32 +88,32 @@
/* 0011: Reserved */
/* 0100: Reserved */
# define COMP_CSR_OUTSEL_BRK2_ (5 << COMP_CSR_INMSEL_SHIFT) /* 0101: Timer 1 break input2 */
# define COMP_CSR_OUTSEL_T1OCCC (6 << COMP_CSR_INMSEL_SHIFT) /* 0110: Timer 1 OCREF_CLR input (COMP2 only) */
# define COMP_CSR_OUTSEL_T1OCC (6 << COMP_CSR_INMSEL_SHIFT) /* 0110: Timer 1 OCREF_CLR input (COMP2 only) */
# define COMP_CSR_OUTSEL_T3CAP3 (6 << COMP_CSR_INMSEL_SHIFT) /* 0110: Timer 3 input apture 3 (COMP4 only) */
# define COMP_CSR_OUTSEL_T2CAP2 (6 << COMP_CSR_INMSEL_SHIFT) /* 0110: Timer 2 input apture 2 (COMP6 only) */
# define COMP_CSR_OUTSEL_T1CAP1 (7 << COMP_CSR_INMSEL_SHIFT) /* 0111: Timer 1 input capture 1 (COMP2 only) */
# define COMP_CSR_OUTSEL_T2CAP4 (8 << COMP_CSR_INMSEL_SHIFT) /* 1000: Timer 2 input capture 4 (COMP2 only) */
# define COMP_CSR_OUTSEL_T15CAP2 (8 << COMP_CSR_INMSEL_SHIFT) /* 1000: Timer 15 input capture 2 (COMP4 only) */
# define COMP_CSR_OUTSEL_T2OCC (8 << COMP_CSR_INMSEL_SHIFT) /* 1000: Timer 2 OCREF CLR input (COMP6 only) */
# define COMP_CSR_OUTSEL_T2OCC (9 << COMP_CSR_INMSEL_SHIFT) /* 1001: Timer 2 OCREF_CLR input (COMP2 only) */
# define COMP6_CSR_OUTSEL_T2OCC (8 << COMP_CSR_INMSEL_SHIFT) /* 1000: Timer 2 OCREF CLR input (COMP6 only) */
# define COMP2_CSR_OUTSEL_T2OCC (9 << COMP_CSR_INMSEL_SHIFT) /* 1001: Timer 2 OCREF_CLR input (COMP2 only) */
# define COMP_CSR_OUTSEL_T16OCC (9 << COMP_CSR_INMSEL_SHIFT) /* 1001: Timer 16 OCREF_CLR input (COMP6 only) */
# define COMP_CSR_OUTSEL_T3CAP1 (10 << COMP_CSR_INMSEL_SHIFT) /* 1010: Timer 3 input capture 1 (COMP2 only) */
# define COMP_CSR_OUTSEL_T3CAP1 (10 << COMP_CSR_INMSEL_SHIFT) /* 1010: Timer 15 OCREF_CLR input (COMP4 only) */
# define COMP_CSR_OUTSEL_T3CAP1 (10 << COMP_CSR_INMSEL_SHIFT) /* 1010: Timer 16 input capture 1 (COMP6 only) */
# define COMP_CSR_OUTSEL_T15OCC (10 << COMP_CSR_INMSEL_SHIFT) /* 1010: Timer 15 OCREF_CLR input (COMP4 only) */
# define COMP_CSR_OUTSEL_T16CAP1 (10 << COMP_CSR_INMSEL_SHIFT) /* 1010: Timer 16 input capture 1 (COMP6 only) */
# define COMP_CSR_OUTSEL_T3OCC (11 << COMP_CSR_INMSEL_SHIFT) /* 1011: Timer 3 OCREF_CLR input (COMP2,COMP4 only) */
/* Bit 14: Reserved */
#define COMP_CSR_POL (1 << 15) /* Bit 15: comparator output polarity */
/* Bits 16-17: Reserved */
#define COMP_CSR_BLANCKING_SHIFT (18) /* Bit 18-20: comparator output blanking source */
#define COMP_CSR_BLANCKING_MASK (7 << COMP_CSR_BLANCKING_SHIFT)
# define COMP_CSR_BLANCKING_DIS (0 << COMP_CSR_BLANCKING_SHIFT) /* 000: No blanking */
# define COMP_CSR_BLANCKING_T1OC5 (1 << COMP_CSR_BLANCKING_SHIFT) /* 001: TIM1 OC5 as blanking source (COMP2 only) */
# define COMP_CSR_BLANCKING_T3OC4 (1 << COMP_CSR_BLANCKING_SHIFT) /* 001: TIM3 OC4 as blanking source (COMP4 only) */
# define COMP_CSR_BLANCKING_T2OC3 (2 << COMP_CSR_BLANCKING_SHIFT) /* 010: TIM2 OC3 as blanking source (COMP2 only) */
# define COMP_CSR_BLANCKING_T3OC3 (3 << COMP_CSR_BLANCKING_SHIFT) /* 011: TIM3 OC3 as blanking source (COMP2 only) */
# define COMP_CSR_BLANCKING_T15OC1 (3 << COMP_CSR_BLANCKING_SHIFT) /* 011: TIM15 OC1 as blanking source (COMP4 only) */
# define COMP_CSR_BLANCKING_T2OC4 (3 << COMP_CSR_BLANCKING_SHIFT) /* 011: TIM2 OC4 as blanking source (COMP6 only) */
# define COMP_CSR_BLANCKING_T15OC2 (4 << COMP_CSR_BLANCKING_SHIFT) /* 011: TIM15 OC2 as blanking source (COMP6 only) */
#define COMP_CSR_BLANKING_SHIFT (18) /* Bit 18-20: comparator output blanking source */
#define COMP_CSR_BLANKING_MASK (7 << COMP_CSR_BLANKING_SHIFT)
# define COMP_CSR_BLANKING_DIS (0 << COMP_CSR_BLANKING_SHIFT) /* 000: No blanking */
# define COMP_CSR_BLANKING_T1OC5 (1 << COMP_CSR_BLANKING_SHIFT) /* 001: TIM1 OC5 as blanking source (COMP2 only) */
# define COMP_CSR_BLANKING_T3OC4 (1 << COMP_CSR_BLANKING_SHIFT) /* 001: TIM3 OC4 as blanking source (COMP4 only) */
# define COMP_CSR_BLANKING_T2OC3 (2 << COMP_CSR_BLANKING_SHIFT) /* 010: TIM2 OC3 as blanking source (COMP2 only) */
# define COMP_CSR_BLANKING_T3OC3 (3 << COMP_CSR_BLANKING_SHIFT) /* 011: TIM3 OC3 as blanking source (COMP2 only) */
# define COMP_CSR_BLANKING_T15OC1 (3 << COMP_CSR_BLANKING_SHIFT) /* 011: TIM15 OC1 as blanking source (COMP4 only) */
# define COMP_CSR_BLANKING_T2OC4 (3 << COMP_CSR_BLANKING_SHIFT) /* 011: TIM2 OC4 as blanking source (COMP6 only) */
# define COMP_CSR_BLANKING_T15OC2 (4 << COMP_CSR_BLANKING_SHIFT) /* 011: TIM15 OC2 as blanking source (COMP6 only) */
/* Bit 21: Reserved */
#define COMP_CSR_INMSEL_DAC2CH1 (1 << 22) /* Bit 22: used with bits 4-6, DAC2_CH1 output */
/* Bits 23-29: Reserved */
+1
View File
@@ -59,6 +59,7 @@
#include "stm32_adc.h"
//#include "stm32_bkp.h"
#include "stm32_can.h"
#include "stm32_comp.h"
#include "stm32_dbgmcu.h"
#include "stm32_dma.h"
#include "stm32_dac.h"
File diff suppressed because it is too large Load Diff
+281
View File
@@ -0,0 +1,281 @@
/************************************************************************************
* arch/arm/src/stm32/stm32_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 __ARCH_ARM_SRC_STM32_STM32_COMP_H
#define __ARCH_ARM_SRC_STM32_STM32_COMP_H
/************************************************************************************
* Included Files
************************************************************************************/
#include <nuttx/config.h>
#include "chip.h"
#if defined(CONFIG_STM32_STM32F30XX)
# error "COMP support for STM32F30XX not implemented yet"
#elif defined(CONFIG_STM32_STM32F33XX)
# include "chip/stm32f33xxx_comp.h"
#elif defined(CONFIG_STM32_STM32F37XX)
# error "COMP support for STM32F37XX ot implemented yet"
#endif
/************************************************************************************
* Pre-processor definitions
************************************************************************************/
#define COMP_BLANKING_DEFAULT COMP_BLANKING_DIS /* No blanking */
#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 */
#ifndef CONFIG_STM32_STM32F33XX
#define COMP_MODE_DEFAULT
#define COMP_HYST_DEFAULT
#define COMP_WINMODE_DEFAULT
#endif
/************************************************************************************
* Public Types
************************************************************************************/
/* Blanking source */
enum stm32_comp_blanking_e
{
COMP_BLANKING_DIS,
#if defined(CONFIG_STM32_STM32F33XX)
COMP_BLANKING_T1OC5,
COMP_BLANKING_T3OC4,
COMP_BLANKING_T2OC3,
COMP_BLANKING_T3OC3,
COMP_BLANKING_T15OC1,
COMP_BLANKING_T2OC4,
COMP_BLANKING_T15OC2,
#endif
};
/* Output polarisation */
enum stm32_comp_pol_e
{
COMP_POL_NONINVERT,
COMP_POL_INVERTED
};
/* Inverting input */
enum stm32_comp_inm_e
{
COMP_INMSEL_1P4VREF,
COMP_INMSEL_1P2VREF,
COMP_INMSEL_3P4VREF,
COMP_INMSEL_VREF,
COMP_INMSEL_DAC1CH1,
COMP_INMSEL_DAC1CH2,
COMP_INMSEL_PIN
};
/* Output selection */
enum stm32_comp_outsel_e
{
COMP_OUTSEL_NOSEL,
#if defined(CONFIG_STM32_STM32F33XX)
COMP_OUTSEL_BRKACTH,
COMP_OUTSEL_BRK2,
COMP_OUTSEL_T1OCC, /* COMP2 only */
COMP_OUTSEL_T3CAP3, /* COMP4 only */
COMP_OUTSEL_T2CAP2, /* COMP6 only */
COMP_OUTSEL_T1CAP1, /* COMP2 only */
COMP_OUTSEL_T2CAP4, /* COMP2 only */
COMP_OUTSEL_T15CAP2, /* COMP4 only */
COMP_OUTSEL_T2OCC, /* COMP6 only */
COMP_OUTSEL_T16OCC, /* COMP2 only */
COMP_OUTSEL_T3CAP1, /* COMP2 only */
COMP_OUTSEL_T15OCC, /* COMP4 only */
COMP_OUTSEL_T16CAP1, /* COMP6 only */
COMP_OUTSEL_T3OCC, /* COMP2 and COMP4 only */
#endif
};
/* CSR register lock state */
enum stm32_comp_lock_e
{
COMP_LOCK_RW,
COMP_LOCK_RO
};
#ifndef CONFIG_STM32_STM32F33XX
/* Hysteresis */
enum stm32_comp_hyst_e
{
COMP_HYST_DIS,
COMP_HYST_LOW,
COMP_HYST_MEDIUM,
COMP_HYST_HIGH
},
/* Power/Speed Modes */
enum stm32_comp_mode_e
{
COMP_MODE_HIGHSPEED,
COMP_MODE_MEDIUMSPEED,
COMP_MODE_LOWPOWER,
COMP_MODE_ULTRALOWPOWER
};
/* Window mode */
enum stm32_comp_winmode_e
{
COMP_WINMODE_DIS,
COMP_WINMODE_EN
};
#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
************************************************************************************/
#ifndef __ASSEMBLY__
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#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
*
* 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);
/****************************************************************************
* 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);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_ARM_SRC_STM32_STM32_COMP_H */
+12
View File
@@ -190,4 +190,16 @@ int stm32_adc_setup(void);
int stm32_can_setup(void);
#endif
/****************************************************************************
* Name: stm32_comp_setup
*
* Description:
* Initialize COMP peripheral for the board.
*
****************************************************************************/
#ifdef CONFIG_COMP
int stm32_comp_setup(void);
#endif
#endif /* __CONFIGS_NUCLEO_F334R8_SRC_NUCLEO_F334R8_H */
+10
View File
@@ -127,6 +127,16 @@ int board_app_initialize(uintptr_t arg)
}
#endif
#ifdef CONFIG_COMP
/* Initialize COMP and register the COMP driver. */
ret = stm32_comp_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: stm32_comp_setup failed: %d\n", ret);
}
#endif
UNUSED(ret);
return OK;
}
+140
View File
@@ -0,0 +1,140 @@
/****************************************************************************
* configs/nucleo-f334r8/src/stm32_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 <stdbool.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/board.h>
/* #include <nuttx/analog/comp.h> */
#include "stm32.h"
#if defined(CONFIG_COMP) && (defined(CONFIG_STM32_COMP2) || \
defined(CONFIG_STM32_COMP4) || \
defined(CONFIG_STM32_COMP6))
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_comp_setup
*
* Description:
* Initialize COMP
*
****************************************************************************/
int stm32_comp_setup(void)
{
static bool initialized = false;
struct stm32_comp_s* comp = NULL;
int ret;
UNUSED(ret);
if (!initialized)
{
#ifdef CONFIG_STM32_COMP2
comp = stm32_compinitialize(2);
if (comp == NULL)
{
aerr("ERROR: Failed to get COMP%d interface\n", 2);
return -ENODEV;
}
#endif
#ifdef CONFIG_STM32_COMP4
comp = stm32_compinitialize(4);
if (comp == NULL)
{
aerr("ERROR: Failed to get COMP%d interface\n", 4);
return -ENODEV;
}
#endif
#ifdef CONFIG_STM32_COMP6
comp = stm32_compinitialize(6);
if (comp == NULL)
{
aerr("ERROR: Failed to get COMP%d interface\n", 6);
return -ENODEV;
}
#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;
}
return OK;
}
#endif /* CONFIG_COMP && (CONFIG_STM32_COMP1 ||
* CONFIG_STM32_COMP2
* CONFIG_STM32_COMP6) */