[COMPLETED] Combining Blink & Count to the bootloader

STM32duino bootloader aka Maple bootloader
User avatar
zoomx
Posts: 541
Joined: Mon Apr 27, 2015 2:28 pm
Location: Mt.Etna, Italy

Re: Combining Blink & Count to the bootloader

Post by zoomx » Mon Jun 19, 2017 2:10 pm

RogerClark wrote:
Mon Jun 19, 2017 10:41 am
I'll see if windows powershell has this

Edit.

No. It does not have that command.
I believe that on Cygwin it will work since it has either cat and truncate commands.

User avatar
RogerClark
Posts: 7500
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Combining Blink & Count to the bootloader

Post by RogerClark » Mon Jun 19, 2017 10:20 pm

I have cygwin but its a big package for other people to install just to combine files

I've written some code to do it, But, I've not tested it yet.

Please feel to tell me if you see any mistakes

Note. My compiler didnt like the type unit8_t so I have declared the buffer as void *, and I'm not sure that will work.

I'll need to find out where unit8_t is defined,

Code: Select all

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fpBootloader;
FILE *fpSketch;
FILE *fpOutput;
int sketchSize;
int bootloaderSize;
void *buf;
const int SKETCH_OFFSET = 0x2000;


    if (argc!=3)
    {
          printf("mergebin bootloader.bin sketch.bin output.bin\nNote. The sketch start address is presumed to be 0x2000 from the start of the binary file\n");
          return -1;
    }

    fpBootloader = fopen (argv[1], "r");
    if (fpBootloader==NULL)
    {
        printf("Unable to open bootloader binary file %s\n",argv[1]);
        return -2;
    }
    fpSketch = fopen (argv[2], "r");
    if (fpSketch==NULL)
    {
        fclose(fpBootloader);
        printf("Unable to open sketch binary file %s\n",argv[2]);
        return -2;
    }
    fpOutput = fopen (argv[3], "w");
    if (fpSketch==NULL)
    {
        fclose(fpBootloader);
        fclose(fpSketch);
        printf("Unable to open output file %s for writing \n",argv[3]);
        return -2;
    }

    fseek(fpSketch, 0, SEEK_END); // seek to send of sketch to determine its length
    sketchSize = ftell (fpSketch);
    fseek(fpSketch, 0, SEEK_SET); // go back to start, so we can read from the beginning

    buf=malloc(sketchSize + SKETCH_OFFSET);
    if (buf==NULL)
    {
        fclose(fpBootloader);
        fclose(fpSketch);
        fclose(fpOutput);

        printf("Unable to open allocate memoery\n");
        return -3;
    }


    fseek(fpBootloader, 0, SEEK_END); // seek to send of bootloader to determine its length
    bootloaderSize = ftell (fpBootloader);
    fseek(fpBootloader, 0, SEEK_SET); // go back to start, so we can read from the beginning
    fread(buf,bootloaderSize,1,fpBootloader);
    fclose(fpBootloader);

    fread(buf + SKETCH_OFFSET,sketchSize,1,fpSketch);
    fclose(fpSketch);

    fwrite(buf,sketchSize+SKETCH_OFFSET,1,fpOutput);

    fclose(fpOutput);
    return 0;
}

User avatar
Rick Kimball
Posts: 1060
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Combining Blink & Count to the bootloader

Post by Rick Kimball » Mon Jun 19, 2017 11:04 pm

why not a script that just loads the bootloader at 0x8000000 and then loads the blink at 0x8000000+8192. Two steps instead of one
-rick

User avatar
RogerClark
Posts: 7500
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Combining Blink & Count to the bootloader

Post by RogerClark » Mon Jun 19, 2017 11:06 pm

Rick Kimball wrote:
Mon Jun 19, 2017 11:04 pm
why not a script that just loads the bootloader at 0x8000000 and then loads the blink at 0x8000000+8192. Two steps instead of one
Depends on how people load the file.

I a lot of people use STM's Windows GUI tool

I just have to make this idiot proof as much as possible for all platforms

User avatar
RogerClark
Posts: 7500
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Combining Blink & Count to the bootloader

Post by RogerClark » Mon Jun 19, 2017 11:09 pm

BTW.

If I have time I'll make the utility general purpose

e.g. mergebin in1,offset1,in2,offset2,in3,offset3, out

User avatar
RogerClark
Posts: 7500
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Combining Blink & Count to the bootloader

Post by RogerClark » Mon Jun 19, 2017 11:25 pm

Umm.

One thing I'd not considered is that I wanted to Blink the LED but it varies, so I can't use the same sketch, or the sketch has to determine the LED by inspecting the bootloader.

ummm. More complications.

If there is room in the bootloader, I may just put a const string which has an ident in it, and also use that to read the pin of the LED

