From bf73b1b54198290ab8905d0ab5770020283e92b1 Mon Sep 17 00:00:00 2001 From: imi415 Date: Sat, 11 Jun 2022 00:10:08 +0800 Subject: [PATCH] Added filesystem helper to LwIP. --- CMakeLists.txt | 3 +- MK60DN512xxx10.mex | 42 ++++++++++++++--- board/board.c | 5 ++ board/board.h | 5 +- board/pin_mux.c | 60 +++++++++++++++++++++--- include/ffconf.h | 2 +- lib/LwIP/port/include/lwipopts.h | 8 +++- src/fatfs_diskio.c | 4 +- src/httpd_helpers.c | 73 +++++++++++++++++++++++++++++ src/ip_stack_helpers.c | 2 - src/main.c | 12 ++--- src/{sdhc_helpers.c => sdhc_host.c} | 2 - 12 files changed, 190 insertions(+), 28 deletions(-) create mode 100644 src/httpd_helpers.c rename src/{sdhc_helpers.c => sdhc_host.c} (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 582fcef..20b5ca6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,9 +67,10 @@ set(TARGET_SOURCES "src/fatfs_diskio.c" "src/freertos_helpers.c" "src/fsl_phy.c" + "src/httpd_helpers.c" "src/ip_stack_helpers.c" "src/main.c" - "src/sdhc_helpers.c" + "src/sdhc_host.c" "src/syscalls.c" "src/system_utilities.c" ) diff --git a/MK60DN512xxx10.mex b/MK60DN512xxx10.mex index c2580af..332bf66 100644 --- a/MK60DN512xxx10.mex +++ b/MK60DN512xxx10.mex @@ -132,12 +132,42 @@ - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/board/board.c b/board/board.c index 274ce3f..8e3a178 100644 --- a/board/board.c +++ b/board/board.c @@ -31,6 +31,7 @@ #include #include "fsl_common.h" #include "fsl_rtc.h" +#include "fsl_sysmpu.h" #include "fsl_debug_console.h" #include "board.h" @@ -58,4 +59,8 @@ void BOARD_EnableRTC(void) { RTC_Init(RTC, &rtc_config); RTC->SR |= RTC_SR_TCE_MASK; +} + +void BOARD_DisableSYSMPU(void) { + SYSMPU_Enable(SYSMPU, false); } \ No newline at end of file diff --git a/board/board.h b/board/board.h index 2481a1b..a6df629 100644 --- a/board/board.h +++ b/board/board.h @@ -163,8 +163,8 @@ /* SDHC base address, clock and card detection pin */ #define BOARD_SDHC_BASEADDR SDHC -#define BOARD_SDHC_CLKSRC kCLOCK_CoreSysClk -#define BOARD_SDHC_CLK_FREQ CLOCK_GetFreq(kCLOCK_CoreSysClk) +#define BOARD_SDHC_CLKSRC kCLOCK_Osc0ErClk +#define BOARD_SDHC_CLK_FREQ CLOCK_GetFreq(kCLOCK_Osc0ErClk) #define BOARD_SDHC_IRQ SDHC_IRQn #define BOARD_SDHC_CD_GPIO_BASE GPIOE #define BOARD_SDHC_CD_GPIO_PIN 28U @@ -185,6 +185,7 @@ extern "C" { void BOARD_InitDebugConsole(void); void BOARD_EnableRTC(void); +void BOARD_DisableSYSMPU(void); #if defined(__cplusplus) } diff --git a/board/pin_mux.c b/board/pin_mux.c index e9baae4..218bd0a 100644 --- a/board/pin_mux.c +++ b/board/pin_mux.c @@ -94,12 +94,12 @@ BOARD_InitPins: - {pin_num: '69', peripheral: ENET, signal: RMII_TXD1, pin_signal: ADC1_SE17/PTA17/SPI0_SIN/UART0_RTS_b/RMII0_TXD1/MII0_TXD1/I2S0_MCLK} - {pin_num: '67', peripheral: ENET, signal: rmii_txen, pin_signal: PTA15/SPI0_SCK/UART0_RX/RMII0_TXEN/MII0_TXEN/I2S0_RXD0} - {pin_num: '72', peripheral: ENET, signal: RMII_CLKIN, pin_signal: EXTAL0/PTA18/FTM0_FLT2/FTM_CLKIN0} - - {pin_num: '4', peripheral: SDHC, signal: CMD, pin_signal: ADC1_SE7a/PTE3/SPI1_SIN/UART1_RTS_b/SDHC0_CMD/SPI1_SOUT} - - {pin_num: '2', peripheral: SDHC, signal: 'D, 0', pin_signal: ADC1_SE5a/PTE1/LLWU_P0/SPI1_SOUT/UART1_RX/SDHC0_D0/I2C1_SCL/SPI1_SIN} - - {pin_num: '1', peripheral: SDHC, signal: 'D, 1', pin_signal: ADC1_SE4a/PTE0/SPI1_PCS1/UART1_TX/SDHC0_D1/I2C1_SDA/RTC_CLKOUT} - - {pin_num: '7', peripheral: SDHC, signal: 'D, 3', pin_signal: PTE4/LLWU_P2/SPI1_PCS0/UART3_TX/SDHC0_D3} - - {pin_num: '8', peripheral: SDHC, signal: 'D, 2', pin_signal: PTE5/SPI1_PCS2/UART3_RX/SDHC0_D2} - - {pin_num: '3', peripheral: SDHC, signal: DCLK, pin_signal: ADC1_SE6a/PTE2/LLWU_P1/SPI1_SCK/UART1_CTS_b/SDHC0_DCLK} + - {pin_num: '4', peripheral: SDHC, signal: CMD, pin_signal: ADC1_SE7a/PTE3/SPI1_SIN/UART1_RTS_b/SDHC0_CMD/SPI1_SOUT, pull_select: up, pull_enable: enable} + - {pin_num: '2', peripheral: SDHC, signal: 'D, 0', pin_signal: ADC1_SE5a/PTE1/LLWU_P0/SPI1_SOUT/UART1_RX/SDHC0_D0/I2C1_SCL/SPI1_SIN, pull_select: up, pull_enable: enable} + - {pin_num: '1', peripheral: SDHC, signal: 'D, 1', pin_signal: ADC1_SE4a/PTE0/SPI1_PCS1/UART1_TX/SDHC0_D1/I2C1_SDA/RTC_CLKOUT, pull_select: up, pull_enable: enable} + - {pin_num: '7', peripheral: SDHC, signal: 'D, 3', pin_signal: PTE4/LLWU_P2/SPI1_PCS0/UART3_TX/SDHC0_D3, pull_select: up, pull_enable: enable} + - {pin_num: '8', peripheral: SDHC, signal: 'D, 2', pin_signal: PTE5/SPI1_PCS2/UART3_RX/SDHC0_D2, pull_select: up, pull_enable: enable} + - {pin_num: '3', peripheral: SDHC, signal: DCLK, pin_signal: ADC1_SE6a/PTE2/LLWU_P1/SPI1_SCK/UART1_CTS_b/SDHC0_DCLK, pull_select: up, pull_enable: enable} * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS *********** */ /* clang-format on */ @@ -295,21 +295,69 @@ void BOARD_InitPins(void) /* PORTE0 (pin 1) is configured as SDHC0_D1 */ PORT_SetPinMux(PORTE, 0U, kPORT_MuxAlt4); + PORTE->PCR[0] = ((PORTE->PCR[0] & + /* Mask bits to zero which are setting */ + (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) + + /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the + * corresponding Port Pull Enable Register field is set. */ + | (uint32_t)(kPORT_PullUp)); + /* PORTE1 (pin 2) is configured as SDHC0_D0 */ PORT_SetPinMux(PORTE, 1U, kPORT_MuxAlt4); + PORTE->PCR[1] = ((PORTE->PCR[1] & + /* Mask bits to zero which are setting */ + (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) + + /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the + * corresponding Port Pull Enable Register field is set. */ + | (uint32_t)(kPORT_PullUp)); + /* PORTE2 (pin 3) is configured as SDHC0_DCLK */ PORT_SetPinMux(PORTE, 2U, kPORT_MuxAlt4); + PORTE->PCR[2] = ((PORTE->PCR[2] & + /* Mask bits to zero which are setting */ + (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) + + /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the + * corresponding Port Pull Enable Register field is set. */ + | (uint32_t)(kPORT_PullUp)); + /* PORTE3 (pin 4) is configured as SDHC0_CMD */ PORT_SetPinMux(PORTE, 3U, kPORT_MuxAlt4); + PORTE->PCR[3] = ((PORTE->PCR[3] & + /* Mask bits to zero which are setting */ + (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) + + /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the + * corresponding Port Pull Enable Register field is set. */ + | (uint32_t)(kPORT_PullUp)); + /* PORTE4 (pin 7) is configured as SDHC0_D3 */ PORT_SetPinMux(PORTE, 4U, kPORT_MuxAlt4); + PORTE->PCR[4] = ((PORTE->PCR[4] & + /* Mask bits to zero which are setting */ + (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) + + /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the + * corresponding Port Pull Enable Register field is set. */ + | (uint32_t)(kPORT_PullUp)); + /* PORTE5 (pin 8) is configured as SDHC0_D2 */ PORT_SetPinMux(PORTE, 5U, kPORT_MuxAlt4); + PORTE->PCR[5] = ((PORTE->PCR[5] & + /* Mask bits to zero which are setting */ + (~(PORT_PCR_PS_MASK | PORT_PCR_PE_MASK | PORT_PCR_ISF_MASK))) + + /* Pull Select: Internal pullup resistor is enabled on the corresponding pin, if the + * corresponding Port Pull Enable Register field is set. */ + | (uint32_t)(kPORT_PullUp)); + SIM->SOPT2 = ((SIM->SOPT2 & /* Mask bits to zero which are setting */ (~(SIM_SOPT2_RMIISRC_MASK))) diff --git a/include/ffconf.h b/include/ffconf.h index 4a0cba8..bee39ff 100644 --- a/include/ffconf.h +++ b/include/ffconf.h @@ -153,7 +153,7 @@ / on character encoding. When LFN is not enabled, these options have no effect. */ -#define FF_FS_RPATH 0 +#define FF_FS_RPATH 1 /* This option configures support for relative path. / / 0: Disable relative path and remove related functions. diff --git a/lib/LwIP/port/include/lwipopts.h b/lib/LwIP/port/include/lwipopts.h index edbfa97..11d7c5d 100644 --- a/lib/LwIP/port/include/lwipopts.h +++ b/lib/LwIP/port/include/lwipopts.h @@ -107,6 +107,7 @@ #define TCP_FR_DEBUG LWIP_DBG_OFF #define TCP_QLEN_DEBUG LWIP_DBG_OFF #define TCP_RST_DEBUG LWIP_DBG_OFF +#define HTTPD_DEBUG LWIP_DBG_ON #endif #define LWIP_DBG_TYPES_ON (LWIP_DBG_ON|LWIP_DBG_TRACE|LWIP_DBG_STATE|LWIP_DBG_FRESH|LWIP_DBG_HALT) @@ -263,6 +264,11 @@ a lot of data that needs to be copied, this should be set high. */ /* ---------- RAW options ---------- */ #define LWIP_RAW 1 +/* ---------- HTTPD options -------- */ +#define LWIP_HTTPD_CUSTOM_FILES 1 +#define LWIP_HTTPD_DYNAMIC_FILE_READ 1 +#define LWIP_HTTPD_FILE_EXTENSION 1 +#define LWIP_HTTPD_DYNAMIC_HEADERS 1 /* ---------- Statistics options ---------- */ @@ -330,7 +336,7 @@ void lwip_platform_assert(const char *msg, int line, const char *file); /* FreeRTOS related settings */ #define TCPIP_THREAD_PRIO 15 #define TCPIP_MBOX_SIZE 32 -#define TCPIP_THREAD_STACKSIZE 1024 +#define TCPIP_THREAD_STACKSIZE 2048 #endif /* LWIP_LWIPOPTS_H */ diff --git a/src/fatfs_diskio.c b/src/fatfs_diskio.c index e21c32f..8e8125f 100644 --- a/src/fatfs_diskio.c +++ b/src/fatfs_diskio.c @@ -132,7 +132,9 @@ DSTATUS disk_initialize(BYTE pdrv) g_sd.host.base = SD_HOST_BASEADDR; g_sd.host.sourceClock_Hz = SD_HOST_CLK_FREQ; - if (kStatus_Success != SD_Init(&g_sd)) + volatile status_t ret = SD_Init(&g_sd); + + if (kStatus_Success != ret) { SD_Deinit(&g_sd); memset(&g_sd, 0U, sizeof(g_sd)); diff --git a/src/httpd_helpers.c b/src/httpd_helpers.c new file mode 100644 index 0000000..52eb090 --- /dev/null +++ b/src/httpd_helpers.c @@ -0,0 +1,73 @@ +#include "FreeRTOS.h" +#include "ff.h" +#include "lwip/apps/fs.h" +#include "task.h" + +#define ROOT_PATH "0:/WEBROOT" + +int fs_open_custom(struct fs_file *file, const char *name) { + FIL *f = pvPortMalloc(sizeof(FIL)); + if (f == NULL) { + goto err_out; + } + + file->pextension = f; + + char *pathname = pvPortMalloc(255); + if (pathname == NULL) { + goto err_out_f; + } + + snprintf(pathname, 255, ROOT_PATH "%s", name); + + if (f_open(f, _T(pathname), FA_READ) != FR_OK) { + goto err_out_p; + } + + FILINFO finfo; + if(f_stat(_T(pathname), &finfo) != FR_OK) { + goto err_out_p; + } + + file->len = finfo.fsize; + + return 1; + +err_out_p: + vPortFree(pathname); +err_out_f: + vPortFree(f); +err_out: + return 0; +} + +void fs_close_custom(struct fs_file *file) { + FIL *f = file->pextension; + + if (f == NULL) { + return; + } + + f_close(f); + + vPortFree(f); +} + +int fs_read_custom(struct fs_file *file, char *buffer, int count) { + FIL *f = file->pextension; + + if (f == NULL) { + return 0; + } + + unsigned int actual_read = 0U; + + FRESULT ret = f_read(f, buffer, count, &actual_read); + if (ret != FR_OK) { + return 0; + } + + file->index += actual_read; + + return actual_read; +} \ No newline at end of file diff --git a/src/ip_stack_helpers.c b/src/ip_stack_helpers.c index 9c90584..ac81e49 100644 --- a/src/ip_stack_helpers.c +++ b/src/ip_stack_helpers.c @@ -89,8 +89,6 @@ static void ip_stack_enable_sntp(void) { void ip_stack_setup(void) { ip4_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw; - SYSMPU_Enable(SYSMPU, false); - tcpip_init(NULL, NULL); /* Initialize netif interface */ diff --git a/src/main.c b/src/main.c index bf5244d..fbde210 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,8 @@ #include "mbedtls/sha256.h" #include "mbedtls/sha512.h" +FATFS g_fs; /* File system object */ + static void vTaskHello(void *pvParameters); static void mtls_selftests(int verbose); @@ -39,9 +41,11 @@ int main(void) { BOARD_BootClockRUN(); BOARD_InitBootPeripherals(); - BOARD_InitDebugConsole(); + BOARD_DisableSYSMPU(); BOARD_EnableRTC(); + BOARD_InitDebugConsole(); + print_hardware(); sram_test(); @@ -57,11 +61,7 @@ int main(void) { } static void vTaskHello(void *pvParameters) { - FATFS fs; /* File system object */ - - - - if (f_mount(&fs, "0:/", 0U)) { + if (f_mount(&g_fs, "0:/", 1U)) { PRINTF("Mount volume failed.\r\n"); vTaskDelete(NULL); } diff --git a/src/sdhc_helpers.c b/src/sdhc_host.c similarity index 99% rename from src/sdhc_helpers.c rename to src/sdhc_host.c index c728234..62f10b8 100644 --- a/src/sdhc_helpers.c +++ b/src/sdhc_host.c @@ -32,7 +32,6 @@ #include "board.h" #include "fsl_host.h" #include "fsl_port.h" -#include "fsl_sysmpu.h" #include "semphr.h" #include "task.h" @@ -144,7 +143,6 @@ status_t HOST_Init(void *host) { sdhc_host_t *sdhcHost = (sdhc_host_t *)host; NVIC_SetPriority(SDHC_IRQn, 6); - SYSMPU_Enable(SYSMPU, false); /* Initializes SDHC. */ sdhcHost->config.cardDetectDat3 = true;