-1

We develop an iot door access control system with ESP8266 on Platfromio IDE. You can see all our works on esp-rfid GitHub repository. We want to use ota updates on our systems but the code cover almost half of the memory sometimes bigger then half. If the code cover bigger than half of the memory we can't use ota update. How we can reduce code size?

Thank for your answers.

You can find the code from there.

The build results:

DATA: [==== ] 43.4% (used 35592 bytes from 81920 bytes) PROGRAM: [===== ] 48.6% (used 507732 bytes from 1044464 bytes)

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 1
    did you play around with compiler flags? – Marek R Aug 07 '18 at 14:11
  • 2
    Generate a map file, figure out what is largest in your code, and focus on that. This is far too broad to answer here. – Retired Ninja Aug 07 '18 at 14:13
  • You could have a small bootloader that just checks for OTA update (and downloads it if it available) and does nothing else. If no update is available, or the current update has finished downloading, it loads the main program. – Spoonless Aug 07 '18 at 14:13
  • @MarekR we tried O3 O2 O1 but binary size increased – Ayberk Karaakın Aug 07 '18 at 14:21
  • 2
    Create and look at the map file. I usually parse the map file into a CVS file so you can use excel to sort things. Convert all addresses to decimal values and add these to the CVS. If you do not get sizes sort into order and assume size is difference to next symbol. Filter obvious errors. Keep the hex as strings. Look at the biggest objects first and and look for savings there. Over use of inlining can cause bloat. Although inlined functions can be faster and in some cases smaller than the function call. Delete dead code. Create common function where code is repeated. – William J Bagshaw Aug 07 '18 at 14:23
  • A common issue is "just in case" virtual methods. Only make methods that need to be virtual virtual. – William J Bagshaw Aug 07 '18 at 14:25
  • Look for repeated instantiations of the same function. This can be due to static methods, inlined functions that are not actually inlined and other such issues. – William J Bagshaw Aug 07 '18 at 14:27
  • @AyberkKaraakın flags `O3 O2 O1` are for optimizing code speed not code size! – Marek R Aug 07 '18 at 14:38
  • [possible duplicate](https://stackoverflow.com/q/200292/1387438). For now I can't mark it using `close`. – Marek R Aug 07 '18 at 14:42

2 Answers2

2

If the code cover bigger than half of the memory we can't use ota update.

Sure you can. Update to a very small program that consumes very little memory, and whose only job is to do another update to the next version of your actual program.

How we can reduce code size?

That's hard to say without knowing anything about your code.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • You can find the code from https://github.com/omersiar/esp-rfid/blob/dev/src/main.cpp also you can clone the project from https://github.com/omersiar/esp-rfid.git – Ayberk Karaakın Aug 07 '18 at 14:23
  • 5
    You seem to be confused about the purpose of this site. We're not a free software consulting agency. – Caleb Aug 07 '18 at 14:28
  • Agree on this,assuming you use httpupdate. Create two bin file, 1. update bin file, 2. main program. once update was triggered, the program will update to 1 then after updating to 1, it will update to 2. – quickbrown May 12 '21 at 04:50
0

Without looking at the source code I can tell you that some of the C++ features are less expensive as memory consumption than others for example: namespaces, classes (especially generic ones) are cheap while exception handling for example is very expensive.

Some tips:

  • when generating the code make sure you strip all the debug symbols
  • use generics since only the code you are using gets converted into binary in your executable not the whole library
  • check compiler/linker options to see how you can reduce that even more
  • You can find the code from https://github.com/omersiar/esp-rfid/blob/dev/src/main.cpp also you can clone the project from https://github.com/omersiar/esp-rfid.git – Ayberk Karaakın Aug 07 '18 at 14:26