GlobLib
HAL and API libraries for MCUs and hardware.

This module contains low level functions for timer interaction. More...

Files

file  stm32f103cb_timer.h
 Header file for stm32f103cb timers.
 

Enumerations

Functions

mcu_error TIMER_setupCount (timer_main timerNumber, uint32_t frequency, void(*handler)(void))
 Initialize given timer for standard count with interrupt on timeout. More...
 
mcu_error TIMER_setupIC (timer_main timerNumber, timer_channel timerChannel)
 Initialize given timer for input capture. More...
 
mcu_error TIMER_setupPWM (timer_main timerNumber, timer_channel timerChannel, uint32_t frequency, uint8_t duty)
 Initialize given timer for PWM mode. More...
 
mcu_error TIMER_setupPulse (timer_main timerNumber, timer_channel timerChannel, uint32_t frequency, uint32_t pulse)
 Initialize given timer for Pulse mode. More...
 
mcu_error TIMER_setPeriod (timer_main timerNumber, uint32_t period)
 Set the period for the timer. (in microseconds) More...
 
mcu_error TIMER_setFrequency (timer_main timerNumber, uint32_t frequency)
 Set the frequency for the timer. (in hertz) More...
 
mcu_error TIMER_setDuty (timer_main timerNumber, timer_channel channel, uint8_t duty)
 Set the duty cycle for the waveform (PWM only, 0 - 100%). More...
 
mcu_error TIMER_setPulse (timer_main timerNumber, timer_channel channel, uint32_t pulse)
 Set the pulse width for the output. (in microseconds) More...
 
uint16_t TIMER_getCount (timer_main timerNumber)
 Get the current value of a running timer. More...
 
mcu_error TIMER_setCount (timer_main timerNumber, uint16_t count)
 Set the current value of a running timer. More...
 
uint16_t TIMER_getIC (timer_main timerNumber, timer_channel channel)
 Get the last input capture time from a channel of a timer. More...
 
mcu_error TIMER_enableISR (timer_main timerNumber, void(*handler)(void))
 Set the ISR target for timeout. More...
 
mcu_error TIMER_disableISR (timer_main timerNumber)
 Disable the timeout IRQ. More...
 
mcu_error TIMER_pause (timer_main timerNumber)
 Pause an already running timer. More...
 
mcu_error TIMER_resume (timer_main timerNumber)
 Resume a paused timer. More...
 

Detailed Description

This module contains low level functions for timer interaction.

The STM32F1x3 has four timers, each with four compare/capture channels.

Each timer can be configured to a different function, however each channel of that timer should have the same function. Exceptions are:

A timer can have PWM and pulse outputs on different channels as long as the frequency is the same. The ISR of the output timers, (PWM, pulse) can be targeted to function calls. The ISR will trigger at the same frequency as the timer.

The resolution of the base timer is set as:

The period is modified with TIMER_setPeriod()

Timer output/input channels are mapped to:

Author
Stuart Ianna
Version
0.1
Date
June 2018
Warning
None
Bug:
None
Todo:
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Working example
#include <coms128.h>
void up(void);
volatile uint8_t flag;
int main(void){
//Clock setup
//Serial setup
COMS(USART_2);
//Setup timer 3 to call function "up" at 10Hz
TIMER_setupCount(TIMER_3,20,&up); //Period = 50000 -> Resolution = 1us
//Setup timer 1 for pulse width output at 1000Hz
TIMER_setupPulse(TIMER_1,CHANNEL_1,1000,400); //400us pulse
TIMER_setupPulse(TIMER_1,CHANNEL_2,1000,50); //50us pulse
//Setup timer 2 for PWM at 10kHz
TIMER_setupPWM(TIMER_2,CHANNEL_1,10000,50); //50% duty cycle
//Setup timer four for input capture
//Change duty cycle and pulse output
while (1){
if(flag){
flag = 0;
//Time how long it takes to compete print function with TIMER_3
//Print input capture values
printl(TIMER_getCount(TIMER_3)); //Printing the time difference
}
}
return 0;
}
//This is called by TIMER_3 on timeout
void up(void){
flag = 1;
}

Enumeration Type Documentation

Timers channels available for each timer.

Enumerator
CHANNEL_1 

Channel 1.

CHANNEL_2 

Channel 2.

CHANNEL_3 

Channel 3.

CHANNEL_4 

Channel 4.

Definition at line 99 of file stm32f103cb_timer.h.

enum timer_main

Main timers available on the MCU.

Enumerator
TIMER_1 

Timer 1.

TIMER_2 

Timer 2.

TIMER_3 

Timer 3.

TIMER_4 

Timer 4.

Definition at line 88 of file stm32f103cb_timer.h.

Function Documentation

mcu_error TIMER_disableISR ( timer_main  timerNumber)

Disable the timeout IRQ.

This disables any previous use of timeout interrupts.

Parameters
timerNumberThe timer number to modify
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.

Definition at line 751 of file stm32f103cb_timer.c.

mcu_error TIMER_enableISR ( timer_main  timerNumber,
void(*)(void)  handler 
)

Set the ISR target for timeout.

This feature can be used for Count, Pulse and PWM modes. Function is called on timeout event recursivly until TIMER_disableISR() is used.

Parameters
timerNumberThe timer number to modify
handlerThe function to call on timeout
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.

Definition at line 696 of file stm32f103cb_timer.c.

uint16_t TIMER_getCount ( timer_main  timerNumber)

Get the current value of a running timer.

