Write-in data to SD-card file "readme_txt" and another patterns during

TFTP transfer session - SUCCESS.
master
maxxir_w 7 years ago
parent 4414955447
commit 4e2862981a

@ -35,6 +35,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Ethernet/W5500}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Ethernet/W5500}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Application/loopback}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Application/loopback}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/ff}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/ff}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/${ProjName}/Internet/TFTP}&quot;"/>
</option> </option>
<inputType id="de.innot.avreclipse.compiler.winavr.input.167604838" name="C Source Files" superClass="de.innot.avreclipse.compiler.winavr.input"/> <inputType id="de.innot.avreclipse.compiler.winavr.input.167604838" name="C Source Files" superClass="de.innot.avreclipse.compiler.winavr.input"/>
</tool> </tool>
@ -76,5 +77,9 @@
</scannerConfigBuildInfo> </scannerConfigBuildInfo>
</storageModule> </storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/> <storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope"/> <storageModule moduleId="refreshScope" versionNumber="2">
<configuration configurationName="Release">
<resource resourceType="PROJECT" workspacePath="/21_m1284p_WIZNET_TFTP_client_FATFS"/>
</configuration>
</storageModule>
</cproject> </cproject>

@ -19,7 +19,7 @@
/* Extern Functions ---------------------------------------------*/ /* Extern Functions ---------------------------------------------*/
#ifdef F_STORAGE #ifdef F_STORAGE
extern void save_data(uint8_t *data, uint32_t data_len, uint16_t block_number); extern uint8_t save_data(uint8_t *data, uint32_t data_len, uint16_t block_number);
static uint32_t g_tftp_save_data = 0; static uint32_t g_tftp_save_data = 0;
@ -32,17 +32,53 @@ void clear_tftp_received_size(void)
{ {
g_tftp_save_data = 0; g_tftp_save_data = 0;
} }
void save_data(uint8_t *data, uint32_t data_len, uint16_t block_number)
void close_tftp_file(void)
{
if(tftp_fr == FR_OK)
{
tftp_fr = f_close(&tftp_fil);
if(tftp_fr != FR_OK){
PRINTF("--f_close failed\r\n");
}
}
}
uint8_t save_data(uint8_t *data, uint32_t data_len, uint16_t block_number)
{ {
uint8_t save_data_result = 0;
#ifdef TFTP_RCV_DBG
//Nothing to do with received data yet.. //Nothing to do with received data yet..
//TODO: Add your own handler here //TODO: Add your own handler here
//Print out data as string //Print out received data as string
uint8_t * str; uint8_t * str;
str = data; str = data;
str += data_len; str += data_len;
*str = 0x0; *str = 0x0;
g_tftp_save_data += data_len; //Store received data size
PRINTF("\r\n++Data #%d-%lu:\r\n%s\r\n", block_number, data_len, data); PRINTF("\r\n++Data #%d-%lu:\r\n%s\r\n", block_number, data_len, data);
#endif
//Calculate received data size
g_tftp_save_data += data_len;
//Save TFTP block to file
if(tftp_fr == FR_OK)
{
uint16_t _blocklen;
tftp_fr = f_write(&tftp_fil, data, (UINT)data_len, &_blocklen);
if(tftp_fr != FR_OK){
PRINTF("--f_write failed #2\r\n");
save_data_result = 2;
}
else
{
f_sync(&tftp_fil); //Flush data to SDCARD from cache
}
}
else
{
PRINTF("--f_write failed #1\r\n");
save_data_result = 1;
}
return save_data_result;
} }
#endif #endif
@ -80,6 +116,13 @@ int dbg_level = (INFO_DBG | ERROR_DBG | IPC_DBG | DEBUG_DBG);
static void set_filename(uint8_t *file, uint32_t file_size) static void set_filename(uint8_t *file, uint32_t file_size)
{ {
memcpy(g_filename, file, file_size); memcpy(g_filename, file, file_size);
#if defined(F_STORAGE)
//Rewrite file onto SD-card
tftp_fr = f_open(&tftp_fil, (const char *)g_filename, FA_CREATE_ALWAYS | FA_WRITE);
if(tftp_fr != FR_OK){
PRINTF("--f_open failed\r\n");
}
#endif
} }
static inline void set_server_ip(uint32_t ipaddr) static inline void set_server_ip(uint32_t ipaddr)
@ -444,7 +487,10 @@ static void recv_tftp_data(uint8_t *msg, uint32_t msg_len)
set_tftp_state(STATE_DATA); set_tftp_state(STATE_DATA);
set_block_number(data->block_num); set_block_number(data->block_num);
#ifdef F_STORAGE #ifdef F_STORAGE
save_data(data->data, msg_len - 4, data->block_num); if (save_data(data->data, msg_len - 4, data->block_num))
{
g_progress_state = TFTP_FAIL;
}
#endif #endif
tftp_cancel_timeout(); tftp_cancel_timeout();
} }
@ -461,7 +507,11 @@ static void recv_tftp_data(uint8_t *msg, uint32_t msg_len)
if(data->block_num == (get_block_number() + 1)) { if(data->block_num == (get_block_number() + 1)) {
set_block_number(data->block_num); set_block_number(data->block_num);
#ifdef F_STORAGE #ifdef F_STORAGE
save_data(data->data, msg_len - 4, data->block_num); if(save_data(data->data, msg_len - 4, data->block_num))
{
g_progress_state = TFTP_FAIL;
}
#endif #endif
tftp_cancel_timeout(); tftp_cancel_timeout();
} }
@ -671,11 +721,26 @@ int TFTP_run(void)
#ifdef __TFTP_DEBUG__ #ifdef __TFTP_DEBUG__
DBG_PRINT(ERROR_DBG, "[%s] recv_udp_packet error\r\n", __func__); DBG_PRINT(ERROR_DBG, "[%s] recv_udp_packet error\r\n", __func__);
#endif #endif
#ifdef F_STORAGE
if(g_progress_state != TFTP_PROGRESS)
{
//Close TFTP file if transfer complete
close_tftp_file();
}
#endif
return g_progress_state; return g_progress_state;
} }
recv_tftp_packet(g_tftp_rcv_buf, len, from_ip, from_port); recv_tftp_packet(g_tftp_rcv_buf, len, from_ip, from_port);
#ifdef F_STORAGE
if(g_progress_state != TFTP_PROGRESS)
{
//Close TFTP file if transfer complete
close_tftp_file();
}
#endif
return g_progress_state; return g_progress_state;
} }

