From 268c6d0b7d53fef8ae3aab0552c73b9ab2e5c4e7 Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Thu, 15 Jun 2017 16:45:21 +0200 Subject: [PATCH] stm32_hrtim: outputs enable, period and compare functions, cosmetics --- arch/arm/src/stm32/stm32_hrtim.c | 237 +++++++++++++++++++++++++++++-- arch/arm/src/stm32/stm32_hrtim.h | 40 +++++- 2 files changed, 256 insertions(+), 21 deletions(-) diff --git a/arch/arm/src/stm32/stm32_hrtim.c b/arch/arm/src/stm32/stm32_hrtim.c index c2b65b023f..bb96c53274 100644 --- a/arch/arm/src/stm32/stm32_hrtim.c +++ b/arch/arm/src/stm32/stm32_hrtim.c @@ -222,9 +222,7 @@ struct stm32_hrtim_capture_s struct stm32_hrtim_timcmn_s { - uint16_t cmp[4]; /* Compare registers */ uint32_t base; /* The base adress of the timer */ - uint32_t frequency; /* Current frequency setting */ uint32_t pclk; /* The frequency of the peripheral clock * that drives the timer module */ #ifdef CONFIG_STM32_HRTIM_DMA @@ -408,6 +406,7 @@ static uint32_t hrtim_tim_getreg(FAR struct stm32_hrtim_s *priv, uint8_t timer, int offset); static FAR struct stm32_hrtim_tim_s *hrtim_tim_get(FAR struct stm32_hrtim_s *priv, uint8_t timer); +static uint32_t hrtim_base_get(FAR struct stm32_hrtim_s* priv, uint8_t timer); /* Configuration */ @@ -440,6 +439,14 @@ static int hrtim_eev_config(FAR struct stm32_hrtim_s *priv); #ifdef HRTIM_HAVE_INTERRUPTS static int hrtim_irq_config(FAR struct stm32_hrtim_s *priv); #endif +static int hrtim_cmp_update(FAR struct stm32_hrtim_s *priv, uint8_t timer, + uint8_t index, uint16_t cmp); +static int hrtim_per_update(FAR struct stm32_hrtim_s *priv, uint8_t timer, + uint16_t per); +static uint16_t hrtim_per_get(FAR struct stm32_hrtim_s *priv, uint8_t timer); +static uint16_t hrtim_cmp_get(FAR struct stm32_hrtim_s *priv, uint8_t timer, + uint8_t index); + /* Initialization */ @@ -941,7 +948,7 @@ static void hrtim_tim_modifyreg(FAR struct stm32_hrtim_s *priv, uint8_t timer, } /**************************************************************************** - * Name: stm32_dll_cal + * Name: hrtim_dll_cal * * Description: * Calibrate HRTIM DLL @@ -984,7 +991,7 @@ static int hrtim_dll_cal(FAR struct stm32_hrtim_s *priv) } /**************************************************************************** - * Name: stm32_tim_clock_config + * Name: hrtim_tim_clock_config * * Description: * Configure HRTIM Timer clock @@ -1061,7 +1068,7 @@ errout: } /**************************************************************************** - * Name: stm32_tim_clocks_config + * Name: hrtim_tim_clocks_config * * Description: * Configure HRTIM Timers Clocks @@ -1141,7 +1148,7 @@ errout: } /**************************************************************************** - * Name: stm32_gpios_config + * Name: hrtim_gpios_config * * Description: * Configure HRTIM GPIO @@ -1339,7 +1346,7 @@ static int hrtim_gpios_config(FAR struct stm32_hrtim_s *priv) #endif /**************************************************************************** - * Name: stm32_inputs_config + * Name: hrtim_inputs_config * * Description: * Configure HRTIM Inputs @@ -1369,7 +1376,7 @@ static int hrtim_inputs_config(FAR struct stm32_hrtim_s *priv) /**************************************************************************** - * Name: stm32_synch_config + * Name: hrtim_synch_config * * Description: * Configure HRTIM Synchronization Input/Output @@ -1391,7 +1398,7 @@ static int hrtim_synch_config(FAR struct stm32_hrtim_s *priv) #endif /**************************************************************************** - * Name: stm32_tim_outputs_config + * Name: hrtim_tim_outputs_config * * Description: * Configure HRTIM Slave Timer Outputs (CH1 and CH2) @@ -1458,7 +1465,7 @@ errout: #endif /**************************************************************************** - * Name: stm32_outputs_config + * Name: hrtim_outputs_config * * Description: * Configure HRTIM Outputs @@ -1529,10 +1536,50 @@ static int hrtim_outputs_config(FAR struct stm32_hrtim_s *priv) errout: return ret; } + +/**************************************************************************** + * Name: hrtim_outputs_enable + * + * Description: + * Enable/disable HRTIM outputs (bulk operation) + * + * Input Parameters: + * priv - A reference to the HRTIM structure + * outputs - outputs to set + * state - Enable/disable operation + * + * Returned Value: + * 0 on success, a negated errno value on failure + * + ****************************************************************************/ + +static int hrtim_outputs_enable(FAR struct stm32_hrtim_s *priv, uint16_t outputs, + bool state) +{ + uint32_t reg = 0; + + /* Get register offset */ + + if (state == true) + { + reg = STM32_HRTIM_CMN_OENR_OFFSET; + } + else + { + reg = STM32_HRTIM_CMN_ODISR_OFFSET; + } + + /* Write register */ + + hrtim_putreg(priv, reg, outputs); + + return OK; +} + #endif /**************************************************************************** - * Name: stm32_adc_config + * Name: hrtim_adc_config * * Description: * Configure HRTIM ADC triggers @@ -1554,7 +1601,7 @@ static int hrtim_adc_config(FAR struct stm32_hrtim_s *priv) #endif /**************************************************************************** - * Name: stm32_faults_config + * Name: hrtim_faults_config * * Description: * Configure HRTIM Faults @@ -1576,7 +1623,7 @@ static int hrtim_faults_config(FAR struct stm32_hrtim_s *priv) #endif /**************************************************************************** - * Name: stm32_eev_config + * Name: hrtim_eev_config * * Description: * Configure HRTIM External Events @@ -1598,7 +1645,7 @@ static int hrtim_eev_config(FAR struct stm32_hrtim_s *priv) #endif /**************************************************************************** - * Name: stm32_irq_config + * Name: hrtim_irq_config * * Description: * Configure HRTIM interrupts @@ -1620,7 +1667,7 @@ static int hrtim_irq_config(FAR struct stm32_hrtim_s *priv) #endif /**************************************************************************** - * Name: stm32_preload_config + * Name: hrtim_preload_config * * Description: * Configure HRTIM preload registers @@ -1668,6 +1715,165 @@ static void hrtim_preload_config(FAR struct stm32_hrtim_s *priv) } +/**************************************************************************** + * Name: hrtim_cmp_update + * + * Description: + * Try update HRTIM Timer compare register. + * + * Input parameters: + * priv - A reference to the HRTIM block + * timer - HRTIM Timer timer + * index - Compare register timer + * cmp - New compare register value + * + * Returned Value: + * Zero on success; a negated errno value on failure + * + ****************************************************************************/ + +static int hrtim_cmp_update(FAR struct stm32_hrtim_s *priv, uint8_t timer, + uint8_t index, uint16_t cmp) +{ + int ret = OK; + uint32_t offset = 0; + + switch (cmp) + { + case HRTIM_CMP1: + { + offset = STM32_HRTIM_TIM_CMP1R_OFFSET; + break; + } + case HRTIM_CMP2: + { + offset = STM32_HRTIM_TIM_CMP2R_OFFSET; + break; + } + case HRTIM_CMP3: + { + offset = STM32_HRTIM_TIM_CMP3R_OFFSET; + break; + } + case HRTIM_CMP4: + { + offset = STM32_HRTIM_TIM_CMP4R_OFFSET; + break; + } + default: + { + ret = -EINVAL; + goto errout; + } + } + + hrtim_tim_putreg(priv, timer, offset, cmp); + +errout: + return ret; +} + +/**************************************************************************** + * Name: hrtim_per_update + * + * Description: + * Try update HRTIM Timer period register. + * + * Input parameters: + * priv - A reference to the HRTIM block + * timer - HRTIM Timer timer + * per - New period register value + * + * Returned Value: + * Zero on success; a negated errno value on failure + * + ****************************************************************************/ + +static int hrtim_per_update(FAR struct stm32_hrtim_s *priv, uint8_t timer, + uint16_t per) +{ + hrtim_tim_putreg(priv, timer, STM32_HRTIM_TIM_PER_OFFSET, per); + + return OK; +} + +/**************************************************************************** + * Name: hrtim_per_get + * + * Description: + * Get HRTIM Timer period value + * + * Input parameters: + * priv - A reference to the HRTIM block + * timer - HRTIM Timer timer + * + * Returned Value: + * Zero on success; a negated errno value on failure + * + ****************************************************************************/ + +static uint16_t hrtim_per_get(FAR struct stm32_hrtim_s *priv, uint8_t timer) +{ + return (uint16_t)hrtim_tim_getreg(priv, timer, STM32_HRTIM_TIM_PER_OFFSET); +} + +/**************************************************************************** + * Name: hrtim_cmp_update + * + * Description: + * Get HRTIM Timer compare register + * + * Input parameters: + * priv - A reference to the HRTIM block + * timer - HRTIM Timer timer + * index - Compare register timer + * + * Returned Value: + * Zero on success; a negated errno value on failure + * + ****************************************************************************/ + +static uint16_t hrtim_cmp_get(FAR struct stm32_hrtim_s *priv, uint8_t timer, + uint8_t index) +{ + uint16_t cmpx = 0; + uint32_t offset = 0; + + switch (index) + { + case HRTIM_CMP1: + { + offset = STM32_HRTIM_TIM_CMP1R_OFFSET; + break; + } + case HRTIM_CMP2: + { + offset = STM32_HRTIM_TIM_CMP2R_OFFSET; + break; + } + case HRTIM_CMP3: + { + offset = STM32_HRTIM_TIM_CMP3R_OFFSET; + break; + } + case HRTIM_CMP4: + { + offset = STM32_HRTIM_TIM_CMP4R_OFFSET; + break; + } + default: + { + cmpx = 0; + goto errout; + } + } + + cmpx = (uint16_t)hrtim_tim_getreg(priv, timer, offset); + +errout: + return cmpx; +} + /**************************************************************************** * Name: stm32_hrtimconfig * @@ -1856,6 +2062,7 @@ errout: * ****************************************************************************/ + FAR struct hrtim_dev_s* stm32_hrtiminitialize(void) { FAR struct hrtim_dev_s *dev; diff --git a/arch/arm/src/stm32/stm32_hrtim.h b/arch/arm/src/stm32/stm32_hrtim.h index e20d71e630..8fa0864d47 100644 --- a/arch/arm/src/stm32/stm32_hrtim.h +++ b/arch/arm/src/stm32/stm32_hrtim.h @@ -64,21 +64,21 @@ enum stm32_hrtim_tim_e { - HRTIM_TIMER_MASTER, + HRTIM_TIMER_MASTER = 0, #ifdef CONFIG_STM32_HRTIM_TIMA - HRTIM_TIMER_TIMA, + HRTIM_TIMER_TIMA = 1, #endif #ifdef CONFIG_STM32_HRTIM_TIMB - HRTIM_TIMER_TIMB, + HRTIM_TIMER_TIMB = 2, #endif #ifdef CONFIG_STM32_HRTIM_TIMC - HRTIM_TIMER_TIMC, + HRTIM_TIMER_TIMC = 3, #endif #ifdef CONFIG_STM32_HRTIM_TIMD - HRTIM_TIMER_TIMD, + HRTIM_TIMER_TIMD = 4, #endif #ifdef CONFIG_STM32_HRTIM_TIME - HRTIM_TIMER_TIME, + HRTIM_TIMER_TIME = 5, #endif }; @@ -242,6 +242,34 @@ enum stm32_hrtim_eev_src_e HRTIM_EEV_SRC_ADC }; +/* Compare register index */ + +enum stm32_hrtim_cmp_index_e +{ + HRTIM_CMP1, + HRTIM_CMP2, + HRTIM_CMP3, + HRTIM_CMP4 +}; + +/* HRTIM Slave Timer Outputs */ + +enum stm32_outputs_e +{ + HRTIM_OUT_TIMA_CH1 = (1 << 0), + HRTIM_OUT_TIMA_CH2 = (1 << 1), + HRTIM_OUT_TIMB_CH1 = (1 << 2), + HRTIM_OUT_TIMB_CH2 = (1 << 3), + HRTIM_OUT_TIMC_CH1 = (1 << 4), + HRTIM_OUT_TIMC_CH2 = (1 << 5), + HRTIM_OUT_TIMD_CH1 = (1 << 6), + HRTIM_OUT_TIMD_CH2 = (1 << 7), + HRTIM_OUT_TIME_CH1 = (1 << 8), + HRTIM_OUT_TIME_CH2 = (1 << 9), +}; + +/* */ + struct hrtim_dev_s { #ifdef CONFIG_HRTIM