Helpful ReplyHot!bootloader for PIC24FJ128GC010

Page: 12 > Showing page 1 of 2
Author
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
2018/01/17 00:47:31 (permalink)
0

bootloader for PIC24FJ128GC010

Good morning,
in my device I mounted a PIC24FJ128GC010 and a flash memory with
which I communicate via SPI.
My target would be to load a bootloader at startup that loads
the firmware previously written into the flash memory and write
it in the program memory.
 
Logical scheme of what I would like to do is something like that:
            1) via uart/USB/something :) I receive the new FW
            2) I check it and write it into an external FLASH memory (via SPI)
            3) FW or HW RESET
            4) The bootloader starts first and updates the new FW
 
I don't need the bootloader also updates itself      
                       
I'm totally inexperienced with regard to bootloaders, can I ask you
advice on how to get started? where do I find step by step
documentation for a newbie?
In general what are the difficulties with bootloaders?
 
thanks 
Daniele
#1
NKurzman
A Guy on the Net
  • Total Posts : 16583
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/01/17 06:41:37 (permalink)
0
#2
Liem Dung Phan
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2017/09/14 21:09:39
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/01/17 07:17:51 (permalink)
0
Hi,
This is my source, hope to help you!
https://github.com/PLElectronics/XC16Training.git
#3
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/02/15 11:01:17 (permalink)
0
Thanks, i will try
#4
RISC
Super Member
  • Total Posts : 5322
  • Reward points : 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/02/15 15:25:26 (permalink)
0
Hi,
A much more recent 16 bits bootloader is here : http://www.microchip.com/ezbl
It is much easier to use as it does not require linker file ;=)
However, you'll need to adapt it for SPI operation
Regards
#5
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/06 02:00:47 (permalink)
0
After about a month of research, even if discontinuous, I have not yet reached the result.
The EZBL bootloader is a great resource, but it's not for my case.
First of all, it does not have the SPI and adapting it is really difficult for a newbie like me.
Especially if, as mentioned earlier, I miss the basics on bootloaders.
Then the guide exercises (which I tried) show a use that I thought was designed as a substitute
for the classic programming with PicKit or other programmers and not specific to make it custom.
A simple project, which could be for me, could be this:
 
https://www.codeproject.com/Articles/376725/Writing-A-Bootloader
 
in this case the bootloader consists of a simple project that receives the FW from the UART
(I would replace it with an external FLASH reading via SPI) and then jumps to a memory location
in which the FW is present through a goto instruction.
 
Some question...
1)in this discussion it is said: "To get a compiler to generate code for the device, you will need to tell it to put code at the 0×800 offset, otherwise"
of course...the space required for the bootloader must be left in memory!!! how??????????
i tried to use a -Ttext address in linker's options. 
in fact in the program memory I found as a first instruction a goto 0x800 instead of goto 0x200,
but the memory from 0x200 to 0x7FE was not completely empty as I expected! 
however, a small part of memory starting from address 0x76E to 0x7FE was filled!
(obviously the addresses from 0x00 to 0x1FE were rightly occupied from the interrupt vector)
 
2) if i compile the BL and the FW separately how can i create a single hex with Bl+FW?
In this case what about the addresses occupied by the interrupts vector?
 
 
 
 
#6
DPerez
Junior Member
  • Total Posts : 81
  • Reward points : 0
  • Joined: 2014/01/15 00:26:02
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/06 02:27:24 (permalink) ☄ Helpfulby aliaskappa 2018/04/10 03:41:03
5 (1)
Hello aliaskappa,
If you want to avoid the "EZBL bootloader" (as I did in his day), my recommendation is to go step by step, and to split up the big problem in some more little ones.
 
So, the first target you should accomplish is a complete procedure of storing the new program (hex file) inside your external flash memory, checking with a read sequence that everything went well (written == read) -> files for SPI and external flash memory are mandatory here (apart from clocks and more stuff that could be needed).


