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-26 12:33:47 -07:00
|
|
|
/* Max timer clock for most timers on STM32 is CPU clock / 2 */
|
|
|
|
#define SENT_TIMER_CLOCK_DIV 2
|
|
|
|
#define SENT_ICU_FREQ (CORE_CLOCK / SENT_TIMER_CLOCK_DIV) // == CPU freq / 2
|
2022-10-09 03:38:28 -07:00
|
|
|
|
|
|
|
/* ICU callbacks */
|
|
|
|
static void icuperiodcb_in1(ICUDriver *icup)
|
|
|
|
{
|
2022-10-26 12:33:47 -07:00
|
|
|
SENT_ISR_Handler(0, icuGetPeriodX(icup) * SENT_TIMER_CLOCK_DIV);
|
2022-10-09 03:38:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ICU configs */
|
2022-10-26 12:33:47 -07:00
|
|
|
static ICUConfig icucfg[SENT_INPUT_COUNT] =
|
2022-10-09 03:38:28 -07:00
|
|
|
{
|
2022-10-26 12:33:47 -07:00
|
|
|
{
|
|
|
|
.mode = ICU_INPUT_ACTIVE_LOW,
|
|
|
|
.frequency = SENT_ICU_FREQ,
|
|
|
|
.width_cb = NULL,
|
|
|
|
.period_cb = icuperiodcb_in1,
|
|
|
|
.overflow_cb = NULL,
|
|
|
|
.channel = ICU_CHANNEL_1, /* will be overwriten on startSent() */
|
|
|
|
.dier = 0U,
|
|
|
|
.arr = 0xFFFFFFFFU,
|
|
|
|
}
|
2022-10-09 03:38:28 -07:00
|
|
|
};
|
|
|
|
|
2023-03-01 12:47:37 -08:00
|
|
|
void startSent() {
|
2022-10-20 10:33:16 -07:00
|
|
|
for (int i = 0; i < SENT_INPUT_COUNT; i++) {
|
|
|
|
brain_input_pin_e sentPin = engineConfiguration->sentInputPins[i];
|
2022-10-19 14:08:15 -07:00
|
|
|
|
2022-10-26 12:33:47 -07:00
|
|
|
if (!isBrainPinValid(sentPin)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICUConfig *cfg = &icucfg[i];
|
|
|
|
ICUDriver *icu;
|
|
|
|
iomode_t pinAF;
|
|
|
|
uint32_t baseClock;
|
2022-10-09 03:38:28 -07:00
|
|
|
|
2022-10-26 12:33:47 -07:00
|
|
|
if (getIcuParams(sentPin, &pinAF, &icu, &cfg->channel, &baseClock) != true) {
|
|
|
|
/* this pin has no ICU functionality, of ICU driver is not enabled for TIM on this pin */
|
2023-03-01 12:47:37 -08:00
|
|
|
firmwareError(OBD_PCM_Processor_Fault, "No ICU on selected SENT pin");
|
2022-10-26 12:33:47 -07:00
|
|
|
continue;
|
2022-10-20 10:33:16 -07:00
|
|
|
}
|
2022-10-26 12:33:47 -07:00
|
|
|
|
|
|
|
efiSetPadMode("SENT", sentPin, PAL_MODE_ALTERNATE(pinAF));
|
|
|
|
|
|
|
|
icuStart(icu, cfg);
|
|
|
|
icuStartCapture(icu);
|
|
|
|
icuEnableNotifications(icu);
|
2022-10-09 03:38:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 12:47:37 -08:00
|
|
|
void stopSent() {
|
2022-10-20 10:33:16 -07:00
|
|
|
for (int i = 0; i < SENT_INPUT_COUNT; i++) {
|
|
|
|
brain_input_pin_e sentPin = activeConfiguration.sentInputPins[i];
|
2022-10-19 14:08:15 -07:00
|
|
|
|
2022-10-26 12:33:47 -07:00
|
|
|
if (!isBrainPinValid(sentPin)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICUDriver *icu;
|
2022-10-09 03:38:28 -07:00
|
|
|
|
2022-10-26 12:33:47 -07:00
|
|
|
if (getIcuParams(sentPin, NULL, &icu, NULL, NULL) != true) {
|
|
|
|
/* this pin has no ICU functionality, of ICU driver is not enabled for TIM on this pin */
|
|
|
|
/* throw error? */
|
|
|
|
continue;
|
2022-10-20 10:33:16 -07:00
|
|
|
}
|
2022-10-26 12:33:47 -07:00
|
|
|
|
|
|
|
icuDisableNotifications(icu);
|
|
|
|
icuStopCapture(icu);
|
|
|
|
icuStop(icu);
|
|
|
|
|
|
|
|
efiSetPadUnused(sentPin);
|
2022-10-09 03:38:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAL_USE_ICU */
|
|
|
|
|
|
|
|
#endif /* EFI_PROD_CODE */
|