#include <
stdlib.h
>
/**************************************************************************/
// Keil uv4 MICROLIB compatible heap initialization
extern unsigned long __microlib_freelist;
extern unsigned long __microlib_freelist_initialised;
extern unsigned long __heap_base;
extern unsigned long __heap_limit;
/**************************************************************************/
void add_heap(unsigned long addr, unsigned long size)
{
unsigned long *p = (unsigned long *)addr;
unsigned long span;
size >>= 2; // size in 32-bit words
size -= 2; // two words used for internal structures
if (__microlib_freelist_initialised == 0) // free list created yet?
{
__microlib_freelist_initialised = 1;
__microlib_freelist = 0;
span = 0;
}
else
{
span = __microlib_freelist - addr - (size << 2); // faux allocation across the discontinuity
}
p[0] = size << 2; // size of list entry
p[1] = __microlib_freelist; // next entry
p[size] = span;
p[size+1] = 0; // terminal value
__microlib_freelist = addr; // current entry
}
/**************************************************************************/
void alloc_heaps(void) // Called prior to any malloc()
{
// Enumerate available heap memory regions in DECENDING memory order
//add_heap(0xD0000000, 0x800000); // SDRAM 8MB - Must be initialized
add_heap((unsigned long)&__heap_base,(unsigned long)&__heap_limit - (unsigned long)&__heap_base); // Classic HEAP section
// add_heap(0x20004000, 0x14000); // SRAM (PORTION OF)
add_heap(0x10000000, 0x10000); // CCM 64K
}
===============================================================================
// STM32 Keil SDRAM HEAP STM32F429I-DISCO
// Use MicroLIB checked in options
// Use Scatter file in Linker options (not Memory Layout from Target Dialog)
/* heap.sct
LR_IROM1 0x08000000 0x00200000 { ; load region size_region
ER_IROM1 0x08000000 0x00200000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$$Sections)
.ANY (+RO)
}
RW_IRAM1 0x20000000 0x00030000 { ; Internal SRAM
.ANY (+RW +ZI)
}
RW_IRAM2 0x10000000 UNINIT 0x00010000 { ; Internal CCM SRAM
*(.ccm)
}
RW_RAM1 0xD0000000 UNINIT 0x00800000 { ; External SDRAM
*(HEAP)
}
}
*/
/* startup_stm32f429_439xx.s
...
Heap_Size EQU 0x00800000
AREA HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit
...
*/
#include <
stdio.h
>
#include <
stdlib.h
>
#include "stm32f429i_discovery.h"
#include "stm32f429i_discovery_sdram.h"
/**************************************************************************************/
int main(void)
{
char *p;
/* Make sure to initialize the external memory, pins and buses before using them */
/* SDRAM Initialization */
SDRAM_Init();
/* Disable write protection */
FMC_SDRAMWriteProtectionConfig(FMC_Bank2_SDRAM,DISABLE);
p = malloc(0x1000);
printf("Allocation at %p\n", p);
free(p);
while(1); // Don't want to exit
}
//******************************************************************************
// Hosting of stdio functionality through SWV - Serial Wire Viewer
//******************************************************************************
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
ITM_SendChar(ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
ch = '?';
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
ITM_SendChar(ch);
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
For FREERTOS
__attribute__((section("HEAP"))) static unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];