js_thumb bannerPic
Location:
Homepage
/
/
/
Implementation of customized OTA service on ING918xx

Implementation of customized OTA service on ING918xx

1. Summary

This article introduces the migration and implementation of a customer private OTA service process on ING918xx. Please refer to the Link for INGCHIPS OTA Service reference FOTA quick demo.

2. ING918xx OTA method

1. Download the new program (Application 2 in the following figure) to the free space of Flash.

2. Provide the starting address of the new program.

3. Restart, and the BootLoader will automatically move and complete the update.

OTA memory layout

3. Implementation

Create ota_private_service.c

3.1 Prepare the status structure

// 包含必须的头文件

#include <stdio.h>

#include <stdint.h>

#include <string.h>

#include "ingsoc.h"

#include "platform_api.h"

#include "rom_tools.h"

#include "eflash.h"

#include "ota_service.h"

 
// 创建状态记录信息结构体
#define PAGE_SIZE (8192)//static size for one flash page

typedef enum
{
  OTA_SERVICE_DISABLED  = 0,
  OTA_SERVICE_START     = (1 << 0),
  OTA_SERVICE_END       = (1 << 1)
} ota_service_flag_e;

typedef struct
{
  ota_ver_t             ota_ver;//place to save app version
  ota_service_flag_e    ota_service_flag;//start/end

  // static data from controller
  uint32_t    ota_load_addr;//load address of new app
  uint32_t    ota_flash_base_addr;//flash address to save ota data
  uint32_t    ota_file_size;//total size of new app bin

  // dynamic variables to hold downloading progess
  uint32_t                    ota_curr_page_addr;
  uint32_t                    ota_curr_page_size;
  uint8_t                     page_buffer[PAGE_SIZE];

  uint32_t    ota_total_size;//size that has been program to flash
} ota_service_data_s;

ota_service_data_s ota_service_data =
{
  .ota_service_flag = OTA_SERVICE_DISABLED,
  .ota_ver = { .app = {.major = 0, .minor = 0, .patch = 0} },
  .ota_curr_page_addr = 0,
  .ota_total_size = 0
};

3.2 Create the initialization interface

Three information needed before download:

1.load addr:start address of new program

2.flash addr:base address for new program before restart

3.file size: size of the new program

//需要注意的是所有地址都必须是word对齐的,文件大小不能超过flash的空闲区域大小。
uint8_t ota_service_init(uint32_t load_addr, uint32_t flash_addr, uint32_t file_size)
{
  if ((load_addr & 0x3) || (flash_addr & 0x3) || (!file_size) ||
      ((flash_addr + file_size) > 0x84000))
  {
      return 0;
  }

  ota_service_data.ota_load_addr = load_addr;
  ota_service_data.ota_flash_base_addr = flash_addr;
  ota_service_data.ota_file_size = file_size;
  ota_service_data.ota_curr_page_addr = flash_addr;
  ota_service_data.ota_service_flag = OTA_SERVICE_START;
  return 1;
}

Return is 0 when initialization failed.

3.3 Create data interface

Prepare the data interface for OTA:

// 将接受到的数据暂时放在buffer中,满足一个page的大小才可以擦写到flash
#define OTA_SERVICE_SAVE_PAGE_DATA(data, len) \
{ \
    memcpy(ota_service_data.page_buffer + ota_service_data.ota_curr_page_size, data, len); \
    ota_service_data.ota_curr_page_size += len; \
}

 

 

// 将一个page的数据写到flash
#define OTA_SERVICE_FLUSH_PAGE_DATA \
{ \
    if(ota_service_data.ota_curr_page_size > 0) {\
    program_flash(ota_service_data.ota_curr_page_addr, ota_service_data.page_buffer, ota_service_data.ota_curr_page_size); \
    ota_service_data.ota_curr_page_addr += 0x2000; \
    ota_service_data.ota_curr_page_size = 0; }\
}
 
