forked from cvra/nastya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_.c
144 lines (108 loc) · 3.47 KB
/
init_.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
#include <ucos_ii.h>
#include <nios2.h>
#include <lwip/sys.h>
#include <lwip/tcpip.h>
#include <lwip/ip.h>
#include <netif/slipif.h>
#include <error.h>
#include <trace/trace_over_ip.h>
#include "control.h"
#include "match.h"
#include "position_integration.h"
#include "drive.h"
#include "encoder_readout_task.h"
#include "tasks.h"
OS_STK init_task_stk[INIT_TASK_STACKSIZE];
//
// IP stack init
//
/** Shared semaphore to signal when lwIP init is done. */
sys_sem_t lwip_init_done;
void list_netifs(void)
{
struct netif *n; /* used for iteration. */
for(n=netif_list;n !=NULL;n=n->next) {
/* Converts the IP adress to a human readable format. */
char buf[16+1];
ipaddr_ntoa_r(&n->ip_addr, buf, 17);
printf("%s%d: %s\n", n->name, n->num, buf);
}
}
/** @rief Callback for lwIP init completion.
*
* This callback is automatically called from the lwIP thread after the
* initialization is complete. It must then tell the main init task that it
* can proceed. To do thism we use a semaphore that is posted from the lwIP
* thread and on which the main init task is pending. */
void ipinit_done_cb(void *a)
{
sys_sem_signal(&lwip_init_done);
}
/** @brief Inits the IP stack and the network interfaces.
*
* This function is responsible for the following :
* 1. Initialize the lwIP library.
* 2. Wait for lwIP init to be complete.
* 3. Create the SLIP interface and give it a static adress/netmask.
* 4. Set the SLIP interface as default and create a gateway.
* 5. List all network interfaces and their settings, for debug purposes.
*/
void ip_stack_init(void) {
/* Netif configuration */
static ip_addr_t ipaddr, netmask, gw;
static struct netif slipf;
#ifdef __unix__
/* for some reason 192.168.4.9 fails on my test station. */
IP4_ADDR(&gw, 10,0,0,1);
IP4_ADDR(&ipaddr, 10,0,0,2);
IP4_ADDR(&netmask, 255,255,255,0);
#else
IP4_ADDR(&gw, 10,0,0,1);
IP4_ADDR(&ipaddr, 10,0,0,20);
IP4_ADDR(&netmask, 255,255,255,255);
#endif
/* Creates the "Init done" semaphore. */
sys_sem_new(&lwip_init_done, 0);
/* We start the init of the IP stack. */
tcpip_init(ipinit_done_cb, NULL);
/* We wait for the IP stack to be fully initialized. */
printf("Waiting for LWIP init...\n");
sys_sem_wait(&lwip_init_done);
/* Deletes the init done semaphore. */
sys_sem_free(&lwip_init_done);
printf("LWIP init complete\n");
#ifdef __unix__
/* Adds a tap pseudo interface for unix debugging. */
netif_add(&slipf,&ipaddr, &netmask, &gw, NULL, tapif_init, tcpip_input);
#else
/* Adds the serial interface to the list of network interfaces and makes it the default route. */
netif_add(&slipf, &ipaddr, &netmask, &gw, NULL, slipif_init, tcpip_input);
#endif
netif_set_default(&slipf);
netif_set_up(&slipf);
printf("Listing network interfaces...\n");
list_netifs();
}
void init_task(void *pdata)
{
NOTICE(0, "System boot !");
control_init();
ip_stack_init();
luaconsole_init();
start_position_integration();
trace_over_ip_init(10000);
start_drive_task();
ready_for_match();
OSTaskDel(INIT_TASK_PRIORITY);
}
void create_init_task(void)
{
OSTaskCreateExt(init_task,
NULL,
&init_task_stk[INIT_TASK_STACKSIZE-1],
INIT_TASK_PRIORITY,
INIT_TASK_PRIORITY,
&init_task_stk[0],
INIT_TASK_STACKSIZE,
NULL, 0);
}