优化flash部分驱动+完善注释+格式化部分代码

This commit is contained in:
Rbb666 2024-07-31 18:25:19 +08:00 committed by Rbb666
parent b06843b91c
commit f4cb323919
11 changed files with 270 additions and 50 deletions

View File

@ -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)

View File

@ -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 <rtdevice.h>
//#define DRV_DEBUG
#define LOG_TAG "drv.flash"
#include <drv_log.h>
#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);

View File

@ -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 <rtthread.h>
#include <rtdevice.h>
extern rt_err_t rt_onchipflash_init(const char* mtd_name);
#endif

View File

@ -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 <rtdbg.h>

View File

@ -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 <rtthread.h>
/*
* 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 <rtdevice.h>
#include "drv_uart.h"
#include "fsl_lpuart.h"
#include "fsl_common.h"
#ifdef RT_USING_SERIAL
#include <rtdevice.h>
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 */

View File

@ -11,7 +11,6 @@
* 2020-09-21 supperthomas fix the main.c
*
*/
#include <rtdevice.h>
#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

View File

@ -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"
@ -83,29 +81,32 @@ menu "On-chip Peripheral Drivers"
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
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

View File

@ -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']

View File

@ -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 <rtthread.h>
#include <dfs_fs.h>
#include <dfs_file.h>
#define DBG_TAG "app.filesystem"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
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);

View File

@ -103,7 +103,7 @@
<bEvRecOn>1</bEvRecOn>
<bSchkAxf>0</bSchkAxf>
<bTchkAxf>0</bTchkAxf>
<nTsel>13</nTsel>
<nTsel>14</nTsel>
<sDll></sDll>
<sDllPa></sDllPa>
<sDlgDll></sDlgDll>
@ -114,9 +114,14 @@
<tDlgDll></tDlgDll>
<tDlgPa></tDlgPa>
<tIfile></tIfile>
<pMon>BIN\UL2V8M.DLL</pMon>
<pMon>BIN\CMSIS_AGDI_V8M.DLL</pMon>
</DebugOpt>
<TargetDriverDllRegistry>
<SetRegEntry>
<Number>0</Number>
<Key>CMSIS_AGDI_V8M</Key>
<Name>-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)</Name>
</SetRegEntry>
<SetRegEntry>
<Number>0</Number>
<Key>UL2V8M</Key>
@ -171,7 +176,7 @@
<EnableFlashSeq>1</EnableFlashSeq>
<EnableLog>0</EnableLog>
<Protocol>2</Protocol>
<DbgClock>5000000</DbgClock>
<DbgClock>10000000</DbgClock>
</DebugDescription>
</TargetOption>
</Target>

View File

@ -52,7 +52,7 @@
<OutputName>rtthread</OutputName>
<CreateExecutable>1</CreateExecutable>
<CreateLib>0</CreateLib>
<CreateHexFile>0</CreateHexFile>
<CreateHexFile>1</CreateHexFile>
<DebugInformation>1</DebugInformation>
<BrowseInformation>1</BrowseInformation>
<ListingPath>.\build\</ListingPath>
@ -138,7 +138,7 @@
</Flash1>
<bUseTDR>1</bUseTDR>
<Flash2>BIN\UL2V8M.DLL</Flash2>
<Flash3></Flash3>
<Flash3>"" ()</Flash3>
<Flash4></Flash4>
<pFcarmOut></pFcarmOut>
<pFcarmGrp></pFcarmGrp>
@ -314,7 +314,7 @@
</ArmAdsMisc>
<Cads>
<interw>1</interw>
<Optim>1</Optim>
<Optim>2</Optim>
<oTime>0</oTime>
<SplitLS>0</SplitLS>
<OneElfS>1</OneElfS>