Skip to content
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

drivers/common/opentimers: add ISR timers #541

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions drivers/common/opentimers.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,19 @@ opentimers_id_t opentimers_create(uint8_t timer_id, uint8_t task_prio){
}
}

if (timer_id==TIMER_ISR){
for (id = TIMER_TSCH + 1; id < TIMER_NUMBER_NON_GENERAL; id++){
if (opentimers_vars.timersBuf[id].isUsed == FALSE){
opentimers_vars.timersBuf[id].isUsed = TRUE;
opentimers_vars.timersBuf[id].timer_task_prio = task_prio;
ENABLE_INTERRUPTS();
return id;
}
}
}

if (timer_id==TIMER_GENERAL_PURPOSE){
for (id=TIMER_NUMBER_NON_GENERAL;id<MAX_NUM_TIMERS;id++){
for (id =TIMER_NUMBER_NON_GENERAL;id<MAX_NUM_TIMERS;id++){
if (opentimers_vars.timersBuf[id].isUsed == FALSE){
opentimers_vars.timersBuf[id].isUsed = TRUE;
opentimers_vars.timersBuf[id].timer_task_prio = task_prio;
Expand Down Expand Up @@ -370,11 +381,11 @@ void opentimers_timer_callback(void){
sctimer_setCompare(sctimer_readCounter()+SPLITE_TIMER_DURATION);
return;
} else {
if (opentimers_vars.timersBuf[TIMER_INHIBIT].currentCompareValue == opentimers_vars.currentCompareValue){
if (opentimers_vars.currentCompareValue == opentimers_vars.timersBuf[TIMER_INHIBIT].currentCompareValue){
// this is the timer interrupt right after inhibit timer, pre call the non-tsch, non-inhibit timer interrupt here to avoid interrupt during receiving serial bytes
for (i=0;i<MAX_NUM_TIMERS;i++){
if (opentimers_vars.timersBuf[i].isrunning==TRUE){
if (i!=TIMER_TSCH && i!=TIMER_INHIBIT && opentimers_vars.timersBuf[i].currentCompareValue - opentimers_vars.currentCompareValue < PRE_CALL_TIMER_WINDOW){
if (i >= TIMER_NUMBER_NON_GENERAL && opentimers_vars.timersBuf[i].currentCompareValue - opentimers_vars.currentCompareValue < PRE_CALL_TIMER_WINDOW){
opentimers_vars.timersBuf[i].currentCompareValue = opentimers_vars.currentCompareValue;
}
}
Expand All @@ -385,7 +396,7 @@ void opentimers_timer_callback(void){
if (opentimers_vars.currentCompareValue == opentimers_vars.timersBuf[i].currentCompareValue){
// this timer expired, mark as expired
opentimers_vars.timersBuf[i].lastCompareValue = opentimers_vars.timersBuf[i].currentCompareValue;
if (i==TIMER_TSCH){
if (i < TIMER_NUMBER_NON_GENERAL && i != 0 ){
opentimers_vars.insideISR = TRUE;
opentimers_vars.timersBuf[i].isrunning = FALSE;
opentimers_vars.timersBuf[i].callback(i);
Expand Down Expand Up @@ -460,4 +471,4 @@ void opentimers_timer_callback(void){
} else {
opentimers_vars.running = FALSE;
}
}
}
9 changes: 5 additions & 4 deletions drivers/common/opentimers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef __OPENTIMERS_H
#define __OPENTIMERS_H

#include "config.h"
#include "opendefs.h"

/**
Expand All @@ -19,17 +20,17 @@
//=========================== define ==========================================

/// Maximum number of timers that can run concurrently
#define MAX_NUM_TIMERS 15
#define MAX_NUM_TIMERS (15 + TIMER_ISR_NUMBER)
#define MAX_TICKS_IN_SINGLE_CLOCK (uint32_t)(((PORT_TIMER_WIDTH)0xFFFFFFFF)>>1)
#define ERROR_NO_AVAILABLE_ENTRIES 255
#define MAX_DURATION_ISR 33 // 33@32768Hz = 1ms
#define opentimers_id_t uint8_t

#define TIMER_INHIBIT 0
#define TIMER_TSCH 1
#define TIMER_ISR 254
#define TIMER_GENERAL_PURPOSE 255

#define TIMER_NUMBER_NON_GENERAL 2
#define TIMER_NUMBER_NON_GENERAL (2 + TIMER_ISR_NUMBER)

#define SPLITE_TIMER_DURATION 15 // in ticks
#define PRE_CALL_TIMER_WINDOW PORT_TsSlotDuration
Expand Down Expand Up @@ -98,4 +99,4 @@ bool opentimers_isRunning(opentimers_id_t id);
\}
*/

#endif
#endif
14 changes: 14 additions & 0 deletions inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,20 @@
#define BOARD_FASTSIM_ENABLED (0)
#endif

// ======================== Driver configuration ========================


/**
* \def TIMER_ISR_NUMBER
*
* Number of TIMER_ISR, this does not include TIMER_TSCH or TIMER_INHIBIT
*
*/
#ifndef TIMER_ISR_NUMBER
#define TIMER_ISR_NUMBER (0)
#endif


// ======================== Kernel configuration ========================

/**
Expand Down