After that, you should focus on the "Intel Hex" protocol, which is related to the hex file that you will write inside the internal flash of the choosen microcontroller (and the sequence to write it of course), but we could talk about this when you have complete the other task.
 
Best regards.
Daniel.
#7
NKurzman
A Guy on the Net
  • Total Posts : 16583
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/06 05:44:23 (permalink)
0
There is also an microchip Application note for a PIC24 bootloader
#8
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 00:22:58 (permalink)
0
Hi Dperez!
 
in my fw I have already implemented a USB-cdc communication and a GPRS communication through a serial module. With these I can already receive/send data with the microcontroller, set parameters that are saved in an external EEPROM and/or in the external FLASH memory.
 
I have already implemented a checksum control in the communication but as you rightly recommend a READ = WRITE check in this case is necessary!
 
Is there something to be careful about the procedure of storing the new program?
Can I simply copy bit by bit into flash memory?
 
thanks
D.
 
#9
DPerez
Junior Member
  • Total Posts : 81
  • Reward points : 0
  • Joined: 2014/01/15 00:26:02
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 00:58:55 (permalink) ☄ Helpfulby aliaskappa 2018/04/10 03:41:21
5 (1)
Hello aliaskappa,
 
in my fw I have already implemented a USB-cdc communication and a GPRS communication through a serial module. With these I can already receive/send data with the microcontroller, set parameters that are saved in an external EEPROM and/or in the external FLASH memory.

 
That sounds great!! You are following the same path as I did (in my case was a UART debug TX/RX and a RN4871 BLE module as a reception ways, but in the end it is the same).
 
I have already implemented a checksum control in the communication but as you rightly recommend a READ = WRITE check in this case is necessary!

 
Terrific!! With that, and the CRC already included in every Intel Hex line, you will be quite sure that your exchange is going to be without any problem. Nevertheless, you can improve your exchange data protocol with some stuff that could be useful (for instance, an additional exchange to say that you are sending X lines that will have to be checked on the other side of the communication, whatever you like).
 
Is there something to be careful about the procedure of storing the new program?
Can I simply copy bit by bit into flash memory?

 
Yes, why not? You can check bits, bytes, lines, groups of lines... it actually depends on you of what you are interested in checking.
 
Tip: The exchange has to take as much time as needed. Just remember that this vital information will be the one used to program your device. In my humble opinion, I think that in this step we must sacrifie time, resources and so on to be sure that the future firmware is perfectly recorded, because otherwise, in case that the device will be unreachable (for example, a streetlight), we will lost it forever!!!
 
According to me, you are on the right path!! Keep working and feel free to ask me whatever you want to!!
 
Best regards.
Daniel.
 
#10
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 03:11:08 (permalink)
0
thank you DPerez, 
your words give me courage!
 
in the day I will try to write the firmware in the external memory and I let you know!
 
a small question ... how should I compile the firmware in anticipation of the bootloader? how do I set up a shift in the program memory?
#11
DPerez
Junior Member
  • Total Posts : 81
  • Reward points : 0
  • Joined: 2014/01/15 00:26:02
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 03:38:31 (permalink)
0
Hello aliaskappa,
 
a small question ... how should I compile the firmware in anticipation of the bootloader? how do I set up a shift in the program memory?

 
Could you explain that a little bit more? I do not fully understand what you are attempting to ask.
 
Best regards.
Daniel.
post edited by DPerez - 2018/04/09 03:39:54
#12
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 03:57:49 (permalink)
0
I apologize DPerez!

I try to explain better, when compile the firmware in the debug version and I look into the memory program as a first instruction I see a goto 0x200, how can a change it? for example in 0x800...
my target would be to free the memory from 0x200 to 0x7ff, space that I would like to dedicate to boot
 
Thanking you in advance (for my strange question)
D.
#13
DPerez
Junior Member
  • Total Posts : 81
  • Reward points : 0
  • Joined: 2014/01/15 00:26:02
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 04:21:19 (permalink) ☄ Helpfulby aliaskappa 2018/04/10 03:41:34
0
Hello aliaskappa,
I believe I am understanding your doubt, so take a look at that part of my bootloader code:
 

