Coder Social home page Coder Social logo

intel-hex-file-parser's Introduction

Intel-HEX-file-parser

This HEX file parser analyses each byte by using state machine method.

The fields of Intel HEX records includes:
#f03c15 Start code #c5f015 Byte count #1589F0 Address #330077 Record type #7700F0 Data #3333F0 Checksum

For example:
If the input string is ":020000040800F2:10"
The parser will iterate through the string, the last character will be recognized as "Byte count last nibble" field. Then, the parser will save current state and return back to the caller function.

If the 2nd input string is "8000000804002001810008C582000871810008" The parser will parse the string based on previous saved state. Hence, the first character will be recognized as "Address" field.

Test Code:

bool testcase_long_str()
{
    printf("=== testcase_long_str\n");

    const uint8_t ihex_long0[] = ":020000040800F2\n:108000000804002001810008C582000871810008";
    const uint8_t ihex_long1[] = "71\n:10801000C38200086D8100089583000800000000FD\r\n:1080";
    const uint8_t ihex_long2[] = "2000000000000000000000000000C9820008FD\n:10803000";
    const uint8_t ihex_long3[] = "6F81000800000000C7820008CB82";
    const uint8_t ihex_long4[] = "0008A2\r\n:108040001B8100081B8100081B8100081B810008";
    const uint8_t ihex_long5[] = "A0\n:08847000A683000800A24A04E3\n:04000005080080ED82\n:00000001FF";

    if (!ihex_parser(ihex_long0, sizeof(ihex_long0)))
    {
        printf("Parse error long0\n");
        return false;
    }
    if (!ihex_parser(ihex_long1, sizeof(ihex_long1)))
    {
        printf("Parse error long1\n");
        return false;
    }
    if (!ihex_parser(ihex_long2, sizeof(ihex_long2)))
    {
        printf("Parse error long2\n");
        return false;
    }
    if (!ihex_parser(ihex_long3, sizeof(ihex_long3)))
    {
        printf("Parse error long3\n");
        return false;
    }
    if (!ihex_parser(ihex_long4, sizeof(ihex_long4)))
    {
        printf("Parse error long4\n");
        return false;
    }
    if (!ihex_parser(ihex_long5, sizeof(ihex_long5)))
    {
        printf("Parse error long5\n");
        return false;
    }
    return true;
}

Result:

Set Linear Address:08000000
WriteData (0x08008000):0804002001810008C582000871810008
WriteData (0x08008010):C38200086D8100089583000800000000
WriteData (0x08008020):000000000000000000000000C9820008
WriteData (0x08008030):6F81000800000000C7820008CB820008
WriteData (0x08008040):1B8100081B8100081B8100081B810008
WriteData (0x08008470):A683000800A24A04
Start linear address
EOF

Example Code - use fread() to read and parse the ihex file:

    ihex_set_callback_func(write_flash_data);
    ihex_reset_state();
    
    /* Open HEX file, read per 256 byte block size*/
    bool return_status = false;

    uint8_t fbuf[256];
    size_t readcount = 0;

    FILE *fp = fopen("test.hex", "r");
    if (fp == NULL)
    {
        printf("fopen: cannot open hex file\n");
        goto EXIT;
    }
    
    while ((readcount = fread(fbuf, 1, sizeof(fbuf), fp)) > 0)
    {
        if (readcount < sizeof(fbuf))
        {
            fbuf[readcount] = '\0';     // Add null teminated char
        }
        if (!ihex_parser(fbuf, sizeof(fbuf)))
        {
            printf("parse failed\n");
            goto EXIT;
        }
    }
    
    return_status = true;

EXIT:
    if (fp)
        fclose(fp);

    return return_status;

intel-hex-file-parser's People

Contributors

sfyip avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.