Recent

Author Topic: dynamic memory on a bluepill  (Read 606 times)

chucky

  • New Member
  • *
  • Posts: 30
dynamic memory on a bluepill
« on: October 08, 2022, 09:14:05 pm »
Hi all,

I managed to get the blinking led example to work:

program stmpal;

{$O-}
var
  p : Pointer;
  MyInt : Integer;

procedure delay;
var
  i : uint32;
begin
  for i := 0 to 500000 do
    begin
      asm
        nop   // empty instruction
      end;
    end;
end;

begin
  // turn on Port C
  RCC.APB2ENR := RCC.APB2ENR or (%1 shl 4);

  // Pin 13 from Port C to Output

  PortC.CRH := $00300000;

  MyInt := 350;     // works as expected.
  GetMem(p,4);   // crashes here.

  // Note: The LED lights up at LOW.
  while true do
    begin
      // Pin 13 - High
      PortC.BSRR := 1 shl 13;
      Delay;

      // Pin 13 - Low
      PortC.BRR := 1 shl 13;
      Delay;
    end;
 end.

However if I add a getmem statement as shown above it crashes. Im at a loss to understand where to proceed. Im assuming I have to do some extra intialization for the getmem statement to work properly. I can add static vars and they work but if i try to reserve memory on the heap to store a value there it crashes.

What am i doing wrong, or not doing? Can someone please point me in the right direction?


MiR

  • Full Member
  • ***
  • Posts: 234
Re: dynamic memory on a bluepill
« Reply #1 on: October 08, 2022, 09:15:24 pm »
You need to include heapmgr

chucky

  • New Member
  • *
  • Posts: 30
Re: dynamic memory on a bluepill
« Reply #2 on: October 08, 2022, 09:28:39 pm »
Dang, it was that simple?
I just assumed that the compiler worked the same as in the native compiler.

Thanks alot!


PascalDragon

  • Hero Member
  • *****
  • Posts: 4868
  • Compiler Developer
Re: dynamic memory on a bluepill
« Reply #3 on: October 09, 2022, 05:23:18 pm »
I just assumed that the compiler worked the same as in the native compiler.

The compiler does, however the RTL has not everything enabled by default due to the restrictions of the embedded targets. Thus you need to explicitely register a memory manager for example which is what the heapmgr unit does.

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
Re: dynamic memory on a bluepill
« Reply #4 on: October 09, 2022, 06:28:34 pm »
@PascalDragon
I tried to find that info in the wiki, and did not succeed. only one occurrance of 'heapmgr' in the unrelated page 'garbage collection'.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4868
  • Compiler Developer
Re: dynamic memory on a bluepill
« Reply #5 on: October 10, 2022, 07:29:43 am »
Best location would probably be TARGET Embedded in some general information section or so...

The size of the heap is controlled with the {$Memory} directive or the -Ch option. Additional regions beside the initial one can be registered using HeapMgr.RegisterHeapBlock.

AlexTP

  • Hero Member
  • *****
  • Posts: 1993
    • UVviewsoft
Re: dynamic memory on a bluepill
« Reply #6 on: October 10, 2022, 03:01:01 pm »
Added here, https://wiki.freepascal.org/TARGET_Embedded#How_to_use_heap
Feel tree to add more infos.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4868
  • Compiler Developer
Re: dynamic memory on a bluepill
« Reply #7 on: October 11, 2022, 07:35:22 am »
Would have been nice to include the link I provided as well, cause the {$Memory} directive takes two arguments... ::)

 

TinyPortal © 2005-2018