Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 """
marcozecchini 0:9fca2b23d0ba 2 @copyright (c) 2012 ON Semiconductor. All rights reserved.
marcozecchini 0:9fca2b23d0ba 3 ON Semiconductor is supplying this software for use with ON Semiconductor
marcozecchini 0:9fca2b23d0ba 4 processor based microcontrollers only.
marcozecchini 0:9fca2b23d0ba 5 THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
marcozecchini 0:9fca2b23d0ba 6 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
marcozecchini 0:9fca2b23d0ba 7 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
marcozecchini 0:9fca2b23d0ba 8 ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
marcozecchini 0:9fca2b23d0ba 9 INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
marcozecchini 0:9fca2b23d0ba 10 """
marcozecchini 0:9fca2b23d0ba 11
marcozecchini 0:9fca2b23d0ba 12 from __future__ import absolute_import
marcozecchini 0:9fca2b23d0ba 13 from __future__ import print_function
marcozecchini 0:9fca2b23d0ba 14
marcozecchini 0:9fca2b23d0ba 15 import itertools
marcozecchini 0:9fca2b23d0ba 16 import binascii
marcozecchini 0:9fca2b23d0ba 17 import intelhex
marcozecchini 0:9fca2b23d0ba 18 from tools.config import Config
marcozecchini 0:9fca2b23d0ba 19
marcozecchini 0:9fca2b23d0ba 20 FIB_BASE = 0x2000
marcozecchini 0:9fca2b23d0ba 21 TRIM_BASE = 0x2800
marcozecchini 0:9fca2b23d0ba 22 FLASH_BASE = 0x3000
marcozecchini 0:9fca2b23d0ba 23 FLASHA_SIZE = 0x52000
marcozecchini 0:9fca2b23d0ba 24 FLASHB_BASE = 0x00102000
marcozecchini 0:9fca2b23d0ba 25 FLASHB_SIZE = 0x52000
marcozecchini 0:9fca2b23d0ba 26 FW_REV = 0x01000100
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28 def ranges(i):
marcozecchini 0:9fca2b23d0ba 29 for _, b in itertools.groupby(enumerate(i), lambda x_y: x_y[1] - x_y[0]):
marcozecchini 0:9fca2b23d0ba 30 b = list(b)
marcozecchini 0:9fca2b23d0ba 31 yield b[0][1], b[-1][1]
marcozecchini 0:9fca2b23d0ba 32
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34 def add_fib_at_start(arginput):
marcozecchini 0:9fca2b23d0ba 35 input_file = arginput + ".hex"
marcozecchini 0:9fca2b23d0ba 36 file_name_hex = arginput + ".hex"
marcozecchini 0:9fca2b23d0ba 37 file_name_bin = arginput + ".bin"
marcozecchini 0:9fca2b23d0ba 38
marcozecchini 0:9fca2b23d0ba 39 # Read in hex file
marcozecchini 0:9fca2b23d0ba 40 input_hex_file = intelhex.IntelHex()
marcozecchini 0:9fca2b23d0ba 41 input_hex_file.loadhex(input_file)
marcozecchini 0:9fca2b23d0ba 42 #set padding value to be returned when reading from unspecified address
marcozecchini 0:9fca2b23d0ba 43 input_hex_file.padding = 0xFF
marcozecchini 0:9fca2b23d0ba 44 # Create new hex file
marcozecchini 0:9fca2b23d0ba 45 output_hex_file = intelhex.IntelHex()
marcozecchini 0:9fca2b23d0ba 46
marcozecchini 0:9fca2b23d0ba 47 # Get the starting and ending address
marcozecchini 0:9fca2b23d0ba 48 addresses = input_hex_file.addresses()
marcozecchini 0:9fca2b23d0ba 49 addresses.sort()
marcozecchini 0:9fca2b23d0ba 50 start_end_pairs = list(ranges(addresses))
marcozecchini 0:9fca2b23d0ba 51 regions = len(start_end_pairs)
marcozecchini 0:9fca2b23d0ba 52
marcozecchini 0:9fca2b23d0ba 53 if regions == 1:
marcozecchini 0:9fca2b23d0ba 54 #single range indicating fits within first flash block (<320K)
marcozecchini 0:9fca2b23d0ba 55 start, end = start_end_pairs[0]
marcozecchini 0:9fca2b23d0ba 56 print("Memory start 0x%08X, end 0x%08X" % (start, end))
marcozecchini 0:9fca2b23d0ba 57 # Compute checksum over the range (don't include data at location of crc)
marcozecchini 0:9fca2b23d0ba 58 size = end - start + 1
marcozecchini 0:9fca2b23d0ba 59 data = input_hex_file.tobinarray(start=start, size=size)
marcozecchini 0:9fca2b23d0ba 60 crc32 = binascii.crc32(data) & 0xFFFFFFFF
marcozecchini 0:9fca2b23d0ba 61 else:
marcozecchini 0:9fca2b23d0ba 62 #multiple ranges indicating requires both flash blocks (>320K)
marcozecchini 0:9fca2b23d0ba 63 start, end = start_end_pairs[0]
marcozecchini 0:9fca2b23d0ba 64 start2, end2 = start_end_pairs[1]
marcozecchini 0:9fca2b23d0ba 65 print("Region 1: memory start 0x%08X, end 0x%08X" % (start, end))
marcozecchini 0:9fca2b23d0ba 66 print("Region 2: memory start 0x%08X, end 0x%08X" % (start2, end2))
marcozecchini 0:9fca2b23d0ba 67 # Compute checksum over the range (don't include data at location of crc)
marcozecchini 0:9fca2b23d0ba 68 # replace end with end of flash block A
marcozecchini 0:9fca2b23d0ba 69 end = FLASHA_SIZE - 1
marcozecchini 0:9fca2b23d0ba 70 size = end - start + 1
marcozecchini 0:9fca2b23d0ba 71 data = input_hex_file.tobinarray(start=start, size=size)
marcozecchini 0:9fca2b23d0ba 72
marcozecchini 0:9fca2b23d0ba 73 # replace start2 with base of flash block B
marcozecchini 0:9fca2b23d0ba 74 start2 = FLASHB_BASE
marcozecchini 0:9fca2b23d0ba 75 size2 = end2 - start2 + 1
marcozecchini 0:9fca2b23d0ba 76 data2 = input_hex_file.tobinarray(start=start2, size=size2)
marcozecchini 0:9fca2b23d0ba 77
marcozecchini 0:9fca2b23d0ba 78 #concatenate data and data2 arrays together
marcozecchini 0:9fca2b23d0ba 79 data.extend(data2)
marcozecchini 0:9fca2b23d0ba 80 crc32 = binascii.crc32(data) & 0xFFFFFFFF
marcozecchini 0:9fca2b23d0ba 81
marcozecchini 0:9fca2b23d0ba 82 #replace size with sum of two memory region sizes
marcozecchini 0:9fca2b23d0ba 83 size = size + size2
marcozecchini 0:9fca2b23d0ba 84
marcozecchini 0:9fca2b23d0ba 85 assert start >= FLASH_BASE, ("Error - start 0x%x less than begining of user\
marcozecchini 0:9fca2b23d0ba 86 flash area" %start)
marcozecchini 0:9fca2b23d0ba 87
marcozecchini 0:9fca2b23d0ba 88 assert regions <= 2, ("Error - more than 2 memory regions found")
marcozecchini 0:9fca2b23d0ba 89
marcozecchini 0:9fca2b23d0ba 90 fw_rev = FW_REV
marcozecchini 0:9fca2b23d0ba 91
marcozecchini 0:9fca2b23d0ba 92 checksum = (start + size + crc32 + fw_rev) & 0xFFFFFFFF
marcozecchini 0:9fca2b23d0ba 93
marcozecchini 0:9fca2b23d0ba 94 print("Writing FIB: base 0x%08X, size 0x%08X, crc32 0x%08X, fw rev 0x%08X,\
marcozecchini 0:9fca2b23d0ba 95 checksum 0x%08X" % (start, size, crc32, fw_rev, checksum))
marcozecchini 0:9fca2b23d0ba 96
marcozecchini 0:9fca2b23d0ba 97 #expected initial values used by daplink to validate that it is a valid bin
marcozecchini 0:9fca2b23d0ba 98 #file added as dummy values in this file because the fib area preceeds the
marcozecchini 0:9fca2b23d0ba 99 #application area the bootloader will ignore these dummy values
marcozecchini 0:9fca2b23d0ba 100 # 00 is stack pointer (RAM address)
marcozecchini 0:9fca2b23d0ba 101 # 04 is Reset vector (FLASH address)
marcozecchini 0:9fca2b23d0ba 102 # 08 NMI_Handler (FLASH address)
marcozecchini 0:9fca2b23d0ba 103 # 0C HardFault_Handler(FLASH address)
marcozecchini 0:9fca2b23d0ba 104 # 10 dummy
marcozecchini 0:9fca2b23d0ba 105 dummy_sp = 0x3FFFFC00
marcozecchini 0:9fca2b23d0ba 106 dummy_reset_vector = 0x00003625
marcozecchini 0:9fca2b23d0ba 107 dummy_nmi_handler = 0x00003761
marcozecchini 0:9fca2b23d0ba 108 dummy_hardfault_handler = 0x00003691
marcozecchini 0:9fca2b23d0ba 109 dummy_blank = 0x00000000
marcozecchini 0:9fca2b23d0ba 110
marcozecchini 0:9fca2b23d0ba 111 #expected fib structure
marcozecchini 0:9fca2b23d0ba 112 #typedef struct fib{
marcozecchini 0:9fca2b23d0ba 113 #uint32_t base; /**< Base offset of firmware, indicating what flash the
marcozecchini 0:9fca2b23d0ba 114 # firmware is in. (will never be 0x11111111) */
marcozecchini 0:9fca2b23d0ba 115 #uint32_t size; /**< Size of the firmware */
marcozecchini 0:9fca2b23d0ba 116 #uint32_t crc; /**< CRC32 for firmware correctness check */
marcozecchini 0:9fca2b23d0ba 117 #uint32_t rev; /**< Revision number */
marcozecchini 0:9fca2b23d0ba 118 #uint32_t checksum; /**< Check-sum of information block */
marcozecchini 0:9fca2b23d0ba 119 #}fib_t, *fib_pt;
marcozecchini 0:9fca2b23d0ba 120
marcozecchini 0:9fca2b23d0ba 121 fib_start = FIB_BASE
marcozecchini 0:9fca2b23d0ba 122 dummy_fib_size = 20
marcozecchini 0:9fca2b23d0ba 123 fib_size = 20
marcozecchini 0:9fca2b23d0ba 124 trim_size = 24
marcozecchini 0:9fca2b23d0ba 125 user_code_start = FLASH_BASE
marcozecchini 0:9fca2b23d0ba 126 trim_area_start = TRIM_BASE
marcozecchini 0:9fca2b23d0ba 127
marcozecchini 0:9fca2b23d0ba 128 # Write FIB to the file in little endian
marcozecchini 0:9fca2b23d0ba 129 output_hex_file[fib_start + 0] = (dummy_sp >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 130 output_hex_file[fib_start + 1] = (dummy_sp >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 131 output_hex_file[fib_start + 2] = (dummy_sp >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 132 output_hex_file[fib_start + 3] = (dummy_sp >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 133
marcozecchini 0:9fca2b23d0ba 134 output_hex_file[fib_start + 4] = (dummy_reset_vector >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 135 output_hex_file[fib_start + 5] = (dummy_reset_vector >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 136 output_hex_file[fib_start + 6] = (dummy_reset_vector >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 137 output_hex_file[fib_start + 7] = (dummy_reset_vector >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 138
marcozecchini 0:9fca2b23d0ba 139 output_hex_file[fib_start + 8] = (dummy_nmi_handler >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 140 output_hex_file[fib_start + 9] = (dummy_nmi_handler >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 141 output_hex_file[fib_start + 10] = (dummy_nmi_handler >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 142 output_hex_file[fib_start + 11] = (dummy_nmi_handler >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 143
marcozecchini 0:9fca2b23d0ba 144 output_hex_file[fib_start + 12] = (dummy_hardfault_handler >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 145 output_hex_file[fib_start + 13] = (dummy_hardfault_handler >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 146 output_hex_file[fib_start + 14] = (dummy_hardfault_handler >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 147 output_hex_file[fib_start + 15] = (dummy_hardfault_handler >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 148
marcozecchini 0:9fca2b23d0ba 149 output_hex_file[fib_start + 16] = (dummy_blank >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 150 output_hex_file[fib_start + 17] = (dummy_blank >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 151 output_hex_file[fib_start + 18] = (dummy_blank >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 152 output_hex_file[fib_start + 19] = (dummy_blank >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 153
marcozecchini 0:9fca2b23d0ba 154 # Write FIB to the file in little endian
marcozecchini 0:9fca2b23d0ba 155 output_hex_file[fib_start + 20] = (start >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 156 output_hex_file[fib_start + 21] = (start >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 157 output_hex_file[fib_start + 22] = (start >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 158 output_hex_file[fib_start + 23] = (start >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 159
marcozecchini 0:9fca2b23d0ba 160 output_hex_file[fib_start + 24] = (size >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 161 output_hex_file[fib_start + 25] = (size >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 162 output_hex_file[fib_start + 26] = (size >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 163 output_hex_file[fib_start + 27] = (size >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 164
marcozecchini 0:9fca2b23d0ba 165 output_hex_file[fib_start + 28] = (crc32 >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 166 output_hex_file[fib_start + 29] = (crc32 >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 167 output_hex_file[fib_start + 30] = (crc32 >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 168 output_hex_file[fib_start + 31] = (crc32 >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 169
marcozecchini 0:9fca2b23d0ba 170 output_hex_file[fib_start + 32] = (fw_rev >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 171 output_hex_file[fib_start + 33] = (fw_rev >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 172 output_hex_file[fib_start + 34] = (fw_rev >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 173 output_hex_file[fib_start + 35] = (fw_rev >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 174
marcozecchini 0:9fca2b23d0ba 175 output_hex_file[fib_start + 36] = (checksum >> 0) & 0xFF
marcozecchini 0:9fca2b23d0ba 176 output_hex_file[fib_start + 37] = (checksum >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 177 output_hex_file[fib_start + 38] = (checksum >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 178 output_hex_file[fib_start + 39] = (checksum >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 179
marcozecchini 0:9fca2b23d0ba 180 #pad the rest of the file
marcozecchini 0:9fca2b23d0ba 181 for i in range(fib_start + dummy_fib_size + fib_size, trim_area_start):
marcozecchini 0:9fca2b23d0ba 182 output_hex_file[i] = 0xFF
marcozecchini 0:9fca2b23d0ba 183
marcozecchini 0:9fca2b23d0ba 184 # Read in configuration data from the config parameter in targets.json
marcozecchini 0:9fca2b23d0ba 185 configData = Config('NCS36510')
marcozecchini 0:9fca2b23d0ba 186 paramData = configData.get_target_config_data()
marcozecchini 0:9fca2b23d0ba 187 for v in paramData.values():
marcozecchini 0:9fca2b23d0ba 188 if (v.name == "target.mac-addr-high"):
marcozecchini 0:9fca2b23d0ba 189 mac_addr_high = int(v.value, 16)
marcozecchini 0:9fca2b23d0ba 190 elif (v.name == "target.mac-addr-low"):
marcozecchini 0:9fca2b23d0ba 191 mac_addr_low = int(v.value,16)
marcozecchini 0:9fca2b23d0ba 192 elif (v.name == "target.32KHz-clk-trim"):
marcozecchini 0:9fca2b23d0ba 193 clk_32k_trim = int(v.value,16)
marcozecchini 0:9fca2b23d0ba 194 elif (v.name == "target.32MHz-clk-trim"):
marcozecchini 0:9fca2b23d0ba 195 clk_32m_trim = int(v.value,16)
marcozecchini 0:9fca2b23d0ba 196 elif (v.name == "target.rssi-trim"):
marcozecchini 0:9fca2b23d0ba 197 rssi = int(v.value,16)
marcozecchini 0:9fca2b23d0ba 198 elif (v.name == "target.txtune-trim"):
marcozecchini 0:9fca2b23d0ba 199 txtune = int(v.value,16)
marcozecchini 0:9fca2b23d0ba 200 else:
marcozecchini 0:9fca2b23d0ba 201 print("Not a valid param")
marcozecchini 0:9fca2b23d0ba 202
marcozecchini 0:9fca2b23d0ba 203 output_hex_file[trim_area_start + 0] = mac_addr_low & 0xFF
marcozecchini 0:9fca2b23d0ba 204 output_hex_file[trim_area_start + 1] = (mac_addr_low >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 205 output_hex_file[trim_area_start + 2] = (mac_addr_low >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 206 output_hex_file[trim_area_start + 3] = (mac_addr_low >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 207
marcozecchini 0:9fca2b23d0ba 208 output_hex_file[trim_area_start + 4] = mac_addr_high & 0xFF
marcozecchini 0:9fca2b23d0ba 209 output_hex_file[trim_area_start + 5] = (mac_addr_high >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 210 output_hex_file[trim_area_start + 6] = (mac_addr_high >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 211 output_hex_file[trim_area_start + 7] = (mac_addr_high >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 212
marcozecchini 0:9fca2b23d0ba 213 output_hex_file[trim_area_start + 8] = clk_32k_trim & 0xFF
marcozecchini 0:9fca2b23d0ba 214 output_hex_file[trim_area_start + 9] = (clk_32k_trim >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 215 output_hex_file[trim_area_start + 10] = (clk_32k_trim >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 216 output_hex_file[trim_area_start + 11] = (clk_32k_trim >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 217
marcozecchini 0:9fca2b23d0ba 218 output_hex_file[trim_area_start + 12] = clk_32m_trim & 0xFF
marcozecchini 0:9fca2b23d0ba 219 output_hex_file[trim_area_start + 13] = (clk_32m_trim >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 220 output_hex_file[trim_area_start + 14] = (clk_32m_trim >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 221 output_hex_file[trim_area_start + 15] = (clk_32m_trim >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 222
marcozecchini 0:9fca2b23d0ba 223 output_hex_file[trim_area_start + 16] = rssi & 0xFF
marcozecchini 0:9fca2b23d0ba 224 output_hex_file[trim_area_start + 17] = (rssi >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 225 output_hex_file[trim_area_start + 18] = (rssi >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 226 output_hex_file[trim_area_start + 19] = (rssi >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 227
marcozecchini 0:9fca2b23d0ba 228 output_hex_file[trim_area_start + 20] = txtune & 0xFF
marcozecchini 0:9fca2b23d0ba 229 output_hex_file[trim_area_start + 21] = (txtune >> 8) & 0xFF
marcozecchini 0:9fca2b23d0ba 230 output_hex_file[trim_area_start + 22] = (txtune >> 16) & 0xFF
marcozecchini 0:9fca2b23d0ba 231 output_hex_file[trim_area_start + 23] = (txtune >> 24) & 0xFF
marcozecchini 0:9fca2b23d0ba 232
marcozecchini 0:9fca2b23d0ba 233 # pad the rest of the area with 0xFF
marcozecchini 0:9fca2b23d0ba 234 for i in range(trim_area_start + trim_size, user_code_start):
marcozecchini 0:9fca2b23d0ba 235 output_hex_file[i] = 0xFF
marcozecchini 0:9fca2b23d0ba 236
marcozecchini 0:9fca2b23d0ba 237 #merge two hex files
marcozecchini 0:9fca2b23d0ba 238 output_hex_file.merge(input_hex_file, overlap='error')
marcozecchini 0:9fca2b23d0ba 239
marcozecchini 0:9fca2b23d0ba 240 # Write out file(s)
marcozecchini 0:9fca2b23d0ba 241 output_hex_file.tofile(file_name_hex, 'hex')