Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hash Tables When the timer task executes, it starts by incrementing RTOSTmrTickC

ID: 3598688 • Letter: H

Question

Hash Tables

When the timer task executes, it starts by incrementing RTOSTmrTickCtr and goes through the list (linearly) and checks each of the RTOSTmrMatch fields is equal the OSTmtTickCtr. When equal, the timer manager executes the callback function associated with the timer.

When inserting the timer in the link list RTOSTmrMatch field is calculated as OSTmtTickCtr + RTOSTmrDly. If the timer is set to periodic, reloads the RTOSTmrMatch is calculated as OSTmtTickCtr + RTOSTmrPeriod. If the timer is configured as a one-shot timer, the timer is removed from the list upon expiration.

Timer management occurs at the task level. The list must be protected using an internal mutual exclusion semaphore (mutex) or, by locking the scheduler. It’s recommend that you use (and thus enable) mutexes because locking the scheduler impacts task responsiveness of other, higher priority tasks in your application.

Your timer management module might need to literally maintain hundreds of timers, so it needs to be implemented such that it does not take too much of CPU time to update the timers. So you may need to design a hash table of link lists to keep the length of the link lists short. You may want to use the value of (OSTmtTickCtr + RTOSTmrDly) % N to calculate the index of the hash table.

RTOSTmrTickCtr is incremented by RTOSTmrTask() every time the tick ISR signal the task.

Timers are inserted in the timer list by calling RTOSTmrSTart().

(1)

RTOSTmrListEntries contains the current number of entries in the list. This variable is updated whenever timers are added or removed from the list.

(2)

RTOSTmrListPtr contains a pointer to a doubly linked list of timers that the timer manager will need to update.

(3)

RTOSTmrTickCtr is incremented by RTOS_TmrTask() every time the tick ISR signals the task. This counter basically keeps track of the number of times the timer task has been signaled.

Timers are inserted in the timer list by calling RTOSTmrStart() and, a timer must be created before it can be used. Newly created timers are always inserted at the beginning of the list as shown in the figure following the code listing below and the code listing itself.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

RTOS_TMR  MyTmr1;

RTOS_TMR  MyTmr2;

  

  

void MyTmrCallbackFnct1 (void *p_arg)

{

    /* Do something when timer #1 expires */

}

  

  

void MyTmrCallbackFnct2 (void *p_arg)

{

    /* Do something when timer #2 expires */

}

  

  

void MyTask (void *p_arg)

{

    RTOS_ERR  err;

  

  

    while (DEF_ON) {

        :

        RTOSTmrCreate((RTOS_TMR            *)&MyTmr1,

                   (RTOS_CHAR           *)“My Timer #1”,

                   (RTOS_TICK            )1,

                   (RTOS_TICK            )0,

                   (RTOS_OPT             )RTOS_OPT_TMR_ONE_SHOT,

                   (RTOS_TMR_CALLBACK_PTR)MyTmrCallbackFnct1,

                   (void              *)0,

                   (RTOS_ERR            *)&err);

        /* Check ’err” */

        RTOSTmrStart ((RTOS_TMR *)&MyTmr1,

                   (RTOS_ERR *)&err);

        /* Check “err” */

        // Continues in the next code listing!

The code below shows creating and starting another timer. This is performed “before” the timer task is signaled.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

        // Continuation of code from previous code listing.

        :

        :

        RTOSTmrCreate((RTOS_TMR            *)&MyTmr2,

                   (RTOS_CHAR           *)“My Timer #2”,

                   (RTOS_TICK            )10,

                   (RTOS_TICK            )0,

                   (RTOS_OPT             )RTOS_OPT_TMR_ONE_SHOT,

                   (RTOS_TMR_CALLBACK_PTR)MyTmrCallbackFnct2,

                   (void              *)0,

                   (RTOS_ERR            *)&err);

        /* Check ’err” */

        RTOSTmrStart ((RTOS_TMR *)&MyTmr,

                   (RTOS_ERR *)&err);

        /* Check ’err” */

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

RTOS_TMR  MyTmr1;

RTOS_TMR  MyTmr2;

  

  

void MyTmrCallbackFnct1 (void *p_arg)

{

    /* Do something when timer #1 expires */

}

  

  

void MyTmrCallbackFnct2 (void *p_arg)

{

    /* Do something when timer #2 expires */

}

  

  

void MyTask (void *p_arg)

{

    RTOS_ERR  err;

  

  

    while (DEF_ON) {

        :

        RTOSTmrCreate((RTOS_TMR            *)&MyTmr1,

                   (RTOS_CHAR           *)“My Timer #1”,

                   (RTOS_TICK            )1,

                   (RTOS_TICK            )0,

                   (RTOS_OPT             )RTOS_OPT_TMR_ONE_SHOT,

                   (RTOS_TMR_CALLBACK_PTR)MyTmrCallbackFnct1,

                   (void              *)0,

                   (RTOS_ERR            *)&err);

        /* Check ’err” */

        RTOSTmrStart ((RTOS_TMR *)&MyTmr1,

                   (RTOS_ERR *)&err);

        /* Check “err” */

        // Continues in the next code listing!

OSCfg TmrWheell] [0]0 0 [20 [3 0 0 NextPtr NextPtr [5]0 [6]0 170 [8]0 0- PrevPtr Remain = 1 "Match = 13 OS TMFR PrevPtr Remain = 10 "Match = 22 OS TMFR NbrEntriesMax FirstPtr OSTmrTickCtr= 12 NbrEntries

Explanation / Answer

RTOS_TMR  MyTmr1;

RTOS_TMR  MyTmr2;

  

  

void MyTmrCallbackFnct1 (void *p_arg)

{

    /* Do something when timer #1 expires */

}

  

  

void MyTmrCallbackFnct2 (void *p_arg)

{

    /* Do something when timer #2 expires */

}

  

  

void MyTask (void *p_arg)

{

    RTOS_ERR  err;

  

  

    while (DEF_ON) {

        :

        RTOSTmrCreate((RTOS_TMR            *)&MyTmr1,

                   (RTOS_CHAR           *)“My Timer #1”,

                   (RTOS_TICK            )1,

                   (RTOS_TICK            )0,

                   (RTOS_OPT             )RTOS_OPT_TMR_ONE_SHOT,

                   (RTOS_TMR_CALLBACK_PTR)MyTmrCallbackFnct1,

                   (void              *)0,

                   (RTOS_ERR            *)&err);

        /* Check ’err” */

        RTOSTmrStart ((RTOS_TMR *)&MyTmr1,

                   (RTOS_ERR *)&err);

        /* Check “err” */

        // Continues in the next code listing!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote