移植文件 ef_port.c
-
/*
-
* 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 each platform.
-
* Created on: 2015-01-16
-
*/
-
-
#include <easyflash.h>
-
#include <stdarg.h>
-
-
/* default environment variables set for user */
-
static const ef_env default_env_set[] = {
-
-
};
-
-
/**
-
* 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]);
-
-
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);
-
-
/* You can add your code under here. */
-
-
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 EF_ERASE_MIN_SIZE */
-
EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
-
-
/* You can add your code under here. */
-
-
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);
-
-
/* You can add your code under here. */
-
-
return result;
-
}
-
-
/**
-
* lock the ENV ram cache
-
*/
-
void ef_port_env_lock(void) {
-
-
/* You can add your code under here. */
-
-
}
-
-
/**
-
* unlock the ENV ram cache
-
*/
-
void ef_port_env_unlock(void) {
-
-
/* You can add your code under here. */
-
-
}
-
-
-
/**
-
* 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);
-
-
/* You can add your code under here. */
-
-
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);
-
-
/* You can add your code under here. */
-
-
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);
-
-
/* You can add your code under here. */
-
-
va_end(args);
-
}
所谓的移植,是为了适应特殊的需求,而移植接口,往往是把特殊进行一般化,移植就是要把一般化的接口根据实际的情况来进行实例化。移植接口有点类似于C++里面的虚函数,我们在使用的时候要实现其函数。
该移植文件里面只有一个变量default_env_set 用来存储默认的用户环境变量,有9个移植接口函数:
1.ef_port_init //Flash port for hardware initialize. 硬件端口初始化
2.ef_port_read //Read data from flash. 从flash中读数据
3.ef_port_erase //Erase data on flash. 从flash中擦除数据
4.ef_port_write //ef_port_write 写数据到flash
5.ef_port_env_lock // lock the ENV ram cache 锁定环境缓存
6.ef_port_env_unlock //unlock the ENV ram cache 解锁环境缓存
7.ef_log_debug //This function is print flash debug info. 打印flash调试信息
8.ef_log_info //This function is print flash routine info. 打印flash程序信息
9.ef_print //This function is print flash non-package info. 打印flash非包信息
按传统操作flash的方式,对flash的硬件初始化是必不可少的,该部分对应于1函数;而flash的操作往往包括了读、写和擦除,该部分对应2、3和4函数;程序运行过程往往会有很多调试信息,在测试的时候打开调试选项,在发布的时候可以把调试选项关闭,该部分对应于7、8和9函数;在多线程中,往往需要对全局的变量或内存或设备采取防竞争的手段,在同一个时间点只允许一个或多个线程可以访问,避免数据出现不可预料的变化,函数5和6是对环境缓存的一种保护机制。
本文乃黎社林原创文章,由于水平有限,如有错误,请联系lishelin0505@163.com ,在此表示感谢!
阅读(3877) | 评论(0) | 转发(0) |