[bsp][imxrt] add 'fal' 'easyflash' port files

This commit is contained in:
guozhanxin 2018-12-29 19:06:25 +08:00
parent 06c21e7bf1
commit 1d35defcb8
6 changed files with 397 additions and 0 deletions

View File

@ -10,6 +10,17 @@ board.c
CPPPATH = [cwd]
CPPDEFINES = []
if GetDepend('PKG_USING_EASYFLASH'):
src += ['ports/ef_fal_port.c']
if GetDepend('PKG_USING_FAL'):
src += ['ports/fal_flexspi_nor_flash_port.c']
if GetDepend('PKG_USING_FAL') and GetDepend('PKG_USING_EASYFLASH'):
src += ['ports/fal_flash_init.c', 'ports/ef_update.c']
CPPPATH += [cwd + '/ports']
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES=CPPDEFINES)
Return('group')

View File

@ -0,0 +1,213 @@
/*
* This file is part of the EasyFlash Library.
*
* Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Function: Portable interface for FAL (Flash Abstraction Layer) partition.
* Created on: 2018-05-19
*/
#include <easyflash.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <rthw.h>
#include <rtthread.h>
#include <fal.h>
/* EasyFlash partition name on FAL partition table */
#define FAL_EF_PART_NAME "env"
/* default ENV set for user */
static const ef_env default_env_set[] = {
{"stay_in_bootloader","0"},
{"check_upgrade","0"},
{"bootdelay","1"},
};
static char log_buf[RT_CONSOLEBUF_SIZE];
static struct rt_semaphore env_cache_lock;
static const struct fal_partition *part = NULL;
/**
* Flash port for hardware initialize.
*
* @param default_env default ENV set for user
* @param default_env_size default ENV size
*
* @return result
*/
EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size) {
EfErrCode result = EF_NO_ERR;
*default_env = default_env_set;
*default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);
rt_sem_init(&env_cache_lock, "env lock", 1, RT_IPC_FLAG_PRIO);
part = fal_partition_find(FAL_EF_PART_NAME);
EF_ASSERT(part);
return result;
}
/**
* Read data from flash.
* @note This operation's units is word.
*
* @param addr flash address
* @param buf buffer to store read data
* @param size read bytes size
*
* @return result
*/
EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size) {
EfErrCode result = EF_NO_ERR;
EF_ASSERT(size % 4 == 0);
fal_partition_read(part, addr, (uint8_t *)buf, size);
return result;
}
/**
* Erase data on flash.
* @note This operation is irreversible.
* @note This operation's units is different which on many chips.
*
* @param addr flash address
* @param size erase bytes size
*
* @return result
*/
EfErrCode ef_port_erase(uint32_t addr, size_t size) {
EfErrCode result = EF_NO_ERR;
/* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
if (fal_partition_erase(part, addr, size) < 0)
{
result = EF_ERASE_ERR;
}
return result;
}
/**
* Write data to flash.
* @note This operation's units is word.
* @note This operation must after erase. @see flash_erase.
*
* @param addr flash address
* @param buf the write data buffer
* @param size write bytes size
*
* @return result
*/
EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size) {
EfErrCode result = EF_NO_ERR;
EF_ASSERT(size % 4 == 0);
if (fal_partition_write(part, addr, (uint8_t *)buf, size) < 0)
{
result = EF_WRITE_ERR;
}
return result;
}
/**
* lock the ENV ram cache
*/
void ef_port_env_lock(void) {
rt_sem_take(&env_cache_lock, RT_WAITING_FOREVER);
}
/**
* unlock the ENV ram cache
*/
void ef_port_env_unlock(void) {
rt_sem_release(&env_cache_lock);
}
/**
* This function is print flash debug info.
*
* @param file the file which has call this function
* @param line the line number which has call this function
* @param format output format
* @param ... args
*
*/
void ef_log_debug(const char *file, const long line, const char *format, ...) {
#ifdef PRINT_DEBUG
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
ef_print("[Flash] (%s:%ld) ", file, line);
/* must use vprintf to print */
rt_vsprintf(log_buf, format, args);
ef_print("%s", log_buf);
va_end(args);
#endif
}
/**
* This function is print flash routine info.
*
* @param format output format
* @param ... args
*/
void ef_log_info(const char *format, ...) {
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
ef_print("[Flash] ");
/* must use vprintf to print */
rt_vsprintf(log_buf, format, args);
ef_print("%s", log_buf);
va_end(args);
}
/**
* This function is print flash non-package info.
*
* @param format output format
* @param ... args
*/
void ef_print(const char *format, ...) {
va_list args;
/* args point to the first variable parameter */
va_start(args, format);
/* must use vprintf to print */
rt_vsprintf(log_buf, format, args);
rt_kprintf("%s", log_buf);
va_end(args);
}

View File

@ -0,0 +1,29 @@
#include <easyflash.h>
#include <rtthread.h>
#include <fsl_wdog.h>
void reset(void)
{
SCB->AIRCR = 0x05fa0000 | 0x04UL;
while (1)
{
}
}
MSH_CMD_EXPORT(reset, reset system.);
#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) && defined(EF_USING_ENV)
#include <finsh.h>
#if defined(EF_USING_ENV)
static void update(uint8_t argc, char **argv)
{
ef_set_env("check_upgrade", "1");
ef_save_env();
reset();
}
MSH_CMD_EXPORT(update, reset and check update.);
#endif /* defined(EF_USING_ENV) */
#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */

View File

