-
Notifications
You must be signed in to change notification settings - Fork 4
/
Phantom_sci.c
195 lines (148 loc) · 4.02 KB
/
Phantom_sci.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
#include "Phantom_sci.h"
#include <halcogen_vcu/include/sci.h>
#include <halcogen_vcu/include/gio.h>
#include "string.h"
#include "stdarg.h"
#include "stdio.h"
#include "board_hardware.h"
#include "FreeRTOS.h"
#include "os_task.h"
#include "task_logger.h"
#include "task_event_handler.h"
#include "state_machine.h"
#define NUMBER_OF_SIMULATION_MESSAGES 3
static volatile uint8_t messageCounter = 0;
static volatile uint32_t serialData = 0; // there is 24 bit standard type so when we cast, we have to cast to 32 bit hence 4 bytes
#define TASK_LIST_SIZE 512
enum eCommands{
ECHO_THROTTLE='1',
ECHO_APPS1='2',
ECHO_APPS2='3',
ECHO_BSE='4',
STAT_RUN='5', // vTaskGetRunTimeStats
STAT_START='6', // xTaskGetTickCount
TASK_LIST='7', // vTaskList
RESET_CAR='r',
START_ENGINE='s',
TURN_TRACTIVE_ON='o',
};
void UARTSend(sciBASE_t *sci, char data[])
{
char *first = &data[0];
sciSend(sci, strlen(data),(uint8 *)first);
}
void UARTInit(sciBASE_t *sci, uint32 baud)
{
_enable_IRQ();
sciInit();
sciSetBaudrate(sci, baud);
sciEnableNotification(sci, SCI_RX_INT);
sciReceive(sci, 1, (unsigned char*)NULL); // clear interrupt flag
}
void UARTprintf(const char *_format, ...)
{
char str[128];
memset(str, '\0', 128 * sizeof(char));
int8_t length = -1;
va_list argList;
va_start( argList, _format );
length = vsnprintf(str, sizeof(str), _format, argList);
str[127] = '\0';
va_end( argList );
if (length > 0)
{
sciSend(PC_UART, (unsigned)length, (unsigned char*)str);
}
}
void UARTprintln(const char *_format, ...)
{
char str[128];
memset(str, '\0', 128 * sizeof(char));
int8_t length = -1;
va_list argList;
va_start( argList, _format );
length = vsnprintf(str, sizeof(str), _format, argList);
str[127] = '\0';
va_end( argList );
if (length > 0)
{
sciSend(PC_UART, (unsigned)length, (unsigned char*)str);
// only diff between this and UARTprintf because passing variable args between functions in C are weird and I don't wanna deal with it rn
sciSend(PC_UART, 2, "\r\n");
}
}
uint32_t getSimData()
{
while(messageCounter < NUMBER_OF_SIMULATION_MESSAGES);
gioSetBit(gioPORTA, 5, 1);
uint32_t ret = serialData;
// reset volatile values
messageCounter = 0;
serialData = 0;
return ret;
}
void GetRuntimeStatistics(void* x)
{
// TODO: not configured!
// vTaskGetRunTimeStats(ptrTaskList);
char ptrTaskList[TASK_LIST_SIZE];
vTaskList(ptrTaskList);
UARTprintln("\n");
UARTSend(PC_UART, ptrTaskList);
UARTprintln("\r");
}
void sciReceiveCallback(sciBASE_t *sci, uint32 flags, uint8 data)
{
#ifdef VCU_SIM_MODE
if (messageCounter < NUMBER_OF_SIMULATION_MESSAGES)
{
serialData |= data << (messageCounter*8);
messageCounter++;
}
#else
char buffer[16];
switch(data){
case TASK_LIST:
HandleEventFromISR(GetRuntimeStatistics, 0);
break;
case ECHO_THROTTLE:
LogFromISR(UWHT, "No data available yet.");
break;
case ECHO_APPS1:
LogFromISR(UWHT, "No data available yet.");
break;
case ECHO_APPS2:
LogFromISR(UWHT, "No data available yet.");
break;
case ECHO_BSE:
LogFromISR(UWHT, "No data available yet.");
break;
case STAT_START:
{
snprintf(buffer, 16, "%dms", xTaskGetTickCountFromISR());
LogFromISR(UWHT, buffer);
break;
}
case TURN_TRACTIVE_ON:
{
NotifyStateMachineFromISR(EVENT_TRACTIVE_ON);
break;
}
case START_ENGINE:
{
NotifyStateMachineFromISR(EVENT_READY_TO_DRIVE);
break;
}
case RESET_CAR:
{
NotifyStateMachineFromISR(EVENT_RESET_CAR);
break;
}
default:
{
LogFromISR(UWHT, "Unknown: ");
break;
}
}
#endif
}