// 数据处理接口,全部OTA数据下载完成后,返回1,否则返回0
uint8_t ota_service_handle(const uint8_t* data, uint16_t len)
{
  uint8_t complete = 0;
  uint32_t part1_size = 0;
  uint32_t part2_size = 0;

  if(ota_service_data.ota_service_flag != OTA_SERVICE_START) return 0;

  if((ota_service_data.ota_curr_page_size + len) > PAGE_SIZE)
  {
    part1_size = PAGE_SIZE - ota_service_data.ota_curr_page_size;
    part2_size = len - part1_size;
  }
  else
  {
    part1_size = len;
  }

  if(part1_size > 0)
  {
    OTA_SERVICE_SAVE_PAGE_DATA(data, part1_size);
  }

  if((part2_size > 0) || (ota_service_data.ota_curr_page_size == PAGE_SIZE))
  {
    OTA_SERVICE_FLUSH_PAGE_DATA;
  }

  if(part2_size > 0)
  {
    OTA_SERVICE_SAVE_PAGE_DATA(data+part1_size, part2_size);
  }

  ota_service_data.ota_total_size += len;
  if(ota_service_data.ota_total_size == ota_service_data.ota_file_size)
  {
    complete = 1;
    OTA_SERVICE_FLUSH_PAGE_DATA;
    ota_service_data.ota_service_flag = OTA_SERVICE_END;
  }

  return(complete);
}

3.4 Create restart interface
// 数据下载完成后,重启系统
void ota_service_reboot()
{
  uint8_t buffer[50];

  if(ota_service_data.ota_service_flag != OTA_SERVICE_END) return;

  ota_meta_t  *meta = (ota_meta_t *)(buffer + 0);
  meta->entry = 0;
  meta->blocks[0].dest = ota_service_data.ota_load_addr;
  meta->blocks[0].src = ota_service_data.ota_flash_base_addr;
  meta->blocks[0].size = ota_service_data.ota_total_size;

  program_fota_metadata(meta->entry, 1, meta->blocks);

  platform_reset();
}

4. Details

The call process is as the following:

1.find the API in profile.c, and specify the handle(eg. HANDLE_OF_OTA_DATA)for OTA.

 static int att_write_callback(……)
 {
     switch (att_handle)
     {
     case HANDLE_OF_OTA_DATA:
         // add your ota code
         return 0;

     default:
         return 1;
     }
 }

2.Host device start OTA cmd,and send the information (load_addr, flash_addr, file_size)

     case HANDLE_OF_DATA:
         ...
         if(start cmd)
         {
         ret = ota_service_init(load_addr, flash_addr, file_size);
         //check ret
         }
         return 0;

3.Host device send the data cmd to start OTA

     case HANDLE_OF_DATA:
         ...
         if(data cmd)
         {
         ret &= ota_service_handle(buffer, buffer_size);
         //check ret
         }
         return 0;

4.Host device send reboot cmd to finish the OTA

   case HANDLE_OF_DATA:
         ...
         if(reboot cmd) && (ret == 1)
         {
         ota_service_reboot();
         }
         return 0;

5. Potential Issues

1.The download program does not add application layer check, which can be implemented by designer, such as CRC.

2.There is no data check function (read the downloaded program to the main device for comparative check). The read part can be implemented with memcpy and can be added according to the request.

imgboxbg
Series application of Ingchips BLE5.1 SoC in Power Grid Industry
The ING918X series SoC of Ingchips Technology have officially passed the mass production certification of 《the Bluetooth communication specification of the State Grid Intelligent IoT Power Meter》 organized by the Chinese Academy of Electrical Sciences. At the same time, this is the first Chinese BLE SoC to obtain this certification.
INGCHIPS delivered automotive BLE5.1 SoC
ING91870CQ is a low power consumption SoC delivered by Ingchips Technology. After nine months of reliability testing, the chip finally obtained AEC-Q100 test certification.

Quick navigation

Contact Us

Tel:010-85160285

Email:service@ingchips.com

 

Resume delivery:hr@ingchips.com

Office

Beijing:Room 803, Building #3, Zijin Digital Park, Haidian District

Shanghai:Room 316, Tower A, Juxin Building, Xiangke Road #58

Shenzhen:Room 1009, Shuguang Building, Science Park, Nanshan District

公众号
桃芯

Wechat

Developer Website

Github

Zhihu

Copyright:Ingchips Technology Co., Ltd.  苏ICP备2022018764号-2       BY : xinnet