Bar Logo 4kW dsPIC33C PSFB DC-DC DA (Part-No. )
 
Content
     
Loading...
Searching...
No Matches
os_timer.c
1//=======================================================================================================
2// Copyright(c) 2018 Microchip Technology Inc. and its subsidiaries.
3// Subject to your compliance with these terms, you may use Microchip software and any derivatives
4// exclusively with Microchip products. It is your responsibility to comply with third party license
5// terms applicable to your use of third-party software (including open source software) that may
6// accompany Microchip software.
7// THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY,
8// APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND
9// FITNESS FOR A PARTICULAR PURPOSE.
10// IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
11// LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
12// MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
13// ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT
14// EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
15//=======================================================================================================
16
17//=======================================================================================================
18// @file os_timer.c
19//
20// @brief contains the functions for using software timers
21//
22// @note in this file you will find functions for using timers and countdowns for your application
23// These software timers are updated by the hardware timer interrupt of the os_scheduler
24// The number of timers need to be defined in main/project_settings.h :
25// #define OS_TIMER_NUMBER_OF_TIMERS 3
26// You should also put your your individual Timer defines with the corresponding index there
27// #define OS_TIMER_COUNTDOWN_EXAMPLE_SWITCHOFFTHELIGHT 0
28// #define OS_TIMER_COUNTDOWN_EXAMPLE_SWITCHOFFCOFFEEMACHINE 1
29// #define OS_TIMER_COUNTDOWN_EXAMPLE_IAMGOINGHOMENOW 2
30//
31//
32// @version v1.0
33// @date 2019-08-30
34// @author M52409
35//
36//=======================================================================================================
37
38#include <stdbool.h>
39#include <stdint.h>
40#include <string.h>
41#include "../project_settings.h"
42#include "os_timer.h"
43
44#ifndef OS_TIMER_NUMBER_OF_TIMERS
45 #error OS_TIMER_NUMBER_OF_TIMERS needs to be defined in the file project_settings.h
46#endif //OS_TIMER_NUMBER_OF_TIMERS
47
48#if OS_TIMER_NUMBER_OF_TIMERS > 0
49
50typedef struct
51{
52 uint16_t timerval;
53 uint8_t countdown:1;
54 uint8_t countup:1;
55 uint8_t expired:1;
56 uint8_t reserved:5;
57} OS_TIMER_DATA_t;
58
59/* The variable below need not be static */
60/* LDRA_EXCLUDE 27 D */
61#if OS_TIMER_NUMBER_OF_TIMERS > 0
62OS_TIMER_DATA_t os_timer_data[OS_TIMER_NUMBER_OF_TIMERS];
63#endif
64
65//=======================================================================================================
66// @brief OS_Timer_Init initializes the Software Timers
67// @note call this function at boot up to initialize the Software Timers
68//=======================================================================================================
69void OS_Timer_Init(void)
70{
71#if OS_TIMER_NUMBER_OF_TIMERS > 0
72 uint8_t i;
73 /* Return of the memset is not required */
74 /* LDRA_EXCLUDE 382 S */
75 memset(os_timer_data, 0, sizeof(os_timer_data));
76 for (i=0U; i < OS_TIMER_NUMBER_OF_TIMERS; i++)
77 {
78 os_timer_data[i].expired = true;
79 }
80#endif
81}
82
83
84//=======================================================================================================
85// @brief OS_Timer_StartCountdown_100us/1ms starts a new Countdown
86// @note call this function if you want to use a new countdown
87// @param timer_index - index of the timer that should be used for that countdown
88// @param numOfTicks - depending on the scheduler timing, the number of 100µs or 1ms ticks to count
89//=======================================================================================================
90
91/* OS_Timer_StartCountdown_1ms, OS_Timer_StartCountdown_100us and
92 OS_Timer_IsCountdownExpired are not called in the project, hence the violations
93 76 D and 61 D */
94/* LDRA_EXCLUDE 76 D */
95/* LDRA_EXCLUDE 61 D */
96#if OS_USE_SCHEDULER_100us == 1
97void OS_Timer_StartCountdown_100us(uint8_t timer_index, uint16_t numOfTicks)
98#elif OS_USE_SCHEDULER_1ms == 1
99void OS_Timer_StartCountdown_1ms(uint8_t timer_index, uint16_t numOfTicks)
100#else
101#error OS_USE_SCHEDULER_100us or OS_USE_SCHEDULER_1ms needs to be definied as 1 to get the right timer functions
102#endif
103{
104 if (timer_index < OS_TIMER_NUMBER_OF_TIMERS)
105 {
106 os_timer_data[timer_index].countup = false;
107 os_timer_data[timer_index].countdown = true;
108 os_timer_data[timer_index].expired = false;
109 os_timer_data[timer_index].timerval = numOfTicks;
110 }
111}
112
113/* LDRA_EXCLUDE 76 D */
114/* LDRA_EXCLUDE 61 D */
115bool OS_Timer_IsCountdownExpired(uint8_t timer_index)
116{
117 if (timer_index >= OS_TIMER_NUMBER_OF_TIMERS)
118 {
119 return false;
120 }
121 if (os_timer_data[timer_index].expired == 0U)
122 {
123 return false;
124 }
125 return true;
126}
127
128void OS_Timer_Tick(void)
129{
130 uint8_t i;
131 for (i=0U; i < OS_TIMER_NUMBER_OF_TIMERS; i++)
132 {
133 if (os_timer_data[i].countdown == 1U)
134 {
135 if (os_timer_data[i].timerval != 0U)
136 {
137 os_timer_data[i].timerval -= 1U;
138 if (os_timer_data[i].timerval == 0U)
139 {
140 os_timer_data[i].expired = true;
141 }
142 }
143 }
144 }
145}
146
147#endif
uint16_t reserved
Definition uart1.c:97