You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
3.0 KiB
172 lines
3.0 KiB
#include "timer.h"
|
|
|
|
|
|
#define BASE ((TIM_TypeDef *)timerBase_Addr_List[timer])
|
|
|
|
void timerReset(timerNo_t timer)
|
|
{
|
|
if(timerBus_No[timer]==1)
|
|
{
|
|
RCC->APB1RSTR |= (1<<timerBus_Rst_bitPos[timer]);
|
|
RCC->APB1RSTR &=~ (1<<timerBus_Rst_bitPos[timer]);
|
|
return;
|
|
}
|
|
RCC->APB2RSTR |= (1<<timerBus_Rst_bitPos[timer]);
|
|
RCC->APB2RSTR &=~ (1<<timerBus_Rst_bitPos[timer]);
|
|
}
|
|
|
|
void timerActivateBus(timerNo_t timer)
|
|
{
|
|
if(timerBus_No[timer]==1)
|
|
{
|
|
RCC->APB1ENR |= (1<<timerBus_En_bitPos[timer]);
|
|
return;
|
|
}
|
|
RCC->APB2ENR |= (1<<timerBus_En_bitPos[timer]);
|
|
}
|
|
|
|
void timerEnable(timerNo_t timer)
|
|
{
|
|
BASE->CR1 |= TIM_CR1_CEN; //all the timers have the same CEN bit in CR1 register pos 0
|
|
}
|
|
|
|
void timerDisable(timerNo_t timer)
|
|
{
|
|
BASE->CR1 &=~ TIM_CR1_CEN; //all the timers have the same CEN bit in CR1 register pos 0
|
|
}
|
|
|
|
void timerSetMode(timerNo_t timer, timerMode_t mode)
|
|
{
|
|
//Implement
|
|
}
|
|
|
|
void timerSetCountDirection(timerNo_t timer, timerCountDirection_t direction)
|
|
{
|
|
if(direction == upCounting)
|
|
{
|
|
|
|
BASE->CR1 &=~ TIM_CR1_DIR;
|
|
return;
|
|
}
|
|
|
|
BASE->CR1 |= TIM_CR1_DIR;
|
|
}
|
|
|
|
void timerSetPrescaler(timerNo_t timer, uint32_t prescaler)
|
|
{
|
|
if(prescaler > timerRes_Prescaler[timer])
|
|
{
|
|
timerThrowError(prescalerOutOfRange);
|
|
return; // To prevent writing wrong value in to the Register
|
|
}
|
|
BASE->PSC = prescaler;
|
|
}
|
|
|
|
void timerSetPostscaler(timerNo_t timer, uint32_t postscaler)
|
|
{
|
|
timerThrowError(functionNotSupported);
|
|
}
|
|
|
|
void timerSetAutoReload(timerNo_t timer, uint32_t reload)
|
|
{
|
|
BASE->ARR = reload;
|
|
}
|
|
|
|
void timerClearCounter(timerNo_t timer)
|
|
{
|
|
BASE->CNT = 0;
|
|
}
|
|
|
|
uint8_t timerGetUpdateInterrupt(timerNo_t timer)
|
|
{
|
|
return (BASE->SR & 1);
|
|
}
|
|
|
|
void timerClearUpdateInterrupt(timerNo_t timer)
|
|
{
|
|
BASE->SR &= ~1;
|
|
}
|
|
|
|
|
|
/* Second stage configuration */
|
|
void timerInitCounter ( timerNo_t timer,
|
|
uint32_t prescaler,
|
|
uint32_t period,
|
|
timerCountDirection_t direction)
|
|
{
|
|
timerActivateBus(timer);
|
|
timerReset(timer);
|
|
timerSetMode(timer, counter);
|
|
timerSetCountDirection(timer,direction);
|
|
timerSetPrescaler(timer, prescaler);
|
|
timerSetAutoReload(timer, period);
|
|
}
|
|
|
|
/* Bus Clock
|
|
* ------------------------------ = Duty (Hz)
|
|
* (Prescaler-1) * (Period-1)
|
|
*/
|
|
void timerSetHz(timerNo_t timer, uint16_t hz)
|
|
{
|
|
uint32_t prescaler = 8000000;
|
|
uint32_t period = 0;
|
|
uint32_t temp = 0;
|
|
|
|
do{
|
|
prescaler = prescaler / 10;
|
|
}while(prescaler > 0xffff);
|
|
|
|
|
|
do{
|
|
period = period + 1;
|
|
temp = 8000000/(prescaler*(period));
|
|
}while(temp >= hz);
|
|
|
|
timerSetPrescaler(timer, prescaler-1);
|
|
timerSetAutoReload(timer, period-1);
|
|
timerClearCounter(timer);
|
|
}
|
|
|
|
void timerSetMs(timerNo_t timer, uint16_t ms)
|
|
{
|
|
|
|
}
|
|
|
|
void timerSetNs(timerNo_t timer, uint16_t ns)
|
|
{
|
|
|
|
}
|
|
|
|
void timerSetPs(timerNo_t timer, uint16_t ps)
|
|
{
|
|
|
|
}
|
|
|
|
void timerSart(timerNo_t timer)
|
|
{
|
|
timerEnable(timer);
|
|
}
|
|
|
|
void timerHalt(timerNo_t timer)
|
|
{
|
|
timerDisable(timer);
|
|
}
|
|
|
|
void timerStop(timerNo_t timer)
|
|
{
|
|
timerDisable(timer);
|
|
timerClearCounter(timer);
|
|
}
|
|
|
|
uint32_t timerGetCount(timerNo_t timer)
|
|
{
|
|
return BASE->CNT;
|
|
}
|
|
|
|
|
|
void timerThrowError(timerError_t error)
|
|
{
|
|
while(1);
|
|
}
|
|
|