/
mainTask.c
113 lines (86 loc) · 2.85 KB
/
mainTask.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
/*
* mainTask.c
*
* Created: 11.10.2014 20:18:00
* Author: Tomas Baca
*/
#include "mainTask.h"
#include "cspTask.h"
#include "system.h"
#include "queue.h"
#include "usart_driver_RTOS.h"
// to save outcomming time of ping
uint32_t pingSent;
// for ping time difference
int16_t timediff;
/* -------------------------------------------------------------------- */
/* The main task */
/* -------------------------------------------------------------------- */
void mainTask(void *p) {
// packet used to handle incoming communication
csp_packet_t * outcomingPacket = csp_buffer_get(CSP_PACKET_SIZE);
// character received from uart
char inChar;
/* The variable used to receive from the queue. */
xCSPStackEvent_t xReceivedEvent;
int i; // iterator
// text buffer for uart
char text[20];
// infinite while loop of the program
while (1) {
if (xQueueReceive(xCSPEventQueue, &xReceivedEvent, 1)) {
switch (xReceivedEvent.eEventType) {
case generalCommEvent:
// send its content to the uart
for (i = 0; i < ((csp_packet_t *) xReceivedEvent.pvData)->length; i++) {
usartBufferPutByte(pc_usart_buffer, ((csp_packet_t *) xReceivedEvent.pvData)->data[i], 10);
}
break;
case pingReceivedEvent:
// calculate the ping return time
if ((int16_t) milisecondsTimer - (int16_t) pingSent > 0)
timediff = milisecondsTimer - pingSent;
else
timediff = (int16_t) milisecondsTimer - (int16_t) pingSent + (int16_t) 1000;
itoa(timediff, text, 10);
usartBufferPutString(pc_usart_buffer, "ping received in ", 10);
usartBufferPutString(pc_usart_buffer, text, 10);
usartBufferPutString(pc_usart_buffer, "ms\n\r", 10);
break;
default:
/* Should not get here. */
break;
}
}
// if there is something from the uart
if (usartBufferGetByte(pc_usart_buffer, &inChar, 0)) {
outcomingPacket->data[0] = inChar;
outcomingPacket->length = 1;
switch (inChar) {
// ask board for free memory heap
case 'm':
csp_sendto(CSP_PRIO_NORM, CSP_BOARD_ADDRESS, 16, 15, CSP_O_NONE, outcomingPacket, 10);
break;
// ask board for status
case 'h':
csp_sendto(CSP_PRIO_NORM, CSP_BOARD_ADDRESS, 17, 15, CSP_O_NONE, outcomingPacket, 10);
break;
// ask board for status
case 'a':
csp_sendto(CSP_PRIO_NORM, CSP_BOARD_ADDRESS, 18, 15, CSP_O_NONE, outcomingPacket, 10);
break;
// ask board for status
case 'p':
outcomingPacket->data[0] = 0;
outcomingPacket->length = 1;
pingSent = milisecondsTimer;
csp_sendto(CSP_PRIO_NORM, CSP_BOARD_ADDRESS, 1, 32, CSP_O_NONE, outcomingPacket, 10);
break;
// sends the char and is supposed to receive it back
default:
csp_sendto(CSP_PRIO_NORM, CSP_BOARD_ADDRESS, 15, 15, CSP_O_NONE, outcomingPacket, 10);
break;
}
}
}
}