/* * 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 "globals.h" #include "Ethernet/socket.h" #include "Ethernet/wizchip_conf.h" #include "Application/loopback/loopback.h" #include "Application/telnet/telnet.h" #ifdef IP_WORK //NIC metrics for WORK PC wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address .ip = {192, 168, 0, 199}, // IP address .sn = {255, 255, 255, 0}, // Subnet mask .dns = {8,8,8,8}, // DNS address (google dns) .gw = {192, 168, 0, 1}, // Gateway address .dhcp = NETINFO_STATIC}; //Static IP configuration #else //NIC metrics for another PC (second IP configuration) wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address .ip = {192, 168, 1, 199}, // IP address .sn = {255, 255, 255, 0}, // Subnet mask .dns = {8,8,8,8}, // DNS address (google dns) .gw = {192, 168, 1, 1}, // Gateway address .dhcp = NETINFO_STATIC}; //Static IP configuration #endif //#include // itoa etc.. /* * (7)Add Telnet simple server (23) - unblocking type via telnet_srv(..) (v2.0) * (7)Add Telnet authorization (v2.1) (24) - unblocking type via telnet_auth_srv(..) (v2.1) * Use telnet to connect to defined IP and port. In this example, connect to 192.168.1.192 port 23. The telnet client will receive a message and port closed. * XP/Windows_7 * > telnet 192.168.1.192 23 * * Windows 10 * run telnet client * open 192.168.1.192 23 * * (3) Trying WIZNET5500 init with using official Wiznet ioLibrary_Driver * working ping on 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 * */ /* * m1284p minimum template, with one button & one led */ //***********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 //*********Global vars #define TICK_PER_SEC 1000UL volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days //*********Program metrics const char compile_date[] PROGMEM = __DATE__; // Mmm dd yyyy - Дата компиляции const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - Время компиляции const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v2.1 Static IP Telnet server WIZNET_5500 ETHERNET 01/12/2018\r\n"; // Program name #if defined(__AVR_ATmega128__) const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128 #elif defined (__AVR_ATmega2560__) const char PROGMEM str_mcu[] = "ATmega2560"; //CPU is m2560 #elif defined (__AVR_ATmega2561__) const char PROGMEM str_mcu[] = "ATmega2561"; //CPU is m2561 #elif defined (__AVR_ATmega328P__) const char PROGMEM str_mcu[] = "ATmega328P"; //CPU is m328p #elif defined (__AVR_ATmega32U4__) const char PROGMEM str_mcu[] = "ATmega32u4"; //CPU is m32u4 #elif defined (__AVR_ATmega644P__) const char PROGMEM str_mcu[] = "ATmega644p"; //CPU is m644p #elif defined (__AVR_ATmega1284P__) const char PROGMEM str_mcu[] = "ATmega1284p"; //CPU is m1284p #else const char PROGMEM str_mcu[] = "Unknown CPU"; //CPU is unknown #endif //FUNC headers static void avr_init(void); void timer0_init(void); //Wiznet FUNC headers void print_network_information(void); // RAM Memory usage test int freeRam (void) { extern int __heap_start, *__brkval; int v; int _res = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); return _res; } //******************* MILLIS ENGINE: BEGIN //ISR (TIMER0_COMP_vect ) ISR (TIMER0_COMPA_vect) { // Compare match Timer0 // Here every 1ms _millis++; // INC millis tick // Тест мигаем при в ходе в прерывание // 500Hz FREQ OUT // LED_TGL; } unsigned long millis(void) { unsigned long i; cli(); // Atomic tick reading i = _millis; sei(); return i; } //******************* MILLIS ENGINE: END //***************** UART0: BEGIN // Assign I/O stream to UART /* define CPU frequency in Mhz here if not defined in Makefile */ //#ifndef F_CPU //#define F_CPU 16000000UL //#endif /* 19200 baud */ #define UART_BAUD_RATE 19200 //#define UART_BAUD_RATE 38400 static int uart0_putchar(char ch,FILE *stream); static void uart0_rx_flash(void); static FILE uart0_stdout = FDEV_SETUP_STREAM(uart0_putchar, NULL, _FDEV_SETUP_WRITE); //PS. stdin не переназначаю, т.к. удобнее с ним работать через uart.h - api: /* * Т.е. например так c = uart1_getc(); if (( c & UART_NO_DATA ) == 0) { uart1_putc( (unsigned char)c ); } При этом чекаем что буфер приема не пуст и опрос идет неблокирующий (+ работаем через UART RX RINGBUFFER), а если работаем в стиле stdin->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()); //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(); } //Wizchip WIZ5500 Ethernet initialize IO_LIBRARY_Init(); //After that ping must working print_network_information(); /* *To run blocking telnet server (deprecated) while(1) { _telnet_srv(TELNET_PORT); } */ PRINTF("> Telnet server is running on port %u\r\n", TELNET_PORT); PRINTF("> Telnet AUTH server is running on port %u\r\n", TELNET_AUTH_PORT); //Telnet SERVER (23) + Loopback Test: TCP Server (5000) and UDP (3000) // 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 * */ telnet_srv(3, ethBut3_TELNET, TELNET_PORT); telnet_auth_srv(4, ethBut4_TELNET, TELNET_AUTH_PORT); loopback_tcps(0,ethBuf0,5000); loopback_udps(1, ethBuf0, 3000); //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 //Shouldn't use LED for ethernet link here, because it used on telnet command processor /* 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<