/* * main.c * * Created on: 22 нояб. 2018 г. * Author: maxx */ /* * 21.Example TFTP client + SD card FATFS (for reading and saving a file from PC TFTP server). ** After pressing the SW1 button, it reads the “readme.txt” file from the PC TFTP server and saves it on the SD card. * Then prints out it contents head in a serial terminal. * TODO: * OK(v1.1) 1. Print-out received file from TFTP to serial console (small file < 512 bytes OK). * OK(v1.2) 2. Print-out received file from TFTP to serial console (multi-packet files > 512 bytes). * OK(v1.3) 3. Write-in data to SD-card file "test.txt" and another patterns. * OK(v1.4) 4. Print out "test.txt" contents head (from SD-Card ) in a serial terminal. * OK(v1.5) 5. Add handlers for CHK_RAM_LEAKAGE && CHK_UPTIME. * 6. Add FSM for TFTP client in main loop * 7. All my debug code in tftp.c replace PRINTF(..) to DBG_PRINT(INFO_DBG, ..) * 8.?? Clear the code from the loopback sockets (Is it really need to do?) * * Remark: * Checked with PC tftp-server (WIN7) - tftpd64.exe */ #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" //Global definitions for project #include "stdbool.h" #include "Ethernet/socket.h" #include "Ethernet/wizchip_conf.h" #include "Application/loopback/loopback.h" #include "Internet/TFTP/tftp.h" #define _MAIN_DEBUG_ //***********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 v1.5 Static IP TFTP Client && FATFS SDCARD WIZNET_5500 ETHERNET 29/03/2019\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) { static uint16_t tftp_1sec; // Compare match Timer0 // Here every 1ms _millis++; // INC millis tick // Тест мигаем при в ходе в прерывание // 500Hz FREQ OUT // LED_TGL; if(++tftp_1sec > 999) { //Here every 1 sec tftp_1sec = 0; //TFTP time handler tftp_timeout_handler(); } } 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 #define UART_BAUD_RATE 115200 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<> 9) + 1980, (_Finfo.fdate >> 5) & 15, _Finfo.fdate & 31, (_Finfo.ftime >> 11), (_Finfo.ftime >> 5) & 63, _Finfo.fsize, &(_Finfo.fname[0])); #if _USE_LFN for (p2 = strlen(_Finfo.fname); p2 < 14; p2++) xputc(' '); xprintf(PSTR("%s\r\n"), Lfname); #else PRINTF("\r\n"); #endif } f_closedir(&Dir); } void fatfs_head_file(const char * fn) { FRESULT f_err_code; FIL fil_obj; //trying to open and read file.. f_chdir("/"); f_err_code=f_open(&fil_obj, fn,FA_READ); //Open *fn - for reading if(f_err_code==0) { DWORD file_len = fil_obj.fsize; UINT br; uint8_t _buf[128] = {0, }; PRINTF("++Content <%s> = %lu bytes found on SDCARD\r\n", fn, file_len); PRINTF("++Trying to read head file..\r\n"); f_err_code = f_read(&fil_obj,&_buf[0], 128, &br); if(f_err_code == 0) { if(br < 128) _buf[br] = 0x0; else _buf[127] = 0x0; PRINTF("OK\r\n"); PRINTF("text contents reading %u bytes:\r\n", br); PRINTF("%s", _buf); } else { PRINTF ("ERROR "); put_rc(f_err_code); PRINTF("But anyway text contents:\r\n"); PRINTF("%s", _buf); } f_close(&fil_obj); } else { PRINTF ("ERROR opening file <%s> ", fn); put_rc(f_err_code); } PRINTF("\r\n"); } void fatfs_init(void) { if( disk_status (0) == STA_NOINIT ) // Initialise the SD Card here, before we do anything else. { if( disk_initialize (0) ) // If it didn't initialise, or the card is write protected, try again. { if( disk_initialize (0) ) // If it didn't initialise, or the card is write protected, then call it out. { PRINTF("\r\nSDCard initialization failed..!\r\nPlease power cycle the SDCard.\r\nCheck write protect.\r\n"); PRINTF("\r\nReboot the Board"); while(1) { _delay_ms(1000); PRINTF("."); } } else { PRINTF("\r\nSDCard initialization OK\r\n"); } } else { PRINTF("\r\nSDCard initialization OK\r\n"); } PRINTF(">>FS MOUNT "); put_rc(f_mount(&Fatfs, (const TCHAR *)"", 1)); PRINTF(">>GO ROOT DIRECTORY "); put_rc(f_chdir((const TCHAR *)"/") ); PRINTF ("\r\n\r\nSD-Card root file list:\r\n"); PRINTF ("===============================================\r\n"); ls_dir("/"); PRINTF ("===============================================\r\n\r\n"); } } // Blocking (~3.5sec) receive one symbol from uart /* char uart0_receive(void) { unsigned int c; uint32_t wait_start = millis(); do { wdt_reset(); c = uart_getc(); if (( c & UART_NO_DATA ) == 0) { uart_putc( (unsigned char)c ); return (char)c ; } //After 3.5 sec waiting return with no symbol if((millis()-wait_start) > 3500) { return 0; } } while(( c & UART_NO_DATA )); return 0; } */ //****************************FAT FS initialize: END /* void spi_speed_tst(void) { // Here on SPI pins: MOSI 400Khz freq out, on SCLK 3.2MhzOUT while(1) { SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); SPI_WRITE(0xF0); } } */ int main() { uint8_t prev_sw1 = 1; // VAR for sw1 pressing detect // INIT MCU avr_init(); spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5) //spi_speed_tst(); / Here on SPI pins: MOSI 400Khz freq out, on SCLK 3.2MhzOUT (Witk SPI CLK 4Mhz) // 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()); //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(); } //FAT_FS init and quick test(root directory list && print out head index.htm) fatfs_init(); //fatfs_head_file("index.htm"); //Wizchip WIZ5500 Ethernet initialize IO_LIBRARY_Init(); //After that ping must working print_network_information(); //TFTP init TFTP_init(SOCK_TFTP, g_tftp_socket_rcv_buf); // Test for Ethernet data transfer validation uint32_t timer_link_1sec = millis(); uint32_t timer_uptime_60sec = millis(); while(1) { //Here at least every 1sec wdt_reset(); // WDT reset at least every sec if((millis()-timer_link_1sec)> 1000) { //here every 1 sec timer_link_1sec = millis(); //!! SW1 pressing action if(!sw1_read())// Check for SW1 pressed every second { // SW1 is pressed //led1_high(); //LED1 ON if(prev_sw1) { //!! Здесь по факту нажатия кнопки (1->0 SW1) printf("\r\n########## SW1 was pressed.\r\n"); memset(tftp_filename, 0x0, TFTP_FILE_NAME_SIZE); //!!Don't forget about 8.3 file name rule!! strncpy(tftp_filename, "test.txt", TFTP_FILE_NAME_SIZE); //strncpy(tftp_filename, "README.md", TFTP_FILE_NAME_SIZE); //strncpy(tftp_filename, "tftpd32.ini", TFTP_FILE_NAME_SIZE); //strncpy(tftp_filename, "ff_lfn.txt", TFTP_FILE_NAME_SIZE); tftp_server = ((uint32_t)tftp_destip[0] << 24) | ((uint32_t)tftp_destip[1] << 16) | ((uint32_t)tftp_destip[2] << 8) | ((uint32_t)tftp_destip[3]); PRINTF("TFTP IP address : %d.%d.%d.%d\r\n",tftp_destip[0],tftp_destip[1],tftp_destip[2],tftp_destip[3]); PRINTF("TFTP IP address (32 bit) : 0x%lX\r\n",tftp_server); TFTP_read_request(tftp_server, tftp_filename); clear_tftp_received_size(); uint8_t _ret = 0; while(1){ wdt_reset(); _ret = TFTP_run(); if(_ret != TFTP_PROGRESS) { //Print-out result TFTP complete if(_ret == TFTP_SUCCESS) { PRINTF("\r\n++TFTP transfer complete:[%u] SUCCESS, received %lu bytes\r\n", _ret, get_tftp_received_size()); //Print-out head received file fatfs_head_file(tftp_filename); } else if(_ret == TFTP_FAIL) { PRINTF("\r\n--TFTP transfer complete:[%u] FAIL, received %lu bytes\r\n", _ret, get_tftp_received_size()); } else { PRINTF("\r\n??TFTP transfer complete:[%u] UNKNOWN, received %lu bytes\r\n\r\n", _ret, get_tftp_received_size()); } break; } } _delay_ms(1000); //!! Debug only //PRINTF("SW1 is pressed\r\n"); }//if(prev_sw1) prev_sw1 = 0; // Store SW1 state for next iteration }//if(!sw1_read()) else { // SW1 is unpressed //led1_low(); // LED1 OFF prev_sw1 = 1;// Store SW1 state for next iteration }//if(!sw1_read())else.. //Check ETHERNET PHY link if(wizphy_getphylink() == PHY_LINK_ON) { led1_high(); } else { led1_low(); } } if((millis()-timer_uptime_60sec)> 60000) { //here every 60 sec timer_uptime_60sec = millis(); #ifdef CHK_RAM_LEAKAGE //Printout RAM usage every 1 minute PRINTF(">> Free RAM is: %d bytes\r\n", freeRam()); #endif #ifdef CHK_UPTIME //Printout RAM usage every 1 minute PRINTF(">> Uptime %lu sec\r\n", millis()/1000); #endif } } 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<