Bootloader/Self write updates via WiFi

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
bernardj
Posts: 10
Joined: Fri Jul 10, 2015 7:52 pm

Bootloader/Self write updates via WiFi

Post by bernardj » Mon Apr 17, 2017 12:14 pm

Hi

I would like to perform over-the-air updates of my various home automation boards via WiFi. The boards run on 18F26K22 and connect to my Wifi SSID using ESP8266 modules. It all works well.

Before the bootloader can start, I need to establish the TCP connection via WiFi link, then connect to the PC, whereafter data will become available via the PIC's USART2 using some handshaking protocol between the PC and the PIC.

So, the PC will be ready, connected to the LAN, with an application open, waiting to start downloading the latest version via WiFi. The PIC starts, connects to the WiFi, opens the port on the PC, send a handshaking signal, which then trigger the download process in the PC application.

I have done a lot of reading on this topic. I can't get my head around how to first connect to the WiFi, then only perform the self-write flash updates.

The way I see it is that this is more of a self write function than a bootloader because the system has already ran a lot of code to get the WiFi connection going before the flash updates can start - maybe I am wrong.

What I am asking:
1) How do I prevent the bootloader/self write routine (somewhere in memory) to be overwritten by the flash update process? I need some way to force the position of the bootloader/self write routine to a specific location. How do I do that?
2) Would it be better and even possible to position the self write function in upper memory? If so, again, how do I force that routine to a specific address?
3) If the self write routine is in lower memory, how do I know its upper memory limit to ensure the self write updates do not overwrite the upper parts of the self write routine?

Regards

Bernard

Jerry Messina
Swordfish Developer
Posts: 1237
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Bootloader/Self write updates via WiFi

Post by Jerry Messina » Mon Apr 17, 2017 5:27 pm

The way I see it is that this is more of a self write function than a bootloader
If I follow what you're saying, the problem I see with that approach is that if the "self-write" routine doesn't have enough of the code in it to completely download and program a new image things can go belly-up fast, esp. since you're using a wireless connection.

What if your connection drops out half way through doing the update?

We can deal with how to locate the code once you have a good scheme.

bernardj
Posts: 10
Joined: Fri Jul 10, 2015 7:52 pm

Re: Bootloader/Self write updates via WiFi

Post by bernardj » Tue Apr 18, 2017 8:00 am

Hi Jerry, thanks for the reply.

I appreciate the consequences of a failed over-the-air update with the risk of an non-usable PIC thereafter.

The risk for this undertaking is very limited: I am developing this functionality purely for own use in my home environment - nothing commercial at all. I will be responsible for the maintenance of the boards.

I am sure many of the users on this forum go through the same thing as me, whereby we come up with improvements for the PIC firmware occasionally. I simply do not want to go through the pains of having to disconnect the board's cables, reprogram it inside the house with the new and improved version (using Pickit3) and then redeploy. That's where an over-the-air update capability will be super.

If an update fails and the PIC goes into an unusable state, then I would still have to do the manual process as set out above.

Hope this clarifies your concerns that were raised yesterday.

Regards

B

Jerry Messina
Swordfish Developer
Posts: 1237
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Bootloader/Self write updates via WiFi

Post by Jerry Messina » Tue Apr 18, 2017 10:12 am

Ok, as long as you're aware of the consequences.

The IDE builtin HELP topic you're looking for is under 'Language Reference | Code Relocation'
There you'll find '#option org_reset, org_program, vector_isr_hi, and vector_isr_lo'

You can find more info by searching the forum, like this one: need for ORG_RESET ORG_PROGRAM usage help

One thing I've done in situations where I didn't have space for a full-blown bootloader is to add a serial flash device and have the main app download the update code into that. After a successful download I transfer to the "self-write code" that's fixed in memory and let that do the reprogramming.

Let us know if you need anything else.

Jerry Messina
Swordfish Developer
Posts: 1237
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Bootloader/Self write updates via WiFi

Post by Jerry Messina » Tue Apr 18, 2017 3:33 pm

Sounds workable.

When you download the update you'll want to watch for the first few instructions at addresses 0-7. These'll contain the GOTO address of your application. Keep a copy of those instructions at a fixed location right below your upper memory bootloader and you can just do a GOTO there.

Post Reply