-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_task.c
202 lines (139 loc) · 5.08 KB
/
init_task.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
/*
* init_task.c
*
* Created on: 7. 1. 2024
* Author: vojtechlukas
*/
// === INCLUDES =================================================================================================
#include <source/ethernet/IPAddress.h>
#include <source/ethernet/Ethernet.h>
#include <source/oled_gui/gui.h>
#include <source/utils/log.h>
#include <source/utils/restart.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/SPI.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <source/utils/stv.h>
#include <source/html/html.h>
#include <source/utils/handler_funcs.h>
#include "ti_drivers_config.h"
#include <ti/drivers/Timer.h>
// ===============================================================================================================
// === INTERNAL FUNCTIONS =======================================================================================
static inline void byte2hex(uint8_t byte, char *pHex)
{
uint8_t nib = (byte & 0xF0) >> 4;
*pHex++ = nib > 9 ? nib + 'A' : nib + '0';
nib = (byte & 0xF);
*pHex = nib > 9 ? nib + 'A' : nib + '0';
return;
}
static void mac2string(uint8_t *pSrc, char *pDst)
{
uint8_t i;
for (i = 0; i < 6; i++)
{
byte2hex(*pSrc, pDst);
pDst += 2;
pSrc += 1;
if (i <= 5)
{
*pDst = '-';
pDst++;
}
}
}
// ==============================================================================================================
// === EXTERNAL SYMBOLS =========================================================================================
extern Semaphore_Handle Init_SemaphoreHandle;
extern void Main_CreateDashboardTask();
extern void Main_CreateSniffingTask();
// ==============================================================================================================
// === MAIN TASK FUNCTION =======================================================================================
/*
* === Init_Main
* Initialization task function. Its objectives are:
* - Driver initializations: Ensure all peripherals are initialized.
* - Status Vector Initialization: Ensure Status Vectors are copied from FLASH to RAM.
* - Network Connection: Ensure that the device successfully connects to the network. If DHCP request of
* IP address fails, the device restarts and assigns custom IP address to itself.
* -
* Parameters:
* a0 - Not used
* a1 - Not used
* Returns:
* N/A
*/
void Init_Main(UArg a0, UArg a1)
{
uint8_t retVal = 0;
IPAddress dvcIp, tgtIp, gtwIp, dnsIp, msk;
char pDvcIpBuf[16] = {0};
char pTgtIpBuf[16] = {0};
char pMacStr[17] = {0};
///////////////////////////
// Clear restart flag if
// present
//
STV_WriteAtAddress(STV_REQUEST_RESTART, 0x00);
///////////////////////////
// Driver Initialization:
// Initialization functions are called
// and startup vectors copied
// from flash to RAM
//
SPI_begin();
GUI_Init();
STV_CopyStvFromFlashIfNotYet();
///////////////////////////
// Ethernet initialization:
// According to STV, use either DHCP or static address.
// Note, that if DHCP fails (request timeout occurs),
// device is restarted with STV_DHCP flag set to STV_DHCP_FALSE.
// Device then sets itself static IP address.
//
if (STV_ReadFromAddress(STVW_USING_DHCP) == STV_DHCP_TRUE)
{
GUI_ChangeDeviceIp("DHCP pending...");
Log_print("Waiting for DHCP...", NULL, None);
retVal = Ethernet_begin_mac((uint8_t *)STVR_MAC_ADDRESS);
if (retVal != 1)
{
Log_print("DHCP failed, switching to static", NULL, None);
STV_WriteAtAddress(STVW_USING_DHCP, STV_DHCP_FALSE);
RestartMCU();
}
}
else if (STV_ReadFromAddress(STVW_USING_DHCP) == STV_DHCP_FALSE)
{
IPAddress_Init_str(&dvcIp, (uint8_t *)STVW_DEVICE_IP_ADDRESS);
IPAddress_Init_str(>wIp, (uint8_t *)STVW_GATEWAY_IP_ADDRESS);
IPAddress_Init_str(&msk, (uint8_t *)STVW_NETWORK_MASK);
dnsIp.dword = 0x01010101;
Ethernet_begin_mac_ip_dns_gateway_subnet((uint8_t *)STVR_MAC_ADDRESS,
dvcIp, dnsIp, gtwIp, msk);
}
dvcIp = Ethernet_localIP();
IPAddress_toString(dvcIp, pDvcIpBuf);
Log_print("Device IP: ", &dvcIp, IpAddress);
GUI_ChangeDeviceIp(pDvcIpBuf);
///////////////////////////
// Target IP Setting:
//
IPAddress_Init_str(&tgtIp, (uint8_t *)STVW_TARGET_IP_ADDRESS);
Log_print("Target IP: ", &tgtIp, IpAddress);
IPAddress_toString(tgtIp, pTgtIpBuf);
GUI_ChangeTargetIp(pTgtIpBuf);
///////////////////////////
// Other settings:
//
mac2string((uint8_t *)STVR_MAC_ADDRESS, pMacStr);
Html_SetKeyValueInBuffer('m', pMacStr);
GUI_ChangeRx(false);
GUI_ChangeProto(STV_ReadFromAddress(STVW_RF_PROTOCOL) == STV_RF_PROTO_BLE ? 0 : 1);
GUI_ChangeChannel(STV_ReadFromAddress(STVW_RF_CHANNEL));
Main_CreateDashboardTask();
Main_CreateSniffingTask();
return;
}