#define addApp 0x4400

#define STRINGIZE2(X) #X
#define STRINGIZE(X) STRINGIZE2(X)
 
    ...   
 
    if((strstr(receiveBuffer, NEW_FW_MESSAGE) != NULL) && ((RCON & 0xC3) != 0)) {
        #ifdef DEBUG_INFO
            UARTPrintString(debug, "Bootloader check - The device needs to be "
                    "updated\n");
        #endif
    }
    else {
        /* No message will be shown here unless an application has been
         * programmed before
         */
        __asm__("goto " STRINGIZE(addApp));
    }
 
    /* aliaskappa should place his code here in order to read external flash and to write lines on the microcontroller */
 

 
As you can see, in the reboot statements, I am checking a variable written at the end of my external flash memory (which is read before), and moreover, I am checking if there is a reboot as well. If both statements are accomplished, then I execute the internal programing procedure: 1- read from external flash; 2- write it to the internal flash.
 
(You can feel free to put your own detonator, that was only an example).
 
If not, a goto order is being executed with the main purpose of jumping to the old user application, because an update in this case is unneeded. You can select these addresses as you wish, that is totally up to you... but this should be a part which has to be done after ending the storage of the new firmware and also after having functions to write the internal memory of the microcontroller.
 
I hope this helps.
 
Best regards.
Daniel.
#14
rodims
Super Member
  • Total Posts : 1377
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: online
Re: bootloader for PIC24FJ128GC010 2018/04/09 04:39:48 (permalink)
3 (1)
Logical scheme of what I would like to do is something like that:
            1) via uart/USB/something :) I receive the new FW
            2) I check it and write it into an external FLASH memory (via SPI)
            3) FW or HW RESET
            4) The bootloader starts first and updates the new FW
...
 
in the day I will try to write the firmware in the external memory and I let you know!

 

in my fw I have already implemented a USB-cdc communication and a GPRS communication through a serial module. With these I can already receive/send data with the microcontroller, set parameters that are saved in an external EEPROM and/or in the external FLASH memory.

 
It's not clear to me whether your point 2) I check it and write it into an external FLASH memory (via SPI)
is a requirement for your application (safety or e.g. new firmware received via GPRS) or whether you just think that's the way a bootloader should work.
(since you emphasized in January that you are not experienced with bootloaders)
 
In the case you do not really need to use an extra external flash memory and firmware updates always can be done via USB:
Since your board seems to support USB with your GC010, did you check / read about the Microchip USB HID Bootloader contained in the MAL (Microchip Application Library, current version v2017-03-06) ?
 
 
 
#15
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 06:10:34 (permalink)
0
ok DPerez, wow!! remind me to offer you a dinner wherever you are!!
I perfectly understand your code! simple and clear as I like it!
 
a clarification on "#define addApp 0x4400" ... 0x4400 is the address where your app starts, right?
in this case,do you compile the bootloader and application firmware separately?
If so, how do I adjust the start address in the application fw (for example 0x4400 instead the usual 0x0200)? 
and interrupt addresses? they still remain from 0x0001 to 0x01ff?
--------------------------------------------------------------------------
hi rodims,
thanks for your attention!
I know that it is not  necessary to save my FW in an external FLASH memory, but in my application I receive the FW through a GPRS connection that could be slow or even could be interrupted.
So I and my colleagues have opted to save it in this memory (that communicates via SPI) and check that the FW has been correctly received.
At this point I reset the device with the RESET sw command, so that the boot loader can perform the update.
 
 
 
#16
DPerez
Junior Member
  • Total Posts : 81
  • Reward points : 0
  • Joined: 2014/01/15 00:26:02
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/09 07:08:17 (permalink)
5 (1)
Hello aliaskappa,
 
remind me to offer you a dinner wherever you are!!

 
A beer will be good enough grin: grin... Anyway, coming back to the main topic:
 
a clarification on "#define addApp 0x4400" ... 0x4400 is the address where your app starts, right?

 
Exactly, you are right.
 
