wip direct, unfinished config

This commit is contained in:
2018-02-20 22:58:24 +01:00
parent 20dfa7e158
commit 75efa12338
6 changed files with 144 additions and 33 deletions
+60 -6
View File
@@ -7,6 +7,10 @@
#define FCAP_INTERNAL
#include "_fcap_internal.h"
void UFCAP_StopMeasurement(Unit *unit);
void UFCAP_ConfigureForPWMCapture(Unit *unit);
void UFCAP_ConfigureForDirectCapture(Unit *unit);
static void UFCAP_PWMBurstReportJob(Job *job)
{
Unit *unit = job->unit;
@@ -39,7 +43,6 @@ void UFCAP_TimerHandler(void *arg)
if (priv->opmode == OPMODE_PWM_CONT) {
if (LL_TIM_IsActiveFlag_CC1(TIMx)) {
// assert_param(!LL_TIM_IsActiveFlag_CC1OVR(TIMx));
if (priv->n_skip > 0) {
priv->n_skip--;
} else {
@@ -51,7 +54,6 @@ void UFCAP_TimerHandler(void *arg)
}
if (LL_TIM_IsActiveFlag_CC2(TIMx)) {
// assert_param(!LL_TIM_IsActiveFlag_CC2OVR(TIMx));
priv->pwm_cont.ontime = LL_TIM_IC_GetCaptureCH2(TIMx);
LL_TIM_ClearFlag_CC2(TIMx);
LL_TIM_ClearFlag_CC2OVR(TIMx);
@@ -59,7 +61,6 @@ void UFCAP_TimerHandler(void *arg)
}
else if (priv->opmode == OPMODE_PWM_BURST) {
if (LL_TIM_IsActiveFlag_CC1(TIMx)) {
// assert_param(!LL_TIM_IsActiveFlag_CC1OVR(TIMx));
const uint32_t period = LL_TIM_IC_GetCaptureCH1(TIMx);
const uint32_t ontime = priv->pwm_burst.ontime;
@@ -85,7 +86,6 @@ void UFCAP_TimerHandler(void *arg)
}
if (LL_TIM_IsActiveFlag_CC2(TIMx)) {
// assert_param(!LL_TIM_IsActiveFlag_CC2OVR(TIMx));
priv->pwm_burst.ontime = LL_TIM_IC_GetCaptureCH2(TIMx);
LL_TIM_ClearFlag_CC2(TIMx);
LL_TIM_ClearFlag_CC2OVR(TIMx);
@@ -95,6 +95,9 @@ void UFCAP_TimerHandler(void *arg)
// clear everything - in idle it would cycle in the handler forever
TIMx->SR = 0;
}
else {
trap("Unhandled fcap TIMx irq");
}
}
static void UFCAP_ClearTimerConfig(Unit *unit)
@@ -120,9 +123,9 @@ static void UFCAP_ClearTimerConfig(Unit *unit)
void UFCAP_StopMeasurement(Unit *unit)
{
struct priv * const priv = unit->data;
TIM_TypeDef * const TIMx = priv->TIMx;
LL_TIM_DeInit(TIMx); // clear all flags and settings
LL_TIM_DeInit(priv->TIMx); // clear all flags and settings
LL_TIM_DeInit(priv->TIMy); // clear all flags and settings
}
/**
@@ -161,6 +164,11 @@ void UFCAP_SwitchMode(Unit *unit, enum fcap_opmode opmode)
priv->n_skip = 1; // discard the first cycle (will be incomplete)
UFCAP_ConfigureForPWMCapture(unit); // is also stopped and restarted
break;
case OPMODE_COUNTER_CONT:
UFCAP_ConfigureForDirectCapture(unit);
break;
default:
trap("Unhandled opmode %d", (int)opmode);
}
@@ -208,3 +216,49 @@ void UFCAP_ConfigureForPWMCapture(Unit *unit)
LL_TIM_EnableIT_CC2(TIMx);
LL_TIM_EnableCounter(TIMx);
}
/**
* Configure peripherals for an indirect capture (PWM measurement) - continuous or burst
* @param unit
*/
void UFCAP_ConfigureForDirectCapture(Unit *unit)
{
struct priv * const priv = unit->data;
const uint32_t ll_ch_a = priv->ll_ch_a; //
UFCAP_ClearTimerConfig(unit);
{
TIM_TypeDef *const TIMy = priv->TIMy;
uint16_t presc = PLAT_AHB_MHZ * 500; // this produces 2 kHz
uint32_t count = 2001;
LL_TIM_SetPrescaler(TIMy, (uint32_t) (presc - 1));
LL_TIM_SetAutoReload(TIMy, count - 1);
LL_TIM_EnableARRPreload(TIMy);
LL_TIM_GenerateEvent_UPDATE(TIMy);
LL_TIM_SetOnePulseMode(TIMy, LL_TIM_ONEPULSEMODE_SINGLE); // TODO check if this works
LL_TIM_OC_EnableFast(TIMy, LL_TIM_CHANNEL_CH1);
dbg("TIMy presc %d, count %d", (int) presc, (int) count);
LL_TIM_SetTriggerOutput(TIMy, LL_TIM_TRGO_OC1REF);
LL_TIM_OC_SetMode(TIMy, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_PWM1); // 1 until CC, then 0
LL_TIM_OC_SetCompareCH1(TIMy, count - 1); // XXX maybe this must be lower
LL_TIM_CC_EnableChannel(TIMy, LL_TIM_CHANNEL_CH1); // enable the output channel that produces a trigger
}
{
// TIMx - the slave
TIM_TypeDef *const TIMx = priv->TIMx;
LL_TIM_SetSlaveMode(TIMx, LL_TIM_SLAVEMODE_GATED);
LL_TIM_SetTriggerInput(TIMx, LL_TIM_TS_ITR3); // ITR3 is TIM14 which we use as TIMy
LL_TIM_EnableMasterSlaveMode(TIMx);
LL_TIM_EnableExternalClock(TIMx); // TODO must check and deny this mode if the pin is not on CH1 = external trigger input
LL_TIM_EnableCounter(TIMx);
}
LL_TIM_EnableCounter(priv->TIMy); // XXX this will start the pulse (maybe)
}
+12 -1
View File
@@ -33,6 +33,9 @@ error_t UFCAP_init(Unit *unit)
TIM_TypeDef * const TIMx = TIM2;
Resource timRsc = R_TIM2;
TIM_TypeDef * const TIMy = TIM14;
Resource tim2Rsc = R_TIM14;
uint32_t ll_ch_a = 0;
uint32_t ll_ch_b = 0;
@@ -79,6 +82,7 @@ error_t UFCAP_init(Unit *unit)
TRY(rsc_claim_pin(unit, priv->signal_pname, priv->signal_pnum));
TRY(rsc_claim(unit, timRsc));
TRY(rsc_claim(unit, tim2Rsc));
// ---- INIT ----
assert_param(ll_ch_a != ll_ch_b);
@@ -91,7 +95,9 @@ error_t UFCAP_init(Unit *unit)
TRY(hw_configure_gpio_af(priv->signal_pname, priv->signal_pnum, ll_timpin_af));
hw_periph_clock_enable(TIMx);
hw_periph_clock_enable(TIMy);
irqd_attach(TIMx, UFCAP_TimerHandler, unit);
// TODO attach TIMy to a handler
UFCAP_SwitchMode(unit, OPMODE_IDLE);
@@ -108,8 +114,13 @@ void UFCAP_deInit(Unit *unit)
UFCAP_SwitchMode(unit, OPMODE_IDLE);
TIM_TypeDef *TIMx = priv->TIMx;
TIM_TypeDef *TIMy = priv->TIMy;
LL_TIM_DeInit(TIMx);
irqd_attach(TIMx, UFCAP_TimerHandler, unit);
LL_TIM_DeInit(TIMy);
irqd_detach(TIMx, UFCAP_TimerHandler);
// TODO detach TIMy when any handler is added
hw_periph_clock_disable(TIMx);
hw_periph_clock_disable(TIMy);
}
// Release all resources, deinit pins
+9 -6
View File
@@ -16,6 +16,8 @@ enum fcap_opmode {
OPMODE_BUSY = 1, // used after capture is done, before it's reported
OPMODE_PWM_CONT = 2,
OPMODE_PWM_BURST = 3, // averaging
OPMODE_COUNTER_CONT = 4,
OPMODE_COUNTER_BURST = 5, // averaging
};
/** Private data structure */
@@ -26,6 +28,7 @@ struct priv {
// internal state
TIM_TypeDef *TIMx;
TIM_TypeDef *TIMy; // used as a timebase source for TIMx in direct mode
uint32_t ll_ch_b;
uint32_t ll_ch_a;
bool a_direct;
@@ -49,6 +52,10 @@ struct priv {
uint16_t n_count; //!< Periods captured
uint16_t n_target; //!< Periods captured - requested count
} pwm_burst;
struct {
uint32_t last_count; //!< Pulse count in the last capture window
} cnt_cont;
};
};
@@ -79,12 +86,8 @@ void UFCAP_deInit(Unit *unit);
// ------------------------------------------------------------------------
void UFCAP_TimerHandler(void *arg);
void UFCAP_StopMeasurement(Unit *unit);
void UFCAP_ConfigureForPWMCapture(Unit *unit);
void UFCAP_SwitchMode(Unit *unit, enum fcap_opmode opmode);
void UFCAP_TimerHandler(void *arg);
#endif //GEX_F072_FCAP_INTERNAL_H
+30 -7
View File
@@ -12,29 +12,32 @@
enum FcapCmd_ {
CMD_STOP = 0,
CMD_PWM_CONT_START = 1,
CMD_PWM_BURST_START = 2,
CMD_PWM_CONT_READ = 10,
CMD_INDIRECT_CONT_START = 1,
CMD_INDIRECT_BURST_START = 2,
CMD_DIRECT_CONT_START = 3,
CMD_INDIRECT_CONT_READ = 10,
CMD_DIRECT_CONT_READ = 11,
};
/** Handle a request message */
static error_t UFCAP_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command, PayloadParser *pp)
{
struct priv *priv = unit->data;
PayloadBuilder pb = pb_start(unit_tmp512, UNIT_TMP_LEN, NULL);
switch (command) {
case CMD_STOP:
UFCAP_SwitchMode(unit, OPMODE_IDLE);
return E_SUCCESS;
case CMD_PWM_CONT_START:
case CMD_INDIRECT_CONT_START:
if (priv->opmode == OPMODE_PWM_CONT) return E_SUCCESS; // no-op
if (priv->opmode != OPMODE_IDLE) return E_BUSY;
UFCAP_SwitchMode(unit, OPMODE_PWM_CONT);
return E_SUCCESS;
case CMD_PWM_BURST_START:
case CMD_INDIRECT_BURST_START:
if (priv->opmode != OPMODE_IDLE) return E_BAD_MODE;
uint16_t count = pp_u16(pp);
@@ -43,7 +46,7 @@ static error_t UFCAP_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command,
UFCAP_SwitchMode(unit, OPMODE_PWM_BURST);
return E_SUCCESS;
case CMD_PWM_CONT_READ:
case CMD_INDIRECT_CONT_READ:
if (priv->opmode != OPMODE_PWM_CONT) {
return E_BAD_MODE;
}
@@ -51,7 +54,6 @@ static error_t UFCAP_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command,
return E_BUSY;
}
PayloadBuilder pb = pb_start(unit_tmp512, UNIT_TMP_LEN, NULL);
pb_u16(&pb, PLAT_AHB_MHZ);
pb_u32(&pb, priv->pwm_cont.last_period);
pb_u32(&pb, priv->pwm_cont.last_ontime);
@@ -59,6 +61,27 @@ static error_t UFCAP_handleRequest(Unit *unit, TF_ID frame_id, uint8_t command,
com_respond_pb(frame_id, MSG_SUCCESS, &pb);
return E_SUCCESS;
case CMD_DIRECT_CONT_START:
if (priv->opmode == OPMODE_COUNTER_CONT) return E_SUCCESS; // no-op
if (priv->opmode != OPMODE_IDLE) return E_BUSY;
UFCAP_SwitchMode(unit, OPMODE_COUNTER_CONT);
return E_SUCCESS;
case CMD_DIRECT_CONT_READ:
if (priv->opmode != OPMODE_COUNTER_CONT) {
return E_BAD_MODE;
}
if (priv->cnt_cont.last_count == 0) {
return E_BUSY;
}
// TODO also add the window len - may need to shorten it for fast signals (or if fast capture is required)
pb_u32(&pb, priv->cnt_cont.last_count);
com_respond_pb(frame_id, MSG_SUCCESS, &pb);
return E_SUCCESS;
default:
return E_UNKNOWN_COMMAND;
}