rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_comp_lld.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio
3 Copyright (C) 2017 Fabien Poussin (fabien.poussin (at) google's mail)
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16*/
17
18
19/**
20 * @file Kinetis/hal_comp_lld.c
21 * @brief Kinetis KE1xF Comp subsystem low level driver header.
22 * @author andreika <prometheus.pcb@gmail.com>
23 *
24 * @addtogroup COMP
25 * @{
26 */
27
28#include "hal.h"
29
30#if HAL_USE_COMP || defined(__DOXYGEN__)
31
32#include "hal_comp.h"
33
34/*===========================================================================*/
35/* Driver local definitions. */
36/*===========================================================================*/
37
38/*===========================================================================*/
39/* Driver exported variables. */
40/*===========================================================================*/
41
42/**
43 * @brief COMPD1 driver identifier.
44 * @note The driver COMPD1 allocates the comparator COMP1 when enabled.
45 */
46#if KINETIS_COMP_USE_COMP0 || defined(__DOXYGEN__)
48#endif
49
50/**
51 * @brief COMPD2 driver identifier.
52 * @note The driver COMPD2 allocates the comparator COMP2 when enabled.
53 */
54#if KINETIS_COMP_USE_COMP1 || defined(__DOXYGEN__)
56#endif
57
58/**
59 * @brief COMPD3 driver identifier.
60 * @note The driver COMPD3 allocates the comparator COMP3 when enabled.
61 */
62#if KINETIS_COMP_USE_COMP2 || defined(__DOXYGEN__)
64#endif
65
66/*===========================================================================*/
67/* Driver local variables and types. */
68/*===========================================================================*/
69
70/*===========================================================================*/
71/* Driver local functions. */
72/*===========================================================================*/
73
74
75/*===========================================================================*/
76/* Driver interrupt handlers. */
77/*===========================================================================*/
78
79
80/*===========================================================================*/
81/* Driver exported functions. */
82/*===========================================================================*/
83
84/**
85 * @brief Low level COMP driver initialization.
86 *
87 * @notapi
88 */
89void comp_lld_init(void) {
90#if KINETIS_COMP_USE_COMP0
91 /* Driver initialization.*/
92 compObjectInit(&COMPD1);
93 COMPD1.reg = CMP0;
94 nvicEnableVector(CMP0_IRQn, KINETIS_COMP_IRQ_PRIORITY);
95#endif
96
97#if KINETIS_COMP_USE_COMP1
98 /* Driver initialization.*/
99 compObjectInit(&COMPD2);
100 COMPD2.reg = CMP1;
101 nvicEnableVector(CMP1_IRQn, KINETIS_COMP_IRQ_PRIORITY);
102#endif
103
104#if KINETIS_COMP_USE_COMP2
105 /* Driver initialization.*/
106 compObjectInit(&COMPD3);
107 COMPD3.reg = CMP2;
108 nvicEnableVector(CMP2_IRQn, KINETIS_COMP_IRQ_PRIORITY);
109#endif
110}
111
112static void comp_lld_irq_handler(COMPDriver *compp) {
113 if (compp->config->cb != NULL)
114 compp->config->cb(compp);
115
116 /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
117 exception return operation might vector to incorrect interrupt */
118#if defined __CORTEX_M && (__CORTEX_M == 4U)
119 __DSB();
120#endif
121}
122
123/**
124 * @brief COMP1, COMP2, COMP3 interrupt handler.
125 *
126 * @isr
127 */
128#if KINETIS_COMP_USE_COMP0 || defined(__DOXYGEN__)
129OSAL_IRQ_HANDLER(KINETIS_COMP0_IRQ_VECTOR) {
130 OSAL_IRQ_PROLOGUE();
132 OSAL_IRQ_EPILOGUE();
133}
134#endif
135
136#if KINETIS_COMP_USE_COMP1 || defined(__DOXYGEN__)
137OSAL_IRQ_HANDLER(KINETIS_COMP1_IRQ_VECTOR) {
138 OSAL_IRQ_PROLOGUE();
140 OSAL_IRQ_EPILOGUE();
141}
142#endif
143
144#if KINETIS_COMP_USE_COMP2 || defined(__DOXYGEN__)
145OSAL_IRQ_HANDLER(KINETIS_COMP2_IRQ_VECTOR) {
146 OSAL_IRQ_PROLOGUE();
148 OSAL_IRQ_EPILOGUE();
149}
150#endif
151
152/**
153 * @brief Configures and activates an EXT channel (used by comp)
154 *
155 * @param[in] compp pointer to the @p COMPDriver object
156 * @param[in] channel EXT channel (0..7)
157 *
158 * @notapi
159 */
161 osalDbgAssert(channel <= 7, "invalid channel number");
162 memset(&compp->acmpChannelCfg, 0, sizeof(compp->acmpChannelCfg));
166 compp->acmpChannelCfg.minusMuxInput = 0U;
168}
169
170/**
171 * @brief Deactivate an EXT channel (used by comp)
172 *
173 * @param[in] compp pointer to the @p COMPDriver object
174 * @param[in] channel EXT channel
175 *
176 * @notapi
177 */
179 // todo: implement
180}
181
182/**
183 * @brief Set the DAC value used by comp
184 *
185 * @param[in] compp pointer to the @p COMPDriver object
186 * @param[in] value DAC value (0..255?)
187 *
188 * @notapi
189 */
190void comp_lld_set_dac_value(COMPDriver *compp, uint32_t value) {
191 compp->acmpDacCfg.DACValue = value;
192 ACMP_SetDACConfig(compp->reg, &compp->acmpDacCfg);
193}
194
195/**
196 * @brief Configures and activates the COMP peripheral.
197 *
198 * @param[in] compp pointer to the @p COMPDriver object
199 *
200 * @notapi
201 */
205 compp->acmpCfg.enableInvertOutput = true;
206 compp->acmpCfg.enableHighSpeed = true;
207 compp->acmpCfg.useUnfilteredOutput = true; //false;
208 compp->acmpCfg.hysteresisMode = kACMP_HysteresisLevel2; // L1=~16mV, L2=~32mV
209
210 ACMP_Init(compp->reg, &compp->acmpCfg);
211 compp->irq_mask = 0;
212
213#if 0
214 acmp_filter_config_t filterCfg = { 0 };
215 filterCfg.filterCount = 2U;
216 filterCfg.filterPeriod = 2U;
217 filterCfg.enableSample = false;
218 ACMP_SetFilterConfig(compp->reg, &filterCfg);
219#endif
220
221 memset(&compp->acmpDacCfg, 0, sizeof(compp->acmpDacCfg));
222 compp->acmpDacCfg.referenceVoltageSource = kACMP_VrefSourceVin1; // connected to VDDA on KE1xF
223}
224
225/**
226 * @brief Deactivates the comp peripheral.
227 *
228 * @param[in] compp pointer to the @p COMPDriver object
229 *
230 * @notapi
231 */
233 ACMP_Deinit(compp->reg);
234}
235
236/**
237 * @brief Enables the output.
238 *
239 * @param[in] compp pointer to the @p COMPDriver object
240 *
241 * @notapi
242 */
244 compp->irq_mask = 0;
245
246 /* Programming edge registers.*/
247 if (compp->config->irq_mode == COMP_IRQ_RISING || compp->config->irq_mode == COMP_IRQ_BOTH)
249 if (compp->config->irq_mode == COMP_IRQ_FALLING || compp->config->irq_mode == COMP_IRQ_BOTH)
251
252 ACMP_EnableInterrupts(compp->reg, compp->irq_mask);
253 ACMP_Enable(compp->reg, true);
254}
255
256/**
257 * @brief Disables the output.
258 *
259 * @param[in] compp pointer to the @p COMPDriver object
260 *
261 * @notapi
262 */
264 ACMP_Enable(compp->reg, false);
265 ACMP_DisableInterrupts(compp->reg, compp->irq_mask);
266 compp->irq_mask = 0;
267}
268
270 uint32_t status = ACMP_GetStatusFlags(compp->reg);
271 // clear flags to continue
272 ACMP_ClearStatusFlags(compp->reg, status);
273 return status;
274}
275
276#endif /* HAL_USE_COMP */
277
278/** @} */
uint16_t channel
Definition adc_inputs.h:104
void comp_lld_channel_disable(COMPDriver *compp, uint32_t channel)
Deactivate an EXT channel (used by comp)
OSAL_IRQ_HANDLER(KINETIS_COMP0_IRQ_VECTOR)
COMP1, COMP2, COMP3 interrupt handler.
COMPDriver COMPD1
COMPD1 driver identifier.
void comp_lld_set_dac_value(COMPDriver *compp, uint32_t value)
Set the DAC value used by comp.
void comp_lld_enable(COMPDriver *compp)
Enables the output.
void comp_lld_init(void)
Low level COMP driver initialization.
static void comp_lld_irq_handler(COMPDriver *compp)
void comp_lld_stop(COMPDriver *compp)
Deactivates the comp peripheral.
void comp_lld_channel_enable(COMPDriver *compp, uint32_t channel)
Configures and activates an EXT channel (used by comp)
uint32_t comp_lld_get_status(COMPDriver *compp)
COMPDriver COMPD2
COMPD2 driver identifier.
void comp_lld_start(COMPDriver *compp)
Configures and activates the COMP peripheral.
void comp_lld_disable(COMPDriver *compp)
Disables the output.
COMPDriver COMPD3
COMPD3 driver identifier.
@ COMP_OUTPUT_INVERTED
@ COMP_IRQ_FALLING
@ COMP_IRQ_RISING
@ COMP_IRQ_BOTH
void ACMP_SetFilterConfig(CMP_Type *base, const acmp_filter_config_t *config)
Configures the filter.
Definition fsl_acmp.c:315
acmp_hysteresis_mode_t hysteresisMode
Definition fsl_acmp.h:117
void ACMP_Enable(CMP_Type *base, bool enable)
Enables or disables the ACMP.
Definition fsl_acmp.c:172
uint32_t ACMP_GetStatusFlags(CMP_Type *base)
Gets status flags.
Definition fsl_acmp.c:559
bool enableHighSpeed
Definition fsl_acmp.h:118
void ACMP_SetChannelConfig(CMP_Type *base, const acmp_channel_config_t *config)
Sets the channel configuration.
Definition fsl_acmp.c:230
uint32_t minusMuxInput
Definition fsl_acmp.h:139
bool enableInvertOutput
Definition fsl_acmp.h:119
acmp_port_input_t positivePortInput
Definition fsl_acmp.h:133
uint32_t DACValue
Definition fsl_acmp.h:154
void ACMP_Init(CMP_Type *base, const acmp_config_t *config)
Initializes the ACMP.
Definition fsl_acmp.c:70
uint32_t filterPeriod
Definition fsl_acmp.h:147
void ACMP_DisableInterrupts(CMP_Type *base, uint32_t mask)
Disables interrupts.
Definition fsl_acmp.c:522
void ACMP_Deinit(CMP_Type *base)
Deinitializes the ACMP.
Definition fsl_acmp.c:120
acmp_port_input_t negativePortInput
Definition fsl_acmp.h:137
uint32_t plusMuxInput
Definition fsl_acmp.h:135
void ACMP_SetDACConfig(CMP_Type *base, const acmp_dac_config_t *config)
Configures the internal DAC.
Definition fsl_acmp.c:348
bool useUnfilteredOutput
Definition fsl_acmp.h:120
void ACMP_EnableInterrupts(CMP_Type *base, uint32_t mask)
Enables interrupts.
Definition fsl_acmp.c:485
acmp_reference_voltage_source_t referenceVoltageSource
Definition fsl_acmp.h:153
void ACMP_ClearStatusFlags(CMP_Type *base, uint32_t mask)
Clears status flags.
Definition fsl_acmp.c:589
void ACMP_GetDefaultConfig(acmp_config_t *config)
Gets the default configuration for ACMP.
Definition fsl_acmp.c:148
uint32_t filterCount
Definition fsl_acmp.h:146
@ kACMP_OutputFallingInterruptEnable
Definition fsl_acmp.h:38
@ kACMP_OutputRisingInterruptEnable
Definition fsl_acmp.h:37
@ kACMP_VrefSourceVin1
Definition fsl_acmp.h:82
@ kACMP_HysteresisLevel2
Definition fsl_acmp.h:75
@ kACMP_PortInputFromDAC
Definition fsl_acmp.h:90
@ kACMP_PortInputFromMux
Definition fsl_acmp.h:91
Configuration for filter.
Definition fsl_acmp.h:144
compcallback_t cb
Callback.
comp_irq_mode_t irq_mode
Ouput mode.
comp_output_mode_t output_mode
Ouput mode.
Structure representing an COMP driver.
COMP_DRIVER_EXT_FIELDS COMP_TypeDef * reg
Pointer to the COMPx registers block.
acmp_channel_config_t acmpChannelCfg
ACMP driver channel config.
uint32_t irq_mask
acmp_config_t acmpCfg
ACMP driver config.
acmp_dac_config_t acmpDacCfg
ACMP driver DAC config.
const COMPConfig * config
Current configuration data.