AS=arm-none-eabi-as
MKIMG=arm-none-eabi-objcopy
-LDFLAGS+= -mthumb -mcpu=cortex-m3
+LDFLAGS+= -mthumb -mcpu=cortex-m0
CFLAGS+= -mcpu=cortex-m3 -mthumb -g
as:
* */
-#define BSS_BASE ((volatile uint32_t)(0x20000000));
-#define MEM_SIZE 512000;
+#define BSS_BASE ((volatile uint32_t *)(0x20000800)) //TODO: .data flexible siz
+#define TOTAL_MEM_SIZE 64000;
/* SYSTEM INFO AND DEBUG */
#define MCU_ID ((volatile uint32_t*)( 0xE0042000))
/* LIB.C */
extern void addrtohex(const uint32_t);
+/* MM.C */
+extern void mm_init();
+extern void * mm_alloc(size_t);
+extern void free(void *);
+extern void test_memory(uint32_t *);
+
#endif
char hexbuf[8] = {'0', '0','0', '0','0', '0','0', '0'};
-void addrtohex(const uint32_t addr) {
+void addrtohex(uint32_t addr) {
char tmpbuf[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
memset(&hexbuf, 0, sizeof(uint32_t) * 8);
- uint32_t tmp = addr;
for (int i = 0; i < 8 ; i++) {
- tmp = addr;
+ uint32_t tmp = addr;
tmp = tmp >> (i * 4);
tmp = tmp & 0xF;
if ((tmp >= 0) && tmp < 10) {
}
uart_puts("ADDRESS: 0x");
- for (int i = 7; i >= 0; i--) {
+ for (int i = 7; i >= 0; i--) {
uart_putc(hexbuf[i]);
- }
+ }
+ //uart_puts(hexbuf);
uart_putc('\n');
}
-
-void *malloc(size_t size) {
-
-
-
-}
-
-void free(void * ptr) {
-
-
-}
-/*
- * MEMORY MAP
+/* */
+MEMORY
{
FLASH (xr) : ORIGIN = 0x08000000, LENGTH = 512K
SRAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
-} */
+}
ENTRY(_start)
.data :
{
*(.data)
- }
+ }
.bss : ALIGN(256)
{
*(.bss)
- }
+ }
}
systick_init();
uart_puts("LOADING SYSTEM 0.1 ...\n");
sysinfo();
- addrtohex((volatile uint32_t) 0x12345678 );
- addrtohex((volatile uint32_t) *SCB_VTOR );
+ mm_init();
+
+ int * p2 = mm_alloc(200);
+ *p2 = 0x12345678;
+
+ test_memory(p2);
+
+ addrtohex(p2);
+ addrtohex(*p2);
+
+
+ //addrtohex((volatile uint32_t) 0x12345678 );
+ //addrtohex((volatile uint32_t) SCB_VTOR );
// asm("cpsie i"); // enable irq , cpsied f (disable faukts(
#include <stm32.h>
#include <mmap.h>
-uint32_t free_base;
+/* TOTAL SRAM MEMORY: 64kB
+ * 64 chunks of 1kB
+ * 128 chunks of 512 bytes
+ * SIMPLE BITMAP IMPLEMENTATION
+ * */
+
+#define CHUNKS 128
+#define FREE 0x00
+#define ALLOC 0x01
+
+#define BASE 0x20000400
+
+#define SET_SIZE(s) (s << 8)
+#define SET_FLAGS(f) (f << 24)
+#define IS_ALLOC(c) ((c >> 24) & 0x0F )
+#define PADDR(i) (0x20000400 + (i * 0x200))
+#define I_OFFSET(p) ((p - 0x20000400))
+
+uint32_t chunk[CHUNKS];
+
+/*
+ * | FLAGS | SIZE | RESERVED |
+ * | 0x00 | 0x0000 | 0x00 |
+ *
+ * */
void mm_init() {
- free_base = MEM_SIZE;
+ // interrupt vector
+ chunk[0] = SET_SIZE(96) | SET_FLAGS(ALLOC);
+
+ // test
+ /* uart_puts("ALLOC:\n");
+ int * p = mm_alloc(100);
+ *p = 0x12345678;
+
+ addrtohex(p);
+ addrtohex(*p);
+
+ uart_puts("FREE:\n");
+
+ int * p2 = mm_alloc(100);
+ *p2 = 0xFFFFAAAA;
+ addrtohex(p2);
+ addrtohex(*p2);
+ free(p);
+ free(p2); */
}
-size_t mm_free() {
+ void test_memory(uint32_t * ptr) {
+
+ *ptr = 0xEEEEEEEE;
+ for (int i = 0; i < 100; i++) {
+ ptr++;
+ *ptr = 0xEEEEEEEE;
+ }
+
+}
+
+
+void * mm_alloc(size_t size) { // in bytes
+
+ if (size > 512) {
+ uart_puts("SYSERROR: WE CAN'T ALLOCATE THAT MUCH!\n");
+ return NULL;
+ }
+
+ /* check which chunk is free */
+ for (int i = 1; i < CHUNKS; i++) {
+ if (!IS_ALLOC(chunk[i])) {
+ chunk[i] = SET_SIZE(size) | SET_FLAGS(ALLOC);
+ return (void *) PADDR(i);
+ }
+ }
+
+ uart_puts("SYSERROR: OUT OF MEMORY\n");
+ return NULL;
+}
+
+void free(void *ptr) {
+
+ uint32_t index = (uint32_t) I_OFFSET(ptr) / 0x200;
- return MEM_SIZE;
+ uint32_t tmp = chunk[index];
+ if (!IS_ALLOC(tmp))
+ uart_puts("SYSERROR: ALREADY FREED!\n");
+ else if(index < CHUNKS) {
+ chunk[index] = SET_FLAGS(FREE) | SET_SIZE(0);
+ }
+
}
- .equ STACK_TOP, 0x20000800
+ .equ STACK_TOP, 0x20008000 /* placed at 32kB, TODO: could place at top of SRAM? */
.text
.global _start
.global reset, nmi, hardfault
mov R1,#0
udiv.w R2, R0, R1 */
- .data
- .word 'x'
+/* .data
+ .word 'x' */
.end
*/
}
+void wait() {
+ for (int i = 0; i < 100; i++);
+}
extern void uart_putc(unsigned char ch) {
while (*USART1_SR & 0x0C) { } // transmit data register empty and complete
*USART1_DR = 0x0D; // return line
}
-
- while ((*USART1_SR & 0xFF) == 0x0C) {} // busy bit
+
+
+ while (*USART1_SR & 0x0C) {}
*USART1_DR = ch;
-}
-void wait() {
- for (int i = 0; i < 100; i++);
+
+ wait();
}
+
extern void uart_puts(unsigned char *str)
{
for (i = 0; i < strlen(str); i++)
{
- wait();
uart_putc(str[i]);
}
}