since a more complex code give me HardFault when debugger is active
Ok, so the core is halted on entry to HardFault handler.
Now what is the value of HFSR and CFSR? Those indicate the cause of that fault.
I've this values:
Address 0xE000ED2C HFSR - 0x40000000
Address 0xE000ED28 CFSR - 0x00008200
But I've also recorded all Fault Register Values several times, this is values I get:
stacked_r0 0x60000001 0x60000001 0x404086f1 0x404086f1 0x64000000 0x00403ee9
stacked_r1 0xfffffff9 0xfffffff9 0xfffffff9 0xfffffff9 0xfffffff9 0xfffffff9
stacked_r2 0x00045c13 0x00045c13 0xffffffff 0xffffffff 0x20001142 0x40460e00
stacked_r3 0x00000000 0x00000000 0x41600000 0x41600000 0x00000000 0x10000000
stacked_r12 0x0000f00d 0x0000f00d 0x0000f00d 0x0000f00d 0x64000004 0x00000001
stacked_lr 0x60000001 0x60000001 0x404086f1 0x404086f1 0x64000000 0x00403ee9
stacked_pc 0x00000001 0x00000001 0x79ee6ed6 0x7d759044 0xfffffff8 0x20001600
stacked_psr 0x004011e5 0x004011e5 0x00401369 0x00401335 0x00404b97 0x0040405d
_CFSR 0x00000100 0x00000100 0x00000100 0x00000100 0x00000400 0x00000400
_HFSR 0x40000000 0x40000000 0x40000000 0x40000000 0x40000000 0x40000000
_DFSR 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
_AFSR 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
_MMAR 0xe000ed34 0xe000ed34 0xe000ed34 0xe000ed34 0xe000ed34 0xe000ed34
_BFAR 0xe000ed38 0xe000ed38 0xe000ed38 0xe000ed38 0xe000ed38 0xe000ed38
1° and 2° was an ADC initialization call, 3° and 4° at same point when calling I2C read, 5° when writing an array(not related to any peripherals), 6° related to GPIO pins.
Errors triggers on different point, some time on the same, but most of time at random.