2018-04-22 09:43:49 -03:00
|
|
|
/*
|
|
|
|
* This file 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.
|
|
|
|
*
|
|
|
|
* This file 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-02-20 23:44:56 -04:00
|
|
|
#include <hal.h>
|
2018-04-22 09:43:49 -03:00
|
|
|
#include "SoftSigReaderInt.h"
|
2018-04-25 20:10:27 -03:00
|
|
|
#include "hwdef/common/stm32_util.h"
|
2018-04-22 09:43:49 -03:00
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
|
|
|
|
|
|
|
using namespace ChibiOS;
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#if HAL_USE_EICU == TRUE
|
|
|
|
|
2018-06-02 10:19:46 -03:00
|
|
|
#if STM32_EICU_USE_TIM10 || STM32_EICU_USE_TIM11 || STM32_EICU_USE_TIM13 || STM32_EICU_USE_TIM14
|
|
|
|
#error "Timers with only one channel are not supported"
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 09:43:49 -03:00
|
|
|
// singleton instance
|
2019-02-10 14:53:21 -04:00
|
|
|
SoftSigReaderInt *SoftSigReaderInt::_singleton;
|
2018-04-22 09:43:49 -03:00
|
|
|
|
|
|
|
SoftSigReaderInt::SoftSigReaderInt()
|
|
|
|
{
|
2019-02-10 14:53:21 -04:00
|
|
|
_singleton = this;
|
2018-04-22 09:43:49 -03:00
|
|
|
}
|
|
|
|
|
2018-06-02 10:19:46 -03:00
|
|
|
eicuchannel_t SoftSigReaderInt::get_pair_channel(eicuchannel_t channel)
|
|
|
|
{
|
|
|
|
switch (channel) {
|
|
|
|
case EICU_CHANNEL_1:
|
|
|
|
return EICU_CHANNEL_2;
|
|
|
|
case EICU_CHANNEL_2:
|
|
|
|
return EICU_CHANNEL_1;
|
|
|
|
case EICU_CHANNEL_3:
|
|
|
|
return EICU_CHANNEL_4;
|
|
|
|
case EICU_CHANNEL_4:
|
|
|
|
return EICU_CHANNEL_3;
|
|
|
|
case EICU_CHANNEL_ENUM_END:
|
|
|
|
return EICU_CHANNEL_ENUM_END;
|
|
|
|
}
|
|
|
|
return EICU_CHANNEL_ENUM_END;
|
|
|
|
}
|
|
|
|
|
2018-04-22 09:43:49 -03:00
|
|
|
void SoftSigReaderInt::init(EICUDriver* icu_drv, eicuchannel_t chan)
|
|
|
|
{
|
|
|
|
last_value = 0;
|
|
|
|
_icu_drv = icu_drv;
|
2018-06-02 10:19:46 -03:00
|
|
|
eicuchannel_t aux_chan = get_pair_channel(chan);
|
2018-04-22 09:43:49 -03:00
|
|
|
icucfg.dier = 0;
|
|
|
|
icucfg.frequency = INPUT_CAPTURE_FREQUENCY;
|
|
|
|
for (int i=0; i< EICU_CHANNEL_ENUM_END; i++) {
|
|
|
|
icucfg.iccfgp[i]=nullptr;
|
|
|
|
}
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-06-02 10:19:46 -03:00
|
|
|
//configure main channel
|
2018-04-22 09:43:49 -03:00
|
|
|
icucfg.iccfgp[chan] = &channel_config;
|
2018-04-30 15:41:55 -03:00
|
|
|
#ifdef HAL_RCIN_IS_INVERTED
|
2018-04-22 09:43:49 -03:00
|
|
|
channel_config.alvl = EICU_INPUT_ACTIVE_HIGH;
|
2018-04-30 15:41:55 -03:00
|
|
|
#else
|
|
|
|
channel_config.alvl = EICU_INPUT_ACTIVE_LOW;
|
|
|
|
#endif
|
2018-06-02 10:19:46 -03:00
|
|
|
channel_config.capture_cb = nullptr;
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-06-02 10:19:46 -03:00
|
|
|
//configure aux channel
|
|
|
|
icucfg.iccfgp[aux_chan] = &aux_channel_config;
|
|
|
|
#ifdef HAL_RCIN_IS_INVERTED
|
|
|
|
aux_channel_config.alvl = EICU_INPUT_ACTIVE_LOW;
|
|
|
|
#else
|
|
|
|
aux_channel_config.alvl = EICU_INPUT_ACTIVE_HIGH;
|
|
|
|
#endif
|
|
|
|
aux_channel_config.capture_cb = _irq_handler;
|
|
|
|
|
2018-04-22 09:43:49 -03:00
|
|
|
eicuStart(_icu_drv, &icucfg);
|
2018-04-25 20:10:27 -03:00
|
|
|
//sets input filtering to 4 timer clock
|
|
|
|
stm32_timer_set_input_filter(_icu_drv->tim, chan, 2);
|
2019-10-20 10:31:12 -03:00
|
|
|
//sets input for aux_chan
|
2018-06-02 10:19:46 -03:00
|
|
|
stm32_timer_set_channel_input(_icu_drv->tim, aux_chan, 2);
|
2018-04-22 09:43:49 -03:00
|
|
|
eicuEnable(_icu_drv);
|
|
|
|
}
|
|
|
|
|
2020-01-28 22:21:54 -04:00
|
|
|
void SoftSigReaderInt::disable(void)
|
|
|
|
{
|
|
|
|
eicuDisable(_icu_drv);
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:19:46 -03:00
|
|
|
void SoftSigReaderInt::_irq_handler(EICUDriver *eicup, eicuchannel_t aux_channel)
|
2018-05-27 11:37:43 -03:00
|
|
|
{
|
2018-06-02 10:19:46 -03:00
|
|
|
eicuchannel_t channel = get_pair_channel(aux_channel);
|
2018-11-05 07:02:06 -04:00
|
|
|
pulse_t pulse;
|
|
|
|
pulse.w0 = eicup->tim->CCR[channel];
|
|
|
|
pulse.w1 = eicup->tim->CCR[aux_channel];
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2019-02-10 14:53:21 -04:00
|
|
|
_singleton->sigbuf.push(pulse);
|
2018-11-05 07:02:06 -04:00
|
|
|
|
2019-10-20 10:31:12 -03:00
|
|
|
//check for missed interrupt
|
2018-06-02 10:19:46 -03:00
|
|
|
uint32_t mask = (STM32_TIM_SR_CC1OF << channel) | (STM32_TIM_SR_CC1OF << aux_channel);
|
|
|
|
if ((eicup->tim->SR & mask) != 0) {
|
2018-05-27 11:37:43 -03:00
|
|
|
//we have missed some pulses
|
2018-06-02 10:19:46 -03:00
|
|
|
//try to reset RCProtocol parser by returning invalid value (i.e. 0 width pulse)
|
2018-11-05 07:02:06 -04:00
|
|
|
pulse.w0 = 0;
|
|
|
|
pulse.w1 = 0;
|
2019-02-10 14:53:21 -04:00
|
|
|
_singleton->sigbuf.push(pulse);
|
2018-06-02 10:19:46 -03:00
|
|
|
//reset overcapture mask
|
|
|
|
eicup->tim->SR &= ~mask;
|
2018-05-27 11:37:43 -03:00
|
|
|
}
|
2018-04-22 09:43:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SoftSigReaderInt::read(uint32_t &widths0, uint32_t &widths1)
|
|
|
|
{
|
|
|
|
if (sigbuf.available() >= 2) {
|
2018-11-05 07:02:06 -04:00
|
|
|
pulse_t pulse;
|
|
|
|
if (sigbuf.pop(pulse)) {
|
|
|
|
widths0 = uint16_t(pulse.w0 - last_value);
|
|
|
|
widths1 = uint16_t(pulse.w1 - pulse.w0);
|
|
|
|
last_value = pulse.w1;
|
2018-04-22 09:43:49 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAL_USE_EICU
|
|
|
|
|
|
|
|
#endif //CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|