int main (void) { #ifdef BOOTLOADER_SUPPORT _IVREG = _BV (IVCE); /* prepare ivec change */ _IVREG = _BV (IVSEL); /* change ivec to bootloader */ #endif /* Default DDR Config */ #if IO_HARD_PORTS >= 4 && DDR_MASK_A != 0 DDRA = DDR_MASK_A; #endif #if DDR_MASK_B != 0 DDRB = DDR_MASK_B; #endif #if DDR_MASK_C != 0 DDRC = DDR_MASK_C; #endif #if DDR_MASK_D != 0 DDRD = DDR_MASK_D; #endif #if IO_HARD_PORTS >= 6 #if DDR_MASK_E != 0 DDRE = DDR_MASK_E; #endif #if DDR_MASK_F != 0 DDRF = DDR_MASK_F; #endif #endif #if IO_HARD_PORTS >= 7 #if DDR_MASK_G != 0 DDRG = DDR_MASK_G; #endif #endif #ifdef STATUSLED_POWER_SUPPORT PIN_SET(STATUSLED_POWER); #endif //FIXME: zum ethersex meta system hinzufügen, aber vor allem anderem initalisieren debug_init(); debug_printf("%S (Debug mode)\n", pstr_E6_VERSION_STRING_LONG); #ifdef DEBUG_RESET_REASON if (bit_is_set (mcusr_mirror, BORF)) debug_printf("reset: Brown-out\n"); else if (bit_is_set (mcusr_mirror, PORF)) debug_printf("reset: Power on\n"); else if (bit_is_set (mcusr_mirror, WDRF)) debug_printf("reset: Watchdog\n"); else if (bit_is_set (mcusr_mirror, EXTRF)) debug_printf("reset: Extern\n"); else debug_printf("reset: Unknown\n"); #endif #ifdef BOOTLOADER_SUPPORT /* disable interrupts */ cli (); wdt_disable(); #endif //BOOTLOADER_SUPPORT /* enable interrupts */ sei (); #ifdef USE_WATCHDOG debug_printf("enabling watchdog\n"); #ifdef DEBUG /* for debugging, test reset cause and jump to bootloader */ if (MCU_STATUS_REGISTER & _BV (WDRF)) { debug_printf("bootloader...\n"); jump_to_bootloader(); } #endif /* set watchdog to 2 seconds */ wdt_enable(WDTO_2S); wdt_kick(); #else //USE_WATCHDOG debug_printf("disabling watchdog\n"); wdt_disable(); #endif //USE_WATCHDOG #ifdef SPI_SUPPORT spi_init(); #endif ethersex_meta_init(); /* must be called AFTER all other initialization */ #ifdef PORTIO_SUPPORT portio_init(); #elif defined(NAMED_PIN_SUPPORT) np_simple_init(); #endif #ifdef ENC28J60_SUPPORT debug_printf ("enc28j60 revision 0x%x\n", read_control_register (REG_EREVID)); debug_printf ("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", uip_ethaddr.addr[0], uip_ethaddr.addr[1], uip_ethaddr.addr[2], uip_ethaddr.addr[3], uip_ethaddr.addr[4], uip_ethaddr.addr[5]); #endif #ifdef STATUSLED_BOOTED_SUPPORT PIN_SET(STATUSLED_BOOTED); #endif ethersex_meta_startup(); /* main loop */ while (1) { wdt_kick(); ethersex_meta_mainloop(); #ifdef SD_READER_SUPPORT if (sd_active_partition == NULL) { if (!sd_try_init()) { #ifdef VFS_SD_SUPPORT vfs_sd_try_open_rootnode(); #endif } wdt_kick(); } #endif #ifdef BOOTLOADER_JUMP if (status.request_bootloader) { #ifdef MBR_SUPPORT mbr_config.bootloader = 1; write_mbr(); #endif #ifdef CLOCK_CRYSTAL_SUPPORT TIMER_8_AS_1_INT_OVERFLOW_OFF; #endif #ifdef DCF77_SUPPORT ACSR &= ~_BV (ACIE); #endif cli(); jump_to_bootloader(); } #endif #ifndef TEENSY_SUPPORT if (status.request_wdreset) { cli(); wdt_enable(WDTO_15MS); for (;;); } #endif if (status.request_reset) { cli(); void (*reset) (void) = NULL; reset(); } } }
int main(void) { #ifdef BOOTLOADER_SUPPORT _IVREG = _BV(IVCE); /* prepare ivec change */ _IVREG = _BV(IVSEL); /* change ivec to bootloader */ #endif /* Clear the MCUSR Register to avoid endless wdreset loops */ unsigned char reset_reason = 0; #ifdef MCUCSR reset_reason = MCUCSR; MCUCSR = 0; #else #ifdef MCUSR reset_reason = MCUSR; MCUSR = 0; #endif #endif /* Default DDR Config */ #if IO_HARD_PORTS == 4 && DDR_MASK_A != 0 DDRA = DDR_MASK_A; #endif #if DDR_MASK_B != 0 DDRB = DDR_MASK_B; #endif #if DDR_MASK_C != 0 DDRC = DDR_MASK_C; #endif #if DDR_MASK_D != 0 DDRD = DDR_MASK_D; #endif #if IO_HARD_PORTS == 6 #if DDR_MASK_E != 0 DDRE = DDR_MASK_E; #endif #if DDR_MASK_F != 0 DDRF = DDR_MASK_F; #endif #endif #ifdef STATUSLED_POWER_SUPPORT PIN_SET(STATUSLED_POWER); #endif //FIXME: zum ethersex meta system hinzufügen, aber vor allem anderem initalisieren debug_init(); debug_printf("Ethersex " VERSION_STRING " (Debug mode)\n"); #ifdef DEBUG_RESET_REASON if (bit_is_set(reset_reason, BORF)) debug_printf("reset: Brown-out\n"); else if (bit_is_set(reset_reason, PORF)) debug_printf("reset: Power on\n"); else if (bit_is_set(reset_reason, WDRF)) debug_printf("reset: Watchdog\n"); else if (bit_is_set(reset_reason, EXTRF)) debug_printf("reset: Extern\n"); else debug_printf("reset: Unknown\n"); #endif #ifdef BOOTLOADER_SUPPORT /* disable interrupts */ cli(); #else /* enable interrupts */ sei(); #endif //BOOTLOADER_SUPPORT #ifdef USE_WATCHDOG debug_printf("enabling watchdog\n"); #ifdef DEBUG /* for debugging, test reset cause and jump to bootloader */ if (MCUSR & _BV(WDRF)) { debug_printf("bootloader...\n"); jump_to_bootloader(); } #endif /* set watchdog to 2 seconds */ wdt_enable(WDTO_2S); wdt_kick(); #else //USE_WATCHDOG debug_printf("disabling watchdog\n"); wdt_disable(); #endif //USE_WATCHDOG #ifdef ADC_SUPPORT /* ADC Prescaler to 64 */ ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1); /* ADC set Voltage Reference to extern*/ /* FIXME: move config to the right place */ ADMUX = ADC_REF; //_BV(REFS0) | _BV(REFS1); #endif #if defined(RFM12_SUPPORT) || defined(ENC28J60_SUPPORT) \ || defined(DATAFLASH_SUPPORT) spi_init(); #endif ethersex_meta_init(); #ifdef RFM12_SUPPORT rfm12_init(); #ifdef TEENSY_SUPPORT cli (); rfm12_trans (0xa620); /* rfm12_setfreq(RFM12FREQ(433.92)); */ rfm12_trans (0x94ac); /* rfm12_setbandwidth(5, 1, 4); */ #ifdef RFM12_IP_SUPPORT rfm12_trans (0xc610); /* rfm12_setbaud(192); */ rfm12_trans (0x9820); /* rfm12_setpower(0, 2); */ rfm12_rxstart(); #endif /* RFM12_IP_SUPPORT */ sei (); #else /* TEENSY_SUPPORT */ rfm12_setfreq(RFM12FREQ(433.92)); rfm12_setbandwidth(5, 1, 4); #ifdef RFM12_IP_SUPPORT rfm12_setbaud(CONF_RFM12_BAUD / 100); rfm12_setpower(0, 2); rfm12_rxstart(); #endif /* RFM12_IP_SUPPORT */ #endif /* not TEENSY_SUPPORT */ #endif /* RFM12_SUPPORT */ /* must be called AFTER all other initialization */ #ifdef PORTIO_SUPPORT portio_init(); #elif defined(NAMED_PIN_SUPPORT) np_simple_init(); #endif #ifdef ENC28J60_SUPPORT debug_printf("enc28j60 revision 0x%x\n", read_control_register(REG_EREVID)); debug_printf("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", uip_ethaddr.addr[0], uip_ethaddr.addr[1], uip_ethaddr.addr[2], uip_ethaddr.addr[3], uip_ethaddr.addr[4], uip_ethaddr.addr[5] ); #endif #ifdef STATUSLED_BOOTED_SUPPORT PIN_SET(STATUSLED_BOOTED); #endif ethersex_meta_startup(); /* main loop */ while(1) { wdt_kick(); ethersex_meta_mainloop(); #ifdef SD_READER_SUPPORT if (sd_active_partition == NULL) { if (! sd_try_init ()) vfs_sd_try_open_rootnode (); wdt_kick(); } #endif #ifndef BOOTLOAD_SUPPORT if(status.request_bootloader) { #ifdef CLOCK_CRYSTAL_SUPPORT _TIMSK_TIMER2 &= ~_BV(TOIE2); #endif #ifdef DCF77_SUPPORT ACSR &= ~_BV(ACIE); #endif cli(); jump_to_bootloader(); } #ifndef TEENSY_SUPPORT if(status.request_wdreset) { cli(); wdt_enable(WDTO_15MS); for(;;); } #endif if(status.request_reset) { cli(); void (* reset)(void) = NULL; reset(); } #endif } }
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; } } }