1 |
4 |
ja_rd |
#ifndef INTEL_HEX_H
|
2 |
|
|
#define INTEL_HEX_H
|
3 |
|
|
/**
|
4 |
|
|
* \file ihex.h
|
5 |
|
|
* \brief Low-level utility functions to create, read, write, and print Intel HEX8 binary records.
|
6 |
|
|
* \author Vanya A. Sergeev <vsergeev@gmail.com>
|
7 |
|
|
* \date February 2011
|
8 |
|
|
* \version 1.0.5
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
|
|
#include <stdio.h>
|
12 |
|
|
#include <stdint.h>
|
13 |
|
|
#include <stdlib.h>
|
14 |
|
|
#include <string.h>
|
15 |
|
|
|
16 |
|
|
/* General definition of the Intel HEX8 specification */
|
17 |
|
|
enum _IHexDefinitions {
|
18 |
|
|
/* 768 should be plenty of space to read in a Intel HEX8 record */
|
19 |
|
|
IHEX_RECORD_BUFF_SIZE = 768,
|
20 |
|
|
/* Offsets and lengths of various fields in an Intel HEX8 record */
|
21 |
|
|
IHEX_COUNT_OFFSET = 1,
|
22 |
|
|
IHEX_COUNT_LEN = 2,
|
23 |
|
|
IHEX_ADDRESS_OFFSET = 3,
|
24 |
|
|
IHEX_ADDRESS_LEN = 4,
|
25 |
|
|
IHEX_TYPE_OFFSET = 7,
|
26 |
|
|
IHEX_TYPE_LEN = 2,
|
27 |
|
|
IHEX_DATA_OFFSET = 9,
|
28 |
|
|
IHEX_CHECKSUM_LEN = 2,
|
29 |
|
|
IHEX_MAX_DATA_LEN = 512,
|
30 |
|
|
/* Ascii hex encoded length of a single byte */
|
31 |
|
|
IHEX_ASCII_HEX_BYTE_LEN = 2,
|
32 |
|
|
/* Start code offset and value */
|
33 |
|
|
IHEX_START_CODE_OFFSET = 0,
|
34 |
|
|
IHEX_START_CODE = ':',
|
35 |
|
|
};
|
36 |
|
|
|
37 |
|
|
/**
|
38 |
|
|
* All possible error codes the Intel HEX8 record utility functions may return.
|
39 |
|
|
*/
|
40 |
|
|
enum IHexErrors {
|
41 |
|
|
IHEX_OK = 0, /**< Error code for success or no error. */
|
42 |
|
|
IHEX_ERROR_FILE = -1, /**< Error code for error while reading from or writing to a file. You may check errno for the exact error if this error code is encountered. */
|
43 |
|
|
IHEX_ERROR_EOF = -2, /**< Error code for encountering end-of-file when reading from a file. */
|
44 |
|
|
IHEX_ERROR_INVALID_RECORD = -3, /**< Error code for error if an invalid record was read. */
|
45 |
|
|
IHEX_ERROR_INVALID_ARGUMENTS = -4, /**< Error code for error from invalid arguments passed to function. */
|
46 |
|
|
IHEX_ERROR_NEWLINE = -5, /**< Error code for encountering a newline with no record when reading from a file. */
|
47 |
|
|
};
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Intel HEX8 Record Types 00-05
|
51 |
|
|
*/
|
52 |
|
|
enum IHexRecordTypes {
|
53 |
|
|
IHEX_TYPE_00 = 0, /**< Data Record */
|
54 |
|
|
IHEX_TYPE_01, /**< End of File Record */
|
55 |
|
|
IHEX_TYPE_02, /**< Extended Segment Address Record */
|
56 |
|
|
IHEX_TYPE_03, /**< Start Segment Address Record */
|
57 |
|
|
IHEX_TYPE_04, /**< Extended Linear Address Record */
|
58 |
|
|
IHEX_TYPE_05, /**< Start Linear Address Record */
|
59 |
|
|
};
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* Structure to hold the fields of an Intel HEX8 record.
|
63 |
|
|
*/
|
64 |
|
|
typedef struct {
|
65 |
|
|
uint16_t address; /**< The 16-bit address field. */
|
66 |
|
|
uint8_t data[IHEX_MAX_DATA_LEN/2]; /**< The 8-bit array data field, which has a maximum size of 256 bytes. */
|
67 |
|
|
int dataLen; /**< The number of bytes of data stored in this record. */
|
68 |
|
|
int type; /**< The Intel HEX8 record type of this record. */
|
69 |
|
|
uint8_t checksum; /**< The checksum of this record. */
|
70 |
|
|
} IHexRecord;
|
71 |
|
|
|
72 |
|
|
/**
|
73 |
|
|
* Sets all of the record fields of an Intel HEX8 record structure.
|
74 |
|
|
* \param type The Intel HEX8 record type (integer value of 0 through 5).
|
75 |
|
|
* \param address The 16-bit address of the data.
|
76 |
|
|
* \param data A point to the 8-bit array of data.
|
77 |
|
|
* \param dataLen The size of the 8-bit data array.
|
78 |
|
|
* \param ihexRecord A pointer to the target Intel HEX8 record structure where these fields will be set.
|
79 |
|
|
* \return IHEX_OK on success, otherwise one of the IHEX_ERROR_ error codes.
|
80 |
|
|
* \retval IHEX_OK on success.
|
81 |
|
|
* \retval IHEX_ERROR_INVALID_ARGUMENTS if the record pointer is NULL, or if the length of the 8-bit data array is out of range (less than zero or greater than the maximum data length allowed by record specifications, see IHexRecord.data).
|
82 |
|
|
*/
|
83 |
|
|
int New_IHexRecord(int type, uint16_t address, const uint8_t *data, int dataLen, IHexRecord *ihexRecord);
|
84 |
|
|
|
85 |
|
|
/**
|
86 |
|
|
* Reads an Intel HEX8 record from an opened file.
|
87 |
|
|
* \param ihexRecord A pointer to the Intel HEX8 record structure that will store the read record.
|
88 |
|
|
* \param in A file pointer to an opened file that can be read.
|
89 |
|
|
* \return IHEX_OK on success, otherwise one of the IHEX_ERROR_ error codes.
|
90 |
|
|
* \retval IHEX_OK on success.
|
91 |
|
|
* \retval IHEX_ERROR_INVALID_ARGUMENTS if the record pointer or file pointer is NULL.
|
92 |
|
|
* \retval IHEX_ERROR_EOF if end-of-file has been reached.
|
93 |
|
|
* \retval IHEX_ERROR_FILE if a file reading error has occured.
|
94 |
|
|
* \retval IHEX_INVALID_RECORD if the record read is invalid (record did not match specifications or record checksum was invalid).
|
95 |
|
|
*/
|
96 |
|
|
int Read_IHexRecord(IHexRecord *ihexRecord, FILE *in);
|
97 |
|
|
|
98 |
|
|
/**
|
99 |
|
|
* Writes an Intel HEX8 record to an opened file.
|
100 |
|
|
* \param ihexRecord A pointer to the Intel HEX8 record structure.
|
101 |
|
|
* \param out A file pointer to an opened file that can be written to.
|
102 |
|
|
* \return IHEX_OK on success, otherwise one of the IHEX_ERROR_ error codes.
|
103 |
|
|
* \retval IHEX_OK on success.
|
104 |
|
|
* \retval IHEX_ERROR_INVALID_ARGUMENTS if the record pointer or file pointer is NULL.
|
105 |
|
|
* \retval IHEX_ERROR_INVALID_RECORD if the record's data length is out of range (greater than the maximum data length allowed by record specifications, see IHexRecord.data).
|
106 |
|
|
* \retval IHEX_ERROR_FILE if a file writing error has occured.
|
107 |
|
|
*/
|
108 |
|
|
int Write_IHexRecord(const IHexRecord *ihexRecord, FILE *out);
|
109 |
|
|
|
110 |
|
|
/**
|
111 |
|
|
* Prints the contents of an Intel HEX8 record structure to stdout.
|
112 |
|
|
* The record dump consists of the type, address, entire data array, and checksum fields of the record.
|
113 |
|
|
* \param ihexRecord A pointer to the Intel HEX8 record structure.
|
114 |
|
|
* \return Always returns IHEX_OK (success).
|
115 |
|
|
* \retval IHEX_OK on success.
|
116 |
|
|
*/
|
117 |
|
|
void Print_IHexRecord(const IHexRecord *ihexRecord);
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Calculates the checksum of an Intel HEX8 IHexRecord structure.
|
121 |
|
|
* See the Intel HEX8 specifications for more details on the checksum calculation.
|
122 |
|
|
* \param ihexRecord A pointer to the Intel HEX8 record structure.
|
123 |
|
|
* \return The 8-bit checksum.
|
124 |
|
|
*/
|
125 |
|
|
uint8_t Checksum_IHexRecord(const IHexRecord *ihexRecord);
|
126 |
|
|
|
127 |
|
|
#endif
|