/* * 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 "Internet/DHCP/dhcp.h" #include "Application/loopback/loopback.h" //***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN uint8_t mcucsr_mirror __attribute__ ((section (".noinit"))); // This is for fast WDT disable & and save reason of reset/power-up void get_mcusr(void) \ __attribute__((naked)) \ __attribute__((section(".init3"))); void get_mcusr(void) { mcucsr_mirror = MCUSR; MCUSR = 0; wdt_disable(); } //***********Prologue for fast WDT disable & and save reason of reset/power-up: END //#include // itoa etc.. /* * (4) Trying WIZNET5500 init with using official Wiznet ioLibrary_Driver * working ping, on DHCP client (3 times), if failure 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 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< 236+312=548 Bytes * */ #define SOCK_DHCPC 7 #define DATA_BUF_SIZE_DHCP 550 uint8_t gDATABUF_DHCP[DATA_BUF_SIZE_DHCP]; #define MY_MAX_DHCP_RETRY 3 uint8_t my_dhcp_retry = 0; /******************************************************* * @ brief Call back for ip assing & ip update from DHCP *******************************************************/ void my_ip_assign(void) { getIPfromDHCP(netInfo.ip); getGWfromDHCP(netInfo.gw); getSNfromDHCP(netInfo.sn); getDNSfromDHCP(netInfo.dns); netInfo.dhcp = NETINFO_DHCP; /* Network initialization */ //Net_Conf(); // apply from DHCP wizchip_setnetinfo(&netInfo);// apply from DHCP //#ifdef _MAIN_DEBUG_ // Display_Net_Conf(); print_network_information(); #if 1 printf("DHCP LEASED TIME : %ld Sec.\r\n", getDHCPLeasetime()); printf("\r\n"); #endif } /************************************ * @ brief Call back for ip Conflict ************************************/ void my_ip_conflict(void) { #ifdef _MAIN_DEBUG_ printf("CONFLICT IP from DHCP\r\n"); #endif //halt or reset or any... while(1); // this example is halt. } //*************************DHCP-callback: END //***************** WIZCHIP INIT: END int main() { uint8_t prev_sw1 = 1; // VAR for sw1 pressing detect uint8_t run_user_applications = 0; // INIT MCU avr_init(); spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5) // Print program metrics PRINTF("%S", str_prog_name);// Название программы PRINTF("Compiled at: %S %S\r\n", compile_time, compile_date);// Время Дата компиляции PRINTF(">> 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 /* DHCP client Initialization */ if(netInfo.dhcp == NETINFO_DHCP) { //DHCP IP DHCP_init(SOCK_DHCPC, gDATABUF_DHCP); // if you want different action instead default ip assign, update, conflict. // if cbfunc == 0, act as default. reg_dhcp_cbfunc(my_ip_assign, my_ip_assign, my_ip_conflict); run_user_applications = 0; // flag for running user's code } else { // Static IP print_network_information(); run_user_applications = 1; // flag for running user's code } //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(); } //Set-up virtual timers uint32_t timer_link_1sec = millis(); uint32_t timer_dhcp_1sec = millis(); uint32_t timer_dhcp_1sec_count = 0; while(1) { //Here at least every 1sec wdt_reset(); // WDT reset at least every sec /* DHCP */ /*DHCP timer 1 sec interval tick*/ if((millis()-timer_dhcp_1sec)> 1000) { //here every 1 sec timer_dhcp_1sec = millis(); //////////////////////////////////////////////////////// // SHOULD BE Added DHCP Timer Handler your 1s tick timer DHCP_time_handler(); // for DHCP timeout counter //////////////////////////////////////////////////////// if (timer_dhcp_1sec_count++ % 60 == 0) { //Every 1min print-out DHCP leased time elapse #ifdef _DHCP_LEASED_TIME_DEBUG_ PRINTF("DHCP LEASED TIME elapse: %lu Sec.\r\n", timer_dhcp_1sec_count ); #endif } } /* DHCP IP allocation and check the DHCP lease time (for IP renewal) */ if(netInfo.dhcp == NETINFO_DHCP) { switch(DHCP_run()) { case DHCP_IP_ASSIGN: case DHCP_IP_CHANGED: /* If this block empty, act with default_ip_assign & default_ip_update */ // // This example calls my_ip_assign in the two case. // // Add to ... // break; case DHCP_IP_LEASED: // // TODO: insert user's code here run_user_applications = 1; // break; case DHCP_FAILED: /* ===== Example pseudo code ===== */ // The below code can be replaced your code or omitted. // if omitted, retry to process DHCP my_dhcp_retry++; if(my_dhcp_retry > MY_MAX_DHCP_RETRY) { netInfo.dhcp = NETINFO_STATIC; DHCP_stop(); // if restart, recall DHCP_init() //#ifdef _MAIN_DEBUG_ #if 1 printf(">> DHCP %d Failed\r\n", my_dhcp_retry); //Net_Conf(); //Display_Net_Conf(); // print out static netinfo to serial IO_LIBRARY_Init(); //After that ping must working print_network_information(); #endif my_dhcp_retry = 0; run_user_applications = 1; } break; default: break; } } /*PHY LINK DETECT*/ 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(); } } /*TCP-IP & UDP user's applications*/ /* Loopback Test: TCP Server and UDP */ // Test for Ethernet data transfer validation if(run_user_applications) { //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 } } 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<