diff --git a/bsp/nxp/mcx/mcxa/Libraries/drivers/SConscript b/bsp/nxp/mcx/mcxa/Libraries/drivers/SConscript index a83afdec90..e4d5a6224e 100644 --- a/bsp/nxp/mcx/mcxa/Libraries/drivers/SConscript +++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/SConscript @@ -34,6 +34,9 @@ if GetDepend('BSP_USING_WDT'): if GetDepend('BSP_USING_PWM'): src += ['drv_pwm.c'] +if GetDepend('BSP_USING_FLASH'): + src += ['drv_chipflash.c'] + path = [cwd,cwd + '/config'] group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path) diff --git a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_chipflash.c b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_chipflash.c new file mode 100644 index 0000000000..4c0e7bb122 --- /dev/null +++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_chipflash.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2006-2024 RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-07-26 Ltbonewstart the first version + * + */ +#include "fsl_romapi.h" + +#include + +//#define DRV_DEBUG +#define LOG_TAG "drv.flash" +#include + +#define SECTOR_INDEX_FROM_END 2U /* start from the last 2 Sector */ + +struct mcx_mtd_chipflash +{ + struct rt_mtd_nor_device mtd_device; + struct rt_mutex flash_lock; + flash_config_t s_flashDriver; /* flash driver */ + uint32_t destAdrss; /* Address of the target location */ + uint32_t pflashBlockBase; /* 块基地址 */ + uint32_t pflashBlockSize; /* 块大小 */ + uint32_t pflashBlockCount; /* 块数量 */ + uint32_t pflashTotalSize; /* 总大小 */ + uint32_t pflashSectorSize; /* 扇区大小 */ + uint32_t PflashPageSize; /* 页大小 */ +}; + +struct mcx_mtd_chipflash mtd; + +/** + * device MTD nor 设备句柄 + * offset 偏移量 + * data 读取的数据 + * length 读取的数据长度 + */ +rt_ssize_t nxp_chipflash_read(struct rt_mtd_nor_device *device, rt_off_t offset, rt_uint8_t *data, rt_size_t length) +{ + rt_mutex_take(&mtd.flash_lock, RT_WAITING_FOREVER); + memcpy(data, ((const void *)(mtd.destAdrss + offset)), length); + rt_mutex_release(&mtd.flash_lock); + return length; +} + +/** + * device MTD nor 设备句柄 + * offset 偏移量 + * data 读取的数据 + * length 读取的数据长度 + */ +rt_ssize_t nxp_chipflash_write(struct rt_mtd_nor_device *device, rt_off_t offset, const rt_uint8_t *data, rt_size_t length) +{ + rt_mutex_take(&mtd.flash_lock, RT_WAITING_FOREVER); + int32_t status = FLASH_ProgramPhrase(&mtd.s_flashDriver, mtd.destAdrss + offset, (uint8_t *)data, length); + if (status != kStatus_Success) + { + length = 0; + } + rt_mutex_release(&mtd.flash_lock); + return length; +} + +/** + * device MTD nor 设备句柄 + * offset 偏移量 + * length 长度 + */ +rt_err_t nxp_chipflash_erase_block(struct rt_mtd_nor_device *device, rt_off_t offset, rt_size_t length) +{ + rt_mutex_take(&mtd.flash_lock, RT_WAITING_FOREVER); + FLASH_EraseSector(&mtd.s_flashDriver, mtd.destAdrss + offset, mtd.pflashSectorSize, kFLASH_ApiEraseKey); + rt_mutex_release(&mtd.flash_lock); + return RT_EOK; +} + +struct rt_mtd_nor_driver_ops mcx_mtd_chipflashops = +{ + RT_NULL, + nxp_chipflash_read, + nxp_chipflash_write, + nxp_chipflash_erase_block, +}; + +int rt_onchip_flash_init(void) +{ + rt_err_t result = RT_EOK; + + memset(&mtd.s_flashDriver, 0, sizeof(flash_config_t)); + if (FLASH_Init(&mtd.s_flashDriver) != kStatus_Success) + { + result = -RT_ERROR; + } + + /* 获取参数 */ + FLASH_GetProperty(&mtd.s_flashDriver, kFLASH_PropertyPflashBlockBaseAddr, &mtd.pflashBlockBase); + FLASH_GetProperty(&mtd.s_flashDriver, kFLASH_PropertyPflashSectorSize, &mtd.pflashSectorSize); + FLASH_GetProperty(&mtd.s_flashDriver, kFLASH_PropertyPflashTotalSize, &mtd.pflashTotalSize); + FLASH_GetProperty(&mtd.s_flashDriver, kFLASH_PropertyPflashPageSize, &mtd.PflashPageSize); + FLASH_GetProperty(&mtd.s_flashDriver, kFLASH_PropertyPflashBlockSize, &mtd.pflashBlockSize); + FLASH_GetProperty(&mtd.s_flashDriver, kFLASH_PropertyPflashBlockCount, &mtd.pflashBlockCount); + + LOG_D("flash_BlockBase: %d", mtd.pflashBlockBase); + LOG_D("flash_BlockCount: %d", mtd.pflashBlockCount); + LOG_D("flash_BlockSize: %d", mtd.pflashBlockSize); + LOG_D("flash_SectorSize: %d", mtd.pflashSectorSize); + LOG_D("flash_TotalSize: %d", mtd.pflashTotalSize); + LOG_D("flash_PageSize: %d", mtd.PflashPageSize); + + /* 设置要测试flash的基地址 */ + /* flash基地址+ flash总大小 - 数量*扇区大小 */ + mtd.destAdrss = mtd.pflashBlockBase + (mtd.pflashTotalSize - (SECTOR_INDEX_FROM_END) * mtd.pflashSectorSize); + LOG_D("flash_destAdrss: %#x", mtd.destAdrss); + + /* initialize mutex */ + if (rt_mutex_init(&mtd.flash_lock, "m_flash", RT_IPC_FLAG_PRIO) != RT_EOK) + { + rt_kprintf("init mflash lock mutex failed\n"); + return -RT_ERROR; + } + + mtd.mtd_device.block_start = 0; + mtd.mtd_device.block_end = (mtd.pflashTotalSize - mtd.destAdrss) / mtd.pflashSectorSize; + mtd.mtd_device.block_size = mtd.pflashSectorSize; + + /* set ops */ + mtd.mtd_device.ops = &mcx_mtd_chipflashops; + rt_mtd_nor_register_device("mflash", &(mtd.mtd_device)); + + return result; +} +INIT_DEVICE_EXPORT(rt_onchip_flash_init); diff --git a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_chipflash.h b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_chipflash.h new file mode 100644 index 0000000000..f0a9ddf860 --- /dev/null +++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_chipflash.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2006-2024 RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-07-26 Ltbonewstart the first version + * + */ +#ifndef __DRV_CHIPFLASH_H__ +#define __DRV_CHIPFLASH_H__ + +#include +#include + +extern rt_err_t rt_onchipflash_init(const char* mtd_name); + +#endif diff --git a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_log.h b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_log.h new file mode 100644 index 0000000000..fc8d3e99ec --- /dev/null +++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_log.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-15 SummerGift first version + */ + +/* + * NOTE: DO NOT include this file on the header file. + */ + +#ifndef LOG_TAG + #define DBG_TAG "drv" +#else + #define DBG_TAG LOG_TAG +#endif /* LOG_TAG */ + +#ifdef DRV_DEBUG + #define DBG_LVL DBG_LOG +#else + #define DBG_LVL DBG_INFO +#endif /* DRV_DEBUG */ + +#include diff --git a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_uart.c b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_uart.c index 2b2e0810ce..6088bae2e6 100644 --- a/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_uart.c +++ b/bsp/nxp/mcx/mcxa/Libraries/drivers/drv_uart.c @@ -1,24 +1,18 @@ -///* -// * Copyright (c) 2006-2024, RT-Thread Development Team -// * -// * SPDX-License-Identifier: Apache-2.0 -// * -// * Change Logs: -// * Date Author Notes -// * 2024-02-06 yandld The first version for MCX -// */ - -#include +/* + * Copyright (c) 2006-2024, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-02-06 yandld The first version for MCX + */ +#include #include "drv_uart.h" - #include "fsl_lpuart.h" -#include "fsl_common.h" #ifdef RT_USING_SERIAL - -#include - struct mcx_uart { struct rt_serial_device *serial; @@ -33,7 +27,6 @@ struct mcx_uart static void uart_isr(struct rt_serial_device *serial); - #if defined(BSP_USING_UART0) struct rt_serial_device serial0; @@ -43,8 +36,6 @@ void LPUART0_IRQHandler(void) } #endif - - static const struct mcx_uart uarts[] = { #ifdef BSP_USING_UART0 @@ -61,7 +52,6 @@ static const struct mcx_uart uarts[] = #endif }; - static rt_err_t mcx_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { struct mcx_uart *uart; @@ -118,7 +108,6 @@ static rt_err_t mcx_control(struct rt_serial_device *serial, int cmd, void *arg) break; } - return RT_EOK; } @@ -194,7 +183,5 @@ int rt_hw_uart_init(void) return 0; } - INIT_BOARD_EXPORT(rt_hw_uart_init); - #endif /*BSP_USING_SERIAL */ diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/applications/main.c b/bsp/nxp/mcx/mcxa/frdm-mcxa153/applications/main.c index d759debb56..afc44bf9de 100644 --- a/bsp/nxp/mcx/mcxa/frdm-mcxa153/applications/main.c +++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/applications/main.c @@ -11,7 +11,6 @@ * 2020-09-21 supperthomas fix the main.c * */ - #include #include "drv_pin.h" @@ -30,8 +29,7 @@ int main(void) #endif rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT); /* Set GPIO as Output */ - rt_kprintf("MCXA153 HelloWorld\r\n"); - + rt_kprintf("MCXA153 HelloWorld\n"); while (1) { @@ -41,5 +39,3 @@ int main(void) rt_thread_mdelay(500); /* Delay 500mS */ } } - -// end file diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig index a2424e2aa8..802cfd2746 100644 --- a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig +++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/Kconfig @@ -29,10 +29,8 @@ menu "On-chip Peripheral Drivers" config BSP_USING_UART0 bool "Enable LPUART as UART" default y - endif - menuconfig BSP_USING_I2C config BSP_USING_I2C bool "Enable I2C" @@ -82,30 +80,33 @@ menu "On-chip Peripheral Drivers" config BSP_USING_ADC0_CH8 bool "Enable ADC0 Channel8" default n - config BSP_USING_ADC0_CH13 bool "Enable ADC0 Channel13" default n - config BSP_USING_ADC0_CH26 bool "Enable ADC0 Channel26" default n - endif - config BSP_USING_SDIO - bool "Enable SDIO SD Card Interface" - select RT_USING_SDIO - select RT_USING_DFS - select RT_USING_DFS_ELMFAT - default y + config BSP_USING_FLASH + bool "Enable onchip driver" + select RT_USING_MTD_NOR + default n - config BSP_USING_RTC - bool "Enable RTC" - select RT_USING_RTC - default y + menuconfig BSP_USING_FS + bool "Enable File System" + select RT_USING_DFS + default n + + if BSP_USING_FS + config BSP_USING_FLASH_LITTLEFS + bool "Enable ONCHIP FLASH(littlefs)" + select BSP_USING_FLASH + select PKG_USING_LITTLEFS + default y + endif config BSP_USING_WDT bool "Enable WatchDog" @@ -165,7 +166,6 @@ menu "On-chip Peripheral Drivers" endif endmenu - menu "Board extended module Drivers" endmenu diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/SConscript b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/SConscript index 428f9aeb5f..7ab71bb3b9 100644 --- a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/SConscript +++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/SConscript @@ -9,6 +9,9 @@ MCUX_Config/board/clock_config.c MCUX_Config/board/pin_mux.c """) +if GetDepend(['BSP_USING_FS']): + src += Glob('drv_filesystem.c') + CPPPATH = [cwd, cwd + '/MCUX_Config/board'] CPPDEFINES = ['DEBUG', 'CPU_MCXA153VLH'] diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/drv_filesystem.c b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/drv_filesystem.c new file mode 100644 index 0000000000..73dd0f0572 --- /dev/null +++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/board/drv_filesystem.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-12-13 balanceTWK add sdcard port file + * 2021-05-10 Meco Man fix a bug that cannot use fatfs in the main thread at starting up + * 2021-07-28 Meco Man implement romfs as the root filesystem + */ +#include +#include +#include + +#define DBG_TAG "app.filesystem" +#define DBG_LVL DBG_INFO +#include + +static int littlefs_mount(void) +{ + if (rt_device_find("mflash") == RT_NULL) + { + LOG_E("mflash device not find!!"); + return -RT_EIO; + } + int ret = dfs_mount("mflash", "/", "lfs", 0, 0); + if (ret != 0) + { + LOG_E("mflash mount to '/' failed!"); + ret = dfs_mkfs("lfs", "mflash"); + if (ret != 0) + return ret; + ret = dfs_mount("mflash", "/", "lfs", 0, 0); + if (ret != 0) + return ret; + } + + LOG_D("mflash mount to '/' successed"); + + return RT_EOK; +} +INIT_APP_EXPORT(littlefs_mount); diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvoptx b/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvoptx index bf01c8b248..36e60397bb 100644 --- a/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvoptx +++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvoptx @@ -103,7 +103,7 @@ 1 0 0 - 13 + 14 @@ -114,9 +114,14 @@ - BIN\UL2V8M.DLL + BIN\CMSIS_AGDI_V8M.DLL + + 0 + CMSIS_AGDI_V8M + -X"Any" -UAny -O206 -S8 -C0 -P00000000 -N00("ARM CoreSight SW-DP") -D00(6BA02477) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP20 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO15 -FD20000000 -FC1000 -FN1 -FF0MCXA15X_128.FLM -FS00 -FL020000 -FP0($$Device:MCXA153VLH$devices\MCXA153\arm\MCXA15X_128.FLM) + 0 UL2V8M @@ -171,7 +176,7 @@ 1 0 2 - 5000000 + 10000000 diff --git a/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvprojx b/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvprojx index a12ad5a4ba..c0efd235b3 100644 --- a/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvprojx +++ b/bsp/nxp/mcx/mcxa/frdm-mcxa153/template.uvprojx @@ -52,7 +52,7 @@ rtthread 1 0 - 0 + 1 1 1 .\build\ @@ -138,7 +138,7 @@ 1 BIN\UL2V8M.DLL - + "" () @@ -314,7 +314,7 @@ 1 - 1 + 2 0 0 1