test
Fork of mbed-dev by
targets/TARGET_Realtek/TARGET_AMEBA/ota_api.c@174:e131a1973e81, 2017-09-15 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Sep 15 14:59:18 2017 +0100
- Revision:
- 174:e131a1973e81
This updates the lib to the mbed lib v 151
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 174:e131a1973e81 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 174:e131a1973e81 | 2 | * Copyright (c) 2013-2017 Realtek Semiconductor Corp. |
AnnaBridge | 174:e131a1973e81 | 3 | * |
AnnaBridge | 174:e131a1973e81 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 174:e131a1973e81 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 174:e131a1973e81 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 174:e131a1973e81 | 7 | * |
AnnaBridge | 174:e131a1973e81 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 174:e131a1973e81 | 9 | * |
AnnaBridge | 174:e131a1973e81 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 174:e131a1973e81 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 174:e131a1973e81 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 174:e131a1973e81 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 174:e131a1973e81 | 14 | * limitations under the License. |
AnnaBridge | 174:e131a1973e81 | 15 | */ |
AnnaBridge | 174:e131a1973e81 | 16 | #include <stdio.h> |
AnnaBridge | 174:e131a1973e81 | 17 | #include <string.h> |
AnnaBridge | 174:e131a1973e81 | 18 | |
AnnaBridge | 174:e131a1973e81 | 19 | #include "mbed_wait_api.h" |
AnnaBridge | 174:e131a1973e81 | 20 | |
AnnaBridge | 174:e131a1973e81 | 21 | #include "rtl8195a.h" |
AnnaBridge | 174:e131a1973e81 | 22 | #include "flash_ext.h" |
AnnaBridge | 174:e131a1973e81 | 23 | |
AnnaBridge | 174:e131a1973e81 | 24 | #define FLASH_TOP 0x200000 |
AnnaBridge | 174:e131a1973e81 | 25 | #define FLASH_SECTOR_SIZE 0x1000 |
AnnaBridge | 174:e131a1973e81 | 26 | #define FLASH_SECTOR_MASK ~(FLASH_SECTOR_SIZE - 1) |
AnnaBridge | 174:e131a1973e81 | 27 | #define OTA_REGION1 0x0b000 |
AnnaBridge | 174:e131a1973e81 | 28 | #define OTA_REGION2 0xc0000 |
AnnaBridge | 174:e131a1973e81 | 29 | #define TAG_OFS 0xc |
AnnaBridge | 174:e131a1973e81 | 30 | #define VER_OFS 0x10 |
AnnaBridge | 174:e131a1973e81 | 31 | |
AnnaBridge | 174:e131a1973e81 | 32 | #define TAG_DOWNLOAD 0x81950001 |
AnnaBridge | 174:e131a1973e81 | 33 | #define TAG_VERIFIED 0x81950003 |
AnnaBridge | 174:e131a1973e81 | 34 | |
AnnaBridge | 174:e131a1973e81 | 35 | static flash_t flash_obj; |
AnnaBridge | 174:e131a1973e81 | 36 | |
AnnaBridge | 174:e131a1973e81 | 37 | typedef struct imginfo_s { |
AnnaBridge | 174:e131a1973e81 | 38 | uint32_t base; |
AnnaBridge | 174:e131a1973e81 | 39 | uint32_t tag; |
AnnaBridge | 174:e131a1973e81 | 40 | uint64_t ver; |
AnnaBridge | 174:e131a1973e81 | 41 | } imginfo_t; |
AnnaBridge | 174:e131a1973e81 | 42 | |
AnnaBridge | 174:e131a1973e81 | 43 | |
AnnaBridge | 174:e131a1973e81 | 44 | void OTA_GetImageInfo(imginfo_t *info) |
AnnaBridge | 174:e131a1973e81 | 45 | { |
AnnaBridge | 174:e131a1973e81 | 46 | uint32_t ver_hi, ver_lo; |
AnnaBridge | 174:e131a1973e81 | 47 | |
AnnaBridge | 174:e131a1973e81 | 48 | flash_ext_read_word(&flash_obj, info->base + TAG_OFS, &info->tag); |
AnnaBridge | 174:e131a1973e81 | 49 | flash_ext_read_word(&flash_obj, info->base + VER_OFS, &ver_lo); |
AnnaBridge | 174:e131a1973e81 | 50 | flash_ext_read_word(&flash_obj, info->base + VER_OFS + 4, &ver_hi); |
AnnaBridge | 174:e131a1973e81 | 51 | |
AnnaBridge | 174:e131a1973e81 | 52 | if (info->tag == TAG_DOWNLOAD) { |
AnnaBridge | 174:e131a1973e81 | 53 | info->ver = ((uint64_t)ver_hi << 32) | (uint64_t) ver_lo; |
AnnaBridge | 174:e131a1973e81 | 54 | } else { |
AnnaBridge | 174:e131a1973e81 | 55 | info->ver = 0; |
AnnaBridge | 174:e131a1973e81 | 56 | } |
AnnaBridge | 174:e131a1973e81 | 57 | } |
AnnaBridge | 174:e131a1973e81 | 58 | |
AnnaBridge | 174:e131a1973e81 | 59 | uint32_t OTA_GetBase(void) |
AnnaBridge | 174:e131a1973e81 | 60 | { |
AnnaBridge | 174:e131a1973e81 | 61 | static uint32_t ota_base = 0; |
AnnaBridge | 174:e131a1973e81 | 62 | imginfo_t region1, region2; |
AnnaBridge | 174:e131a1973e81 | 63 | |
AnnaBridge | 174:e131a1973e81 | 64 | if (ota_base == OTA_REGION1 || ota_base == OTA_REGION2) { |
AnnaBridge | 174:e131a1973e81 | 65 | return ota_base; |
AnnaBridge | 174:e131a1973e81 | 66 | } |
AnnaBridge | 174:e131a1973e81 | 67 | |
AnnaBridge | 174:e131a1973e81 | 68 | region1.base = OTA_REGION1; |
AnnaBridge | 174:e131a1973e81 | 69 | region2.base = OTA_REGION2; |
AnnaBridge | 174:e131a1973e81 | 70 | |
AnnaBridge | 174:e131a1973e81 | 71 | OTA_GetImageInfo(®ion1); |
AnnaBridge | 174:e131a1973e81 | 72 | OTA_GetImageInfo(®ion2); |
AnnaBridge | 174:e131a1973e81 | 73 | |
AnnaBridge | 174:e131a1973e81 | 74 | if (region1.ver >= region2.ver) { |
AnnaBridge | 174:e131a1973e81 | 75 | ota_base = region2.base; |
AnnaBridge | 174:e131a1973e81 | 76 | } else { |
AnnaBridge | 174:e131a1973e81 | 77 | ota_base = region1.base; |
AnnaBridge | 174:e131a1973e81 | 78 | } |
AnnaBridge | 174:e131a1973e81 | 79 | return ota_base; |
AnnaBridge | 174:e131a1973e81 | 80 | } |
AnnaBridge | 174:e131a1973e81 | 81 | |
AnnaBridge | 174:e131a1973e81 | 82 | uint32_t OTA_MarkUpdateDone(void) |
AnnaBridge | 174:e131a1973e81 | 83 | { |
AnnaBridge | 174:e131a1973e81 | 84 | uint32_t addr = OTA_GetBase() + TAG_OFS; |
AnnaBridge | 174:e131a1973e81 | 85 | |
AnnaBridge | 174:e131a1973e81 | 86 | return flash_ext_write_word(&flash_obj, addr, TAG_DOWNLOAD); |
AnnaBridge | 174:e131a1973e81 | 87 | } |
AnnaBridge | 174:e131a1973e81 | 88 | |
AnnaBridge | 174:e131a1973e81 | 89 | uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data) |
AnnaBridge | 174:e131a1973e81 | 90 | { |
AnnaBridge | 174:e131a1973e81 | 91 | uint32_t addr, start, end, count, shift; |
AnnaBridge | 174:e131a1973e81 | 92 | uint8_t *pdata = data; |
AnnaBridge | 174:e131a1973e81 | 93 | uint8_t buf[FLASH_SECTOR_SIZE]; |
AnnaBridge | 174:e131a1973e81 | 94 | |
AnnaBridge | 174:e131a1973e81 | 95 | start = OTA_GetBase() + offset; |
AnnaBridge | 174:e131a1973e81 | 96 | end = start + len; |
AnnaBridge | 174:e131a1973e81 | 97 | |
AnnaBridge | 174:e131a1973e81 | 98 | if (data == NULL || start > FLASH_TOP || end > FLASH_TOP) { |
AnnaBridge | 174:e131a1973e81 | 99 | return 0; |
AnnaBridge | 174:e131a1973e81 | 100 | } |
AnnaBridge | 174:e131a1973e81 | 101 | |
AnnaBridge | 174:e131a1973e81 | 102 | addr = start & FLASH_SECTOR_MASK; |
AnnaBridge | 174:e131a1973e81 | 103 | if (addr != start) { |
AnnaBridge | 174:e131a1973e81 | 104 | shift = start - addr; |
AnnaBridge | 174:e131a1973e81 | 105 | count = MIN(FLASH_SECTOR_SIZE - shift, len); |
AnnaBridge | 174:e131a1973e81 | 106 | flash_ext_stream_read(&flash_obj, addr, shift, buf); |
AnnaBridge | 174:e131a1973e81 | 107 | memcpy((void *)(buf + shift), (void *)pdata, count); |
AnnaBridge | 174:e131a1973e81 | 108 | |
AnnaBridge | 174:e131a1973e81 | 109 | flash_ext_erase_sector(&flash_obj, addr); |
AnnaBridge | 174:e131a1973e81 | 110 | flash_ext_stream_write(&flash_obj, addr, FLASH_SECTOR_SIZE, buf); |
AnnaBridge | 174:e131a1973e81 | 111 | addr += FLASH_SECTOR_SIZE; |
AnnaBridge | 174:e131a1973e81 | 112 | pdata += count; |
AnnaBridge | 174:e131a1973e81 | 113 | } |
AnnaBridge | 174:e131a1973e81 | 114 | |
AnnaBridge | 174:e131a1973e81 | 115 | while (addr < end) { |
AnnaBridge | 174:e131a1973e81 | 116 | printf("OTA: update addr=0x%lx, len=%ld\r\n", addr, len); |
AnnaBridge | 174:e131a1973e81 | 117 | count = MIN(FLASH_SECTOR_SIZE, end - addr); |
AnnaBridge | 174:e131a1973e81 | 118 | flash_ext_erase_sector(&flash_obj, addr); |
AnnaBridge | 174:e131a1973e81 | 119 | flash_ext_stream_write(&flash_obj, addr, count, pdata); |
AnnaBridge | 174:e131a1973e81 | 120 | addr += FLASH_SECTOR_SIZE; |
AnnaBridge | 174:e131a1973e81 | 121 | pdata += count; |
AnnaBridge | 174:e131a1973e81 | 122 | } |
AnnaBridge | 174:e131a1973e81 | 123 | return len; |
AnnaBridge | 174:e131a1973e81 | 124 | } |
AnnaBridge | 174:e131a1973e81 | 125 | |
AnnaBridge | 174:e131a1973e81 | 126 | uint32_t OTA_ReadImage(uint32_t offset, uint32_t len, uint8_t *data) |
AnnaBridge | 174:e131a1973e81 | 127 | { |
AnnaBridge | 174:e131a1973e81 | 128 | uint32_t addr, endaddr; |
AnnaBridge | 174:e131a1973e81 | 129 | |
AnnaBridge | 174:e131a1973e81 | 130 | addr = OTA_GetBase() + offset; |
AnnaBridge | 174:e131a1973e81 | 131 | endaddr = addr + len; |
AnnaBridge | 174:e131a1973e81 | 132 | |
AnnaBridge | 174:e131a1973e81 | 133 | if (data == NULL || addr > FLASH_TOP || endaddr > FLASH_TOP) { |
AnnaBridge | 174:e131a1973e81 | 134 | return 0; |
AnnaBridge | 174:e131a1973e81 | 135 | } |
AnnaBridge | 174:e131a1973e81 | 136 | |
AnnaBridge | 174:e131a1973e81 | 137 | printf("OTA: read addr=0x%lx\r\n", addr); |
AnnaBridge | 174:e131a1973e81 | 138 | return flash_ext_stream_read(&flash_obj, addr, len, data); |
AnnaBridge | 174:e131a1973e81 | 139 | } |
AnnaBridge | 174:e131a1973e81 | 140 | |
AnnaBridge | 174:e131a1973e81 | 141 | void OTA_ResetTarget(void) |
AnnaBridge | 174:e131a1973e81 | 142 | { |
AnnaBridge | 174:e131a1973e81 | 143 | __RTK_CTRL_WRITE32(0x14, 0x00000021); |
AnnaBridge | 174:e131a1973e81 | 144 | wait(1); |
AnnaBridge | 174:e131a1973e81 | 145 | |
AnnaBridge | 174:e131a1973e81 | 146 | // write SCB->AIRCR |
AnnaBridge | 174:e131a1973e81 | 147 | HAL_WRITE32(0xE000ED00, 0x0C, |
AnnaBridge | 174:e131a1973e81 | 148 | (0x5FA << 16) | // VECTKEY |
AnnaBridge | 174:e131a1973e81 | 149 | (HAL_READ32(0xE000ED00, 0x0C) & (7 << 8)) | // PRIGROUP |
AnnaBridge | 174:e131a1973e81 | 150 | (1 << 2)); // SYSRESETREQ |
AnnaBridge | 174:e131a1973e81 | 151 | |
AnnaBridge | 174:e131a1973e81 | 152 | // not reached |
AnnaBridge | 174:e131a1973e81 | 153 | while (1); |
AnnaBridge | 174:e131a1973e81 | 154 | } |