int main(int argc, char *argv[]) { /* Disable driver enable for RS485 ASAP */ //DDRC |= (1 << PC2); //PORTC &= ~(1 << PC2); /* init serial line debugging */ UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; UCSR0B = (1 << RXCIE1) | (1 << RXEN1) | (1 << TXEN0); UCSR0C = (1<<UCSZ00) | (1<<UCSZ01); /* Initialize UART */ net_init(); DBG("READY!\r\n"); DBG("Initializing SPI...\r\n"); spi_init(); DBG("Initializing ENC28J60...\r\n"); init_enc28j60(); DBG("Initialized ENC28J60\r\n"); char obuf[64]; snprintf(obuf, sizeof(obuf), "enc28j60 rev 0x%x\n", read_control_register(REG_EREVID)); DBG(obuf); char buf[16] = "serial: X\n"; int cnt = 0; while (1) { if (eth_to_rs_cnt > 0 && eth_to_rs[eth_to_rs_cnt-1] == '$') { eth_to_rs[eth_to_rs_cnt-1] = '\0'; int dest = 0; int pktlen = 0; char minibuf[16]; minibuf[0] = eth_to_rs[1]; minibuf[1] = eth_to_rs[2]; minibuf[2] = '\0'; if (sscanf(minibuf, "%d", &dest) != 1) { DBG("Could not parse dest\r\n"); eth_to_rs_cnt = 0; continue; } minibuf[0] = eth_to_rs[3]; minibuf[1] = eth_to_rs[4]; if (sscanf(minibuf, "%d", &pktlen) != 1) { DBG("Could not parse len\r\n"); eth_to_rs_cnt = 0; continue; } if (pktlen != (eth_to_rs_cnt - 6)) { DBG("lens are not the same\r\n"); minibuf[2] = '\r'; minibuf[3] = '\n'; minibuf[4] = '\0'; DBG(minibuf); snprintf(minibuf, sizeof(minibuf), "e: %d\r\n", eth_to_rs_cnt-6); DBG(minibuf); snprintf(minibuf, sizeof(minibuf), "p: %d\r\n", pktlen); DBG(minibuf); eth_to_rs_cnt = 0; continue; } fmt_packet(lbuffer, dest, 0xFF, eth_to_rs + 5, pktlen); struct buspkt *packet = (struct buspkt*)lbuffer; send_packet(packet); syslog_send("sent packet", strlen("sent packet")); _delay_ms(25); sendit = 0; eth_to_rs_cnt = 0; } #if 0 network_process(); if (uip_recvlen > 0) { syslog_send("handling ethernet packet", strlen("handling ethernet packet")); DBG("Handling packet\r\n"); handle_icmpv6(); if (uip_recvbuf[20] == 0x11) { syslog_send("handling udp packet", strlen("handling udp packet")); /* UDP */ uint8_t *udp = uip_recvbuf + 14 + 40; uint8_t len = udp[5] - 8; /* TODO: sanity check */ uint8_t *recvpayload = udp + 8 /* udp */; fmt_packet(lbuffer, uip_recvbuf[53], 0xFF, recvpayload, len); struct buspkt *packet = (struct buspkt*)lbuffer; //syslog_send("sending packet", strlen("sending packet")); send_packet(packet); _delay_ms(25); cnt = 85; syslog_send("ethernet to rs485 done", strlen("ethernet to rs485 done")); } //syslog_send("received a packet", strlen("received a packet")); buf[14] = uip_recvlen; //syslog_send(uip_recvbuf, uip_recvlen); uip_recvlen = 0; } #endif _delay_ms(10); if (cnt++ == 100) { fmt_packet(lbuffer, 1, 0, "ping", 4); struct buspkt *packet = (struct buspkt*)lbuffer; syslog_send("ping sent", strlen("ping sent")); send_packet(packet); cnt = 0; snprintf(obuf, sizeof(obuf), "cnt = %d, rem = %d\r\n", eth_to_rs_cnt, eth_to_rs_rem); syslog_send(obuf, strlen(obuf)); } uint8_t status = bus_status(); if (status == BUS_STATUS_IDLE) continue; if (status == BUS_STATUS_MESSAGE) { /* get a copy of the current packet */ struct buspkt *packet = current_packet(); uint8_t *walk = packet; uint8_t *payload = (uint8_t*)packet; payload += sizeof(struct buspkt); /* check for ping replies */ if (packet->destination == 0x00 && memcmp(payload, "pong", strlen("pong")) == 0) { syslog_send("pong received", strlen("pong received")); /* TODO: store that this controller is reachable */ /* check if the controller has any waiting messages */ if (payload[4] > 0) { /* request the message */ fmt_packet(lbuffer, packet->source, 0, "send", 4); struct buspkt *reply = (struct buspkt*)lbuffer; //syslog_send("sending packet", strlen("sending packet")); _delay_ms(25); send_packet(reply); syslog_send("sendreq sent", strlen("sendreq sent")); //syslog_send(reply, reply->length_lo + sizeof(struct buspkt)); _delay_ms(25); cnt = 0; } } #if 0 /* copy packet->destination into the MAC and IPv6 address */ uip_buf[5] = packet->destination; /* MAC */ uip_buf[53] = packet->destination; /* IPv6 */ /* copy packet->source into the MAC and IPv6 address */ uip_buf[11] = packet->source; /* MAC */ uip_buf[37] = packet->source; /* IPv6 */ raw_send(payload, packet->length_lo); #endif char minibuf[16]; snprintf(minibuf, sizeof(minibuf), "^%02d%02d", 0, packet->length_lo); uart_puts(minibuf); uint8_t c; for (c = 0; c < sizeof(struct buspkt); c++) { while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = walk[c]; } for (c = 0; c < packet->length_lo; c++) { while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = payload[c]; } while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = '$'; /* discard the packet from serial buffer */ packet_done(); continue; } if (status == BUS_STATUS_WRONG_CRC) { syslog_send("broken", strlen("broken")); struct buspkt *packet = current_packet(); raw_send(packet, 16); skip_byte(); continue; } } }
void network_init(void) { uip_ipaddr_t ip; (void) ip; /* Keep GCC quiet. */ uip_init(); #if defined(RFM12_IP_SUPPORT) && defined(UIP_MULTI_STACK) uip_stack_set_active(STACK_RFM12); rfm12_stack_init(); #endif #if defined(ZBUS_SUPPORT) && defined(UIP_MULTI_STACK) uip_stack_set_active(STACK_ZBUS); zbus_stack_init(); #endif #ifdef OPENVPN_SUPPORT uip_stack_set_active(STACK_OPENVPN); openvpn_init(); #endif /* load base network settings */ # ifdef DEBUG_NET_CONFIG debug_printf("net: loading base network settings\n"); # endif #ifdef ETHERNET_SUPPORT # ifdef ENC28J60_SUPPORT uip_stack_set_active(STACK_ENC); # else /* TAP_SUPPORT */ uip_stack_set_active(STACK_TAP); #endif /* use uip buffer as generic space here, since when this function is called, * no network packets will be processed */ #ifdef EEPROM_SUPPORT /* use global network packet buffer for configuration */ uint8_t checksum = eeprom_get_chksum(); uint8_t saved_checksum; eeprom_restore_char(crc, &saved_checksum); if (checksum != saved_checksum) eeprom_init(); #endif #ifdef ETHERNET_SUPPORT network_config_load(); #endif /* Do the autoconfiguration after the MAC is set */ # if UIP_CONF_IPV6 && !defined(IPV6_STATIC_SUPPORT) uip_setprefixlen(64); uip_ip6autoconfig(0xFE80, 0x0000, 0x0000, 0x0000); # endif # if defined(IPV6_STATIC_SUPPORT) && defined(TFTPOMATIC_SUPPORT) const char *filename = CONF_TFTP_IMAGE; set_CONF_TFTP_IP(&ip); tftp_fire_tftpomatic(&ip, filename); bootload_delay = CONF_BOOTLOAD_DELAY; # endif /* IPV6_STATIC_SUPPORT && TFTPOMATIC_SUPPORT */ # elif !defined(ROUTER_SUPPORT) /* and not ETHERNET_SUPPORT */ /* Don't allow for eeprom-based configuration of rfm12/zbus IP address, mainly for code size reasons. */ set_CONF_ETHERRAPE_IP(&ip); uip_sethostaddr(&ip); # endif /* not ETHERNET_SUPPORT and not ROUTER_SUPPORT */ ethersex_meta_netinit(); # ifdef ENC28J60_SUPPORT init_enc28j60(); # endif # ifdef ETHERNET_SUPPORT # if UIP_CONF_IPV6 uip_neighbor_init(); # else uip_arp_init(); # endif # else /* ETHERNET_SUPPORT */ /* set at least fixed default gateway address * to allow multi stack routing */ set_CONF_ETHERRAPE_GATEWAY(&ip); uip_setdraddr(&ip); # endif /* ETHERNET_SUPPORT */ }