This function returns the current value of the timer. The resolution of this value is detailed in module outline.

Parameters
timerNumberThe timer number to modify
Returns
The current value of the timer.

Definition at line 2119 of file stm32f103cb_timer.c.

uint16_t TIMER_getIC ( timer_main  timerNumber,
timer_channel  channel 
)

Get the last input capture time from a channel of a timer.

The timer must be setup with TIMER_setupIC() for this function to work.

This function returns zero if no capture has occured within the last period. (Around 63ms). This value could be increased by setting a larger period with TIMER_setPeriod(). This however would scale up the value retuned by 10 or 100, as outlined in the module description.

Parameters
timerNumberThe timer number to modify
channelThe timer chanel to modify
Returns
The last input capture time in microseconds

Definition at line 1442 of file stm32f103cb_timer.c.

mcu_error TIMER_pause ( timer_main  timerNumber)

Pause an already running timer.

The timer must have been already setup to use this.

Parameters
timerNumberThe timer number to modify
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.

Definition at line 825 of file stm32f103cb_timer.c.

mcu_error TIMER_resume ( timer_main  timerNumber)

Resume a paused timer.

The timer must have been already setup and paused.

Parameters
timerNumberThe timer number to modify
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.

Definition at line 859 of file stm32f103cb_timer.c.

mcu_error TIMER_setCount ( timer_main  timerNumber,
uint16_t  count 
)

Set the current value of a running timer.

The resolution of this value is detailed in module outline.

Parameters
timerNumberThe timer number to modify
countThe timer value
Returns
The current value of the timer.

Definition at line 2154 of file stm32f103cb_timer.c.

mcu_error TIMER_setDuty ( timer_main  timerNumber,
timer_channel  channel,
uint8_t  duty 
)

Set the duty cycle for the waveform (PWM only, 0 - 100%).

Parameters
timerNumberThe timer number to modify
channelThe timer chanel to modify
dutyThe desired duty cycle.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist

Definition at line 2382 of file stm32f103cb_timer.c.

mcu_error TIMER_setFrequency ( timer_main  timerNumber,
uint32_t  frequency 
)

Set the frequency for the timer. (in hertz)

Parameters
timerNumberThe timer number to modify
frequencyThe desired timeout frequency.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist E_TIMER_PERIOD if the period is too long

Definition at line 2344 of file stm32f103cb_timer.c.

mcu_error TIMER_setPeriod ( timer_main  timerNumber,
uint32_t  period 
)

Set the period for the timer. (in microseconds)

Parameters
timerNumberThe timer number to modify
periodThe desired timeout period.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist E_TIMER_PERIOD if the period is too long

Definition at line 2193 of file stm32f103cb_timer.c.

mcu_error TIMER_setPulse ( timer_main  timerNumber,
timer_channel  channel,
uint32_t  pulse 
)

Set the pulse width for the output. (in microseconds)

Parameters
timerNumberThe timer number to modify
channelThe timer chanel to modify
pulseThe desired pulse width.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist E_TIMER_PULSE if the pulse is too long

Definition at line 2653 of file stm32f103cb_timer.c.

mcu_error TIMER_setupCount ( timer_main  timerNumber,
uint32_t  frequency,
void(*)(void)  handler 
)

Initialize given timer for standard count with interrupt on timeout.

This mode triggers an ISR on timeout. This function can be impemented for any timer by using TIMER_setISR(), however the frequency will be set to the frequency of the already used timer.

Parameters
timerNumberThe timer number to setup
handlerThe function to call on timeout event.
frequencyThe frequency of the timer count expiration.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.

Definition at line 1001 of file stm32f103cb_timer.c.

mcu_error TIMER_setupIC ( timer_main  timerNumber,
timer_channel  timerChannel 
)

Initialize given timer for input capture.

This supports input capture up for pulses up to 50ms long.

This function utalizes the ISR for the timer, this means that the ISR cannnot be target to an external function using TIMER_enableISR();

Parameters
timerNumberThe timer number to setup
timerChannelThe channel of that timer to use.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist.

Definition at line 897 of file stm32f103cb_timer.c.

mcu_error TIMER_setupPulse ( timer_main  timerNumber,
timer_channel  timerChannel,
uint32_t  frequency,
uint32_t  pulse 
)

Initialize given timer for Pulse mode.

The given timer number timer_main is now locked to this pulse or PWM modes.

All channels of this timer can only be used for pulse of PWM. Each channel must also share the same frequency. However each channel can be turned on and off

Parameters
timerNumberThe timer number to setup
timerChannelThe channel of that timer to use.
frequencyThe frequency of the total signal.
pulseThe initial pulse width of the signal.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist

Definition at line 967 of file stm32f103cb_timer.c.

mcu_error TIMER_setupPWM ( timer_main  timerNumber,
timer_channel  timerChannel,
uint32_t  frequency,
uint8_t  duty 
)

Initialize given timer for PWM mode.

The given timer number timer_main is now locked to this or pulse mode.

All channels of this timer can only be used for PWM of Pulse width output. Each channel must also share the same frequency. However each channel can be turned on and off

Parameters
timerNumberThe timer number to setup
timerChannelThe channel of that timer to use.
frequencyThe frequncy of the signal.
dutyThe initial duty cycle.
Returns
Error number as defined in mcu_error, E_TIMER_NOERROR if no error, E_TIMER_PORT if port doesn't exist

Definition at line 934 of file stm32f103cb_timer.c.