aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal
diff options
context:
space:
mode:
authorFabien Poussin <fabien.poussin@gmail.com>2017-02-06 20:09:28 +0100
committerFabien Poussin <fabien.poussin@gmail.com>2017-02-06 20:09:28 +0100
commit86428716d531d10261170eb990e6f60938e3cfd7 (patch)
treec566cae87cfef1c401a4ff6557b61b724c1fb765 /os/hal
parent11e949d81b4d0f3b94763eb0b29b713bd1e83ae1 (diff)
downloadChibiOS-Contrib-86428716d531d10261170eb990e6f60938e3cfd7.tar.gz
ChibiOS-Contrib-86428716d531d10261170eb990e6f60938e3cfd7.tar.bz2
ChibiOS-Contrib-86428716d531d10261170eb990e6f60938e3cfd7.zip
Adding COMP Driver.
Diffstat (limited to 'os/hal')
-rw-r--r--os/hal/hal.mk3
-rw-r--r--os/hal/include/hal_comp.h132
-rw-r--r--os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.c349
-rw-r--r--os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.h314
-rw-r--r--os/hal/ports/STM32/STM32F3xx/platform.mk2
-rw-r--r--os/hal/src/hal_comp.c155
6 files changed, 954 insertions, 1 deletions
diff --git a/os/hal/hal.mk b/os/hal/hal.mk
index f05ddbc..79e501e 100644
--- a/os/hal/hal.mk
+++ b/os/hal/hal.mk
@@ -19,6 +19,7 @@ HALSRC += ${CHIBIOS_CONTRIB}/os/hal/src/hal_community.c \
${CHIBIOS_CONTRIB}/os/hal/src/hal_timcap.c \
${CHIBIOS_CONTRIB}/os/hal/src/hal_qei.c \
${CHIBIOS_CONTRIB}/os/hal/src/hal_usb_hid.c \
- ${CHIBIOS_CONTRIB}/os/hal/src/hal_usb_msd.c
+ ${CHIBIOS_CONTRIB}/os/hal/src/hal_usb_msd.c \
+ ${CHIBIOS_CONTRIB}/os/hal/src/hal_comp.c
HALINC += ${CHIBIOS_CONTRIB}/os/hal/include
diff --git a/os/hal/include/hal_comp.h b/os/hal/include/hal_comp.h
new file mode 100644
index 0000000..045cef0
--- /dev/null
+++ b/os/hal/include/hal_comp.h
@@ -0,0 +1,132 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
+ Copyright (C) 2017 Fabien Poussin (fabien.poussin (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#ifndef HAL_COMP_H_
+#define HAL_COMP_H_
+
+#include "hal.h"
+
+
+#if (HAL_USE_COMP == TRUE) || 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_READY = 2, /**< Ready. */
+ COMP_ACTIVE = 3, /**< Active cycle phase. */
+} compstate_t;
+
+/**
+ * @brief Type of a structure representing an TIMCAP driver.
+ */
+typedef struct COMPDriver COMPDriver;
+
+
+/**
+ * @brief TIMCAP notification callback type.
+ *
+ * @param[in] comp pointer to a @p COMPDriver object
+ */
+typedef void (*compcallback_t)(COMPDriver *comp);
+
+#include "hal_comp_lld.h"
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @name Macro Functions
+ * @{
+ */
+/**
+ * @brief Enables the input capture.
+ *
+ * @param[in] comp pointer to the @p COMPDriver object
+ *
+ * @iclass
+ */
+#define timcapEnableI(comp) comp_lld_enable(comp)
+
+/**
+ * @brief Disables the input capture.
+ *
+ * @param[in] comp pointer to the @p COMPDriver object
+ *
+ * @iclass
+ */
+#define timcapDisableI(comp) comp_lld_disable(comp)
+/** @} */
+
+
+/**
+ * @name Low Level driver helper macros
+ * @{
+ */
+
+/**
+ * @brief Common ISR code, main event.
+ *
+ * @param[in] comp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+#define _comp_isr_invoke_cb(comp) { \
+ (comp)->config->cb(comp); \
+}
+/** @} */
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void compInit(void);
+ void compObjectInit(COMPDriver *comp);
+ void compStart(COMPDriver *comp, const COMPConfig *config);
+ void compStop(COMPDriver *comp);
+ void compEnable(COMPDriver *comp);
+ void compDisable(COMPDriver *comp);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_COMP */
+
+
+#endif /* HAL_COMP_H_ */
diff --git a/os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.c b/os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.c
new file mode 100644
index 0000000..5289d50
--- /dev/null
+++ b/os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.c
@@ -0,0 +1,349 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
+ Copyright (C) 2017 Fabien Poussin (fabien.poussin (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+
+/**
+ * @file STM32/hal_comp_lld.c
+ * @brief STM32 Comp subsystem low level driver header.
+ *
+ * @addtogroup COMP
+ * @{
+ */
+
+#include "hal.h"
+
+#if HAL_USE_COMP || defined(__DOXYGEN__)
+
+#include "hal_comp.h"
+
+/*===========================================================================*/
+/* Driver local definitions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/**
+ * @brief COMPD1 driver identifier.
+ * @note The driver COMPD1 allocates the comparator COMP1 when enabled.
+ */
+#if STM32_COMP_USE_COMP1 || defined(__DOXYGEN__)
+COMPDriver COMPD1;
+#endif
+
+/**
+ * @brief COMPD2 driver identifier.
+ * @note The driver COMPD2 allocates the comparator COMP2 when enabled.
+ */
+#if STM32_COMP_USE_COMP2 || defined(__DOXYGEN__)
+COMPDriver COMPD2;
+#endif
+
+/**
+ * @brief COMPD3 driver identifier.
+ * @note The driver COMPD3 allocates the comparator COMP3 when enabled.
+ */
+#if STM32_COMP_USE_COMP3 || defined(__DOXYGEN__)
+COMPDriver COMPD3;
+#endif
+
+/**
+ * @brief COMPD4 driver identifier.
+ * @note The driver COMPD4 allocates the comparator COMP4 when enabled.
+ */
+#if STM32_COMP_USE_COMP4 || defined(__DOXYGEN__)
+COMPDriver COMPD4;
+#endif
+
+/**
+ * @brief COMPD5 driver identifier.
+ * @note The driver COMPD5 allocates the comparator COMP5 when enabled.
+ */
+#if STM32_COMP_USE_COMP5 || defined(__DOXYGEN__)
+COMPDriver COMPD5;
+#endif
+
+/**
+ * @brief COMPD6 driver identifier.
+ * @note The driver COMPD6 allocates the comparator COMP6 when enabled.
+ */
+#if STM32_COMP_USE_COMP6 || defined(__DOXYGEN__)
+COMPDriver COMPD6;
+#endif
+
+/**
+ * @brief COMPD7 driver identifier.
+ * @note The driver COMPD7 allocates the comparator COMP7 when enabled.
+ */
+#if STM32_COMP_USE_COMP7 || defined(__DOXYGEN__)
+COMPDriver COMPD7;
+#endif
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+
+/*===========================================================================*/
+/* 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
+
+#if STM32_COMP_USE_COMP2
+ /* Driver initialization.*/
+ compObjectInit(&COMPD2);
+ COMPD2.comp = COMP2;
+#endif
+
+#if STM32_COMP_USE_COMP3
+ /* Driver initialization.*/
+ compObjectInit(&COMPD3);
+ COMPD3.comp = COMP3;
+#endif
+
+#if STM32_COMP_USE_COMP4
+ /* Driver initialization.*/
+ compObjectInit(&COMPD4);
+ COMPD4.comp = COMP4;
+#endif
+
+#if STM32_COMP_USE_COMP5
+ /* Driver initialization.*/
+ compObjectInit(&COMPD5);
+ COMPD8.comp = COMP5;
+#endif
+
+#if STM32_COMP_USE_COMP6
+ /* Driver initialization.*/
+ compObjectInit(&COMPD6);
+ COMPD6.comp = COMP6;
+#endif
+
+#if STM32_COMP_USE_COMP7
+ /* Driver initialization.*/
+ compObjectInit(&COMPD7);
+ COMPD7.comp = COMP7;
+#endif
+
+}
+#if STM32_COMP_USE_INTERRUPTS
+static void comp_lld_cb(EXTDriver *extp, expchannel_t channel) {
+
+ (void) extp;
+ switch (channel) {
+
+#if STM32_COMP_USE_COMP1
+ case 21:
+ COMPD1.config->cb(&COMPD1);
+#endif
+#if STM32_COMP_USE_COMP2
+ case 22:
+ COMPD2.config->cb(&COMPD2);
+#endif
+#if STM32_COMP_USE_COMP3
+ case 29:
+ COMPD3.config->cb(&COMPD3);
+#endif
+#if STM32_COMP_USE_COMP4
+ case 30:
+ COMPD4.config->cb(&COMPD4);
+#endif
+#if STM32_COMP_USE_COMP5
+ case 31:
+ COMPD5.config->cb(&COMPD5);
+#endif
+#if STM32_COMP_USE_COMP6
+ case 32:
+ COMPD6.config->cb(&COMPD6);
+#endif
+#if STM32_COMP_USE_COMP7
+ case 33:
+ COMPD7.config->cb(&COMPD7);
+#endif
+ default:
+ return;
+ }
+
+}
+#endif
+
+/**
+ * @brief Configures and activates the COMP peripheral.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+void comp_lld_start(COMPDriver *compp) {
+
+ // Apply CSR Execpt the enable bit.
+ compp->comp->CSR = compp->config->csr & ~COMP_CSR_COMPxEN;
+
+ // Inverted output
+ if (compp->config->mode == COMP_OUTPUT_INVERTED)
+ compp->comp->CSR |= COMP_CSR_COMPxPOL;
+
+ EXTChannelConfig chn_cfg = {EXT_CH_MODE_BOTH_EDGES, comp_lld_cb};
+ EXTConfig *cfg = (EXTConfig*)EXTD1.config;
+
+
+#if STM32_COMP_USE_COMP1 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD1 == compp) {
+ cfg->channels[21] = chn_cfg;
+ ext_lld_channel_enable(&EXTD1, 21);
+ }
+#endif
+#if STM32_COMP_USE_COMP2 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD2 == compp) {
+ cfg->channels[22] = chn_cfg;
+ ext_lld_channel_enable(&EXTD1, 22);
+ }
+#endif
+#if STM32_COMP_USE_COMP3 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD3 == compp) {
+ cfg->channels[29] = chn_cfg;
+ ext_lld_channel_enable(&EXTD1, 29);
+ }
+#endif
+#if STM32_COMP_USE_COMP4 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD4 == compp) {
+ cfg->channels[30] = chn_cfg;
+ ext_lld_channel_enable(&EXTD1, 30);
+ }
+#endif
+#if STM32_COMP_USE_COMP5 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD5 == compp) {
+ cfg->channels[31] = chn_cfg;
+ ext_lld_channel_enable(&EXTD1, 31);
+ }
+#endif
+#if STM32_COMP_USE_COMP6 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD6 == compp) {
+ cfg->channels[32] = chn_cfg;
+ ext_lld_channel_enable(&EXTD1, 32);
+ }
+#endif
+#if STM32_COMP_USE_COMP7 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD7 == compp) {
+ cfg->channels[33] = chn_cfg;
+ ext_lld_channel_enable(&EXTD1, 33);
+ }
+#endif
+
+}
+
+/**
+ * @brief Deactivates the comp peripheral.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+void comp_lld_stop(COMPDriver *compp) {
+
+ if (compp->state == COMP_READY) {
+
+
+#if STM32_COMP_USE_COMP1 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD1 == compp) {
+ ext_lld_channel_disable(&EXTD1, 21);
+ }
+#endif
+#if STM32_COMP_USE_COMP2 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD2 == compp) {
+ ext_lld_channel_disable(&EXTD1, 22);
+ }
+#endif
+#if STM32_COMP_USE_COMP3 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD3 == compp) {
+ ext_lld_channel_disable(&EXTD1, 29);
+ }
+#endif
+#if STM32_COMP_USE_COMP4 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD4 == compp) {
+ ext_lld_channel_disable(&EXTD1, 30);
+ }
+#endif
+#if STM32_COMP_USE_COMP5 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD5 == compp) {
+ ext_lld_channel_disable(&EXTD1, 31);
+ }
+#endif
+#if STM32_COMP_USE_COMP6 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD6 == compp) {
+ ext_lld_channel_disable(&EXTD1, 32);
+ }
+#endif
+#if STM32_COMP_USE_COMP7 && STM32_COMP_USE_INTERRUPTS
+ if (&COMPD7 == compp) {
+ ext_lld_channel_disable(&EXTD1, 33);
+ }
+#endif
+ }
+}
+
+/**
+ * @brief Enables the output.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+void comp_lld_enable(COMPDriver *compp) {
+
+ compp->comp->CSR |= COMP_CSR_COMPxEN; /* Enable */
+
+}
+
+/**
+ * @brief Disables the output.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @notapi
+ */
+void comp_lld_disable(COMPDriver *compp) {
+
+ compp->comp->CSR &= ~COMP_CSR_COMPxEN; /* Disable */
+
+}
+
+#endif /* HAL_USE_COMP */
+
+/** @} */
diff --git a/os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.h b/os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.h
new file mode 100644
index 0000000..4b66c52
--- /dev/null
+++ b/os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.h
@@ -0,0 +1,314 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
+ Copyright (C) 2017 Fabien Poussin (fabien.poussin (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+/**
+ * @file STM32/comp_lld.h
+ * @brief STM32 Comparator subsystem low level driver header.
+ *
+ * @addtogroup COMP
+ * @{
+ */
+
+#ifndef HAL_COMP_LLD_H_
+#define HAL_COMP_LLD_H_
+
+#include "hal.h"
+
+#if HAL_USE_COMP || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+#if defined(STM32F303x8)
+
+#define STM32_HAS_COMP1 FALSE
+#define STM32_HAS_COMP2 TRUE
+#define STM32_HAS_COMP3 FALSE
+#define STM32_HAS_COMP4 TRUE
+#define STM32_HAS_COMP5 FALSE
+#define STM32_HAS_COMP6 FALSE
+#define STM32_HAS_COMP7 FALSE
+
+#endif
+
+#if defined(STM32F303xC)
+
+#define STM32_HAS_COMP1 TRUE
+#define STM32_HAS_COMP2 TRUE
+#define STM32_HAS_COMP3 TRUE
+#define STM32_HAS_COMP4 TRUE
+#define STM32_HAS_COMP5 TRUE
+#define STM32_HAS_COMP6 TRUE
+#define STM32_HAS_COMP7 TRUE
+
+#endif
+
+#if defined(STM32F303xE)
+
+#define STM32_HAS_COMP1 TRUE
+#define STM32_HAS_COMP2 TRUE
+#define STM32_HAS_COMP3 TRUE
+#define STM32_HAS_COMP4 TRUE
+#define STM32_HAS_COMP5 TRUE
+#define STM32_HAS_COMP6 TRUE
+#define STM32_HAS_COMP7 TRUE
+
+#endif
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @name Configuration options
+ * @{
+ */
+
+/**
+ * @brief COMP INTERRUPTS.
+ * @details If set to @p TRUE the support for COMPD1 is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(STM32_COMP_USE_INTERRUPTS) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_INTERRUPTS FALSE
+#endif
+
+/**
+ * @brief COMPD1 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD1 is included.
+ * @note The default is @p FALSE.
+ */
+#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 FALSE.
+ */
+#if !defined(STM32_COMP_USE_COMP2) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP2 FALSE
+#endif
+
+/**
+ * @brief COMPD3 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD3 is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(STM32_COMP_USE_COMP3) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP3 FALSE
+#endif
+
+/**
+ * @brief COMPD4 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD4 is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(STM32_COMP_USE_COMP4) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP4 FALSE
+#endif
+
+/**
+ * @brief COMPD5 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD4 is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(STM32_COMP_USE_COMP5) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP5 FALSE
+#endif
+
+/**
+ * @brief COMPD6 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD4 is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(STM32_COMP_USE_COMP6) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP6 FALSE
+#endif
+
+/**
+ * @brief COMPD7 driver enable switch.
+ * @details If set to @p TRUE the support for COMPD4 is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(STM32_COMP_USE_COMP7) || defined(__DOXYGEN__)
+#define STM32_COMP_USE_COMP7 FALSE
+#endif
+
+/** @} */
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+#if STM32_COMP_USE_INTERRUPTS && !HAL_USE_EXT
+#error "COMP needs HAL_USE_EXT to use interrupts"
+#endif
+
+#if STM32_COMP_USE_INTERRUPTS
+#include "hal_ext_lld.h"
+#endif
+
+#if STM32_COMP_USE_COMP1 && !STM32_HAS_COMP1
+#error "COMP1 not present in the selected device"
+#endif
+
+#if STM32_COMP_USE_COMP2 && !STM32_HAS_COMP2
+#error "COMP2 not present in the selected device"
+#endif
+
+#if STM32_COMP_USE_COMP3 && !STM32_HAS_COMP3
+#error "COMP3 not present in the selected device"
+#endif
+
+#if STM32_COMP_USE_COMP4 && !STM32_HAS_COMP4
+#error "COMP4 not present in the selected device"
+#endif
+
+#if STM32_COMP_USE_COMP5 && !STM32_HAS_COMP5
+#error "COMP5 not present in the selected device"
+#endif
+
+#if STM32_COMP_USE_COMP6 && !STM32_HAS_COMP6
+#error "COMP6 not present in the selected device"
+#endif
+
+#if STM32_COMP_USE_COMP7 && !STM32_HAS_COMP7
+#error "COMP7 not present in the selected device"
+#endif
+
+#if !STM32_COMP_USE_COMP1 && !STM32_COMP_USE_COMP2 && \
+ !STM32_COMP_USE_COMP3 && !STM32_COMP_USE_COMP4 && \
+ !STM32_COMP_USE_COMP6 && !STM32_COMP_USE_COMP6 && \
+ !STM32_COMP_USE_COMP7
+#error "COMP driver activated but no COMP peripheral assigned"
+#endif
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief COMP output mode.
+ */
+typedef enum {
+ COMP_OUTPUT_NORMAL = 0,
+ COMP_OUTPUT_INVERTED = 1
+} comp_output_mode_t;
+
+
+/**
+ * @brief Driver configuration structure.
+ * @note It could be empty on some architectures.
+ */
+typedef struct {
+ /**
+ * @brief Ouput mode.
+ */
+ comp_output_mode_t mode;
+
+ /**
+ * @brief Callback.
+ */
+ compcallback_t cb;
+
+ /* End of the mandatory fields.*/
+
+ /**
+ * @brief COMP CSR register initialization data.
+ * @note The value of this field should normally be equal to zero.
+ */
+ uint32_t csr;
+} COMPConfig;
+
+/**
+ * @brief Structure representing an TIMCAP 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 COMPx registers block.
+ */
+ COMP_TypeDef *comp;
+};
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#if STM32_COMP_USE_COMP1 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD1;
+#endif
+
+#if STM32_COMP_USE_COMP2 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD2;
+#endif
+
+#if STM32_COMP_USE_COMP3 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD3;
+#endif
+
+#if STM32_COMP_USE_COMP4 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD4;
+#endif
+
+#if STM32_COMP_USE_COMP5 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD5;
+#endif
+
+#if STM32_COMP_USE_COMP6 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD6;
+#endif
+
+#if STM32_COMP_USE_COMP7 && !defined(__DOXYGEN__)
+extern COMPDriver COMPD7;
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void comp_lld_init(void);
+ void comp_lld_start(COMPDriver *timcapp);
+ void comp_lld_stop(COMPDriver *timcapp);
+ void comp_lld_enable(COMPDriver *timcapp);
+ void comp_lld_disable(COMPDriver *timcapp);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_COMP */
+
+#endif /* _comp_lld_H_ */
+
+/** @} */
diff --git a/os/hal/ports/STM32/STM32F3xx/platform.mk b/os/hal/ports/STM32/STM32F3xx/platform.mk
index 92f033c..910fb1f 100644
--- a/os/hal/ports/STM32/STM32F3xx/platform.mk
+++ b/os/hal/ports/STM32/STM32F3xx/platform.mk
@@ -4,7 +4,9 @@ PLATFORMSRC += ${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/CRCv1/hal_crc_lld.c \
${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/TIMv1/hal_eicu_lld.c \
${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/TIMv1/hal_timcap_lld.c \
${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/TIMv1/hal_qei_lld.c \
+ ${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/COMPv1/hal_comp_lld.c \
PLATFORMINC += ${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/CRCv1 \
${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/TIMv1 \
+ ${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD/COMPv1 \
${CHIBIOS_CONTRIB}/os/hal/ports/STM32/LLD
diff --git a/os/hal/src/hal_comp.c b/os/hal/src/hal_comp.c
new file mode 100644
index 0000000..abc0fad
--- /dev/null
+++ b/os/hal/src/hal_comp.c
@@ -0,0 +1,155 @@
+/*
+ ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
+ Copyright (C) 2017 Fabien Poussin (fabien.poussin (at) google's mail)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+/**
+ * @file hal_comp.c
+ * @brief COMP Driver code.
+ *
+ * @addtogroup COMP
+ * @{
+ */
+
+#include "hal_comp.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) {
+
+ osalDbgCheck((compp != NULL) && (config != NULL));
+
+ osalSysLock();
+ osalDbgAssert((compp->state == COMP_STOP) || (compp->state == COMP_READY),
+ "invalid state");
+ compp->config = config;
+ comp_lld_start(compp);
+ compp->state = COMP_READY;
+ osalSysUnlock();
+}
+
+/**
+ * @brief Deactivates the COMP peripheral.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @api
+ */
+void compStop(COMPDriver *compp) {
+
+ osalDbgCheck(compp != NULL);
+
+ osalSysLock();
+ osalDbgAssert((compp->state == COMP_STOP) || (compp->state == COMP_READY),
+ "invalid state");
+ comp_lld_stop(compp);
+ compp->state = COMP_STOP;
+ osalSysUnlock();
+}
+
+/**
+ * @brief Activates the comparator.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @api
+ */
+void compEnable(COMPDriver *compp) {
+
+ osalDbgCheck(compp != NULL);
+
+ osalSysLock();
+ osalDbgAssert(compp->state == COMP_READY, "invalid state");
+ comp_lld_enable(compp);
+ compp->state = COMP_ACTIVE;
+ osalSysUnlock();
+}
+
+/**
+ * @brief Deactivates the comparator.
+ *
+ * @param[in] compp pointer to the @p COMPDriver object
+ *
+ * @api
+ */
+void compDisable(COMPDriver *compp) {
+
+ osalDbgCheck(compp != NULL);
+
+ osalSysLock();
+ osalDbgAssert((compp->state == COMP_READY) || (compp->state == COMP_ACTIVE),
+ "invalid state");
+ comp_lld_disable(compp);
+ compp->state = COMP_READY;
+ osalSysUnlock();
+}
+
+#endif /* HAL_USE_COMP */
+
+/** @} */