Skip to content

Implement HardwareTimer library for Timer management #146

Closed

Description

@fpistm

This issue is to open discussion around Timer management in order to provide useful API.
Current implementation is limited to support basic Arduino function.
Any help/comment are welcome.

Activity

  1. added
    enhancement
    help wanted 🙏
    on Nov 7, 2017
  2. romainreignier commented on Nov 7, 2017

    @romainreignier

    On AVR, I usually use this library, the API is simple enough: https://www.pjrc.com/teensy/td_libs_TimerOne.html

    But it only allows to use timers for interrupt callback. You cannot configure a timer as quadrature encoder counter from it for example.

  3. Adminius commented on Nov 7, 2017

    @Adminius
    Contributor
  4. Adminius commented on Nov 7, 2017

    @Adminius
    Contributor

    My requirements:

    1. Interrupt driven function call with resolution: 1us, 1ms, 1s.
    2. better PWM generation: flexible PWM frequency, flexible counter, up-up/up-down (e.g. https://github.com/Adminius/Dimmer32u4)
  5. franciscogimeno2000 commented on Nov 23, 2017

    @franciscogimeno2000

    Someone knows why libraries do not work
    Timer1 ... 2 ... flexitimer2 in board STMF303 - F303K8

  6. fpistm commented on Nov 23, 2017

    @fpistm
    MemberAuthor

    @franciscogimeno2000 simply because not ported for stm32. Ex Flexitimer uses direct register acces but this is not the same for stm32.
    Goal of this issue is to collect requirements/proposal/API examples... to implement timers management useful for community.
    Any contributions are welcom 😉

  7. franciscogimeno2000 commented on Nov 24, 2017

    @franciscogimeno2000

    OK, you have some example of using a basic timer.?

  8. straccio commented on Nov 29, 2017

    @straccio
    Contributor

    For my projects i usually use particle boards, the are also based on STM32.
    Take a look on their Software Timer https://docs.particle.io/reference/firmware/photon/#software-timers

    They use Free RTOS, also available for more stm32 boards, but if you don't want use Free RTOS can you use the same interface for class.

    PS: remember to use also std::function as callback.

  9. added
    on going
    on Jan 27, 2018
  10. marcdepape commented on Apr 9, 2018

    @marcdepape

    I have a NUCLEO F1 and F4 and spent the weekend trying to use timer.h / timer.c with them. It was a struggle.

    For example (I'm going to truncate my code, and I don't have to use timer_enable_clock(_timer.handle); because I tried, and as far as I can tell it is called by these Inits), this does not work:

    stimer_t _timer;
    TimerHandleInit(&_timer, 100000, 384);
    attachIntHandle(&_timer, handle_clock);

    Yet, this does:

    stimer_t _timer;
    TimerPulseInit(&_timer, 500, 40000, pulse_clock);
    TimerHandleInit(&_timer, 100000, 384);

    In my callback I blinked an led and I used the following to debug after initializing:

    Serial.println(getTimerIrq(_timer.handle.Instance)); Serial.println(getTimerClkSrc(_timer.handle.Instance)); Serial.println(getTimerClkFreq(_timer.handle.Instance));

    In the first example I got: 0, 0, 0
    In the second example I got: 28, 1, 84000000

    From looking through the code, the TimerPulseInit actually assigns a timer:

    obj->timer = TIMER_SERVO;

    Whereas TimerHandleInit does not assign a timer.

    In fact, I couldn't figure out for the life of me how to assign a specific timer to either of these functions, however I tried _timer.timer = TIM2; and it worked:

    stimer_t _timer;
    _timer.timer = TIM2;
    TimerHandleInit(&_timer, 100000, 384);
    attachIntHandle(&_timer, handle_clock);

    Can I suggest the following:

    1. You should be able to assign a timer when you declare your stimer_t obj:

    stimer_t _timer(TIM8);

    1. You should be able to assign a callback to TimerHandleInit:

    void TimerHandleInit(stimer_t *obj, uint16_t period, uint16_t prescaler, void (*irqHandle)(stimer_t *))

    This would greatly simplify everything. I think this is almost there, and the efforts to simplify the setup are good so far... just needs a bit of polish.

  11. removed
    on going
    on Apr 11, 2018
  12. fpistm commented on Apr 11, 2018

    @fpistm
    MemberAuthor

    Thanks for the feedback @marcdepape.
    I will review all inputs.

  13. 18 remaining items

  14. added this to the 1.7.0 milestone on Jun 27, 2019
  15. assigned and unassigned on Jun 28, 2019
  16. Astroghost-Francois commented on Jul 11, 2019

    @Astroghost-Francois

    Hi ,

    will we be able to use arduino timers interrupt commands after ?

    François

  17. ABOSTM commented on Jul 12, 2019

    @ABOSTM
    Contributor

    Hi @Astroghost-Francois,
    Yes, it will be possible to use interrupts on HardwareTimer library (through dedicated API from HardwareTimer library) : on update event (timer rollover) as well as compare match event corresponding to one of the channel.

  18. fpistm commented on Jul 26, 2019

    @fpistm
    MemberAuthor

    Hi all,
    @ABOSTM do the PR of the HarwareTimer implementation.
    Do not hesitate to give us your feedback.
    Thanks

  19. KJansun commented on Aug 4, 2019

    @KJansun

    I installed all latest libs/cores but i dont know how to enable the HardwareTimer:
    'HardwareTimer' was not declared in this scope

    code i used:

    HAL_TIM_MODULE_ENABLED
    
    #if defined(LED_BUILTIN)
    #define pin  LED_BUILTIN
    #else
    #define pin  PA1
    #endif
    
    void setup()
    {
      // no need to configure pin, it will be done by HardwareTimer configuration
      // pinMode(pin, OUTPUT);
    
      // Automatically retrieve TIM instance and channel associated to pin
      // This is used to be compatible with all STM32 series automatically.
      TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
      uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));
    
    
      // Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup() function is finished.
      HardwareTimer *MyTim = new HardwareTimer(Instance);
    
      // Configure and start PWM
      // 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
    }
    
    
    void loop()
    {
      /* Nothing to do all is done by hardware. Even no interrupt required. */
    }
    

    i'm new with the st's core and not familiar with the HAL configs. I tried diffent stuff but i dont understand how to get it to work. How can i enable this lib?

  20. BennehBoy commented on Aug 4, 2019

    @BennehBoy
    Contributor
  21. KJansun commented on Aug 5, 2019

    @KJansun

    You have to install the github version of the core & then apply this PR. The last release 1.6.1 will NOT include these changes.
    …
    On Sun, Aug 4, 2019 at 9:32 AM KJansun @.***> wrote: I installed all latest libs/cores but i dont know how to enable the HardwareTimer: 'HardwareTimer' was not declared in this scope code i found: HAL_TIM_MODULE_ENABLED #if defined(LED_BUILTIN) #define pin LED_BUILTIN #else #define pin PA1 #endif void setup() { // no need to configure pin, it will be done by HardwareTimer configuration // pinMode(pin, OUTPUT); // Automatically retrieve TIM instance and channel associated to pin // This is used to be compatible with all STM32 series automatically. TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM); uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM)); // Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup() function is finished. HardwareTimer *MyTim = new HardwareTimer(Instance); // Configure and start PWM // 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 } void loop() { /* Nothing to do all is done by hardware. Even no interrupt required. */ } i'm new with the st's core and not familiar with the HAL configs. I tried diffent stuff but i dont understand to get it to work. How can i enable this lib? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#146?email_source=notifications&email_token=AF6R4XQOFA3GH63JHULEZGTQC2HZNA5CNFSM4ECSN3NKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3P5CLI#issuecomment-517984557>, or mute the thread https://github.com/notifications/unsubscribe-auth/AF6R4XXCRWO4EIYN25VOJFDQC2HZNANCNFSM4ECSN3NA .

    Ok, thanks

  22. added a commit that references this issue on Aug 19, 2019
    cfe6dd8
  23. removed
    help wanted 🙏
    on going
    on Aug 19, 2019
  24. MacLeod-D commented on Feb 4, 2020

    @MacLeod-D

    Hello,
    I am using Arduino-IDE and STM32F407 Black Board and STM32GENERIC board definition.
    Here is my code to use timers:

    // For Arduino-IDE
    
    // Board: BLACK STM32F407, 168 MHz
    
    // Hardware:      STM32GENERIC  (https://github.com/danieleff/STM32GENERIC)
    // Board:         F407Vxx
    // Serial Comm    SerialUART1
    // Spec. Board    Black F407VE (2.0)
    // Upload         SerialUART1 [TX:PA9, RX:PA10] connected via FTDI (3.3V)
    
    
    #include <Arduino.h>
    #include "HardwareTimer.h"
    
    volatile uint32_t tick1, tick2, tick3;
    
    // ----------------------------------------
    // Attention: STM32F4 specific !!!
    // fast GPIO set/reset:
    #define GPIO_PIN_SET(PORT,pin)          ((PORT)->BSRR = 1 << (uint32_t)pin)
    #define GPIO_PIN_RESET(PORT,pin)        ((PORT)->BSRR = (1 << (uint32_t)pin) << 16)
    #define GPIO_HISPEED_SET(port,pin)      (port)->OSPEEDR = (port)->OSPEEDR | (0b11 << (2*pin))
    // ----------------------------------------
    
    // ----------------------------------------
    // Attention: STM32F4 specific !!!
    // Timer1     clocks with 168 MHz
    // Timer2,3   clocks with 84  MHz
    // ----------------------------------------
    
    
    // Interrupt routines
    
    void Timer1_ISR() {
      tick1++;
    }
    
    
    void Timer2_ISR() {
      tick2++;
      digitalWrite(PA7,!digitalRead(PA7));
    }
    
    void Timer3_ISR() {
      tick3++;
      // Simulate PWM dimmed LED 1/20 = 5% on
      if ((tick3 % 20) == 0)  GPIO_PIN_RESET   (GPIOA,6);  // == digitalWrite(P6,0);
      else                    GPIO_PIN_SET     (GPIOA,6);
      
    }  
    
    void setup() {
      Serial.begin(2000000);
      pinMode(PA6,OUTPUT);
      GPIO_HISPEED_SET(GPIOA,6);
      pinMode(PA7,OUTPUT);
      GPIO_HISPEED_SET(GPIOA,7);
      Timer1.pause();
      Timer1.setPrescaleFactor(1680 -1);                   //  0.1 MHz = 10 us / 1 tick = 1/168 us
      Timer1.setMode(1, TIMER_OUTPUT_COMPARE_TOGGLE);      //  Pin toggles when counter reaches compare (effectively 50% PWM)
      Timer1.setOverflow(20000 - 1);                       //        100000 / 20000 = 5 Interrupts /s 
      Timer1.setCompare(1, 10000);                         // Channel1, 10000 ticks = 50%
      Timer1.attachInterrupt(1, Timer1_ISR);               // channel1, handler
      Timer1.resume();
    
      Timer2.pause();
      Timer2.setPrescaleFactor(840 -1);                    // 0.1 MHz = 10 us / 1 tick = 1/84 us
      Timer2.setMode(2, TIMER_OUTPUT_COMPARE_TOGGLE);      // Pin toggles when counter reaches compare (effectively 50% PWM)
      Timer2.setOverflow(20000 - 1);                       // 100000 / 20000 = 5 Interrupts /s 
      Timer2.setCompare(2, 10000);                         // Channel3, 50%
      Timer2.attachInterrupt( 2, Timer2_ISR);              // channel3, handler
      Timer2.resume();
    
      Timer3.pause();
      Timer3.setPrescaleFactor(84 -1);                     // 1 MHz = 1 us / 1 tick = 1/84 us
      Timer3.setMode(3, TIMER_OUTPUT_COMPARE_TOGGLE);      // Pin toggles when counter reaches compare (effectively 50% PWM)
      Timer3.setOverflow(2 - 1);                           // every 2 us = >>>>>>> 500.000 Interrupts /s <<<<<<<<<<
      Timer3.setCompare(3, 1);                             // Channel3, 50%
      Timer3.attachInterrupt( 3, Timer3_ISR);              // channel3, handler
      Timer3.resume();
    
    }
    
    
    void loop() {
      Serial.print(micros()); Serial.print("us: tick1: "); Serial.println(tick1);
      Serial.print(micros()); Serial.print("us: tick2: "); Serial.println(tick2);
      Serial.print(micros()); Serial.print("us: tick3: "); Serial.println(tick3);
      delay(1000); // 1 second
    }

    output:

    1314059001us: tick1: 6570
    1314059094us: tick2: 6570
    1314059196us: tick3: 657000110
    1315059001us: tick1: 6575
    1315059094us: tick2: 6575
    1315059196us: tick3: 657500110
    1316059001us: tick1: 6580
    1316059094us: tick2: 6580
    1316059196us: tick3: 658000110

    Using digitalWrite - not GPIO - it should work on most STM32 boards.

  25. fpistm commented on Feb 5, 2020

    @fpistm
    MemberAuthor

    Hi @MacLeod-D

    I am using Arduino-IDE and STM32F407 Black Board and STM32GENERIC board definition.

    Don't know what did you expect?
    Here this is for the STM32 core not the STM32GENERIC .
    use the forum which is used for primary support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions