diff --git a/bin2ihex.c b/bin2ihex.c index fc8dae4..d5a1974 100644 --- a/bin2ihex.c +++ b/bin2ihex.c @@ -33,7 +33,7 @@ main (int argc, char *argv[]) { ihex_address_t initial_address = 0; bool write_initial_address = 0; bool debug_enabled = 0; - unsigned int count; + ihex_count_t count; uint8_t buf[1024]; outfile = stdout; @@ -90,7 +90,7 @@ invalid_argument: (void) fprintf(stderr, "Invalid argument: %s\n", arg); usage: (void) fprintf(stderr, "kk_ihex " KK_IHEX_VERSION - " - Copyright (c) 2013-2014 Kimmo Kulovesi\n"); + " - Copyright (c) 2013-2015 Kimmo Kulovesi\n"); (void) fprintf(stderr, "Usage: bin2ihex [-a ]" " [-o ] [-i ] [-v]\n"); return arg ? EXIT_FAILURE : EXIT_SUCCESS; @@ -114,7 +114,7 @@ argument_error: } ihex.flags |= IHEX_FLAG_ADDRESS_OVERFLOW; } - while ((count = (unsigned int) fread(buf, 1, sizeof(buf), infile))) { + while ((count = (ihex_count_t) fread(buf, 1, sizeof(buf), infile))) { ihex_write_bytes(&ihex, buf, count); } ihex_end_write(&ihex); diff --git a/ihex2bin.c b/ihex2bin.c index 628972d..2e8b8a3 100644 --- a/ihex2bin.c +++ b/ihex2bin.c @@ -40,7 +40,7 @@ int main (int argc, char *argv[]) { struct ihex_state ihex; FILE *infile = stdin; - unsigned int count; + ihex_count_t count; char buf[256]; outfile = stdout; @@ -98,7 +98,7 @@ invalid_argument: (void) fprintf(stderr, "Invalid argument: %s\n", arg); usage: (void) fprintf(stderr, "kk_ihex " KK_IHEX_VERSION - " - Copyright (c) 2013-2014 Kimmo Kulovesi\n"); + " - Copyright (c) 2013-2015 Kimmo Kulovesi\n"); (void) fprintf(stderr, "Usage: ihex2bin ([-a ]|[-A])" " [-o ] [-i ] [-v]\n"); return arg ? EXIT_FAILURE : EXIT_SUCCESS; @@ -111,7 +111,7 @@ argument_error: (ihex_address_t) address_offset : 0); while (fgets(buf, sizeof(buf), infile)) { - count = (unsigned int) strlen(buf); + count = (ihex_count_t) strlen(buf); ihex_read_bytes(&ihex, buf, count); line_number += (count && buf[count - 1] == '\n') ? 1 : 0; } diff --git a/kk_ihex.h b/kk_ihex.h index a24d61a..589f010 100644 --- a/kk_ihex.h +++ b/kk_ihex.h @@ -106,7 +106,7 @@ #ifndef KK_IHEX_H #define KK_IHEX_H -#define KK_IHEX_VERSION "2015-02-22" +#define KK_IHEX_VERSION "2015-02-26" #include @@ -119,7 +119,7 @@ typedef uint_fast8_t ihex_bool_t; typedef uint_least32_t ihex_address_t; typedef uint_least16_t ihex_segment_t; -typedef unsigned int ihex_count_t; +typedef int ihex_count_t; // Maximum number of data bytes per line (applies to both reading and // writing!); specify 255 to support reading all possible lengths. Less @@ -129,19 +129,21 @@ typedef unsigned int ihex_count_t; #define IHEX_LINE_MAX_LENGTH 255 #endif -typedef struct ihex_state { - ihex_address_t address; -#ifndef IHEX_DISABLE_SEGMENTS - ihex_segment_t segment; -#endif - uint8_t flags; - uint8_t line_length; - uint8_t length; - uint8_t data[IHEX_LINE_MAX_LENGTH + 1]; -} kk_ihex_t; +enum ihex_flags { + IHEX_FLAG_ADDRESS_OVERFLOW = 0x80 // 16-bit address overflow +}; +typedef uint8_t ihex_flags_t; -#define IHEX_FLAG_ADDRESS_OVERFLOW 0x80 // 16-bit address overflow -// (Other flags are reserved for internal use!) +typedef struct ihex_state { + ihex_address_t address; +#ifndef IHEX_DISABLE_SEGMENTS + ihex_segment_t segment; +#endif + ihex_flags_t flags; + uint8_t line_length; + uint8_t length; + uint8_t data[IHEX_LINE_MAX_LENGTH + 1]; +} kk_ihex_t; enum ihex_record_type { IHEX_DATA_RECORD, diff --git a/kk_ihex_read.c b/kk_ihex_read.c index 7bfcdf9..a9f5739 100644 --- a/kk_ihex_read.c +++ b/kk_ihex_read.c @@ -135,7 +135,7 @@ ihex_read_byte (struct ihex_state * const ihex, const int byte) { break; case (READ_ADDRESS_MSB_LOW >> 1): // high byte of 16-bit address - ihex->address = (ihex->address & ADDRESS_HIGH_MASK); + ihex->address &= ADDRESS_HIGH_MASK; // clear the 16-bit address ihex->address |= ((ihex_address_t) b) << 8U; break; case (READ_ADDRESS_LSB_LOW >> 1): diff --git a/kk_ihex_write.c b/kk_ihex_write.c index ca0cd21..589a55b 100644 --- a/kk_ihex_write.c +++ b/kk_ihex_write.c @@ -68,14 +68,12 @@ ihex_buffer_newline (char * restrict w) { static void ihex_write_end_of_file (struct ihex_state * const ihex) { char * restrict w = ihex_write_buffer; - *w++ = IHEX_START; // : -#if 0 - for (uint_fast8_t i = 7; i; --i) { - *w++ = '0'; - } - *w++ = '1'; - *w++ = 'F'; - *w++ = 'F'; + *w++ = IHEX_START; // : +#if 1 + *w++ = '0'; *w++ = '0'; // length + *w++ = '0'; *w++ = '0'; *w++ = '0'; *w++ = '0'; // address + *w++ = '0'; *w++ = '1'; // record type + *w++ = 'F'; *w++ = 'F'; // checksum #else w = ihex_buffer_byte(w, 0); // length w = ihex_buffer_byte(w, 0); // address msb @@ -94,11 +92,11 @@ ihex_write_extended_address (struct ihex_state * const ihex, char * restrict w = ihex_write_buffer; uint8_t sum = type + 2U; - *w++ = IHEX_START; // : - w = ihex_buffer_byte(w, 2U); // length - w = ihex_buffer_byte(w, 0); // 16-bit address msb - w = ihex_buffer_byte(w, 0); // 16-bit address lsb - w = ihex_buffer_byte(w, type); // record type + *w++ = IHEX_START; // : + w = ihex_buffer_byte(w, 2U); // length + w = ihex_buffer_byte(w, 0); // 16-bit address msb + w = ihex_buffer_byte(w, 0); // 16-bit address lsb + w = ihex_buffer_byte(w, type); // record type w = ihex_buffer_word(w, address, &sum); // high bytes of address w = ihex_buffer_byte(w, (uint8_t)~sum + 1U); // checksum w = ihex_buffer_newline(w);