-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_tasks.c
332 lines (248 loc) · 12.7 KB
/
my_tasks.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* my_tasks.c
*
* Created on: Dec 30, 2021
* Author: Mecit
*/
#include "my_tasks.h"
extern UART_HandleTypeDef huart2;
extern DMA_HandleTypeDef hdma_usart2_tx;
extern DMA_HandleTypeDef hdma_usart2_rx;
TaskHandle_t firstxHandler = NULL;
TaskHandle_t communucationTaskHandler = NULL;
TaskHandle_t communucationReceiverHandler = NULL;
//static char firstxHandlerArr[50];
//static char commTaskHandlerArr[50];
//static char communucationReceiverHandlerArr[50];
//void HAL_UARTEx_RxEventCallback( UART_HandleTypeDef *huart, uint16_t Size )
//{
//
// if ( USART2 == huart->Instance )
// {
//
// // vTaskSuspendAll(); // NOT SAFE CHECK WHAT CAN WE DO
// receivedFlag = RECEIVE_COMPLETED; // to prevent race conditions.
// uartBuffers.LastReceivedByte = Size; // transfer total received bytes to LastReceivedByte
//// xTaskResumeAll(); // NOT SAFE CHECK WHAT CAN WE DO
//
//
//
////
//
// }
//}
void timerCLLBCK( TimerHandle_t pxTimer )
{
struct timerIDS *specificTimer = ( struct timerIDS * )pvTimerGetTimerID(pxTimer);
static const char *TELEM_MES = "TELEM TIMER ACTIVATED.\r\n";
static const char *MOTOR_MES = "MANUAL MOTOR DEACTIVATING..\r\n";
static const char *ERROR_MES = "DATA COULDNT READ SPECIFIC INTERVAL!\r\n";
static const char *DT_OK_MES = "DATA IS READY SENDING..\r\n";
static const char *WAITI_MES = "WAITING FOR DATA...\r\n";
static char telemetryINFO[60] ;
switch (specificTimer->T_ID)
{
case TELEM_TIMER:
// HAL_UART_Transmit( &huart2, ( UINT8* )TELEM_MES, strlen( TELEM_MES ), 500 );
while ( HAL_UART_Transmit_DMA( &huart2, ( uint8_t * )TELEM_MES , strlen( TELEM_MES ) ) != HAL_OK);
// sendWith_UART_DMA( &huart2, ( uint8_t * )TELEM_MES , strlen( TELEM_MES ) );
// if the telemetry is not ready wait 150 ms until its ready.
if (D_READY != dataPcktFlag)
{
UINT8 printFlag = 0;
TickType_t countDownTimer = xTaskGetTickCount();
while (xTaskGetTickCount() - countDownTimer < 150)
{
if (0 == printFlag)
{
printFlag = 1;
// HAL_UART_Transmit( &huart2, ( UINT8* )WAITI_MES , strlen(WAITI_MES), 500 ); // Print waiting for data only just one time.
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )WAITI_MES , strlen(WAITI_MES) ) != HAL_OK ); // Print waiting for data only just one time.
}
if (D_READY == dataPcktFlag) break; // If the data ready break and continue.
}
if (D_NOT_READY == dataPcktFlag) // After while loop dataPcktFlag may didn't change so that check it one more time.
{
// PLEASE ON ERROR FLAG IN HERE.
// HAL_UART_Transmit( &huart2, ( UINT8* )ERROR_MES , strlen(ERROR_MES), 500 ); // Error Message (DATA COULDNT READ SPECIFIC INTERVAL!)
HAL_UART_Transmit_DMA( &huart2, ( UINT8* )ERROR_MES , strlen(ERROR_MES) ); // Error Message (DATA COULDNT READ SPECIFIC INTERVAL!)
return;
}
}
// if The Telemetry datas ready go further.
// HAL_UART_Transmit( &huart2, ( UINT8* )DT_OK_MES , strlen(DT_OK_MES), 500 );
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )DT_OK_MES , strlen(DT_OK_MES) ) != HAL_OK);
vTaskSuspendAll(); // Prevent race condition.
dataPcktFlag = D_NOT_READY;
xTaskResumeAll(); // Prevent race condition.
sprintf( telemetryINFO,"Package No : %ld , Pressure : %.2f \r\n",myPacket.packageNo,myPacket.pressure );
// HAL_UART_Transmit( &huart2, ( UINT8* )telemetryINFO, strlen( telemetryINFO ), 500 );
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )telemetryINFO, strlen( telemetryINFO ) ) != HAL_OK );
break;
case MANUAL_MOTOR_TIMER:
//Deactivate Manual Motor in Here.
// HAL_UART_Transmit( &huart2, ( UINT8* )MOTOR_MES, strlen( MOTOR_MES ), 500 ); // Printing a message for does it work or not?
while (HAL_UART_Transmit_DMA( &huart2, ( UINT8* )MOTOR_MES, strlen( MOTOR_MES ) ) != HAL_OK); // Printing a message for does it work or not?
break;
default:
break;
}
}
void taskFunctionCommRecv( void * pvParameters )
{
static const char *NotifyMesTake = "Notify Taked From taskFunction\r\n";
static const char *NotifyMesGive = "Now Notify Giving to taskFunction\r\n";
static char LEDMesBuffer[30];
static TickType_t currentTime; // Save TickCount in here.
// WaterMark
// static char AvBytes[30];
// UBaseType_t uxHighWaterMark;
// uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
for ( ;; )
{
ulTaskNotifyTake( pdTRUE , portMAX_DELAY ); // wait UNTIL taskFunction gives Permission.
// HAL_UART_Transmit( &huart2, ( UINT8* )NotifyMesTake, strlen( NotifyMesTake ), 500 ); //Print Notify Taked From taskFunction
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )NotifyMesTake, strlen( NotifyMesTake ) ) != HAL_OK); //Print Notify Taked From taskFunction
UINT8 ledCounter = 1;
currentTime = xTaskGetTickCount(); // save current tickcount.
while ( ( xTaskGetTickCount() - currentTime ) < 500 ) // Until 500 TC Led On//off and Print a Message
{
HAL_GPIO_TogglePin( LD2_GPIO_Port, LD2_Pin );
sprintf(LEDMesBuffer,"LED_MESSAGE[-> %u ]\r\n",ledCounter);
ledCounter += 1;
// HAL_UART_Transmit( &huart2, ( UINT8* )LEDMesBuffer, strlen( LEDMesBuffer ), 500 );
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )LEDMesBuffer, strlen( LEDMesBuffer ) ) != HAL_OK);
vTaskDelay(pdMS_TO_TICKS( 80 )); // Wait 80ms.
}
// HAL_UART_Transmit( &huart2, ( UINT8* )NotifyMesGive, strlen( NotifyMesGive ), 500 );
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )NotifyMesGive, strlen( NotifyMesGive ) ) != HAL_OK);
/* WaterMark
uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
sprintf(AvBytes , "tskfnct -> Av: %ld \n\r",uxHighWaterMark);
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )AvBytes, strlen( AvBytes ) ) != HAL_OK);
*/
xTaskNotifyGive( firstxHandler ); // // now Locked this funct down and Give Permission to taskFunction.
}
}
/*This function a model of sensor readings and activates Descent Control function with taskNotify
*
*/
void taskFunction( void * pvParameters )
{
static const char *ButtonMessage = "Button Has been Pressed !\r\n";
static const char *NotifyMesGive = "Now Notify Giving To CommRecv\r\n";
static const char *NotifyMesTake = "Notify Taked from CommRecv\r\n";
static UINT8 BUTTON_STATE = 0;
TimerArray[MANUAL_MOTOR_TIMER].T_ID = MANUAL_MOTOR_TIMER; // timerCallback will understand what should it do according to timer IDS
TimerHandle_t manualMotorTimer = xTimerCreate( "manualMotTimer", pdMS_TO_TICKS( 2000 ), pdFALSE, ( void * )&TimerArray[MANUAL_MOTOR_TIMER], timerCLLBCK );
//WaterMark
// static char AvBytes[30];
// UBaseType_t uxHighWaterMark;
// uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
for( ;; )
{
if ( ( HAL_GPIO_ReadPin( blueButton_GPIO_Port, blueButton_Pin ) == GPIO_PIN_SET ) && ( BUTTON_STATE != 1 ) ) /* This state could be I2C readed sensor */
{
BUTTON_STATE = 1;
//Activate Motor in Here for 10 seconds (After 10 seconds timer will Rise and make it Off!)
xTimerStart( manualMotorTimer, portMAX_DELAY); // Changed from xTimerStart( manualMotorTimer, 0)
HAL_GPIO_TogglePin( LD2_GPIO_Port, LD2_Pin );
// HAL_UART_Transmit( &huart2, ( UINT8* )ButtonMessage, strlen( ButtonMessage ), 500 ); //"Button Has been Pressed !"
// HAL_UART_Transmit( &huart2, ( UINT8* )NotifyMesGive, strlen( NotifyMesGive ), 500 ); //"Now Notify Giving To CommRecv"
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )ButtonMessage, strlen( ButtonMessage ) ) != HAL_OK); //"Button Has been Pressed !"
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )NotifyMesGive, strlen( NotifyMesGive ) ) != HAL_OK); //"Now Notify Giving To CommRecv"
xTaskNotifyGive( communucationReceiverHandler ); // Wake up To taskFunctionCommRecv function and wait till the Take Notify from taskFunctionCommRecv!
ulTaskNotifyTake( pdTRUE , portMAX_DELAY ); // Take it Back!.
// HAL_UART_Transmit( &huart2, ( UINT8* )NotifyMesTake, strlen( NotifyMesTake ), 500 );
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )NotifyMesTake, strlen( NotifyMesTake ) ) != HAL_OK);
vTaskDelay( pdMS_TO_TICKS( 50 ) );
}
/* WaterMark
uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
sprintf(AvBytes , "taskFnction -> Av: %ld \n\r",uxHighWaterMark);
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8* )AvBytes, strlen( AvBytes ) ) != HAL_OK);
vTaskDelay(pdMS_TO_TICKS(200));
*/
BUTTON_STATE = 0;
}
}
// This function an example of triggering Timer and prevents race conditions with vTaskSuspendAll
void taskFunctionComm( void * pvParameters )
{
static char MessageBuffer[50];
static uint32_t COUNTER = 0;
static uint8_t LOCK_TIMER = 0;
static const char * const PREPARE_MES = "DATA HAS BEEN PREPARED!\r\n";
static const char * const RX_MES_RECEIVED = "Rx Message Has been Received\r\n";
TimerArray[TELEM_TIMER].T_ID = TELEM_TIMER;
TimerHandle_t telemetryTimerHandle = xTimerCreate( "timerTest", pdMS_TO_TICKS( 1000 ), pdTRUE, ( void * )&TimerArray[TELEM_TIMER], timerCLLBCK );
// WaterMark
// UBaseType_t uxHighWaterMark;
// uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
// static char AvailableBuf[30];
/* Activate Async UART DMA .*/
HAL_UARTEx_ReceiveToIdle_DMA( &huart2 , uartBuffers.RX_Buffer, RX_BUFFER_SIZE );
__HAL_DMA_DISABLE_IT( &hdma_usart2_rx , DMA_IT_HT );
/* Activate Async UART DMA .*/
static TickType_t MS_CT ;
static uint8_t timerActv = 0;
for( ;; )
{
if (timerActv == 0)
{
MS_CT = xTaskGetTickCount();
timerActv = 1;
}
sprintf(MessageBuffer,"%ld - This is From Rtos!\r\n",COUNTER);
if (xTaskGetTickCount() - MS_CT > 500)
{
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8 * )MessageBuffer, strlen( MessageBuffer ) ) != HAL_OK); // [Counter ] - This is From Rtos!
timerActv = 0;
}
if ( RECEIVE_COMPLETED == receivedFlag ) // if the data transmission has been identified and processed
{
memset( uartBuffers.Main_Uart_Buffer, '\0' , MAIN_BUFFER_SIZE ); // Copy the received bytes to Main Buffers.
memcpy( uartBuffers.Main_Uart_Buffer, uartBuffers.RX_Buffer , uartBuffers.LastReceivedByte ); // Copy the received bytes to Main Buffers.
memset( uartBuffers.RX_Buffer , '\0' ,RX_BUFFER_SIZE );
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8 * )RX_MES_RECEIVED, strlen( RX_MES_RECEIVED ) ) != HAL_OK ); // print RX Message has been received
while ( HAL_UART_Transmit_DMA( &huart2, uartBuffers.Main_Uart_Buffer, uartBuffers.LastReceivedByte ) != HAL_OK ); // print Received Message
receivedFlag = RECEIVE_NOT_COMPLETED;
HAL_UARTEx_ReceiveToIdle_DMA( &huart2 , uartBuffers.RX_Buffer, RX_BUFFER_SIZE ); // Activate UART DMA Again. Why? Because after the event Occures, DMA will be stopped Thats why.
__HAL_DMA_DISABLE_IT( &hdma_usart2_rx , DMA_IT_HT ); // Disable HALF Interrupt
}
if ( ( 0 == COUNTER % 5 ) && ( 0 == LOCK_TIMER ) )
{
xTimerStart( telemetryTimerHandle, portMAX_DELAY);
LOCK_TIMER = 1;
}
if (xTaskGetTickCount() - MS_CT > 500)
{
vTaskSuspendAll();
// HAL_UART_Transmit( &huart2, ( UINT8 * )PREPARE_MES, strlen( PREPARE_MES ), 500 );
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8 * )PREPARE_MES, strlen( PREPARE_MES ) ) != HAL_OK);
// WaterMark
// uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
// sprintf(AvailableBuf , "TaskFnct Comm -> Av : %ld\n\r",uxHighWaterMark);
// while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8 * )AvailableBuf, strlen( AvailableBuf) ) != HAL_OK);
/*
uxHighWaterMark = uxTaskGetStackHighWaterMark( firstxHandler );
sprintf( firstxHandlerArr , "frstx Av : %ld \n\r",uxHighWaterMark);
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8 * )firstxHandlerArr, strlen( firstxHandlerArr) ) != HAL_OK);
uxHighWaterMark = uxTaskGetStackHighWaterMark( communucationTaskHandler );
sprintf( commTaskHandlerArr , "CM HNDLR-> Av : %ld \n\r",uxHighWaterMark);
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8 * )commTaskHandlerArr, strlen( commTaskHandlerArr) ) != HAL_OK);
uxHighWaterMark = uxTaskGetStackHighWaterMark( communucationReceiverHandler );
sprintf( communucationReceiverHandlerArr, "RCVR Comm -> Av : %ld \n\r",uxHighWaterMark);
while ( HAL_UART_Transmit_DMA( &huart2, ( UINT8 * )communucationReceiverHandlerArr, strlen( communucationReceiverHandlerArr) ) != HAL_OK);
*/
myPacket.packageNo += 1;
myPacket.packageType = NORMAL_PACKET;
myPacket.pressure += 2.1f;
dataPcktFlag = D_READY;
xTaskResumeAll();
timerActv = 0;
}
COUNTER++;
vTaskDelay(pdMS_TO_TICKS(50));
}
}