3

I'm using an STM32F100 value line processor with IAR. I'd like to fill the unused code memory with a jump to the Reset_Handler (or HardFault_Handler). How do I do this using IAR?

artless noise
  • 21,212
  • 6
  • 68
  • 105
amo
  • 4,082
  • 5
  • 28
  • 42

2 Answers2

3

Most hex file generators will fill memory if requested, you can probably do that within your IDE, if not the SRecord tools can certainly do that for you.

However simply leaving unused flash blank (0xffff) will generate an invalid instruction exception and minimises your image size.

Note that jumping to the reset handler is not the same as performing a reset - the hardware state will be non-deterministic. A better way perhaps is to enable the watchdog and fill the memory with B . (branch to self) 0xE7FE. The watchdog will then timeout and create a true reset.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • In this case, generating an exception is not what we want. In a system that must continue to operate without intervention, it needs to reset itself safely in the event of an exception. During development, we'll allow it to crash in order to find and fix such bugs, but in deployment, it needs to recover on its own. Your watchdog idea is interesting, I'll try that. – amo Nov 07 '13 at 16:24
  • 2
    But if you can generate an exception, then the exception handler can request a system reset via the System Control Block, which should ultimately end up at the reset handler (I think). – andy mango Nov 07 '13 at 19:16
  • Are you sure `0xFFFF` is an invalid instruction? IAR always seems to happily disassemble it for me, and it always seems to me that the core will execute it until it hits unimplemented addresses when my programs end up in the weeds. – rjp Nov 08 '13 at 02:51
  • @RJP: No I'm not, but setting the PC to a blank area in my experience causes the processor to reset - probably by means other than invalid instruction. – Clifford Nov 08 '13 at 11:25
1

Assuming you are using the Embedded Workbench, under the Project Options -> Linker category, there is a "Checksum" tab which has options to fill unused memory as well as compute a checksum of memory, etc. Of course this begs the question of why you want to fill memory with a handler address. If you want to trap stray execution, then that is what the Memory Protection unit is good for.

andy mango
  • 1,526
  • 1
  • 8
  • 13
  • 2
    The STM32F1 series does not include a Memory Protection Unit. – Clifford Nov 07 '13 at 11:56
  • I did see that memory filling option in the options. The problem I have with that is that the location in memory of Reset_Handler changes with every compile. I'm hoping someone knows the trick to having that happen automatically. – amo Nov 07 '13 at 16:26
  • No Memory Protection Unit on the STM32F1 series. Sad. Very useful for critical systems. – andy mango Nov 07 '13 at 19:17
  • You could fill it with an invalid instruction, trap the fault and trigger a reset. Unfortunately, actually triggering a reset on CM3 is several instructions, so you can't really fill with a one-instruction reset. If you aren't using `SVCall`, you could use it to handle the reset and fill code memory with the instruction `SVC 0`. – rjp Nov 08 '13 at 02:49
  • @amo: The location of the exception handler should not matter if you can actually force the exception rather than code a jump. – Clifford Nov 08 '13 at 11:28