timer.h
#ifndef _DELAY_H_
#define _DELAY_H_
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
typedef enum{
SF_DP0,
SF_DP1,
SF_DP2,
DP_COUNTS
}Audio_dp;
typedef struct{
int delay_ms;
int timeout;
int active;
void (*call_back)(int);
int param;
}delayPack;
extern delayPack delay_pack[DP_COUNTS];
void SysTickConfig(uint32_t ms);
void delayPackInit(delayPack* dp);
int non_blocking_DelayTimer_Start(int ind, uint32_t ms, int param);
int non_blocking_DelayTimer_Reset(int ind);
#endif
timer.c
#include "delay.h"
#include "ARMCM3.h" // Device header
#include "sequenceFun.h"
delayPack delay_pack[DP_COUNTS] ={
// delay_ms using active call_back param
/* SF_DP0 */ {0, 0, 0, SF_DP0_CALL_BACK, 0},
/* SF_DP1 */ {0, 0, 0, SF_DP1_CALL_BACK, 0},
/* SF_DP2 */ {0, 0, 0, SF_DP2_CALL_BACK, 0},
};
void SysTickConfig(uint32_t ms)
{
SysTick->LOAD = (uint32_t)(ms*12000000/1000 - 1UL); /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */ /* Function successful */
}
void SysTick_Handler(void)
{
int i;
for(i=0;i<DP_COUNTS;i++){
if((delay_pack[i].delay_ms -- > 0) && (delay_pack[i].active == 1))
{
if(delay_pack[i].delay_ms == 0)
{
delay_pack[i].timeout = 1;
delay_pack[i].active = 0;
if(delay_pack[i].call_back != NULL){
delay_pack[i].call_back(delay_pack[i].param);
}
}
}
}
}
int non_blocking_DelayTimer_Start(int ind, uint32_t ms, int param)
{
if((delay_pack[ind].timeout == 0) && (delay_pack[ind].active == 0))
{
delay_pack[ind].delay_ms = ms;
delay_pack[ind].active = 1;
delay_pack[ind].param = param;
}
return delay_pack[ind].active;
}
int non_blocking_DelayTimer_Reset(int ind)
{
delay_pack[ind].active = 0;
delay_pack[ind].delay_ms =0;
delay_pack[ind].timeout = 0;
delay_pack[ind].param = 0;
return delay_pack[ind].active;
}
sequenceFun.c
#include <stdio.h>
#include <stdbool.h>
#include "sequenceFun.h"
#include "delay.h"
void SF_DP0_CALL_BACK(int param)
{
printf("SF_DP0_CALL_BACK PARAM:%d \n",param);
}
void SF_DP1_CALL_BACK(int param)
{
printf("SF_DP1_CALL_BACK PARAM:%d \n",param);
}
void SF_DP2_CALL_BACK(int param)
{
printf("SF_DP2_CALL_BACK PARAM:%d \n",param);
}
void SequenceFunction(void)
{
if(non_blocking_DelayTimer_Start(SF_DP0,1,10) == 1)
{
return;
}
if(non_blocking_DelayTimer_Start(SF_DP1,10,100) == 1)
{
return;
}
if(non_blocking_DelayTimer_Start(SF_DP2,1000,1000) == 1)
{
return;
}
non_blocking_DelayTimer_Reset(SF_DP0);
non_blocking_DelayTimer_Reset(SF_DP1);
non_blocking_DelayTimer_Reset(SF_DP2);
}