in this case,do you compile the bootloader and application firmware separately?


Yes I do, I usually have two projects in my MPLABX: the bootloader that is always uploaded on every device, and besides, the user program which obviously, depends on the final application.
 
If so, how do I adjust the start address in the application fw (for example 0x4400 instead the usual 0x0200)? 
and interrupt addresses? they still remain from 0x0001 to 0x01ff?

 
The last step is to modify the GLD files, which are meant to define the memory blocks of the microcontroller. It is quite soon to see it with further details, but it could be good if you have a general idea of how you are going to separate both parts.

Moreover, the MPLABX have one option to achieve programming for the first time two projects without overwriting the bootloader: project properties/ICD3/Preserve Program Memory (Range); by this, you will be able to program the bootloader and the application for the first time without any troubles.
 
I hope this helps.
 
Best regards.
Daniel.
 
#17
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/10 00:18:29 (permalink)
5 (1)
for the moment, I thank you DPerez. I will initially try to build a bootloader that reads and writes "something" into the program memory at the point I want.
Later I will try to understand how to use the mplab function that you introduced to me, i think is explained here:
 
http://microchipdeveloper.com/pickit3:select-memory-to-program
 
I'll keep you updated ... maybe with a bootloader, and a good beer !!!mr green: mr green
 
D.
#18
DPerez
Junior Member
  • Total Posts : 81
  • Reward points : 0
  • Joined: 2014/01/15 00:26:02
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/10 00:45:54 (permalink)
5 (1)
Hello aliaskappa,
Sorry for not attaching a link as good as yours, I tried to explain in my own way but in fact, the link is actually much better.
 
Go ahead, I wish you the best of luck, and I will be looking forward to hearing from you.
 
Best regards.
Daniel.
post edited by DPerez - 2018/04/10 00:58:17
#19
aliaskappa
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2017/10/25 06:07:08
  • Location: 0
  • Status: offline
Re: bootloader for PIC24FJ128GC010 2018/04/10 03:18:21 (permalink)
0
something strange...
I literally copied the example6-6 from the datasheet to program a single word of flash program memory.
I do not understand why it should not work!
 
void write_program_memory(void)
{
    unsigned int offset;
    unsigned long progAddr = 0x000500; // Address of row to write
    unsigned int writeWordL = 0x2222; // Data to program lower word
    unsigned char writeByteH = 0x00; // Data to program upper byte
    
// //for erasing
//
// //Set up pointer to the first memory location to be written
// TBLPAG = progAddr>>16; // Initialize PM Page Boundary SFR
// offset = progAddr & 0xFFFF; // Initialize lower word of address
// __builtin_tblwtl(offset, 0x0000); // Set base address of erase block with dummy latch write
// NVMCON = 0x4042; // Initialize NVMCON
// __builtin_disi(5); // Block all interrupts with priority <7 for next 5 instructions
// __builtin_write_NVM(); // check function to perform unlock sequence and set WR
    
    
    //for writing
    
    //Set up NVMCON for word programming
    NVMCON = 0x4003; // Initialize NVMCON
    
    //Set up pointer to the first memory location to be written
    TBLPAG = progAddr>>16; // Initialize PM Page Boundary SFR
    offset = progAddr & 0xFFFF; // Initialize lower word of address
    
    __builtin_tblwtl(offset,writeWordL); // Write to address low word
    __builtin_tblwth(offset,writeByteH); // Write to upper byte
    __builtin_disi(5); // Block all interrupts with priority <7 for next 5 instructions
    __builtin_write_NVM(); // check function to perform unlock sequence and set WR
    
}
 
 
 
int main(void) {
 
 
 
write_program_memory();

while(1){};
 
 
 
return 0;
 

}
 

 

I assure you that the address where I write was empty.

I tested it by debugging, checking the program memory and reading it with the appropriate "read the device memory" command
 
D.
post edited by aliaskappa - 2018/04/10 03:29:04
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2018 APG vNext Commercial Version 4.5