-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathec_enc28j60.XXX
102 lines (85 loc) · 2.25 KB
/
ec_enc28j60.XXX
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
#include <Arduino.h>
#include "etherShield.h"
#include "xgeneral.h"
#include "ethercattype.h"
#include "ecs_slave.h"
#include "ec_device.h"
#include "fsm_slave.h"
#include "ec_sii.h"
#include "ec_regs.h"
#include "ec_net.h"
#include "ec_cmd.h"
#include "ec_com.h"
// please modify the following two lines. mac and ip have to be unique
// in your local area network. You can not have the same numbers in
// two devices:
static uint8_t mymac[6] = { 0x54, 0x55, 0x58, 0x10, 0x00, 0x24 };
EtherShield es = EtherShield();
#define BUFFER_SIZE 228
unsigned char *buf = 0;
void __ethernetSetup()
{
/*initialize enc28j60 */
es.ES_enc28j60Init(mymac);
es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
delay(10);
/* Magjack leds configuration, see enc28j60 datasheet, page 11 */
// LEDA=green LEDB=yellow
//
// 0x880 is PHLCON LEDB=on, LEDA=on
// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
es.ES_enc28j60PhyWrite(PHLCON, 0x880);
delay(500);
//
// 0x990 is PHLCON LEDB=off, LEDA=off
// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
es.ES_enc28j60PhyWrite(PHLCON, 0x990);
delay(500);
//
// 0x880 is PHLCON LEDB=on, LEDA=on
// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
es.ES_enc28j60PhyWrite(PHLCON, 0x880);
delay(500);
//
// 0x990 is PHLCON LEDB=off, LEDA=off
// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
es.ES_enc28j60PhyWrite(PHLCON, 0x990);
delay(500);
//
// 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
// enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
es.ES_enc28j60PhyWrite(PHLCON, 0x476);
delay(100);
buf = (unsigned char *)malloc(BUFFER_SIZE +1);
if (!buf){
Serial.println("Ethernet setup Error");
}
else {
Serial.println("Ethernet setup");
memset(buf, 0, BUFFER_SIZE);
}
}
extern "C" void ethernetSetup()
{
__ethernetSetup();
}
void ecat_rcv(ecat_slave *ecs)
{
uint16_t len;
memset(buf, 0 , BUFFER_SIZE);
len = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
if (len > 60)
Serial.println(len);
if (len <= 0) {
return;
}
if (eth_hdr(buf)->ether_type != htons(ETH_P_ECAT)){
// Serial.println(len);
return;
}
ec_process_datagrams(ecs, len, buf);
}
extern "C" void ec_tx_pkt(uint8_t *buf, int sz, struct ec_device *txdev)
{
es.ES_WriteRawPacket(buf, sz);
}