@ -0,0 +1,52 @@
/*
* File : fal_cfg.h
* COPYRIGHT (C) 2012-2018, Shanghai Real-Thread Technology Co., Ltd
*
* Change Logs:
* Date Author Notes
* 2018-05-17 armink the first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <rtconfig.h>
#include <board.h>
/* ===================== Flash device Configuration ========================= */
extern struct fal_flash_dev nor_flash0;
#define RT_FAL_BL_PART_LEN (256*1024)
#define RT_FAL_ENV_PART_LEN (1*1024)
#define RT_FAL_PT_PART_LEN (1*1024)
#define RT_FAL_APP_PART_LEN (1*1024*1024)
#define RT_FAL_DL_PART_LEN (1*1024*1024)
#define RT_FAL_FLASH_BASE 0
#define RT_FAL_BL_PART_OFFSET RT_FAL_FLASH_BASE
#define RT_FAL_ENV_PART_OFFSET (RT_FAL_BL_PART_OFFSET + RT_FAL_BL_PART_LEN)
#define RT_FAL_PT_PART_OFFSET (RT_FAL_ENV_PART_OFFSET + RT_FAL_ENV_PART_LEN)
#define RT_FAL_APP_PART_OFFSET (RT_FAL_PT_PART_OFFSET + RT_FAL_PT_PART_LEN)
#define RT_FAL_DL_PART_OFFSET (RT_FAL_APP_PART_OFFSET + RT_FAL_APP_PART_LEN)
#define RT_FAL_FS_PART_OFFSET (RT_FAL_DL_PART_OFFSET + RT_FAL_DL_PART_LEN)
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&nor_flash0, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WROD, "bl", "norflash0", RT_FAL_BL_PART_OFFSET, RT_FAL_BL_PART_LEN, 0}, \
{FAL_PART_MAGIC_WROD, "env", "norflash0", RT_FAL_ENV_PART_OFFSET, RT_FAL_ENV_PART_LEN, 0}, \
{FAL_PART_MAGIC_WROD, "pt", "norflash0", RT_FAL_PT_PART_OFFSET, RT_FAL_PT_PART_LEN, 0}, \
{FAL_PART_MAGIC_WROD, "app", "norflash0", RT_FAL_APP_PART_OFFSET, RT_FAL_APP_PART_LEN, 0}, \
{FAL_PART_MAGIC_WROD, "download", "norflash0", RT_FAL_DL_PART_OFFSET, RT_FAL_DL_PART_LEN, 0}, \
{FAL_PART_MAGIC_WROD, "fs", "norflash0", RT_FAL_FS_PART_OFFSET, 0, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */

View File

@ -0,0 +1,18 @@
#include <rtthread.h>
#include <fal.h>
#include <easyflash.h>
#define FAL_FS_PART_NAME "fs"
#define FAL_DOWNLOAD_PART_NAME "download"
int rt_fal_flash_init(void)
{
fal_init();
easyflash_init();
fal_blk_device_create(FAL_FS_PART_NAME);
fal_char_device_create(FAL_DOWNLOAD_PART_NAME);
return 0;
}
INIT_DEVICE_EXPORT(rt_fal_flash_init);

View File

@ -0,0 +1,74 @@
/*
* File : rt_ota_flash_sfud_port.c
* COPYRIGHT (C) 2012-2018, Shanghai Real-Thread Technology Co., Ltd
*
* Change Logs:
* Date Author Notes
* 2018-01-26 armink the first version
*/
#include <rtthread.h>
#include <fal.h>
#include <drv_flexspi.h>
#include <rthw.h>
static int read(long offset, uint8_t *buf, size_t size)
{
memcpy(buf,(const void *)(FLEXSPI_AMBA_BASE+offset),size);
return size;
}
static int write(long offset, const uint8_t *buf, size_t size)
{
static char write_buffer[FLASH_PAGE_SIZE];
size_t writen_size = 0;
rt_uint32_t level;
level = rt_hw_interrupt_disable();
while(size)
{
if(size >= FLASH_PAGE_SIZE)
{
flexspi_nor_flash_page_program(FLEXSPI,offset,(const unsigned int *)buf);
size -= FLASH_PAGE_SIZE;
writen_size += FLASH_PAGE_SIZE;
}
else
{
memcpy(write_buffer,(const void *)(FLEXSPI_AMBA_BASE+offset),FLASH_PAGE_SIZE);
memcpy(write_buffer,buf,size);
flexspi_nor_flash_page_program(FLEXSPI,offset,(const unsigned int *)write_buffer);
writen_size += size;
size = 0;
}
offset += FLASH_PAGE_SIZE;
buf += FLASH_PAGE_SIZE;
}
rt_hw_interrupt_enable(level);
return writen_size;
}
static int erase(long offset, size_t size)
{
size_t erase_size = size;
rt_uint32_t level;
level = rt_hw_interrupt_disable();
size = offset;
offset = (offset/FLEXSPI_NOR_SECTOR_SIZE)*FLEXSPI_NOR_SECTOR_SIZE;
size = erase_size + size - offset;
while(size)
{
flexspi_nor_flash_erase_sector(FLEXSPI,offset);
if(size >= FLEXSPI_NOR_SECTOR_SIZE)
{
size -= FLEXSPI_NOR_SECTOR_SIZE;
}
else
{
size = 0;
}
offset += FLEXSPI_NOR_SECTOR_SIZE;
}
rt_hw_interrupt_enable(level);
return erase_size;
}
struct fal_flash_dev nor_flash0 = { "norflash0", 0, FLASH_SIZE*1024, FLEXSPI_NOR_SECTOR_SIZE, {NULL, read, write, erase} };