We have used this comparator patch to add comparator / trigger support to the stm32f37x.
Your feed back is appreciated.
How do I attach a patch file? I am getting The extension " " is not allowed for everything I have tried.
From 159def054f0752b7e13d28278cb939aa50d17d3c Mon Sep 17 00:00:00 2001
From: Timothy Webster <[email protected]>
Date: Sun, 1 Dec 2013 20:57:45 -0500
Subject: [PATCH] Analog Comparator
Internal reference and submultiple values 1/4 1/2 3/4
Programable hysteresis
Output to Timer and COMP_OUT
---
os/hal/hal.mk | 1 +
os/hal/include/comp.h | 101 ++++++++
os/hal/include/hal.h | 1 +
os/hal/platforms/STM32/comp_lld.c | 220 ++++++++++++++++++
os/hal/platforms/STM32/comp_lld.h | 344 ++++++++++++++++++++++++++++
os/hal/platforms/STM32F37x/platform.mk | 1 +
os/hal/platforms/STM32F37x/stm32_registry.h | 3 +
os/hal/src/comp.c | 121 ++++++++++
os/hal/src/hal.c | 3 +
9 files changed, 795 insertions(+)
create mode 100644 os/hal/include/comp.h
create mode 100644 os/hal/platforms/STM32/comp_lld.c
create mode 100644 os/hal/platforms/STM32/comp_lld.h
create mode 100644 os/hal/src/comp.c
diff --git a/os/hal/hal.mk b/os/hal/hal.mk
index 8b9d757..45e8b64 100644
--- a/os/hal/hal.mk
+++ b/os/hal/hal.mk
@@ -3,6 +3,7 @@
HALSRC = ${CHIBIOS}/os/hal/src/hal.c \
${CHIBIOS}/os/hal/src/adc.c \
${CHIBIOS}/os/hal/src/can.c \
+ ${CHIBIOS}/os/hal/src/comp.c \
${CHIBIOS}/os/hal/src/dac.c \
${CHIBIOS}/os/hal/src/ext.c \
${CHIBIOS}/os/hal/src/gpt.c \
diff --git a/os/hal/include/comp.h b/os/hal/include/comp.h
new file mode 100644
index 0000000..ef177e3
--- /dev/null
+++ b/os/hal/include/comp.h
@@ -0,0 +1,101 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
+ 2011,2012,2013 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file comp.h
+ * @brief COMP Driver macros and structures.
+ *
+ * @addtogroup COMP
+ * @{
+ */
+
+#ifndef _COMP_H_
+#define _COMP_H_
+
+#if HAL_USE_COMP || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Driver state machine possible states.
+ */
+typedef enum {
+ COMP_UNINIT = 0, /**< Not initialized. */
+ COMP_STOP = 1, /**< Stopped. */
+ COMP_ACTIVE = 2, /**< Active. */
+} compstate_t;
+
+/**
+ * @brief Type of a structure representing an COMP driver.
+ */
+typedef struct COMPDriver COMPDriver;
+
+#include "comp_lld.h"
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @name Macro Functions
+ * @{
+ */
+/** @} */
+
+/**
+ * @name Low Level driver helper macros
+ * @{
+ */
+/** @} */
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void compInit(void);
+ void compObjectInit(COMPDriver *compp);
+ void compStart(COMPDriver *compp, const COMPConfig *config);
+ void compStop(COMPDriver *compp);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_COMP */
+
+#endif /* _COMP_H_ */
+
+/** @} */
diff --git a/os/hal/include/hal.h b/os/hal/include/hal.h
index ceff2e0..ad9a83d 100644
--- a/os/hal/include/hal.h
+++ b/os/hal/include/hal.h
@@ -60,6 +60,7 @@
#include "spi.h"
#include "uart.h"
#include "usb.h"
+#include "comp.h"
/* Complex drivers.*/
#include "mmc_spi.h"
diff --git a/os/hal/platforms/STM32/comp_lld.c b/os/hal/platforms/STM32/comp_lld.c
new file mode 100644
index 0000000..bca5e24
--- /dev/null
+++ b/os/hal/platforms/STM32/comp_lld.c
@@ -0,0 +1,220 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
+ 2011,2012,2013 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ Concepts and parts of this file have been contributed by Fabio Utzig and
+ Xo Wang.
+ */
+
+/**
+ * @file STM32/comp_lld.c
+ * @brief STM32 COMP subsystem low level driver header.
+ *
+ * @addtogroup COMP
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+#if HAL_USE_COMP || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver local definitions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/**
+ * @brief COMPD1 driver identifier.
+ * @note The driver COMPD1 allocates the complex timer TIM1 when enabled.
+ */
+#if STM32_COMP_USE_COMP1 || defined(__DOXYGEN__)
+COMPDriver COMPD1;
+#endif
+
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+#if 0
+
+Comparator #1, #2 use EXTI line 21, 22
+
+Need to plug into EXTI interrupt handler
+
+/**
+ * @brief Shared IRQ handler.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ */
+static void comp_lld_serve_interrupt(COMPDriver *compp) {
+ uint16_t sr;
+
+ sr = icup->comp->SR;
+ sr &= icup->comp->DIER;
+ icup->comp->SR = ~sr;
+ if (icup->config->channel == ICU_CHANNEL_1) {
+ if ((sr & COMP_SR_CC1IF) != 0)
+ _icu_isr_invoke_period_cb(icup);
+ if ((sr & COMP_SR_CC2IF) != 0)
+ _icu_isr_invoke_width_cb(icup);
+ } else {
+ if ((sr & COMP_SR_CC1IF) != 0)
+ _icu_isr_invoke_width_cb(icup);
+ if ((sr & COMP_SR_CC2IF) != 0)
+ _icu_isr_invoke_period_cb(icup);
+ }
+ if ((sr & COMP_SR_UIF) != 0)
+ _icu_isr_invoke_overflow_cb(icup);
+}
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+#if STM32_COMP_USE_COMP1
+#if !defined(STM32_COMP1_UP_HANDLER)
+#error "STM32_COMP1_UP_HANDLER not defined"
+#endif
+/**
+ * @brief COMP1 compare interrupt handler.
+ * @note It is assumed that the various sources are only activated if the
+ * associated callback pointer is not equal to @p NULL in order to not
+ * perform an extra check in a potentially critical interrupt handler.
+ *
+ * @isr
+ */
+CH_IRQ_HANDLER(STM32_COMP1_UP_HANDLER) {
+
+ CH_IRQ_PROLOGUE();
+
+ comp_lld_serve_interrupt(&COMPD1);
+
+ CH_IRQ_EPILOGUE();
+}
+
+#endif /* STM32_COMP_USE_COMP1 */
+#endif /* #if 0 EXTI INTERRUPT*/
+
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Low level COMP driver initialization.
+ *
+ * @notapi
+ */
+void comp_lld_init(void) {
+
+#if STM32_COMP_USE_COMP1
+ /* Driver initialization.*/
+ compObjectInit(&COMPD1);
+ COMPD1.comp = COMP;
+#endif
+
+}
+
+static void disable_comp(COMPDriver *compdp) {
+
+ compdp->comp->CSR &= ~(COMP_CSR_COMP1EN | COMP_CSR_COMP2EN);
+
+}
+
+/**
+ * @brief Configures and activates the COMP peripheral.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+void comp_lld_start(COMPDriver *compdp) {
+ chDbgAssert(compdp, "comp_lld_start(), #1", "invalid input");
+
+ if (compdp->state != COMP_STOP) {
+ chDbgAssert(compdp->comp->CSR & COMP_CSR_COMP1LOCK ||
+ compdp->comp->CSR & COMP_CSR_COMP2LOCK,
+ "comp_lld_start(), #1", "cannot reconfigure, locked");
+
+ /* Driver re-configuration scenario, it must be stopped first.*/
+ disable_comp(compdp);
+ }
+
+ /* COMP configuration.*/
+ compdp->comp->CSR =
+ compdp->config->comp_enable |
+ (compdp->config->comp1_inv_in_src << 4) |
+ (compdp->config->comp2_inv_in_src << 20) |
+ (compdp->config->comp1_hysteresis << 12) |
+ (compdp->config->comp2_hysteresis << 28) |
+ (compdp->config->comp1_lock << 15) |
+ (compdp->config->comp2_lock << 31) |
+ (compdp->config->comp1_opmode << 2) |
+ (compdp->config->comp2_opmode << 18) |
+ (compdp->config->comp1_output_pol << 11) |
+ (compdp->config->comp2_output_pol << 27) |
+ (compdp->config->comp1_output_sel << 8) |
+ (compdp->config->comp2_output_sel << 24);
+}
+
+/**
+ * @brief Deactivates the COMP peripheral.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+void comp_lld_stop(COMPDriver *compdp) {
+
+ if (compdp->state == COMP_ACTIVE) {
+ chDbgAssert(compdp->comp->CSR & COMP_CSR_COMP1LOCK ||
+ compdp->comp->CSR & COMP_CSR_COMP2LOCK,
+ "comp_lld_start(), #1", "cannot reconfigure, locked");
+
+ disable_comp(compdp);
+ }
+}
+
+/**
+ * @brief Returns the value of the output of the comparator
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+bool_t comp_lld_comp1val(COMPDriver *compp) {
+ return (compp->comp->CSR & COMP_CSR_COMP1OUT);
+}
+bool_t comp_lld_comp2val(COMPDriver *compp) {
+ return (compp->comp->CSR & COMP_CSR_COMP2OUT);
+}
+
+
+#endif /* HAL_USE_COMP */
+
+/** @} */
diff --git a/os/hal/platforms/STM32/comp_lld.h b/os/hal/platforms/STM32/comp_lld.h
new file mode 100644
index 0000000..792129d
--- /dev/null
+++ b/os/hal/platforms/STM32/comp_lld.h
@@ -0,0 +1,344 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
+ 2011,2012,2013 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file STM32/comp_lld.h
+ * @brief STM32 COMP subsystem low level driver header.
+ *
+ * @addtogroup COMP
+ * @{
+ */
+
+#ifndef _COMP_LLD_H_
+#define _COMP_LLD_H_
+
+#if HAL_USE_COMP || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @name Configuration options
+ * @{
+ */
+/**
+ * @brief COMPD1 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD1 is included.
+ * @note The default is @p TRUE.
+ */
+#if !defined(STM32_COMP_USE_COMP1) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP1 FALSE
+#endif
+
+/**
+ * @brief COMPD2 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD2 is included.
+ * @note The default is @p TRUE.
+ */
+#if !defined(STM32_COMP_USE_COMP2) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP2 FALSE
+#endif
+
+/**
+ * @brief COMPD1 interrupt priority level setting.
+ */
+#if !defined(STM32_COMP_TIM1_IRQ_PRIORITY) || defined(__DOXYGEN__)
+#define STM32_COMP_TIM1_IRQ_PRIORITY 7
+#endif
+
+/**
+ * @brief COMPD2 interrupt priority level setting.
+ */
+#if !defined(STM32_COMP_TIM2_IRQ_PRIORITY) || defined(__DOXYGEN__)
+#define STM32_COMP_TIM2_IRQ_PRIORITY 7
+#endif
+
+/** @} */
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+#if STM32_COMP_USE_COMP1 && !STM32_HAS_COMP1
+#error "COMP1 not present in the selected device"
+#endif
+
+#if !STM32_COMP_USE_COMP1
+#error "COMP driver activated but no COMP peripheral assigned"
+#endif
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief COMP1 inverting input selection
+ */
+typedef enum {
+ COMP1_INPUT_SRC_14_VREFINT = 0, /**< 1/4 of Vrefint. */
+ COMP1_INPUT_SRC_12_VREFINT = 1, /**< 1/2 of Vrefint. */
+ COMP1_INPUT_SRC_34_VREFINT = 2, /**< 3/4 of Vrefint. */
+ COMP1_INPUT_SRC_VREFINT = 3, /**< Vrefint. */
+ COMP1_INPUT_SRC_DAC1_OUT1_PA4 = 4, /**< DAC1_OUT1 or PA4. */
+ COMP1_INPUT_SRC_DAC1_OUT2_PA5 = 5, /**< DAC1_OUT2 or PA5. */
+ COMP1_INPUT_SRC_PA0 = 6, /**< DAC1_OUT2 or PA5. */
+ COMP1_INPUT_SRC_DAC2_OUT1_PA6 = 7, /**< DAC1_OUT2 or PA5. */
+} comp1inputsrc_t;
+
+/**
+ * @brief COMP2 inverting input selection
+ */
+typedef enum {
+ COMP2_INPUT_SRC_14_VREFINT = 0, /**< 1/4 of Vrefint. */
+ COMP2_INPUT_SRC_12_VREFINT = 1, /**< 1/2 of Vrefint. */
+ COMP2_INPUT_SRC_34_VREFINT = 2, /**< 3/4 of Vrefint. */
+ COMP2_INPUT_SRC_VREFINT = 3, /**< Vrefint. */
+ COMP2_INPUT_SRC_DAC1_OUT1_PA4 = 4, /**< DAC1_OUT1 or PA4. */
+ COMP2_INPUT_SRC_DAC1_OUT2_PA5 = 5, /**< DAC1_OUT2 or PA5. */
+ COMP2_INPUT_SRC_PA2 = 6, /**< PA2. */
+ COMP2_INPUT_SRC_DAC2_OUT1_PA6 = 7, /**< DAC2_OUT2 or PA6. */
+} comp2inputsrc_t;
+
+/**
+ * @brief COMP channel type.
+ */
+typedef enum {
+ COMP_CHANNEL_1 = 0, /**< Use COMP_CH1 */
+ COMP_CHANNEL_2 = 1, /**< Use COMP_CH2 */
+} compchannel_t;
+
+/**
+ * @brief COMP Operation Mode setting
+ */
+typedef enum {
+ COMP_PWR_HIGH_SPEED = 0,
+ COMP_PWR_MED_SPEED = 1,
+ COMP_PWR_LOW_PWR = 2,
+ COMP_PWR_ULTRA_LOW_PWR = 3
+} compopmode_t;
+
+/**
+ * @brief COMP Hysteresis
+ */
+typedef enum {
+ COMP_HYSTERESIS_NONE = 0,
+ COMP_HYSTERESIS_LOW = 1,
+ COMP_HYSTERESIS_MED = 2,
+ COMP_HYSTERESIS_HI = 3
+} comphysteresis_t;
+
+/**
+ * @brief COMP Output polarity
+ */
+typedef enum {
+ COMP_OUTPUT_POL_NOT_INV = 0,
+ COMP_OUTPUT_POL_INVERTED = 1
+} compoutputpolarity_t;
+
+/**
+ * @brief COMP #1 Output Destination
+ */
+typedef enum {
+ COMP1_OUTPUT_DEST_NONE = 0, /* no output selected */
+ COMP1_OUTPUT_DEST_TIM15_BRK_IN = 1, /* output to TIM15 break input */
+ COMP1_OUTPUT_DEST_TIM3_IC1 = 2, /* output to TIM3 input capture 1 */
+ COMP1_OUTPUT_DEST_TIM3_OCRCI = 3, /* output to TIM3 OCrefclear input */
+ COMP1_OUTPUT_DEST_TIM2_IC4 = 4, /* output to TIM2 input capture 4 */
+ COMP1_OUTPUT_DEST_TIM2_OCRCI = 3, /* output to TIM2 OCrefclear input */
+ COMP1_OUTPUT_DEST_TIM5_IC4 = 4, /* output to TIM5 input capture 4 */
+ COMP1_OUTPUT_DEST_TIM5_OCRCI = 3, /* output to TIM5 OCrefclear input */
+} comp1outputsel_t;
+
+/**
+ * @brief COMP #2 Output Destination
+ */
+typedef enum {
+ COMP2_OUTPUT_DEST_NONE = 0, /* no output selected */
+ COMP2_OUTPUT_DEST_TIM16_BRK_IN = 1, /* output to TIM16 break input */
+ COMP2_OUTPUT_DEST_TIM4_IC1 = 2, /* output to TIM4 input capture 1 */
+ COMP2_OUTPUT_DEST_TIM4_OCRCI = 3, /* output to TIM4 OCrefclear input */
+ COMP2_OUTPUT_DEST_TIM2_IC4 = 4, /* output to TIM2 input capture 4 */
+ COMP2_OUTPUT_DEST_TIM2_OCRCI = 3, /* output to TIM2 OCrefclear input */
+ COMP2_OUTPUT_DEST_TIM3_IC4 = 4, /* output to TIM3 input capture 1 */
+ COMP2_OUTPUT_DEST_TIM3_OCRCI = 3, /* output to TIM3 OCrefclear input */
+} comp2outputsel_t;
+
+typedef enum {
+ COMP1_EN = COMP_CSR_COMP1EN, /* enable comparator 1 */
+ COMP2_EN = COMP_CSR_COMP2EN, /* enable comparator 2 */
+} compenable_t;
+
+
+/**
+ * @brief Driver configuration structure.
+ * @note It could be empty on some architectures.
+ */
+typedef struct {
+ /**
+ * @brief Driver mode.
+ */
+ compenable_t comp_enable;
+
+ /**
+ * @brief Comparator #1 inverting input source.
+ */
+ comp1inputsrc_t comp1_inv_in_src;
+ /**
+ * @brief Comparator #2 inverting input source.
+ */
+ comp2inputsrc_t comp2_inv_in_src;
+
+ /**
+ * @brief Comparator #1 Hysteresis setting
+ */
+ comphysteresis_t comp1_hysteresis;
+ /**
+ * @brief Comparator #2 Hysteresis setting
+ */
+ comphysteresis_t comp2_hysteresis;
+
+ /**
+ * @brief Lock comparator #1 config after setting.
+ */
+ bool_t comp1_lock;
+ /**
+ * @brief Lock comparator #2 config after setting.
+ */
+ bool_t comp2_lock;
+
+ /**
+ * @brief Comparator #1 Operation Mode
+ */
+ compopmode_t comp1_opmode;
+ /**
+ * @brief Comparator #2 Operation Mode
+ */
+ compopmode_t comp2_opmode;
+
+ /**
+ * @brief Comparator #1 Output Polarity
+ */
+ compoutputpolarity_t comp1_output_pol;
+ /**
+ * @brief Comparator #2 Output Polarity
+ */
+ compoutputpolarity_t comp2_output_pol;
+
+
+ /**
+ * @brief Comparator #1 Output Selection
+ */
+ comp1outputsel_t comp1_output_sel;
+ /**
+ * @brief Comparator #2 Output Selection
+ */
+ comp2outputsel_t comp2_output_sel;
+
+#if 0
+
+>>>>**** NEED TO FILL IN WITH INTERRUPT CALLBACK ****<<<<<
+
+ /**
+ * @brief Callback for pulse width measurement.
+ */
+ compcallback_t width_cb;
+#endif
+} COMPConfig;
+
+/**
+ * @brief Structure representing an COMP driver.
+ */
+struct COMPDriver {
+ /**
+ * @brief Driver state.
+ */
+ compstate_t state;
+ /**
+ * @brief Current configuration data.
+ */
+ const COMPConfig *config;
+#if defined(COMP_DRIVER_EXT_FIELDS)
+ COMP_DRIVER_EXT_FIELDS
+#endif
+ /* End of the mandatory fields.*/
+ /**
+ * @brief Pointer to the COMP register.
+ */
+ COMP_TypeDef *comp;
+};
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @brief Returns the width of the latest pulse.
+ * @details The pulse width is defined as number of ticks between the start
+ * edge and the stop edge.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ * @return The number of ticks.
+ *
+ * @notapi
+ */
+#define comp_lld_get_width(compp) (*((compp)->wccrp) + 1)
+
+/**
+ * @brief Returns the width of the latest cycle.
+ * @details The cycle width is defined as number of ticks between a start
+ * edge and the next start edge.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ * @return The number of ticks.
+ *
+ * @notapi
+ */
+#define comp_lld_get_period(compp) (*((compp)->pccrp) + 1)
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#if STM32_COMP_USE_COMP1 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD1;
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void comp_lld_init(void);
+ void comp_lld_start(COMPDriver *compp);
+ void comp_lld_stop(COMPDriver *compp);
+ bool_t comp_lld_comp1val(COMPDriver *compp);
+ bool_t comp_lld_comp2val(COMPDriver *compp);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_COMP */
+
+#endif /* _COMP_LLD_H_ */
+
+/** @} */
diff --git a/os/hal/platforms/STM32F37x/platform.mk b/os/hal/platforms/STM32F37x/platform.mk
index 8fd9663..c92ddaa 100644
--- a/os/hal/platforms/STM32F37x/platform.mk
+++ b/os/hal/platforms/STM32F37x/platform.mk
@@ -4,6 +4,7 @@ PLATFORMSRC = ${CHIBIOS}/os/hal/platforms/STM32F37x/stm32_dma.c \
${CHIBIOS}/os/hal/platforms/STM32F37x/adc_lld.c \
${CHIBIOS}/os/hal/platforms/STM32F37x/ext_lld_isr.c \
${CHIBIOS}/os/hal/platforms/STM32/can_lld.c \
+ ${CHIBIOS}/os/hal/platforms/STM32/comp_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/ext_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/GPIOv2/pal_lld.c \
${CHIBIOS}/os/hal/platforms/STM32/I2Cv2/i2c_lld.c \
diff --git a/os/hal/platforms/STM32F37x/stm32_registry.h b/os/hal/platforms/STM32F37x/stm32_registry.h
index 8f08422..29c93af 100644
--- a/os/hal/platforms/STM32F37x/stm32_registry.h
+++ b/os/hal/platforms/STM32F37x/stm32_registry.h
@@ -67,6 +67,9 @@
#define STM32_HAS_CAN2 FALSE
#define STM32_CAN_MAX_FILTERS 14
+/* COMP attributes.*/
+#define STM32_HAS_COMP1 TRUE
+#define STM32_HAS_COMP2 FALSE
/* DAC attributes.*/
#define STM32_HAS_DAC TRUE
diff --git a/os/hal/src/comp.c b/os/hal/src/comp.c
new file mode 100644
index 0000000..60e747e
--- /dev/null
+++ b/os/hal/src/comp.c
@@ -0,0 +1,121 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
+ 2011,2012,2013 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file comp.c
+ * @brief COMP Driver code.
+ *
+ * @addtogroup COMP
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+#if HAL_USE_COMP || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver local definitions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief COMP Driver initialization.
+ * @note This function is implicitly invoked by @p halInit(), there is
+ * no need to explicitly initialize the driver.
+ *
+ * @init
+ */
+void compInit(void) {
+
+ comp_lld_init();
+}
+
+/**
+ * @brief Initializes the standard part of a @p COMPDriver structure.
+ *
+ * @param[out] compp pointer to the @p COMPDriver object
+ *
+ * @init
+ */
+void compObjectInit(COMPDriver *compp) {
+
+ compp->state = COMP_STOP;
+ compp->config = NULL;
+}
+
+/**
+ * @brief Configures and activates the COMP peripheral.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ * @param[in] config pointer to the @p COMPConfig object
+ *
+ * @api
+ */
+void compStart(COMPDriver *compp, const COMPConfig *config) {
+
+ chDbgCheck((compp != NULL) && (config != NULL), "compStart");
+
+ chSysLock();
+ chDbgAssert((compp->state == COMP_STOP) || (compp->state == COMP_ACTIVE),
+ "compStart(), #1", "invalid state");
+ compp->config = config;
+ comp_lld_start(compp);
+ compp->state = COMP_ACTIVE;
+ chSysUnlock();
+}
+
+/**
+ * @brief Deactivates the COMP peripheral.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @api
+ */
+void compStop(COMPDriver *compp) {
+
+ chDbgCheck(compp != NULL, "compStop");
+
+ chSysLock();
+ chDbgAssert((compp->state == COMP_STOP) || (compp->state == COMP_ACTIVE),
+ "compStop(), #1", "invalid state");
+ comp_lld_stop(compp);
+ compp->state = COMP_STOP;
+ chSysUnlock();
+}
+
+#endif /* HAL_USE_COMP */
+
+/** @} */
diff --git a/os/hal/src/hal.c b/os/hal/src/hal.c
index 974a885..c363e17 100644
--- a/os/hal/src/hal.c
+++ b/os/hal/src/hal.c
@@ -74,6 +74,9 @@ void halInit(void) {
#if HAL_USE_CAN || defined(__DOXYGEN__)
canInit();
#endif
+#if HAL_USE_COMP || defined(__DOXYGEN__)
+ compInit();
+#endif
#if HAL_USE_EXT || defined(__DOXYGEN__)
extInit();
#endif
--
1.8.1.2
Analog Comparator patch for stm32f37x
Moderator: RoccoMarco
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: Analog Comparator patch for stm32f37x
Hi,
Comparators are very STM32-specific, I think there is no need to create a layered device driver for this (lld+hld). I recommend creating a monolithic driver to be placed under ./os/hal/platforms/STM32/COMPv1 without an high level counterpart under ./os/hal/src and ./os/hal/include. File names would be stm32_comp.h and stm32_comp.c.
Another problem, you need to plan for all STM32s -OR- put the driver under ./os/hal/platforms/STM32F37x making it STM32F37x-specific.
BTW, I see some PWM leftovers in comp_lld.h, those "get" macros are not required I think.
Giovanni
Comparators are very STM32-specific, I think there is no need to create a layered device driver for this (lld+hld). I recommend creating a monolithic driver to be placed under ./os/hal/platforms/STM32/COMPv1 without an high level counterpart under ./os/hal/src and ./os/hal/include. File names would be stm32_comp.h and stm32_comp.c.
Another problem, you need to plan for all STM32s -OR- put the driver under ./os/hal/platforms/STM32F37x making it STM32F37x-specific.
BTW, I see some PWM leftovers in comp_lld.h, those "get" macros are not required I think.
Giovanni
Re: Analog Comparator patch for stm32f37x
Thank you very much for your contribution. Please use the code tags to include larger code/diff segments in your post next time. To attach them, simply create a .zip archive.
~ Tectu
~ Tectu