@ -17,13 +17,14 @@ extern "C" {
#define F_APP_TFTP #define F_APP_TFTP
#define __TFTP_DEBUG__ #define __TFTP_DEBUG__
//define TFTP_RCV_DBG /* To print-out received TFTP packet as string data */
#define F_STORAGE // If your target support a storage, you have to activate this feature and implement. #define F_STORAGE // If your target support a storage, you have to activate this feature and implement.
#if defined(F_STORAGE) #if defined(F_STORAGE)
#include "ff.h" #include "../../ff/ff.h"
FIL fil; // FatFs File objects static FIL tftp_fil; // FatFs File objects
FRESULT fr; // FatFs function common result code static FRESULT tftp_fr; // FatFs function common result code
#endif #endif

@ -16,7 +16,7 @@
/ data transfer. */ / data transfer. */
#define _FS_READONLY 1 #define _FS_READONLY 0
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only) /* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
/ Read-only configuration removes writing API functions, f_write(), f_sync(), / Read-only configuration removes writing API functions, f_write(), f_sync(),
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree() / f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()

@ -11,8 +11,10 @@
* TODO: * TODO:
* OK(v1.1) 1. Print-out received file from TFTP to serial console (small file < 512 bytes OK). * 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.2) 2. Print-out received file from TFTP to serial console (multi-packet files > 512 bytes).
* 3. Write-in data to SD-card file "readme_txt". * OK(v1.3) 3. Write-in data to SD-card file "readme_txt" and another patterns.
* 4. Add handlers for CHK_RAM_LEAKAGE && CHK_UPTIME. * 4. Print out "readme.txt" contents head (from SD-Card ) in a serial terminal.
* 5. Add handlers for CHK_RAM_LEAKAGE && CHK_UPTIME.
* 6. Clear the code from the loopback sockets
* *
* Remark: * Remark:
* Checked with PC tftp-server (WIN7) - tftpd64.exe * Checked with PC tftp-server (WIN7) - tftpd64.exe
@ -61,7 +63,7 @@ volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days
//*********Program metrics //*********Program metrics
const char compile_date[] PROGMEM = __DATE__; // Mmm dd yyyy - Дата компиляции const char compile_date[] PROGMEM = __DATE__; // Mmm dd yyyy - Дата компиляции
const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - Время компиляции const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - Время компиляции
const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.2 Static IP TFTP Client && FATFS SDCARD WIZNET_5500 ETHERNET 27/03/2019\r\n"; // Program name const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.3 Static IP TFTP Client && FATFS SDCARD WIZNET_5500 ETHERNET 27/03/2019\r\n"; // Program name
#if defined(__AVR_ATmega128__) #if defined(__AVR_ATmega128__)
const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128 const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128
@ -536,18 +538,6 @@ int main()
//Here at least every 1sec //Here at least every 1sec
wdt_reset(); // WDT reset at least every sec 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) if((millis()-timer_link_1sec)> 1000)
{ {
//here every 1 sec //here every 1 sec
@ -565,11 +555,12 @@ int main()
printf("\r\n########## SW1 was pressed.\r\n"); printf("\r\n########## SW1 was pressed.\r\n");
memset(tftp_filename, 0x0, 20); memset(tftp_filename, 0x0, TFTP_FILE_NAME_SIZE);
strncpy(tftp_filename, "test.txt", TFTP_FILE_NAME_SIZE); //!!Don't forget about 8.3 file name rule!!
//strncpy(tftp_filename, "README.md", TFTP_FILE_NAME_SIZE); //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, "tftpd32.ini", TFTP_FILE_NAME_SIZE);
//strncpy(tftp_filename, "ff_lfn_required.c", TFTP_FILE_NAME_SIZE); //strncpy(tftp_filename, "ff_lfn.c", 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]); 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]);