|
1 | | -Writing out data |
2 | | -**************** |
3 | | -Data contained in IntelHex can be written out in a few different formats, |
4 | | -including HEX, bin, or python dictionaries. |
5 | | - |
6 | | -You can write out HEX data contained in object by method ``.write_hex_file(f)``. |
7 | | -Parameter ``f`` should be filename or file-like object. |
8 | | -Note that this can include builtins like sys.stdout. |
9 | | -Also you can use the universal tofile. |
10 | | - |
11 | | -To convert data of IntelHex object to HEX8 file format without actually saving it |
12 | | -to disk you can use the builtin StringIO file-like object, e.g.:: |
13 | | - |
14 | | - >>> from cStringIO import StringIO |
15 | | - >>> from intelhex import IntelHex |
16 | | - >>> ih = IntelHex() |
17 | | - >>> ih[0] = 0x55 |
18 | | - >>> sio = StringIO() |
19 | | - >>> ih.write_hex_file(sio) |
20 | | - >>> hexstr = sio.getvalue() |
21 | | - >>> sio.close() |
22 | | - |
23 | | -Variable ``hexstr`` will contain a string with the content of a HEX8 file. |
24 | | - |
25 | | -You can customize hex file output with following optional arguments |
26 | | -to ``write_hex_file`` call: |
27 | | - |
28 | | - * ``write_start_addr`` - you can disable start address record in new hex file; |
29 | | - * ``eolstyle`` - you can force ``CRLF`` line endings in new hex file. |
30 | | - * ``byte_count`` - you can control how many bytes should be written to each |
31 | | - data record. |
32 | | - |
33 | | - |
34 | | -Data converters |
35 | | -~~~~~~~~~~~~~~~ |
36 | | - |
37 | | -To write data as a hex file with default settings you also can use |
38 | | -universal method ``tofile``:: |
39 | | - |
40 | | - # the code below is the same as "ih.write_hex_file(sio)" |
41 | | - >>> ih.tofile(sio, format='hex') |
42 | | - |
43 | | -Class IntelHex has several methods for converting data of IntelHex objects |
44 | | -into binary form: |
45 | | - |
46 | | -* ``tobinarray`` (returns array of unsigned char bytes); |
47 | | -* ``tobinstr`` (returns string of bytes); |
48 | | -* ``tobinfile`` (convert content to binary form and write to file). |
49 | | - |
50 | | -Example:: |
51 | | - |
52 | | - >>> from intelhex import IntelHex |
53 | | - >>> ih = IntelHex("foo.hex") |
54 | | - >>> ih.tobinfile("foo.bin") |
55 | | - |
56 | | -Also you can use universal method ``tofile`` to write data as binary file:: |
57 | | - |
58 | | - >>> ih.tofile("foo.bin", format='bin') |
59 | | - |
60 | | - |
61 | | -Writing data in chunks |
62 | | -~~~~~~~~~~~~~~~~~~~~~~ |
63 | | -If you need to get binary data from IntelHex as series of chunks then you can |
64 | | -pass to ``tobinarray``/``tobinstr`` methods either start/end addresses |
65 | | -or start address and required size of the chunk. This could be useful if |
66 | | -you're creating Eeprom/Flash IC programmer or bootloader. |
67 | | -:: |
68 | | - |
69 | | - EEPROM_SIZE = 8192 # 8K bytes |
70 | | - BLOCK_SIZE = 128 # 128 bytes |
71 | | - for addr in range(0, EEPROM_SIZE, BLOCK_SIZE): |
72 | | - eeprom.i2c_write(addr, ih.tobinarray(start=addr, size=BLOCK_SIZE)) |
| 1 | +Writing out data |
| 2 | +**************** |
| 3 | +Data contained in IntelHex can be written out in a few different formats, |
| 4 | +including HEX, bin, or python dictionaries. |
| 5 | + |
| 6 | +You can write out HEX data contained in object by method ``.write_hex_file(f)``. |
| 7 | +Parameter ``f`` should be filename or file-like object. |
| 8 | +Note that this can include builtins like sys.stdout. |
| 9 | +Also you can use the universal ``tofile``. |
| 10 | + |
| 11 | +To convert data of IntelHex object to HEX8 file format without actually saving it |
| 12 | +to disk you can use the builtin StringIO file-like object, e.g.:: |
| 13 | + |
| 14 | + >>> from cStringIO import StringIO |
| 15 | + >>> from intelhex import IntelHex |
| 16 | + >>> ih = IntelHex() |
| 17 | + >>> ih[0] = 0x55 |
| 18 | + >>> sio = StringIO() |
| 19 | + >>> ih.write_hex_file(sio) |
| 20 | + >>> hexstr = sio.getvalue() |
| 21 | + >>> sio.close() |
| 22 | + |
| 23 | +Variable ``hexstr`` will contain a string with the content of a HEX8 file. |
| 24 | + |
| 25 | +You can customize hex file output with following optional arguments |
| 26 | +to ``write_hex_file`` call: |
| 27 | + |
| 28 | + * ``write_start_addr`` - you can disable start address record in new hex file. |
| 29 | + * ``eolstyle`` - you can force ``CRLF`` line endings in new hex file. |
| 30 | + * ``byte_count`` - you can control how many bytes should be written to each |
| 31 | + data record. |
| 32 | + * ``ext_addr_mode`` - you can decide how extended address records should be |
| 33 | + resolved and written in new hex file (explained below). |
| 34 | + |
| 35 | +Extended Address records are describing an address in binary space from which |
| 36 | +a block of data is being written. Normally without those record we could write |
| 37 | +only 64KB of binary data into one single ``.hex`` file. This is because in |
| 38 | +one signle data record there are only 2 bytes for address.To have opportunity |
| 39 | +to write more Extended Records are needed to increase address resolution. |
| 40 | +Currently there are two types fo records defined in IntelHex format: |
| 41 | + |
| 42 | + * ``Extended Segment Address`` [02] - you can write up to 1MB of binary data |
| 43 | + * ``Extended Linear Address`` [04] - you can write up to 4GB of binary data |
| 44 | + |
| 45 | +There are 4 modes given by ``ext_addr_mode`` parameter to support every type |
| 46 | +of memory address resolution: |
| 47 | + |
| 48 | + * ``linear`` - forces to use Extended Linear Address records, |
| 49 | + in most of cases this mode is commonly desired (default). |
| 50 | + * ``segment`` - forces to use Extended Segment Address records. |
| 51 | + * ``none`` - forces to not use any Extended Address records (legacy option). |
| 52 | + * ``auto`` - automatically decides which mode to use using last known adress |
| 53 | + where data need to be written. |
| 54 | + |
| 55 | +Whenever data overflow for different adres resoution is detected adequat |
| 56 | +exception will thrown. No mixing allowed. There won't be any Extened Address |
| 57 | +records will written in any mode if data to write need to be placed |
| 58 | +in address under 64KB. |
| 59 | + |
| 60 | + |
| 61 | +Data converters |
| 62 | +~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +To write data as a hex file with default settings you also can use |
| 65 | +universal method ``tofile``:: |
| 66 | + |
| 67 | + # the code below is the same as "ih.write_hex_file(sio)" |
| 68 | + >>> ih.tofile(sio, format='hex') |
| 69 | + |
| 70 | +Class IntelHex has several methods for converting data of IntelHex objects |
| 71 | +into binary form: |
| 72 | + |
| 73 | +* ``tobinarray`` (returns array of unsigned char bytes); |
| 74 | +* ``tobinstr`` (returns string of bytes); |
| 75 | +* ``tobinfile`` (convert content to binary form and write to file). |
| 76 | + |
| 77 | +Example:: |
| 78 | + |
| 79 | + >>> from intelhex import IntelHex |
| 80 | + >>> ih = IntelHex("foo.hex") |
| 81 | + >>> ih.tobinfile("foo.bin") |
| 82 | + |
| 83 | +Also you can use universal method ``tofile`` to write data as binary file:: |
| 84 | + |
| 85 | + >>> ih.tofile("foo.bin", format='bin') |
| 86 | + |
| 87 | + |
| 88 | +Writing data in chunks |
| 89 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 90 | +If you need to get binary data from IntelHex as series of chunks then you can |
| 91 | +pass to ``tobinarray``/``tobinstr`` methods either start/end addresses |
| 92 | +or start address and required size of the chunk. This could be useful if |
| 93 | +you're creating Eeprom/Flash IC programmer or bootloader. |
| 94 | +:: |
| 95 | + |
| 96 | + EEPROM_SIZE = 8192 # 8K bytes |
| 97 | + BLOCK_SIZE = 128 # 128 bytes |
| 98 | + for addr in range(0, EEPROM_SIZE, BLOCK_SIZE): |
| 99 | + eeprom.i2c_write(addr, ih.tobinarray(start=addr, size=BLOCK_SIZE)) |
0 commit comments