OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [ihex2bin/] [README.md] - Blame information for rev 25

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 25 alirezamon
kk_ihex
2
=======
3
 
4
A small library for reading and writing the
5
[Intel HEX](http://en.wikipedia.org/wiki/Intel_HEX) (or IHEX) format. The
6
library is mainly intended for embedded systems and microcontrollers, such
7
as Arduino, AVR, PIC, ARM, STM32, etc - hence the emphasis is on small size
8
rather than features, generality, or error handling.
9
 
10
See the header file `kk_ihex.h` for documentation, or below for simple examples.
11
 
12
~ [Kimmo Kulovesi](http://arkku.com/), 2013-12-27
13
 
14
Writing
15
=======
16
 
17
Basic usage for writing binary data as IHEX ASCII:
18
 
19
    #include "kk_ihex_write.h"
20
 
21
    struct ihex_state ihex;
22
    ihex_init(&ihex);
23
    ihex_write_at_address(&ihex, 0);
24
    ihex_write_bytes(&ihex, my_data_bytes, my_data_size);
25
    ihex_end_write(&ihex);
26
 
27
The function `ihex_write_bytes` may be called multiple times to pass any
28
amount of data at a time.
29
 
30
The actual writing is done by a callback called `ihex_flush_buffer`,
31
which must be implemented, e.g., as follows:
32
 
33
    void ihex_flush_buffer(struct ihex_state *ihex, char *buffer, char *eptr) {
34
        *eptr = '\0';
35
        (void) fputs(buffer, stdout);
36
    }
37
 
38
The length of the buffer can be obtained from `eptr - buffer`. The actual
39
implementation may of course do with the IHEX data as it pleases, e.g.,
40
transmit it over a serial port.
41
 
42
For a complete example, see the included program `bin2ihex.c`.
43
 
44
 
45
Reading
46
=======
47
 
48
Basic usage for reading ASCII IHEX into binary data:
49
 
50
    #include "kk_ihex_read.h"
51
 
52
    struct ihex_state ihex;
53
    ihex_begin_read(&ihex);
54
    ihex_read_bytes(&ihex, my_ascii_bytes, my_ascii_length);
55
    ihex_end_read(&ihex);
56
 
57
The function `ihex_read_bytes` may be called multiple times to pass any
58
amount of data at a time.
59
 
60
The reading functions call the function `ihex_data_read`, which must be
61
implemented by the caller to store the binary data, e.g., as follows:
62
 
63
    ihex_bool_t ihex_data_read (struct ihex_state *ihex,
64
                                ihex_record_type_t type,
65
                                ihex_bool_t checksum_error) {
66
        if (type == IHEX_DATA_RECORD) {
67
            unsigned long address = (unsigned long) IHEX_LINEAR_ADDRESS(ihex);
68
            (void) fseek(outfile, address, SEEK_SET);
69
            (void) fwrite(ihex->data, ihex->length, 1, outfile);
70
        } else if (type == IHEX_END_OF_FILE_RECORD) {
71
            (void) fclose(outfile);
72
        }
73
        return true;
74
    }
75
 
76
Of course an actual implementation is free to do with the data as it chooses,
77
e.g., burn it on an EEPROM instead of writing it to a file.
78
 
79
For an example complete with error handling, see the included program
80
`ihex2bin.c`.
81
 
82
 
83
Example Programs
84
================
85
 
86
The included example programs, `ihex2bin` and `bin2ihex`, implement
87
a very simple conversion between raw binary data and Intel HEX.
88
Usage by example:
89
 
90
    # Simple conversion from binary to IHEX:
91
    bin2ihex outfile.hex
92
 
93
    # Add an offset to the output addresses (i.e., make the address
94
    # of the first byte of the input other than zero):
95
    bin2ihex -a 0x8000000 -i infile.bin -o outfile.hex
96
 
97
    # Simple conversion from IHEX to binary:
98
    ihex2bin outfile.bin
99
 
100
    # Manually specify the initial address written (i.e., subtract
101
    # an offset from the input addresses):
102
    ihex2bin -a 0x8000000 -i infile.hex -o outfile.bin
103
 
104
    # Start output at the first data byte (i.e., make the address offset
105
    # equal to the address of the first data byte read from input):
106
    ihex2bin -A -i infile.hex -o outfile.bin
107
 
108
Both programs also accept the option `-v` to increase verbosity.
109
 
110
When using `ihex2bin` on Intel HEX files produced by compilers and such,
111
it is a good idea to specify the command-line option `-A` to autodetect
112
the address offset. Otherwise the program will simply fill any unused
113
addresses, starting from 0, with zero bytes, which may total mega- or
114
even gigabytes.
115
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.