-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
374 lines (302 loc) · 9.86 KB
/
main.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
This is the main body of the application.
Your application code will go into the infinite loop in main().
*/
#include "include_file.h"
static volatile unsigned long g_ulFlags;
volatile unsigned long g_ulTickCounter = 0;
#define UIP_PERIODIC_TIMER_MS 500
#define UIP_ARP_TIMER_MS 10000
#define FLAG_SYSTICK 0
#define FLAG_RXPKT 1
#define FLAG_TXPKT 2
#define FLAG_RXPKTPEND 3
#define FLAG_ENC_INT 4
#define SYSTICKHZ CLOCK_CONF_SECOND
#define SYSTICKMS (1000 / SYSTICKHZ)
#define SELECT_MEM() MAP_GPIOPinWrite(GPIO_PORTA_BASE, SRAM_CS, 0)
#define DESELECT_MEM() MAP_GPIOPinWrite(GPIO_PORTA_BASE, SRAM_CS, SRAM_CS)
void uip_log(char *msg);
static void cpu_init(void);
void UART4_HANDLER();
static void uart_init(void);
static void spi_init(void);
static void enc28j60_comm_init(void);
void spi_mem_write(uint16_t addr, const uint8_t *buf, uint16_t count);
void spi_mem_read(uint16_t addr, uint8_t *buf, uint16_t count);
void startup();
//This is your MAC ID set this to anything you want, maybe an old router.
const uint8_t mac_addr[] = { 0x00, 0xC0, 0x033, 0x50, 0x48, 0x12 };
long lPeriodicTimer, lARPTimer;
int main(void) {
//char buf[100];//not sure what this is for
startup();
setupIO();
//timerSetup();
ROM_UARTCharPutNonBlocking(UART4_BASE,'a');
while(true) {
SET_RUN_ON
write8Bits(0x00,modRegisters[0]);
write8Bits(0x01,modRegisters[1]);
write8Bits(0x02,modRegisters[2]);
write8Bits(0x03,modRegisters[3]);
if(ROM_UARTCharsAvail(UART4_BASE)) UART4_HANDLER();
//MAP_SysCtlSleep();
/************** Important! ***************
* This is where you program goes.
* Remove the MAP_SysCtlSleep()
* This puts the system in to sleep until
* the next systick interrupt.
* Replace with your app code here inside the
* while loop.
* Ensure the code below is still run.
*/
//Check if there is data waiting at the enc28j60
if( HWREGBITW(&g_ulFlags, FLAG_ENC_INT) == 1 ) {
HWREGBITW(&g_ulFlags, FLAG_ENC_INT) = 0;
enc_action();
}
//Check for systick update
if(HWREGBITW(&g_ulFlags, FLAG_SYSTICK) == 1) {
HWREGBITW(&g_ulFlags, FLAG_SYSTICK) = 0;
lPeriodicTimer += SYSTICKMS;
lARPTimer += SYSTICKMS;
}
if( lPeriodicTimer > UIP_PERIODIC_TIMER_MS ) {
lPeriodicTimer = 0;
int l;
for(l = 0; l < UIP_CONNS; l++) {
uip_periodic(l);
// If the above function invocation resulted in data that
// should be sent out on the network, the global variable
// uip_len is set to a value > 0.
if(uip_len > 0) {
uip_arp_out();
enc_send_packet(uip_buf, uip_len);
uip_len = 0;
}
}
for(l = 0; l < UIP_UDP_CONNS; l++) {
uip_udp_periodic(l);
if( uip_len > 0) {
uip_arp_out();
enc_send_packet(uip_buf, uip_len);
uip_len = 0;
}
}
}
if( lARPTimer > UIP_ARP_TIMER_MS) {
lARPTimer = 0;
uip_arp_timer();
}
}//while(true)
return 0;
}
uint8_t spi_send(uint8_t c) {
unsigned long val;
MAP_SSIDataPut(SSI0_BASE, c);
MAP_SSIDataGet(SSI0_BASE, &val);
return (uint8_t)val;
}
void dhcpc_configured(const struct dhcpc_state *s)
{
uip_sethostaddr(&s->ipaddr);
uip_setnetmask(&s->netmask);
uip_setdraddr(&s->default_router);
printf("IP: %d.%d.%d.%d\n", s->ipaddr[0] & 0xff, s->ipaddr[0] >> 8,
s->ipaddr[1] & 0xff, s->ipaddr[1] >> 8);
}
void SysTickIntHandler(void)
{
// Increment the system tick count.
g_ulTickCounter++;
// Indicate that a SysTick interrupt has occurred.
HWREGBITW(&g_ulFlags, FLAG_SYSTICK) = 1;
}
clock_time_t clock_time(void)
{
return((clock_time_t)g_ulTickCounter);
}
void GPIOPortEIntHandler(void) {
uint8_t p = MAP_GPIOPinIntStatus(GPIO_PORTE_BASE, true) & 0xFF;
MAP_GPIOPinIntClear(GPIO_PORTE_BASE, p);
HWREGBITW(&g_ulFlags, FLAG_ENC_INT) = 1;
}
static void enc28j60_comm_init(void) {
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
MAP_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, ENC_INT);
MAP_GPIOPinWrite(ENC_CS_PORT, ENC_CS, ENC_CS);
}
static void cpu_init(void) {
// A safety loop in order to interrupt the MCU before setting the clock (wrongly)
int i;
for(i=0; i<1000000; i++);
// Setup for 16MHZ external crystal, use 200MHz PLL and divide by 3 = 66MHz
//SYSCTL_SYSDIV_2_5 for 80mhz
MAP_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
}
void UART3_HANDLER(){
ROM_UARTIntClear(UART3_BASE,ROM_UARTIntStatus(UART3_BASE, true));
}
void UART4_HANDLER(){
unsigned long intStatus= ROM_UARTIntStatus(UART4_BASE, true);
ROM_UARTIntClear(UART4_BASE,intStatus);
if(intStatus & UART_INT_RX | ROM_UARTCharsAvail(UART4_BASE)){
//receive here
while(ROM_UARTCharsAvail(UART4_BASE))
{
ROM_UARTCharPutNonBlocking(UART4_BASE,
ROM_UARTCharGetNonBlocking(UART4_BASE));
}
}else if (intStatus & UART_INT_TX){
//TX more
}
}
static void uart_init(void) {
// Configure PD0 and PD1 for UART0
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioInitExpClk(0, 115200);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
// Configure PC6 and PC7 for UART3
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3);
MAP_GPIOPinConfigure(GPIO_PC6_U3RX);
MAP_GPIOPinConfigure(GPIO_PC7_U3TX);
MAP_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7);
ROM_UARTConfigSetExpClk(UART3_BASE, ROM_SysCtlClockGet(), 9600,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
ROM_UARTFIFOEnable(UART3_BASE);
ROM_UARTFIFOLevelSet(UART3_BASE,UART_FIFO_TX7_8,UART_FIFO_RX7_8);
UARTIntRegister(UART3_BASE,&UART3_HANDLER);
ROM_IntEnable(INT_UART3);
ROM_UARTIntEnable(UART3_BASE, UART_INT_RX | UART_INT_RT);
ROM_UARTEnable(UART3_BASE);
// Configure PC4 and PC5 for UART4
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4);
MAP_GPIOPinConfigure(GPIO_PC4_U4RX);
MAP_GPIOPinConfigure(GPIO_PC5_U4TX);
MAP_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
ROM_UARTConfigSetExpClk(UART4_BASE, ROM_SysCtlClockGet(), 9600,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
ROM_UARTFIFOEnable(UART4_BASE);
ROM_UARTFIFOLevelSet(UART4_BASE,UART_FIFO_TX7_8,UART_FIFO_RX7_8);
UARTIntRegister(UART4_BASE,&UART4_HANDLER);
ROM_IntEnable(INT_UART4);
ROM_UARTIntEnable(UART4_BASE, UART_INT_RX | UART_INT_RT);
ROM_UARTEnable(UART4_BASE);
}
static void spi_init(void) {
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// Configure SSI0 for SPI RAM usage
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
MAP_GPIOPinConfigure(GPIO_PA2_SSI0CLK);
MAP_GPIOPinConfigure(GPIO_PA4_SSI0RX);
MAP_GPIOPinConfigure(GPIO_PA5_SSI0TX);
MAP_GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_4 | GPIO_PIN_5);
MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_3);
MAP_SSIConfigSetExpClk(SSI0_BASE, MAP_SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
SSI_MODE_MASTER, 20000000, 8);
MAP_SSIEnable(SSI0_BASE);
unsigned long b;
while(MAP_SSIDataGetNonBlocking(SSI0_BASE, &b)) {}
}
void spi_mem_write(uint16_t addr, const uint8_t *buf, uint16_t count) {
SELECT_MEM();
spi_send(0x02);
spi_send(addr >> 8);
spi_send(addr & 0xFF);
for(;count>0;count--) {
spi_send(*buf);
buf++;
}
DESELECT_MEM();
}
void spi_mem_read(uint16_t addr, uint8_t *buf, uint16_t count) {
SELECT_MEM();
spi_send(0x03);
spi_send(addr >> 8);
spi_send(addr & 0xFF);
for(;count>0;count--) {
*buf = spi_send(0xFF);
buf++;
}
DESELECT_MEM();
}
void uip_log(char *msg) {
printf("UIP: %s\n", msg);
}
void startup()
{
static struct uip_eth_addr eth_addr;
uip_ipaddr_t ipaddr;
cpu_init();
uart_init();
printf("Welcome\n");
spi_init();
enc28j60_comm_init();
//printf("Welcome\n");
enc_init(mac_addr);
//
// Configure SysTick for a periodic interrupt.
//
MAP_SysTickPeriodSet(MAP_SysCtlClockGet() / SYSTICKHZ);
MAP_SysTickEnable();
MAP_SysTickIntEnable();
/************get new interrupt********/
//MAP_IntEnable(INT_GPIOA);
MAP_IntEnable(INT_GPIOE);
MAP_IntMasterEnable();
MAP_SysCtlPeripheralClockGating(false);
printf("int enabled\n");
/*************get new interrupt********/
MAP_GPIOIntTypeSet(GPIO_PORTE_BASE, ENC_INT, GPIO_FALLING_EDGE);
MAP_GPIOPinIntClear(GPIO_PORTE_BASE, ENC_INT);
MAP_GPIOPinIntEnable(GPIO_PORTE_BASE, ENC_INT);
uip_init();
eth_addr.addr[0] = mac_addr[0];
eth_addr.addr[1] = mac_addr[1];
eth_addr.addr[2] = mac_addr[2];
eth_addr.addr[3] = mac_addr[3];
eth_addr.addr[4] = mac_addr[4];
eth_addr.addr[5] = mac_addr[5];
uip_setethaddr(eth_addr);
#define DEFAULT_IPADDR0 10
#define DEFAULT_IPADDR1 0
#define DEFAULT_IPADDR2 0
#define DEFAULT_IPADDR3 201
#define DEFAULT_NETMASK0 255
#define DEFAULT_NETMASK1 255
#define DEFAULT_NETMASK2 255
#define DEFAULT_NETMASK3 0
#undef STATIC_IP
#ifdef STATIC_IP
uip_ipaddr(ipaddr, DEFAULT_IPADDR0, DEFAULT_IPADDR1, DEFAULT_IPADDR2,
DEFAULT_IPADDR3);
uip_sethostaddr(ipaddr);
printf("IP: %d.%d.%d.%d\n", DEFAULT_IPADDR0, DEFAULT_IPADDR1,
DEFAULT_IPADDR2, DEFAULT_IPADDR3);
uip_ipaddr(ipaddr, DEFAULT_NETMASK0, DEFAULT_NETMASK1, DEFAULT_NETMASK2,
DEFAULT_NETMASK3);
uip_setnetmask(ipaddr);
#else
uip_ipaddr(ipaddr, 0, 0, 0, 0);
uip_sethostaddr(ipaddr);
printf("Waiting for IP address...\n");
uip_ipaddr(ipaddr, 0, 0, 0, 0);
uip_setnetmask(ipaddr);
#endif
//httpd_init();
mobdusd_init();
#ifndef STATIC_IP
dhcpc_init(mac_addr, 6);
dhcpc_request();
#endif
lPeriodicTimer = lARPTimer = 0;
}