User avatar
RogerClark
Posts: 7500
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Combining Blink & Count to the bootloader

Post by RogerClark » Tue Jun 20, 2017 12:10 am

There were some bugs in my first attempt. I didnt set binary read mode (doh)!


But I think its working now, and here is a combined binary (attached)

I'm not sure if the code is cross platform, (and I'd like to make it general purpose if I have time)

Note. This code is not a sketch, it needs to be compiled on windows using GCC or similar.
I used Code::Blocks

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(int argc, char **argv)
{
FILE *fpBootloader;
FILE *fpSketch;
FILE *fpOutput;
int sketchSize;
int bootloaderSize;
uint8_t *buf;
const int SKETCH_OFFSET = 0x2000;


    if (argc!=4)
    {
          printf("\nUsage. mergebin bootloader.bin sketch.bin output.bin - Incorrect number of args\n\n");
          return -1;
    }

    fpBootloader = fopen (argv[1], "rb");
    if (fpBootloader==NULL)
    {
        printf("Unable to open bootloader binary file %s\n",argv[1]);
        return -2;
    }

    fseek(fpBootloader, 0, SEEK_END); // seek to send of bootloader to determine its length
    bootloaderSize = ftell (fpBootloader);
    fseek(fpBootloader, 0, SEEK_SET); // go back to start, so we can read from the beginning
    printf("Opened bootloader file %s : size is %d\n",argv[1],bootloaderSize);

    fpSketch = fopen (argv[2], "rb");
    if (fpSketch==NULL)
    {
        fclose(fpBootloader);
        printf("Unable to open sketch binary file %s\n",argv[2]);
        return -2;
    }


    fseek(fpSketch, 0, SEEK_END); // seek to send of sketch to determine its length
    sketchSize = ftell (fpSketch);
    fseek(fpSketch, 0, SEEK_SET); // go back to start, so we can read from the beginning
    printf("Opened sketch file %s : size is %d\n",argv[2],sketchSize);

    buf=malloc(sketchSize + SKETCH_OFFSET);
    if (buf==NULL)
    {
        fclose(fpBootloader);
        fclose(fpSketch);
        printf("Unable to open allocate memory\n");
        return -3;
    }

    printf("Allocated %d bytes for buffer\n",sketchSize + SKETCH_OFFSET);

    fread(buf,bootloaderSize,1,fpBootloader);
    fclose(fpBootloader);

    fread(buf + SKETCH_OFFSET,sketchSize,1,fpSketch);
    fclose(fpSketch);

    fpOutput = fopen (argv[3], "wb");
    if (fpOutput==NULL)
    {
        fclose(fpBootloader);
        fclose(fpSketch);
        printf("Unable to open output file %s for writing \n",argv[3]);
        return -2;
    }

    fwrite(buf,sketchSize+SKETCH_OFFSET,1,fpOutput);

    fclose(fpOutput);
    printf("Wrote combined file to %s \n",argv[3]);

    return 0;
}

Attachments
combined_pc13.zip
(13.24 KiB) Downloaded 15 times

User avatar
RogerClark
Posts: 7500
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Combining Blink & Count to the bootloader

Post by RogerClark » Tue Jun 20, 2017 7:11 am

I have given up on the idea of making the sketch part of the combined bootloader, blink the LED. Because I'd need separate sketches for each board.


I think I'll just compile a blank sketch, of just one that counts, and displays a message, as that would work for all boards.

User avatar
ahull
Posts: 1663
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Combining Blink & Count to the bootloader

Post by ahull » Tue Jun 20, 2017 11:29 am

Could you not specify the two pins for the button and the LED in the make script for the bootloader and the blink, making a bootloader then becomes something like.

mkbootloader BUTTON_PIN LED_PIN, which calls the routines to compile the bootloader and blink, and then merges them together into a single .bin file?

This would obviate the need to maintain a whole bunch of pre-compiled bootloaders in the first place. In fact you could take the idea one stage further and add a lookup table for common boards, so something like mkbootloader blue_pill or mkbootloader maple_mini
- Andy Hull -

User avatar
RogerClark
Posts: 7500
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Combining Blink & Count to the bootloader

Post by RogerClark » Tue Jun 20, 2017 12:00 pm

Hi Andy

I'm not sure what you mean about the Make.

I already have a Makefile which can build all the targets, albeit I don't know enough about Makefiles so that the "Make all" makes all bootloaders

I suppose it would be possible to build the custom Blink sketch, but it would need all the LibMaple core etc, which is not currently inside the bootloader folder.

I think its easier just to have a precompiled blank.bin for all generic boards (not Maple mini etc), and then get the Makefile to do the combining.

But unfortunately I'm not very good at writing Makefiles.

Post Reply