Skip to content

Commit

Permalink
FTP Server for DAQ PCB (#119)
Browse files Browse the repository at this point in the history
* FTP Server Working with WinSCP

* FTP Server Speedup
  • Loading branch information
LukeOxley authored Apr 25, 2024
1 parent d800994 commit b9a49e0
Show file tree
Hide file tree
Showing 9 changed files with 1,752 additions and 32 deletions.
3 changes: 3 additions & 0 deletions common/phal_F4_F7/can/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ bool PHAL_initCAN(CAN_TypeDef* bus, bool test_mode, uint32_t bit_rate)
case 24000000:
bus->BTR = PHAL_CAN_24MHz_500k;
break;
case 42000000:
bus->BTR = PHAL_CAN_42MHz_500k;
break;
default:
return false;
}
Expand Down
1 change: 1 addition & 0 deletions common/phal_F4_F7/can/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
// Bit timing recovered from http://www.bittiming.can-wiki.info/
#define PHAL_CAN_16MHz_500k (0x033a0001) // sample point = 75%, SJW = 4
#define PHAL_CAN_24MHz_500k (0x033a0002) // sample point = 75%, SJW = 4
#define PHAL_CAN_42MHz_500k (0x034e0003) // sample point = 75%, SJW = 4

#define PHAL_CAN_16MHz_250k (0x003a0003) // sample point = 75%
#define PHAL_CAN_24MHz_250k (0x003a0005) // sample point = 75%
Expand Down
94 changes: 66 additions & 28 deletions source/daq/daq_hub/daq_hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/* Module Includes */
#include "buffer.h"
#include "daq_hub.h"
#include "ftpd.h"
#include "main.h"
#include "sdio.h"

Expand All @@ -48,10 +49,13 @@ eth_config_t eth_config = {
.udp_bc_sock = 0,
.tcp_port = 5005,
.tcp_sock = 1,
// NOTE: ftp uses 2, 3, 4
};

daq_hub_t dh;

uint8_t gFTPBUF[_FTP_BUF_SIZE];

// Local protoptypes
static void sd_handle_error(sd_error_t err, FRESULT res);
static void sd_update_connection_state(void);
Expand Down Expand Up @@ -92,6 +96,10 @@ void daq_init(void)
dh.log_enable_tcp = false;
dh.my_watch = 0x420;

// FTP
ftpd_init(eth_config.net_info.ip);
dh.ftp_busy = false;

// General
dh.loop_time_avg_ms = 0;
dh.loop_time_max_ms = 0;
Expand Down Expand Up @@ -227,6 +235,35 @@ static void conv_tcp_frame_to_can_msg(tcp_can_frame_t *t, CanMsgTypeDef_t *c)
for (uint8_t i = 0; i < 8; ++i) c->Data[i] = t->data[i];
}

bool daq_request_sd_mount(void)
{
FRESULT res;
if (dh.sd_state == SD_MOUNTED || dh.sd_state == SD_FILE_CREATED)
{
return true;
}

if (!SD_Detect())
{
sd_handle_error(SD_ERROR_DETEC, 0);
}
else
{
res = f_mount(&dh.fat_fs, "", 1);
if (res == FR_OK)
{
dh.sd_state = SD_MOUNTED;
return true;
}
else
{
sd_handle_error(SD_ERROR_MOUNT, res);
dh.sd_state = SD_FAIL; // force fail from idle
}
}
return false;
}

