Apollo mcu的flash写入函数am_hal_flash_program_info异常

参考系统给的样例,封装了写入函数:

int usr_write_config_to_flash() {
  int ret;
  U32 offset = 0;
  // do {
  // 写入前要先擦除块
  info_block_erase();

  // 写入配置数据
  memset(flash_write_buf, 0, sizeof(flash_write_buf));
  memcpy(flash_write_buf, &sys_config, sizeof(sys_config));
  offset = 0;

  ret = am_hal_flash_program_info(AM_HAL_FLASH_PROGRAM_KEY,
                                  0, // we are only supporting INFO on instance 0.
                                  flash_write_buf,
                                  offset / 4,                    // offset
                                  (sizeof(sys_config) + 3) / 4); // num words
  //} while (ret);

  if (ret) {
    log(ERR, "am_hal_flash_program_info at offset 0x%08x i32ReturnCode = 0x%x.\n", offset, ret);
    return ERROR;
  }
  log(RUN, "flash write success\r\n");
  return SUCCESS;
}

系统的样例是正常的,因为没有别的业务逻辑,我的代码在开机执行也是正常的,但是在业务中调用 am_hal_flash_program_info 函数时就会随机失败,开始一直怀疑是地址区段的问题,各种尝试都不行,后来看函数的原型,发现下面的文字说明,才恍然一惊:

 

看到蓝色标识的部分,才想到是不是被系统的定时器中断影响了,因为系统起来后开启了很多定时器中断,于是在擦除和写入的逻辑区间关闭了中断,于是就没有再发现失败了。

浪费了半天时间,惭愧,特此随记,以示教训:在没有相关文档的情况下,要仔细看函数原型的任何一行说明文字

VSC下clang-format的代码换行宽度设置

遇到的问题:clang-format  启动自动格式化后,每次自动换行的宽度太窄,导致大量的自动换行,影响代码美观。

解决:

以默认的.clang-format 配置文件为模板,修改ColumnLimit参数:

Language: Cpp
ColumnLimit: 120

最后将.clang-format  文件放到工程的根目录下,重新开启VSC,生效。
其中 Language的设置如下:

Language (LanguageKind)
Language, this format style is targeted at.

Possible values:

LK_None (in configuration: None) Do not use.
LK_Cpp (in configuration: Cpp) Should be used for C, C++.
LK_Java (in configuration: Java) Should be used for Java.
LK_JavaScript (in configuration: JavaScript) Should be used for JavaScript.
LK_ObjC (in configuration: ObjC) Should be used for Objective-C, Objective-C++.
LK_Proto (in configuration: Proto) Should be used for Protocol Buffers (https://developers.google.com/protocol-buffers/).
LK_TableGen (in configuration: TableGen) Should be used for TableGen code.
LK_TextProto (in configuration: TextProto) Should be used for Protocol Buffer messages in text format (https://developers.google.com/protocol-buffers/).

相关的选项含义可以参考这里:http://releases.llvm.org/3.6.0/tools/clang/docs/ClangFormatStyleOptions.html