Chinaunix首页 | 论坛 | 博客
  • 博客访问: 63153
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 13:33
个人简介

业精于勤,荒于嬉;行成于思,毁于随。

文章分类

全部博文(14)

分类: 嵌入式

2017-11-21 09:48:20

移植文件 ef_port.c

点击(此处)折叠或打开

  1. /*
  2.  * This file is part of the EasyFlash Library.
  3.  *
  4.  * Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining
  7.  * a copy of this software and associated documentation files (the
  8.  * 'Software'), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sublicense, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be
  15.  * included in all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20.  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21.  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  *
  25.  * Function: Portable interface for each platform.
  26.  * Created on: 2015-01-16
  27.  */

  28. #include <easyflash.h>
  29. #include <stdarg.h>

  30. /* default environment variables set for user */
  31. static const ef_env default_env_set[] = {

  32. };

  33. /**
  34.  * Flash port for hardware initialize.
  35.  *
  36.  * @param default_env default ENV set for user
  37.  * @param default_env_size default ENV size
  38.  *
  39.  * @return result
  40.  */
  41. EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size) {
  42.     EfErrCode result = EF_NO_ERR;

  43.     *default_env = default_env_set;
  44.     *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);

  45.     return result;
  46. }

  47. /**
  48.  * Read data from flash.
  49.  * @note This operation's units is word.
  50.  *
  51.  * @param addr flash address
  52.  * @param buf buffer to store read data
  53.  * @param size read bytes size
  54.  *
  55.  * @return result
  56.  */
  57. EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size) {
  58.     EfErrCode result = EF_NO_ERR;

  59.     EF_ASSERT(size % 4 == 0);

  60.     /* You can add your code under here. */

  61.     return result;
  62. }

  63. /**
  64.  * Erase data on flash.
  65.  * @note This operation is irreversible.
  66.  * @note This operation's units is different which on many chips.
  67.  *
  68.  * @param addr flash address
  69.  * @param size erase bytes size
  70.  *
  71.  * @return result
  72.  */
  73. EfErrCode ef_port_erase(uint32_t addr, size_t size) {
  74.     EfErrCode result = EF_NO_ERR;

  75.     /* make sure the start address is a multiple of EF_ERASE_MIN_SIZE */
  76.     EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);

  77.     /* You can add your code under here. */

  78.     return result;
  79. }
  80. /**
  81.  * Write data to flash.
  82.  * @note This operation's units is word.
  83.  * @note This operation must after erase. @see flash_erase.
  84.  *
  85.  * @param addr flash address
  86.  * @param buf the write data buffer
  87.  * @param size write bytes size
  88.  *
  89.  * @return result
  90.  */
  91. EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size) {
  92.     EfErrCode result = EF_NO_ERR;

  93.     EF_ASSERT(size % 4 == 0);
  94.     
  95.     /* You can add your code under here. */

  96.     return result;
  97. }

  98. /**
  99.  * lock the ENV ram cache
  100.  */
  101. void ef_port_env_lock(void) {
  102.     
  103.     /* You can add your code under here. */
  104.     
  105. }

  106. /**
  107.  * unlock the ENV ram cache
  108.  */
  109. void ef_port_env_unlock(void) {
  110.     
  111.     /* You can add your code under here. */
  112.     
  113. }


  114. /**
  115.  * This function is print flash debug info.
  116.  *
  117.  * @param file the file which has call this function
  118.  * @param line the line number which has call this function
  119.  * @param format output format
  120.  * @param ... args
  121.  *
  122.  */
  123. void ef_log_debug(const char *file, const long line, const char *format, ...) {

  124. #ifdef PRINT_DEBUG

  125.     va_list args;

  126.     /* args point to the first variable parameter */
  127.     va_start(args, format);

  128.     /* You can add your code under here. */
  129.     
  130.     va_end(args);

  131. #endif

  132. }

  133. /**
  134.  * This function is print flash routine info.
  135.  *
  136.  * @param format output format
  137.  * @param ... args
  138.  */
  139. void ef_log_info(const char *format, ...) {
  140.     va_list args;

  141.     /* args point to the first variable parameter */
  142.     va_start(args, format);

  143.     /* You can add your code under here. */
  144.     
  145.     va_end(args);
  146. }
  147. /**
  148.  * This function is print flash non-package info.
  149.  *
  150.  * @param format output format
  151.  * @param ... args
  152.  */
  153. void ef_print(const char *format, ...) {
  154.     va_list args;

  155.     /* args point to the first variable parameter */
  156.     va_start(args, format);

  157.     /* You can add your code under here. */
  158.     
  159.     va_end(args);
  160. }

    所谓的移植,是为了适应特殊的需求,而移植接口,往往是把特殊进行一般化,移植就是要把一般化的接口根据实际的情况来进行实例化。移植接口有点类似于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) |
给主人留下些什么吧!~~