static void sd_update_connection_state(void)
{
FRESULT res;
Expand All @@ -247,7 +284,7 @@ static void sd_update_connection_state(void)
if (dh.sd_state != SD_IDLE)
{
if (!SD_Detect()) sd_handle_error(SD_ERROR_DETEC, 0);
else if (!get_log_enable()) dh.sd_state = SD_SHUTDOWN;
else if (!get_log_enable() && !dh.ftp_busy) dh.sd_state = SD_SHUTDOWN;
}
else if (!get_log_enable())
{
Expand All @@ -264,42 +301,37 @@ static void sd_update_connection_state(void)
PHAL_writeGPIO(SD_ACTIVITY_LED_PORT, SD_ACTIVITY_LED_PIN, 0); // Activity LED disable
if (get_log_enable() && dh.sd_error_ct == 0) // TODO: revert, ensuring no auto-reset for now
{
bActivateTail(&b_rx_can, RX_TAIL_SD);
if (!SD_Detect()) sd_handle_error(SD_ERROR_DETEC, 0);
else
{
res = f_mount(&dh.fat_fs, "", 1);
if (res == FR_OK)
{
dh.sd_state = SD_MOUNTED;
}
else
{
sd_handle_error(SD_ERROR_MOUNT, res);
dh.sd_state = SD_FAIL; // force fail from idle
}
}
daq_request_sd_mount();
}
break;
case SD_MOUNTED:
if (sd_new_log_file())
PHAL_writeGPIO(SD_DETECT_LED_PORT, SD_DETECT_LED_PIN, 1);
if (get_log_enable())
{
dh.sd_state = SD_FILE_CREATED;
dh.sd_last_err = SD_ERROR_NONE; // back to okay
PHAL_writeGPIO(SD_DETECT_LED_PORT, SD_DETECT_LED_PIN, 1);
}
else
{
sd_handle_error(SD_ERROR_FOPEN, 0);
bActivateTail(&b_rx_can, RX_TAIL_SD);
if (sd_new_log_file())
{
dh.sd_state = SD_FILE_CREATED;
dh.sd_last_err = SD_ERROR_NONE; // back to okay
}
else
{
sd_handle_error(SD_ERROR_FOPEN, 0);
}
}
break;
break;
case SD_FILE_CREATED:
if ((tick_ms - dh.log_start_ms) > SD_NEW_FILE_PERIOD_MS)
{
// Swap to a new log file
if ((res = f_close(&dh.log_fp)) != FR_OK) sd_handle_error(SD_ERROR_FCLOSE, res);
else if (!sd_new_log_file()) sd_handle_error(SD_ERROR_FOPEN, 0);
}
if (!get_log_enable())
{
if ((res = f_close(&dh.log_fp)) != FR_OK) sd_handle_error(SD_ERROR_FCLOSE, res);
dh.sd_state = SD_MOUNTED;
}
break;
case SD_FAIL:
// Intentional fall-through
Expand Down Expand Up @@ -419,9 +451,14 @@ static void eth_update_connection_state(void)
static uint32_t last_init_time;
static uint32_t init_try_counter;

if (dh.eth_error_ct - last_error_count > 0) dh.eth_state = ETH_FAIL;
if (dh.eth_error_ct - last_error_count > 0)
{
dh.eth_state = ETH_FAIL;
}
last_error_count = dh.eth_error_ct;

// TODO: on forceful dismount, reset the ports (maybe even ftp_init())

switch(dh.eth_state)
{
case ETH_IDLE:
Expand Down Expand Up @@ -468,6 +505,7 @@ static void eth_update_connection_state(void)
}
last_link_check_time = tick_ms;
}
ftpd_run(gFTPBUF);
break;
case ETH_FAIL:
// Intentional fall-through
Expand All @@ -487,7 +525,7 @@ static void eth_update_connection_state(void)
// Update connection LED
if (dh.eth_state == ETH_LINK_UP)
{
if (dh.eth_tcp_state == ETH_TCP_ESTABLISHED)
if (dh.eth_tcp_state == ETH_TCP_ESTABLISHED || dh.ftp_busy)
{
if (tick_ms - last_blink_time > 250)
{
Expand Down Expand Up @@ -531,7 +569,7 @@ static int8_t eth_init(void)
}

uint8_t rxBufSizes[] = {2, 2, 2, 2, 2, 2, 2, 2}; // kB
uint8_t txBufSizes[] = {2, 2, 2, 2, 2, 2, 2, 2}; // kB
uint8_t txBufSizes[] = {2, 2, 2, 8, 2, 0, 0, 0}; // kB

if(wizchip_init(txBufSizes, rxBufSizes))
{
Expand Down
4 changes: 4 additions & 0 deletions source/daq/daq_hub/daq_hub.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <stdint.h>
#include "ff.h"
#include "common/phal_F4_F7/rtc/rtc.h"


/* Begin CAN Definitions from <linux/can.h> */
Expand Down Expand Up @@ -177,10 +178,13 @@ typedef struct
// General
uint32_t loop_time_max_ms;
uint32_t loop_time_avg_ms;
// FTP
bool ftp_busy;
} daq_hub_t;
extern daq_hub_t dh;

void daq_init(void);
void daq_loop(void);
bool daq_request_sd_mount(void);

#endif
64 changes: 64 additions & 0 deletions source/daq/ftp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# FTP Server

## init wizchip
### variable and function

uint8_t gFTPBUF[_MAX_SS];
uint8_t wiznet_memsize[2][8] = {{8,8,8,8,8,8,8,8}, {8,8,8,8,8,8,8,8}};
#define ETH_MAX_BUF_SIZE 2048
uint8_t ethBuf0[ETH_MAX_BUF_SIZE];
wiz_NetInfo gWIZNETINFO = {
.mac = {0x00, 0x08, 0xdc, 0x11, 0x22, 0x33},
.ip = {192, 168, 15, 111},
.sn = {255, 255, 255, 0},
.gw = {192, 168, 15, 1},
.dns = {8, 8, 8, 8},
.dhcp = NETINFO_STATIC



### run init funciton in main funcion

Reset_WIZCHIP();
reg_wizchip_bus_cbfunc(Chip_read, Chip_write);
reg_wizchip_cs_cbfunc(ChipCsEnable, ChipCsDisable);

if (ctlwizchip(CW_INIT_WIZCHIP, (void*)wiznet_memsize) == -1)
{
printf("WIZchip memory initialization failed\r\n");
}

ctlnetwork(CN_SET_NETINFO, (void *)&gWIZNETINFO);
print_network_information();


## Run function

if((ret = ftpd_run(gFTPBUF)) < 0)
{
porintf(" FTP server Error %d \r\n", ret);
}

## FTP ID & PASSWORD

### USED ID
#### ID Enable in ftpd_init function

ftp.ID_Enable = STATUS_USED;

#### ID Setting

#define ftp_ID "wiznet"

### USED PASSWORD
#### PW Enable in ftpd_init function

ftp.PW_Enable = STATUS_USED;

#### PW Setting

#define ftp_PW "wiznet54321"

### Connedtion remain count

#define remain_time 400000
Loading

0 comments on commit b9a49e0

Please sign in to comment.