2022-10-09 03:38:28 -07:00
|
|
|
/*
|
|
|
|
* sent_hw_icu.cpp
|
|
|
|
*
|
|
|
|
* SENT protocol glue to ChibiOS ICU driver
|
|
|
|
*
|
|
|
|
* @date Oct 01, 2022
|
|
|
|
* @author Andrey Gusakov <dron0gus@gmail.com>, (c) 2022
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#include "sent.h"
|
|
|
|
#include "sent_hw_icu.h"
|
|
|
|
|
|
|
|
#include "io_pins.h"
|
|
|
|
#include "pin_repository.h"
|
|
|
|
|
|
|
|
/* This SENT HW driver is based on ChibiOS ICU driver */
|
|
|
|
#if (HAL_USE_ICU == TRUE)
|
|
|
|
|
|
|
|
/* TODO: get at runtime */
|
2022-10-19 14:08:15 -07:00
|
|
|
#define SENT_ICU_FREQ (CORE_CLOCK / 2) // == CPU freq / 2
|
2022-10-09 03:38:28 -07:00
|
|
|
|
|
|
|
/* TODO: implement helper to get AF from GPIO for TIM2 capture */
|
|
|
|
#define SENT_INPUT_AF PAL_MODE_ALTERNATE(1)
|
|
|
|
|
|
|
|
/* TODO: implement helper to get ICU and channel from GPIO */
|
|
|
|
#define SENT_ICU_UNIT ICUD2 /* TIM2 */
|
|
|
|
#define SENT_ICU_CHANNEL ICU_CHANNEL_2
|
|
|
|
|
|
|
|
/* ICU callbacks */
|
|
|
|
static void icuperiodcb_in1(ICUDriver *icup)
|
|
|
|
{
|
|
|
|
SENT_ISR_Handler(0, icuGetPeriodX(icup));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ICU configs */
|
|
|
|
static ICUConfig icucfg_in1 =
|
|
|
|
{
|
|
|
|
.mode = ICU_INPUT_ACTIVE_LOW,
|
|
|
|
.frequency = SENT_ICU_FREQ,
|
|
|
|
.width_cb = NULL,
|
|
|
|
.period_cb = icuperiodcb_in1,
|
|
|
|
.overflow_cb = NULL,
|
|
|
|
.channel = SENT_ICU_CHANNEL,
|
|
|
|
.dier = 0U,
|
|
|
|
.arr = 0xFFFFFFFFU,
|
|
|
|
};
|
|
|
|
|
|
|
|
void startSent()
|
|
|
|
{
|
2022-10-19 14:08:15 -07:00
|
|
|
brain_pin_e sentPin = engineConfiguration->sentInputPins[0];
|
|
|
|
|
|
|
|
if (isBrainPinValid(sentPin)) {
|
|
|
|
efiSetPadMode("SENT", sentPin, SENT_INPUT_AF);
|
2022-10-09 03:38:28 -07:00
|
|
|
|
|
|
|
icuStart(&SENT_ICU_UNIT, &icucfg_in1);
|
|
|
|
icuStartCapture(&SENT_ICU_UNIT);
|
|
|
|
icuEnableNotifications(&SENT_ICU_UNIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopSent()
|
|
|
|
{
|
2022-10-19 14:08:15 -07:00
|
|
|
brain_pin_e sentPin = engineConfiguration->sentInputPins[0];
|
|
|
|
|
|
|
|
if (isBrainPinValid(sentPin)) {
|
2022-10-09 03:38:28 -07:00
|
|
|
icuDisableNotifications(&SENT_ICU_UNIT);
|
|
|
|
icuStopCapture(&SENT_ICU_UNIT);
|
|
|
|
icuStop(&SENT_ICU_UNIT);
|
|
|
|
|
2022-10-19 14:08:15 -07:00
|
|
|
efiSetPadUnused(sentPin);
|
2022-10-09 03:38:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAL_USE_ICU */
|
|
|
|
|
|
|
|
#endif /* EFI_PROD_CODE */
|