Author Topic: Multiple programs in 1 moteino  (Read 2159 times)

Uncle Buzz

  • Full Member
  • ***
  • Posts: 146
  • Country: fr
Multiple programs in 1 moteino
« on: October 19, 2017, 10:33:48 AM »
Hi all,
As I have some difficulties to put all my functions and libraries in the 32k ROM of the AtMega328p, I wonder if it could be possible to switch the sketch running like the OTA changes the sketch from one writen in flash memory, but keeping both (or more) sketchs in flash to come back to the sketch we want ?
By example, having a sketch for production, a sketch for maintenance, a sketch for ... I run production sketch, but a command (radio, serial or whatever) tells my sketch to reboot and flash the 2nd sketch, so at startup, I use a new sketch for maintenance purpose for my ewample, then a command can reboot the moteino with flashing back the production sketch ?
In my mind, it's similar to the OTA procedure, so it's why I ask it here...
Is this something someone have already envisaged ?
Sorry if my english is quite hard to understand, don't hesitate to rewrite it if it's needed.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Multiple programs in 1 moteino
« Reply #1 on: October 19, 2017, 11:50:57 AM »
Hi all,
As I have some difficulties to put all my functions and libraries in the 32k ROM of the AtMega328p, I wonder if it could be possible to switch the sketch running like the OTA changes the sketch from one writen in flash memory, but keeping both (or more) sketchs in flash to come back to the sketch we want ?
By example, having a sketch for production, a sketch for maintenance, a sketch for ... I run production sketch, but a command (radio, serial or whatever) tells my sketch to reboot and flash the 2nd sketch, so at startup, I use a new sketch for maintenance purpose for my ewample, then a command can reboot the moteino with flashing back the production sketch ?
In my mind, it's similar to the OTA procedure, so it's why I ask it here...
Is this something someone have already envisaged ?
Sorry if my english is quite hard to understand, don't hesitate to rewrite it if it's needed.
You could easily do this if you write the code to manage the images.  You would need to modify the WirelessHex library to store the images in upper pages of flash, rather than lower (or, you could leave it as is, but, after disabling the header,  copy the lower block into a 'save' area in flash) and then, when you want to boot to a different image, you'd copy that saved image into the lowest page, update the header once the image is successfully transferred, and then reset the moteino, this will cause the bootloader to flash the next image.

I'm not sure how this would make any of your images smaller, however.

Tom


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Multiple programs in 1 moteino
« Reply #2 on: October 19, 2017, 01:07:33 PM »
Uncle Buzz,
What size are the sketches relative to the 32K?
If they are roughly 1/3 of the size, or a known expected size (ex the production is 15K, maintenance 10K, other sketch 5K) then you could preset starting points in memory, and use some GPIO/user input to jump to each location. I haven't tried this so obviously I'm just theorizing but it's in line with what Tom suggested.

Tom,
I'm impressed with your knowledge of the wireless flashing library :)

joelucid

  • Hero Member
  • *****
  • Posts: 868
Re: Multiple programs in 1 moteino
« Reply #3 on: October 19, 2017, 04:15:57 PM »
Hi all,
As I have some difficulties to put all my functions and libraries in the 32k ROM of the AtMega328p, I wonder if it could be possible to switch the sketch running like the OTA changes the sketch from one writen in flash memory, but keeping both (or more) sketchs in flash to come back to the sketch we want ?
By example, having a sketch for production, a sketch for maintenance, a sketch for ... I run production sketch, but a command (radio, serial or whatever) tells my sketch to reboot and flash the 2nd sketch, so at startup, I use a new sketch for maintenance purpose for my ewample, then a command can reboot the moteino with flashing back the production sketch ?
In my mind, it's similar to the OTA procedure, so it's why I ask it here...
Is this something someone have already envisaged ?
Sorry if my english is quite hard to understand, don't hesitate to rewrite it if it's needed.

Yeah this is easily done. I created a little bootloader like that to implement a test suite for a radio library. I write multiple images to flash - in my specific case a loader which downloads a test image and stores it in flash. In my case the test image is same loader but linked against the most recent radio lib. This loader then downloads a second test image and runs it. That way the loader itself becomes part of the test suite. the 1st loader instructs the bootloader to activate the 2nd loader and that one asks it to run the test image. If either test image fails to confirm it successfully ran, the bootloader will automatically reinstate the failsafe 1st loader on next (wdt triggered) boot.

Joe

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Multiple programs in 1 moteino
« Reply #4 on: October 19, 2017, 04:38:50 PM »
Tom,
I'm impressed with your knowledge of the wireless flashing library :)
Felix, you would be astounded to learn of all the things I've forgotten over the years!

Tom

Uncle Buzz

  • Full Member
  • ***
  • Posts: 146
  • Country: fr
Re: Multiple programs in 1 moteino
« Reply #5 on: October 20, 2017, 03:24:35 AM »
Thank you for your answers !
So, I don't need to change the bootloader, but I need to arrange the WirelessHex library to manage my differents images, having the first 32k of flash memory for the image to flash, and some reserved space in my flash memory to keep all the images I need.
To change image, I need to copy the chosen image in first 32k as the OTA do, and reboot, all my sketchs should have the mechanism to copy other image in first 32k to switch.
Did I understood what you said ?
Right now my sketch is over 32k and I have to comment some parts when I want to test some others, and not all functions are implemented, but with the possibility to divide functions in different programs should help me to have many images smaller.
I don't know the size of my final sketchs, my question was quite generic, so I have to reserved some 32k block for each image I want to keep in memory, plus the first 32k for the image to flash at boot time.
Do I have to start from the RFM69_OTA or from the old Wireless library ?

Joe, could you send me the code you did for managing your image, so it would be easier for me to start.

Thanks to all, more advices are welcome !

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Multiple programs in 1 moteino
« Reply #6 on: October 20, 2017, 09:45:43 AM »
Thank you for your answers !
So, I don't need to change the bootloader, but I need to arrange the WirelessHex library to manage my differents images, having the first 32k of flash memory for the image to flash, and some reserved space in my flash memory to keep all the images I need.
To change image, I need to copy the chosen image in first 32k as the OTA do, and reboot, all my sketchs should have the mechanism to copy other image in first 32k to switch.
Did I understood what you said ?
exactly right as long as you understand that you need to copy the image and the header.  The image is the raw image as it would be copied into your low memory of the 328P.  The header is a fixed length field that contains, among other things, signature, image size, number of blocks, and maybe starting address.  Look at the WirelessHEX include file for Header contents.
Quote
Right now my sketch is over 32k and I have to comment some parts when I want to test some others, and not all functions are implemented, but with the possibility to divide functions in different programs should help me to have many images smaller.
I don't know the size of my final sketchs, my question was quite generic, so I have to reserved some 32k block for each image I want to keep in memory, plus the first 32k for the image to flash at boot time.
Do I have to start from the RFM69_OTA or from the old Wireless library ?
No, the library doesn't matter, and, in fact, if you're inventive, you don't even need to use the Wireless Hex library.  All you need to do is duplicate what it does in copying an image to that first region of flash chip.

Tom

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Multiple programs in 1 moteino
« Reply #7 on: October 20, 2017, 03:48:47 PM »
Just FYI guys, the WirelessHEX is deprecated as of some time. The new library for OTA is RFM69_OTA.h, included in the RFM69 lib.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Multiple programs in 1 moteino
« Reply #8 on: October 20, 2017, 04:22:24 PM »
Just FYI guys, the WirelessHEX is deprecated as of some time. The new library for OTA is RFM69_OTA.h, included in the RFM69 lib.
Ah, I didn't know that.  Thanks for the update! 

Tom