Skip to content

HardwareTimer implementation #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2019
Merged

HardwareTimer implementation #576

merged 4 commits into from
Aug 19, 2019

Conversation

ABOSTM
Copy link
Contributor

@ABOSTM ABOSTM commented Jul 26, 2019

HardwareTimer

  1. Introduction
  2. API
  3. Usage
  4. Examples
  5. Dependencies

1. Introduction

The HardwareTimer library aims to provide access to part of STM32 hardware Timer feature (If other features are required, they could be accessed through STM32Cube HAL/LL).

The use of this library suppose you have some basic knowledge of STM32 hardware timer architecture.
First of all remind that all timers are not equivalent and doesn't support the same features. Please refer to the Reference Manual of your MCU.

Example:

  • TIM6 and TIM7 doesn't have outpin and this is the reason why, when available, they are used to implement Tone and Servo.
  • Some timers have up to 4 output channels with 4 complementary channels
    whereas other timers have no complementary, or have only 1 or 2 channels...

Each timer may provide several channels, nevertheless it is important to understand that all channels of the same timer share the same counter as thus have the same period/frequency.

Warning For genericity purpose, HardwareTimer library uses all timers like a 16bits timer (even if some may be wider).

2. API

    void pause(void);  // Pause counter and all output channels
    void resume(void); // Resume counter and all output channels

    void setPrescaleFactor(uint32_t format = TICK_FORMAT); // set prescaler register (which is factor value - 1)
    uint32_t getPrescaleFactor();

    void setOverflow(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set AutoReload register depending on format provided
    uint32_t getOverflow(TimerFormat_t format = TICK_FORMAT); // return overflow depending on format provided

    void setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL); // Set all in one command freq in HZ, Duty in percentage. Including both interrup.
    void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL);


    void setCount(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set timer counter to value 'val' depending on format provided
    uint32_t getCount(TimerFormat_t format = TICK_FORMAT);  // return current counter value of timer depending on format provided

    void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC); // Configure timer channel with specified mode on specified pin if available
    void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin);

    uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided

    void setCaptureCompare(uint32_t channel, uint32_t compare, TimerCompareFormat_t format = TICK_COMPARE_FORMAT);  // set Compare register value of specified channel depending on format provided

    //Add interrupt to period update
    void attachInterrupt(void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon update event (timer rollover)
    void detachInterrupt();  // remove interrupt callback which was attached to update event
    //Add interrupt to capture/compare channel
    void attachInterrupt(uint32_t channel, void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon compare match event of specified channel
    void detachInterrupt(uint32_t channel);  // remove interrupt callback which was attached to compare match event of specified channel

    void timerHandleDeinit();  // Timer deinitialization

    void refresh(void); // Generate update event to force all registers (Autoreload, prescaler, compare) to be taken into account

    uint32_t getTimerClkFreq();  // return timer clock frequency in Hz.

    static void captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Caputre and Compare callback which will call user callback
    static void updateCallback(TIM_HandleTypeDef *htim);  // Generic Update (rollover) callback which will call user callback

3. Usage

HardwareTimer is a C++ class, 1st thing to do is to instantiate an object with TIM instance as parameter.

Note Some instances are used by Servo and Tone (see TIMER_SERVO and TIMER_TONE) but only when they are used. Just be sure there is no conflict with your own usage.

Example:

    HardwareTimer *MyTim = new HardwareTimer(TIM3);  // TIM3 is MCU hardware peripheral instance, its definition is provided in CMSIS

Then it is possible to configure mode of a channel.

Note No need to configure pin mode (output/input/AlternateFunction), it will be done automatically by HardwareTimer library.

Note Channel range [1..4], but not all timers support 4 channels.

Example:

    MyTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);

Supported Mode:

typedef enum {
  TIMER_DISABLED,
  // Output Compare
  TIMER_OUTPUT_COMPARE,                   // == TIM_OCMODE_TIMING           no output, useful for only-interrupt
  TIMER_OUTPUT_COMPARE_ACTIVE,            // == TIM_OCMODE_ACTIVE           pin is set high when counter == channel compare
  TIMER_OUTPUT_COMPARE_INACTIVE,          // == TIM_OCMODE_INACTIVE         pin is set low when counter == channel compare
  TIMER_OUTPUT_COMPARE_TOGGLE,            // == TIM_OCMODE_TOGGLE           pin toggles when counter == channel compare
  TIMER_OUTPUT_COMPARE_PWM1,              // == TIM_OCMODE_PWM1             pin high when counter < channel compare, low otherwise
  TIMER_OUTPUT_COMPARE_PWM2,              // == TIM_OCMODE_PWM2             pin low when counter < channel compare, high otherwise
  TIMER_OUTPUT_COMPARE_FORCED_ACTIVE,     // == TIM_OCMODE_FORCED_ACTIVE    pin always high
  TIMER_OUTPUT_COMPARE_FORCED_INACTIVE,   // == TIM_OCMODE_FORCED_INACTIVE  pin always low

  //Input capture
  TIMER_INPUT_CAPTURE_RISING,             // == TIM_INPUTCHANNELPOLARITY_RISING
  TIMER_INPUT_CAPTURE_FALLING,            // == TIM_INPUTCHANNELPOLARITY_FALLING
  TIMER_INPUT_CAPTURE_BOTHEDGE,           // == TIM_INPUTCHANNELPOLARITY_BOTHEDGE

  // Used 2 channels for a single pin. One channel in TIM_INPUTCHANNELPOLARITY_RISING another channel in TIM_INPUTCHANNELPOLARITY_FALLING.
  // Channels must be used by pair: CH1 with CH2, or CH3 with CH4
  // This mode is very useful for Frequency and Dutycycle measurement
  TIMER_INPUT_FREQ_DUTY_MEASUREMENT,

  TIMER_NOT_USED = 0xFFFF  // This must be the last item of this enum
} TimerModes_t;

Then it is possible to configure PrescalerFactor. The Timer clock will be divided by this factor (if timer clock is 10Khz, and prescaler factor is 2, then timer will count at 5kHz).

Note Configuration of prescaler is automatic when using method setOverflow with format == MICROSEC_FORMAT or format == HERTZ_FORMAT.

Note Prescaler is for timer counter and thus is common to all channel.

Note PrescalerFactor range: [1.. 0x10000] (Hardware register will range [0..0xFFFF]).

Example:

    MyTim->setPrescaleFactor(8);

Then it is possible to configure overflow (also called rollover or update).

For output it correspond to period or frequency.

For input capture it is suggested to use max value: 0x1000 to avoid rollover before capture occurs .

Note Configuration of prescaler is automatic when using method setOverflow with format == MICROSEC_FORMAT or format == HERTZ_FORMAT.

Note overflow is common to all channel.

Note Overflow range: [1.. 0x10000] (Hardware register will range [0..0xFFFF]).

Example:

     MyTim->setOverflow(10000); // Default format is TICK_FORMAT. Rollover will occurs when timer counter counts 10000 ticks (it reach it count from 0 to 9999)
     MyTim->setOverflow(10000, TICK_FORMAT);
     MyTim->setOverflow(100000, MICROSEC_FORMAT); // 10000 microseconds
     MyTim->setOverflow(10000, HERTZ_FORMAT); // 10 kHz

Then it is possible to configure CaptureCompare (channel specific CaptureCompare register).

Note CaptureCompare is for one channel only.

Note CaptureCompare range: [1.. 0x10000] (Hardware register will range [0..0xFFFF]).

Example:

    MyTim->setCaptureCompare(channel, 50); // Default format is TICK_FORMAT. 50 ticks
    MyTim->setCaptureCompare(channel, 50, TICK_FORMAT)
    MyTim->setCaptureCompare(channel, 50, MICROSEC_COMPARE_FORMAT); // 50 microseconds    between counter reset and compare
    MyTim->setCaptureCompare(channel, 50, HERTZ_COMPARE_FORMAT); // 50 Hertz -> 1/50    seconds between counter reset and compare
    MyTim->setCaptureCompare(channel, 50, RESOLUTION_8B_COMPARE_FORMAT); // used for    Dutycycle: [0.. 255]
    MyTim->setCaptureCompare(channel, 50, RESOLUTION_12B_COMPARE_FORMAT); // used for   Dutycycle: [0.. 4095]

It is possible to attach a callback on update interruption (rollover) and/or on Capture/Compare interruption.
If no channel is specified, callback is attach to update event.

Example:

    MyTim->attachInterrupt(Update_IT_callback); // Userdefined call back prototype : void     Update_IT_callback(HardwareTimer*);
    MyTim->attachInterrupt(channel, Compare_IT_callback); // Userdefined call back    prototype : void Compare_IT_callback(HardwareTimer*);

It is now time to start timer.

Note All channel of the same timer are started at the same time (as there is only 1 counter per timer).

Example:

    MyTim->resume();

Timer can be paused then resumed

    MyTim->pause();
    ...
    MyTim->resume();

Below is an example of full PWM configuration.

Example:

    MyTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
    // MyTim->setPrescaleFactor(8); // Due to setOverflow with MICROSEC_FORMAT, prescaler   will be computed automatically based on timer input clock
    MyTim->setOverflow(100000, MICROSEC_FORMAT); // 10000 microseconds = 10 milliseconds
    MyTim->setCaptureCompare(channel, 50, PERCENT_COMPARE_FORMAT); // 50%
    MyTim->attachInterrupt(Update_IT_callback);
    MyTim->attachInterrupt(channel, Compare_IT_callback);
    MyTim->resume();

To simplify basic PWM configuration, a dedicated all-in-one API is provided.
Overflow/frequency is in hertz, dutycycle in percentage.

Example:

    MyTim->setPWM(channel, pin, 5, 10, NULL, NULL); // No callback required, we can   simplify the function call
    MyTim->setPWM(channel, pin, 5, 10); // 5 Hertz, 10% dutycycle

Some additional APIs allow to retrieve configurations:

    getPrescaleFactor();
    getOverflow();
    getCaptureCompare(); // In InputCapture mode, this method doesn't retrieve configuration   but retrieve the captured counter value
    getCount();

Also, to get ride of Interrupt callback:

    detachInterrupt()

4. Examples

Following examples are provided in STM32Examples library (available with Arduino Library manager):

  • Timebase_callback.ino

    This example shows how to configure HardwareTimer to execute a callback at regular interval.
    Callback toggles pin.
    Once configured, there is only CPU load for callbacks executions.

  • PWM_FullConfiguration.ino

    This example shows how to fully configure a PWM with HardwareTimer.
    PWM is generated on LED_BUILTIN if available.
    PWM is generated by hardware: no CPU load.
    Nevertheless, in this example both interruption callback are used on Compare match (Falling edge of PWM1 mode) and update event (rising edge of PWM1 mode).
    Those call back are used to toggle a second pin: pin2.
    Once configured, there is only CPU load for callbacks executions.

  • All-in-one_setPWM.ino

    This example shows how to configure a PWM with HardwareTimer in one single function call.
    PWM is generated on LED_BUILTIN if available.
    No interruption callback used: PWM is generated by hardware.
    Once configured, there is no CPU load.

  • InputCapture.ino

    This example shows how to configure HardwareTimer in inputcapture to measure external signal frequency.
    Each time a rising edge is detected on the input pin, hardware will save counter value into CaptureCompare register.
    External signal (signal generator for example) should be connected to D2.
    Measured frequency is displayed on Serial Monitor.

  • Frequency_Dutycycle_measurement.ino

    This example shows how to configure HardwareTimer to measure external signal frequency and dutycycle.
    The input pin will be connected to 2 channel of the timer, one for rising edge the other for falling edge.
    Each time a rising edge is detected on the input pin, hardware will save counter value into one of the CaptureCompare register.
    Each time a falling edge is detected on the input pin, hardware will save counter value into the other CaptureCompare register.
    External signal (signal generator for example) should be connected to D2.

5. Dependencies

Tone, Servo and analogwrite have been updated to use HardwareTimer.

New optional parameter destruct has been added to noTone to decide whether to destruct/free HardwareTimer object.

noTone(uint8_t _pin, bool destruct = false)

TODO:

  • update Ethernet library

Examples provided in STM32Examples library:

stm32duino/STM32Examples#12

Wiki: https://github.com/stm32duino/wiki/wiki/HardwareTimer-library

Fixes #146

@fpistm
Copy link
Member

fpistm commented Jul 31, 2019

Hi @ABOSTM,
could you fix this issue #585 in this PR also. Thanks in advance.

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small updates to perform and questions.
Else LGTM.
Thanks @ABOSTM

ABOSTM and others added 3 commits August 19, 2019 09:05
Remove field 'state' from timerPinInfo_t using digit_io_toggle api.
Remove global variable 'g_lastPin' as TimerTone_pinInfo contains same info.
Add some check to avoid NULL or NC usage.
Avoid return in void returning function.

Signed-off-by: Frederic.Pillon <[email protected]>
@ABOSTM ABOSTM force-pushed the master branch 2 times, most recently from 5285bcc to b3bbeeb Compare August 19, 2019 07:29
Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ABOSTM for the update.

@fpistm fpistm merged commit cfe6dd8 into stm32duino:master Aug 19, 2019
@roc2
Copy link

roc2 commented Sep 26, 2019

The code at this line:

enableTimerClock(&(_HardwareTimerObj.handle));
,maybe something mistake?

@fpistm
Copy link
Member

fpistm commented Sep 26, 2019

The code at this line:

enableTimerClock(&(_HardwareTimerObj.handle));

,maybe something mistake?

Nice catch.
Thanks @roc2
This is now fixed:
cef14f4

@roc2
Copy link

roc2 commented Oct 10, 2019

Could you add the encoder mode of the timer, there is no encoder API of HardwareTimer, but it's important.

@fpistm
Copy link
Member

fpistm commented Oct 10, 2019

Could you add the encoder mode of the timer, there is no encoder API of HardwareTimer, but it's important.

Hi @roc2
This PR is merged. So this is not the right place to ask this.

Comment on lines +911 to +915
#if !defined(STM32F0xx) && !defined(STM32G0xx)
case 2:
uwAPBxPrescaler = clkconfig.APB2CLKDivider;
uwTimclock = HAL_RCC_GetPCLK2Freq();
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ABOSTM, do you remember why you added this exception (that PCLK2 is not supported on F0 and G0)? I just ran into TIM1 not working on a G030, and found this bit of code. It does seem that F0 and G0 actually have PCLK2 (I checked F030, G0x0 and G0x1 reference manuals). Is there maybe some code missing to enable this clock or so? Maybe @fpistm knows?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @matthijskooijman
HAL_RCC_GetPCLK2Freq does not exist for those series.
I've checked the reference manual for STM32G0x0 and found no PCLK2 only TIMPCLK.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthijskooijman , this is an inheritance of getTimerClkFreq() from timer.c before implementation of HardwareTimer.
but this doesn't help you much :-)
The real reason behind is that this case 2 concerns TIMERs that are on APB2 bus, but looking at STM32F030 there is no ABP2. so the function HAL_RCC_GetPCLK2Freq() doesn't exist on G0 and F0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM, we seem to have hit an older bug from a previous core version where getTimerClkSrc() returned a clock source 2 instead of 1 and since case 2 is purposely not defined for the F0/G0 in getTimerClkFreq() it would end up in case 0's Error_handler() :-)
This has been fixed in the commit here (core version 2.0.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement HardwareTimer library for Timer management
5 participants