/* * main.c * * Created on: 22 нояб. 2018 г. * Author: maxx */ #include #include #include #include #include //sbi, cbi etc.. #include "avr/wdt.h" // WatchDog #include // printf etc.. #include "uart_extd.h" #include "spi.h" #include "Ethernet/socket.h" #include "Ethernet/wizchip_conf.h" #include "Application/loopback/loopback.h" #include "Internet/DNS/dns.h" //#include // itoa etc.. /* * (5) Added DNS Lookup example on start-up device * (3) Trying WIZNET5500 init with using official Wiznet ioLibrary_Driver * working ping, assign static IP * LED1 = ON when phy_link detected * and loopback test on TCP-IP:5000 and UDP:3000 ports. * use Hercules terminal utility to check network connection see: * * https://wizwiki.net/wiki/doku.php?id=osh:cookie:loopback_test * https://www.hw-group.com/software/hercules-setup-utility * */ #define _MAIN_DEBUG_ #define PRINTF_EN 1 #if PRINTF_EN #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) #else #define PRINTF(...) #endif /* * m1284p minimum template, with one button & one led */ //M644P/M1284p Users LEDS: //LED1/PORTC.4- m644p/m1284p maxxir #define led1_conf() DDRC |= (1<getchar() там опрос блокируется пока символ не будет принят (поллинг) через UART1_RX, т.е. неудобно. */ // STDOUT UART0 TX handler static int uart0_putchar(char ch,FILE *stream) { uart_putc(ch); return 0; } // Очищаем буфер приема UART1 RX (иногда нужно) static void uart0_rx_flash(void) { // Считываем все из ring-buffer UART1 RX unsigned int c; do { c = uart_getc(); } while (( c & UART_NO_DATA ) == 0); // Check RX1 none-empty } //***************** UART0: END //***************** ADC: BEGIN #ifndef ADC_DIV //12.5MHz or over use this ADC reference clock #define ADC_DIV (1<> MCU is: %S; CLK is: %luHz\r\n", str_mcu, F_CPU);// MCU Name && FREQ PRINTF(">> Free RAM is: %d bytes\r\n", freeRam()); //Wizchip WIZ5500 Ethernet initialize IO_LIBRARY_Init(); //After that ping must working print_network_information(); #ifdef _MAIN_DEBUG_ printf("\r\n===== DNS Servers =====\r\n"); printf("> DNS 1st : %d.%d.%d.%d\r\n", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]); printf("> DNS 2nd : %d.%d.%d.%d\r\n", DNS_2nd[0], DNS_2nd[1], DNS_2nd[2], DNS_2nd[3]); printf("=======================================\r\n"); printf("> [Example] Target Domain Name : %s\r\n", Domain_name); #endif /* DNS client Initialization */ DNS_init(SOCK_DNS, gDATABUF_DNS); /* DNS processing */ int32_t ret; if ((ret = DNS_run(netInfo.dns, Domain_name, Domain_IP)) > 0) // try to 1st DNS { #ifdef _MAIN_DEBUG_ printf("> 1st DNS Respond\r\n"); #endif } else if ((ret != -1) && ((ret = DNS_run(DNS_2nd, Domain_name, Domain_IP))>0)) // retry to 2nd DNS { #ifdef _MAIN_DEBUG_ printf("> 2nd DNS Respond\r\n"); #endif } else if(ret == -1) { #ifdef _MAIN_DEBUG_ printf("> MAX_DOMAIN_NAME is too small. Should be redefined it.\r\n"); #endif ; } else { #ifdef _MAIN_DEBUG_ printf("> DNS Failed\r\n"); #endif ; } if(ret > 0) { #ifdef _MAIN_DEBUG_ printf("> Translated %s to [%d.%d.%d.%d]\r\n\r\n",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3]); #endif // // TODO: To be executed User's code after a successful DNS process // } //Short Blink LED 3 times on startup unsigned char i = 3; while(i--) { led1_high(); _delay_ms(100); led1_low(); _delay_ms(400); wdt_reset(); } /* Loopback Test: TCP Server and UDP */ // Test for Ethernet data transfer validation uint32_t timer_link_1sec = millis(); while(1) { //Here at least every 1sec wdt_reset(); // WDT reset at least every sec //Use Hercules Terminal to check loopback tcp:5000 and udp:3000 /* * https://www.hw-group.com/software/hercules-setup-utility * */ loopback_tcps(SOCK_TCPS,ethBuf0,PORT_TCPS); loopback_udps(SOCK_UDPS,ethBuf0,PORT_UDPS); //loopback_ret = loopback_tcpc(SOCK_TCPS, gDATABUF, destip, destport); //if(loopback_ret < 0) printf("loopback ret: %ld\r\n", loopback_ret); // TCP Socket Error code if((millis()-timer_link_1sec)> 1000) { //here every 1 sec timer_link_1sec = millis(); if(wizphy_getphylink() == PHY_LINK_ON) { led1_high(); } else { led1_low(); } } } return 0; } // Timer0 // 1ms IRQ // Used for millis() timing void timer0_init(void) { /* * * For M128 TCCR0 = (1<250kHz:250-=>1kHz) TIMSK |= 1<250kHz:250-=>1kHz) TIMSK0 |= 1<