Rev 541 | Rev 669 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 541 | Rev 661 | ||
|---|---|---|---|
| Line 13... | Line 13... | ||
| 13 | /* system files */ |
13 | /* system files */ |
| 14 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 15 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 17 | #include <stdio.h> |
17 | #include <stdio.h> |
| - | 18 | #include <string.h> |
|
| - | 19 | ||
| 18 | /* lib files */ |
20 | /* lib files */ |
| 19 | #include < |
21 | #include <mcp2515.h> |
| 20 | //#include <serial.h> |
22 | //#include <serial.h> |
| 21 | #include <timebase.h> |
23 | #include <timebase.h> |
| - | 24 | #include <mcu.h> |
|
| 22 | #include <ip_arp_udp_tcp.h> |
25 | #include <ip_arp_udp_tcp.h> |
| 23 | #include <enc28j60.h> |
26 | #include <enc28j60.h> |
| 24 | #include <timeout.h> |
27 | #include <timeout.h> |
| 25 | #include <avr_compat.h> |
28 | #include <avr_compat.h> |
| 26 | #include <net.h> |
29 | #include <net.h> |
| 27 | 30 | ||
| 28 | // please modify the following two lines. mac and ip have to be unique |
31 | // please modify the following two lines. mac and ip have to be unique |
| 29 | // in your local area network. You can not have the same numbers in |
32 | // in your local area network. You can not have the same numbers in |
| 30 | // two devices: |
33 | // two devices: |
| 31 | static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00, |
34 | static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x27}; |
| 32 | //static uint8_t myip[4] = {192,168,0,50}; |
35 | //static uint8_t myip[4] = {192,168,0,50}; |
| 33 | static uint8_t myip[4] = {193,11,254, |
36 | static uint8_t myip[4] = {193,11,254,26}; |
| 34 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
37 | static uint16_t rmDumperPrt =1200; // remote port for dumping data |
| 35 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
38 | static uint16_t rmCan2SerPrt =1100; // remote port for Can2Serial-data |
| 36 | //static uint16_t locDumperPrt =1200; // listen port |
39 | //static uint16_t locDumperPrt =1200; // listen port |
| 37 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
40 | static uint16_t locCan2SerPrt =1100; // listen port for Can2Serial-data |
| 38 | static uint8_t gotdumpserver = 0; |
41 | static uint8_t gotdumpserver = 0; |
| 39 | static uint8_t gotcan2serserver = 0; |
42 | static uint8_t gotcan2serserver = 0; |
| 40 | 43 | ||
| 41 | // how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX |
44 | // how did I get the mac addr? Translate the first 3 numbers into ascii is: TUX |
| 42 | 45 | ||
| 43 | static uint8_t remotemac[6]; |
46 | static uint8_t remotemac[6]; |
| 44 | static uint8_t remoteip[4]; |
47 | static uint8_t remoteip[4]; |
| 45 | 48 | ||
| Line 55... | Line 58... | ||
| 55 | 58 | ||
| 56 | #define C2S_START_BYTE 253 |
59 | #define C2S_START_BYTE 253 |
| 57 | #define C2S_END_BYTE 250 |
60 | #define C2S_END_BYTE 250 |
| 58 | 61 | ||
| 59 | /* set to 0 for not sending timestamps on bus */ |
62 | /* set to 0 for not sending timestamps on bus */ |
| 60 | #define SENDTIMESTAMP |
63 | #define SENDTIMESTAMP 0 |
| - | 64 | ||
| - | 65 | volatile Can_Message_t rxMsg; // Message storage |
|
| - | 66 | volatile uint8_t rxMsgFull; // Synchronization flag |
|
| 61 | 67 | ||
| 62 | /*---------------------------------------------------------------------------- |
68 | /*---------------------------------------------------------------------------- |
| 63 | * Putchar for udp |
69 | * Putchar for udp |
| 64 | * Implements a small buffer, sends packet over udp if buffer is full or |
70 | * Implements a small buffer, sends packet over udp if buffer is full or |
| 65 | * \n-char is sent. |
71 | * \n-char is sent. |
| 66 | * Needs a timeout to force send. |
72 | * Needs a timeout to force send. |
| 67 | *--------------------------------------------------------------------------*/ |
73 | *--------------------------------------------------------------------------*/ |
| 68 |
|
74 | /*int udp_putchar(char c, FILE* stream) { |
| 69 | if (gotdumpserver != 0) { |
75 | if (gotdumpserver != 0) { |
| 70 | sendbuf[buffpoint] = c; |
76 | sendbuf[buffpoint] = c; |
| 71 | buffpoint++; |
77 | buffpoint++; |
| 72 | 78 | |
|
| 73 | if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) { |
79 | if (c == '\n' || buffpoint > SEND_BUFFER_SIZE) { |
| 74 | send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip); |
80 | send_udp(buf, sendbuf, buffpoint, rmDumperPrt, remotemac, remoteip); |
| 75 | buffpoint = 0; |
81 | buffpoint = 0; |
| 76 | } |
82 | } |
| 77 | } |
83 | } |
| 78 | return 0; |
84 | return 0; |
| 79 | } |
85 | } |
| 80 | 86 | ||
| 81 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
87 | static FILE mystdout = FDEV_SETUP_STREAM(udp_putchar, NULL, _FDEV_SETUP_WRITE); |
| - | 88 | */ |
|
| - | 89 | ||
| - | 90 | void Can_Process(Can_Message_t* msg) { |
|
| - | 91 | if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames. |
|
| - | 92 | ||
| - | 93 | if (!rxMsgFull) { |
|
| - | 94 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
|
| - | 95 | rxMsgFull = 1; |
|
| - | 96 | } |
|
| - | 97 | } |
|
| - | 98 | ||
| - | 99 | ISR(MCP_INT_VECTOR) { |
|
| - | 100 | // Get first available message from controller and pass it to |
|
| - | 101 | // application handler. If both RX buffers contain messages |
|
| - | 102 | // we will get another interrupt as soon as this one returns. |
|
| - | 103 | if (Can_Receive(&rxMsg) == CAN_OK) { |
|
| - | 104 | // Callbacks are run with global interrupts disabled but |
|
| - | 105 | // with controller flag cleared so another msg can be |
|
| - | 106 | // received while this one is processed. |
|
| - | 107 | Can_Process(&rxMsg); |
|
| - | 108 | } |
|
| - | 109 | } |
|
| - | 110 | ||
| 82 | 111 | ||
| 83 | /*----------------------------------------------------------------------------- |
112 | /*----------------------------------------------------------------------------- |
| 84 | * Main Program |
113 | * Main Program |
| 85 | *---------------------------------------------------------------------------*/ |
114 | *---------------------------------------------------------------------------*/ |
| 86 | int main(void) { |
115 | int main(void) { |
| Line 90... | Line 119... | ||
| 90 | uint8_t payloadlen=0; |
119 | uint8_t payloadlen=0; |
| 91 | buffpoint = 0; |
120 | buffpoint = 0; |
| 92 | 121 | ||
| 93 | //can vars |
122 | //can vars |
| 94 | Can_Message_t txMsg; |
123 | Can_Message_t txMsg; |
| 95 | Can_Message_t rxMsg; |
- | |
| 96 | txMsg.Id = 3; |
124 | txMsg.Id = 3; |
| 97 | txMsg.DataLength = 0; |
125 | txMsg.DataLength = 0; |
| 98 | txMsg.RemoteFlag = 0; |
126 | txMsg.RemoteFlag = 0; |
| 99 | txMsg.ExtendedFlag = 1; |
127 | txMsg.ExtendedFlag = 1; |
| 100 | 128 | ||
| - | 129 | rxMsgFull = 0; |
|
| - | 130 | ||
| 101 | uint32_t timeStamp = 0; |
131 | uint32_t timeStamp = 0; |
| - | 132 | ||
| - | 133 | //uint8_t sw=0; |
|
| - | 134 | // LED |
|
| - | 135 | /* enable PB1, LED as output */ |
|
| - | 136 | //DDRB|= (1<<DDB0); |
|
| - | 137 | ||
| - | 138 | /* set output to Vcc, LED off */ |
|
| - | 139 | //PORTB|= (1<<PB0); |
|
| 102 | 140 | ||
| 103 | Mcu_Init(); |
141 | Mcu_Init(); |
| 104 | Timebase_Init(); |
142 | Timebase_Init(); |
| 105 | 143 | ||
| 106 | //alla printf ska skriva till udp |
144 | //alla printf ska skriva till udp |
| 107 | stdout |
145 | //stdout = &mystdout; //set the output stream |
| 108 | - | ||
| 109 | sei(); |
- | |
| 110 | 146 | ||
| 111 | if (Can_Init() != CAN_OK) { |
147 | if (Can_Init() != CAN_OK) { |
| 112 | } |
148 | } |
| 113 | 149 | ||
| 114 | /* set output to gnd, reset the ethernet chip */ |
- | |
| 115 | PORTC &= ~(1<<PC1); |
- | |
| 116 | /* set PC1 as output */ |
- | |
| 117 | DDRC |= (1<<DDC1); |
- | |
| 118 | - | ||
| 119 |
|
150 | sei(); |
| 120 | /* set PC1 as input, resetpin pulled high */ |
- | |
| 121 | DDRC &= ~(1<<DDC1); |
- | |
| 122 | - | ||
| 123 | delay_ms(100); |
- | |
| 124 | 151 | ||
| 125 | /*initialize enc28j60*/ |
152 | /*initialize enc28j60*/ |
| 126 | enc28j60Init(mymac); |
153 | enc28j60Init(mymac); |
| 127 | delay_ms(20); |
154 | delay_ms(20); |
| 128 | 155 | ||
| 129 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
156 | /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ |
| 130 | // LEDB=yellow LEDA=green |
157 | // LEDB=yellow LEDA=green |
| 131 | // |
158 | // |
| 132 | // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit |
159 | // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit |
| 133 | enc28j60PhyWrite(PHLCON,0x476); |
160 | enc28j60PhyWrite(PHLCON,0x476); |
| Line 137... | Line 164... | ||
| 137 | init_ip_arp_udp_tcp(mymac,myip,80); |
164 | init_ip_arp_udp_tcp(mymac,myip,80); |
| 138 | 165 | ||
| 139 | /* main loop */ |
166 | /* main loop */ |
| 140 | while (1) { |
167 | while (1) { |
| 141 | /* service the CAN routines */ |
168 | /* service the CAN routines */ |
| 142 | Can_Service |
169 | //Can_Service(); |
| 143 | 170 | ||
| 144 | #if SENDTIMESTAMP == 1 |
171 | #if SENDTIMESTAMP == 1 |
| 145 | /* send CAN message and check for CAN errors once every second */ |
172 | /* send CAN message and check for CAN errors once every second */ |
| 146 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
173 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 147 | timeStamp = Timebase_CurrentTime(); |
174 | timeStamp = Timebase_CurrentTime(); |
| Line 152... | Line 179... | ||
| 152 | 179 | ||
| 153 | } |
180 | } |
| 154 | #endif |
181 | #endif |
| 155 | 182 | ||
| 156 | /* check if any messages have been received */ |
183 | /* check if any messages have been received */ |
| 157 |
|
184 | if (rxMsgFull) { //(Can_Receive(&rxMsg) == CAN_OK) { |
| 158 | uint8_t i; |
185 | uint8_t i; |
| 159 | 186 | ||
| 160 | /*if (gotdumpserver != 0) { |
187 | /*if (gotdumpserver != 0) { |
| 161 | printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
188 | printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
| 162 | printf("data={ "); |
189 | printf("data={ "); |
| 163 | for (i=0; i<rxMsg.DataLength; i++) { |
190 | for (i=0; i<rxMsg.DataLength; i++) { |
| 164 | printf("%x ", rxMsg.Data.bytes[i]); |
191 | printf("%x ", rxMsg.Data.bytes[i]); |
| 165 | } |
192 | } |
| 166 | printf("}\n"); |
193 | printf("}\n"); |
| 167 | }*/ |
194 | }*/ |
| 168 | 195 | ||
| 169 | if (gotcan2serserver != 0) { |
196 | if (gotcan2serserver != 0) { |
| 170 | //skicka ocks� som udppaket till rmCan2SerPrt |
197 | //skicka ocks� som udppaket till rmCan2SerPrt |
| 171 | char sendbuf2[17]; |
198 | char sendbuf2[17]; |
| 172 | 199 | ||
| 173 | sendbuf2[0] = C2S_START_BYTE; |
200 | sendbuf2[0] = C2S_START_BYTE; |
| Line 178... | Line 205... | ||
| 178 | sendbuf2[5] = rxMsg.ExtendedFlag; |
205 | sendbuf2[5] = rxMsg.ExtendedFlag; |
| 179 | sendbuf2[6] = rxMsg.RemoteFlag; |
206 | sendbuf2[6] = rxMsg.RemoteFlag; |
| 180 | sendbuf2[7] = rxMsg.DataLength; |
207 | sendbuf2[7] = rxMsg.DataLength; |
| 181 | for (i=0; i<8; i++) { |
208 | for (i=0; i<8; i++) { |
| 182 | sendbuf2[8+i] = rxMsg.Data.bytes[i]; |
209 | sendbuf2[8+i] = rxMsg.Data.bytes[i]; |
| 183 | } |
210 | } |
| 184 | sendbuf2[16] = C2S_END_BYTE; |
211 | sendbuf2[16] = C2S_END_BYTE; |
| 185 | 212 | ||
| - | 213 | MCP_INT_DISABLE(); |
|
| 186 | send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip); |
214 | send_udp(buf, sendbuf2, 18, rmCan2SerPrt, prgremotemac, prgremoteip); |
| - | 215 | MCP_INT_ENABLE(); |
|
| 187 | } |
216 | } |
| - | 217 | ||
| - | 218 | rxMsgFull = 0; |
|
| 188 | } |
219 | } |
| 189 | 220 | ||
| 190 | //test-debug-kod |
221 | //test-debug-kod |
| 191 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
222 | if ((enc28j60Read(EIR) & EIR_TXERIF)) { |
| 192 | //printf("EIR_TXERIF set!\n"); |
223 | //printf("EIR_TXERIF set!\n"); |