-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOS.c
223 lines (174 loc) · 5.69 KB
/
SOS.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* TMU.c
*
* Created: 24/02/2020 01:22:29 م
* Author: mo
*/
#include "SOS.h"
#define Buffer_Size 3
uint8_t u8_Init_finished=0;
uint8_t u8_Is_Intialized=0;
start_status start[Buffer_Size];
uint8_t u8_FUN_index=0;
Buffer_Request Buffer_Array[Buffer_Size];
volatile static uint8_t u8Excution_to_be_done=0;
//static uint8_t u8_is_Started=0;
uint8_t Start_status[Buffer_Size];
ERROR_STATUS SOS_Init (const TMU_ConfigType * ConfigPtr )
{uint8_t u8_channel_ID=ConfigPtr->u8_Timer_channel;
uint8_t ret=E_OK;
/*************************************************************************/
/* Intializ the required timer identified by the user */
/************************************************************************/
u8_Is_Intialized++;
if(ConfigPtr==NULL){ret=TMU_MODULE+NULL_PTR;}
switch(u8_channel_ID)
{
case TIMER_CH0:
Timer_Init(&Timer_Configuration0);
break;
case TIMER_CH2:
Timer_Init(&Timer_Configuration2_SOS);
TIMER2OVF_INT = timer_interrupt;
break;
default:
ret+=E_NOK;
break;
}
/*************************************************************************/
/*Intializ the required timer identified by the user using required prescaler */
/************************************************************************/
switch(ConfigPtr->u8_resolution)
{
case TIMER_RESOLUTION_1_MS:
{
switch(u8_channel_ID)
{
case TIMER_CH0 :
Timer_Start(TIMER_CH0,250);
//TCNT0=5;
break;
case TIMER_CH2 :
Timer_Start(TIMER_CH2,250);
break;
default:
ret+=E_NOK;
break;
}
break;
}
default:
ret+=E_NOK;
break;
}
/****************************************************************************/
/* Deal with Errors to be returned */
/***************************************************************************/
return ret;
}
ERROR_STATUS TMU_DeInit( void )
{
uint8_t ret=E_OK;
/************************************************************************************************/
/*1 De_intialize the timer */
/*2 make sure this function is not called unless timer was intialized before by TMU_INIT */
/************************************************************************************************/
if (u8_Is_Intialized==1)
{/*Make the timer without a clk*/
Timer_Init(&Timer_Deinit_Configuration0);
u8_Is_Intialized=0;
}else if(u8_Is_Intialized>1){ret+=MULTIPLE_INIT;}
else{
ret=E_NOK;
}
return ret;
}
ERROR_STATUS SOS_Create_Task(ptr_to_Fun Function_Consumer,uint8_t u8_Preodicity,
uint8_t u8_function_index,uint16_t u16_Time_delay)
{uint8_t ret=E_OK;
u8Excution_to_be_done++;
/*
this enables interrupt and start the timer with the required configurations
and provide the request to request buffer (ptr_to_func,delay) Dispatcher with the call back function needed(consumer)
and make sure this function must not be excuted unless Init happened and De_init not happened
*/
if(u8_Is_Intialized == 1)
{
(Buffer_Array[u8_function_index]).Fuction_consumer=Function_Consumer;
(Buffer_Array[u8_function_index]).preodic=u8_Preodicity;
(Buffer_Array[u8_function_index]).u16_time_delay=u16_Time_delay;
(Buffer_Array[u8_function_index]).u8_flag_is_stopped=1;
}
else if(u8_Is_Intialized>1){ret+=MULTIPLE_INIT;}else{
ret+=NOT_INIT;
}
return ret;
}
ERROR_STATUS SOS_Run(void)
{
uint8_t ret=E_OK;
/*uint8_t status_flag=0;
uint8_t preodic_status_flag=0;
uint16_t u16_Time_needed=0;
ptr_to_Fun Excuted;
*//***********************************************************************/
/* LOOP upon Buffer Request */
/*********************************************************************/
while(1)
{
if (u8Excution_to_be_done)
{ u8Excution_to_be_done=0;
for (u8_FUN_index=0;u8_FUN_index<Buffer_Size;u8_FUN_index++)
{/*
Excuted=((Buffer_Array[u8_FUN_index]).Fuction_consumer);
u16_Time_needed =(Buffer_Array[u8_FUN_index]).u16_time_delay;
status_flag =(Buffer_Array[u8_FUN_index]).u8_flag_is_stopped;
preodic_status_flag=(Buffer_Array[u8_FUN_index]).preodic;
*/
/****************************************************************/
/* check if the event is stopped ... do nothing*/
if((Buffer_Array[u8_FUN_index]).u8_flag_is_stopped==0){ret=E_OK;}
else{
/***************************************************************/
/*if the timer started .. and condition met excute the consumer*/
/***************************************************************/
/********************************************/
/*Count up when flag is set**/
((Buffer_Array[u8_FUN_index]).u16_Counter)++;
/*make the flag down*/
/* u16Excution_to_be_done=100; ISR*/
if(((Buffer_Array[u8_FUN_index]).u16_time_delay) == ((Buffer_Array[u8_FUN_index]).u16_Counter) )
{
((Buffer_Array[u8_FUN_index]).Fuction_consumer)();
/******************************************/
/*Make it zero so that it will work the next time at the required time (to stop it of ovf)*/
(Buffer_Array[u8_FUN_index]).u16_Counter=0;
}
/*********************************************************/
/* if not preodic increment the index after excuteing */
/*********************************************************/
if((Buffer_Array[u8_FUN_index]).preodic==0)
{
/*to make it see the next event in the next time
Also Status_flag =1 so that this function will be stopped
*/
(Buffer_Array[u8_FUN_index]).u8_flag_is_stopped=0;
}
}
}
}
return ret;
}
}
void TMU_Stop_Timer(uint8_t u8_function_index)
{
/***************************************************/
/* Set the stop flag */
/***************************************************/
(Buffer_Array[u8_function_index]).u8_flag_is_stopped=0;
}
void timer_interrupt(void)
{
u8Excution_to_be_done=1;
PIR1 &= (~TMR2IF);
}