e9b2429c259c2b9951e733e813b645925a67a823
[cortex-from-scratch] / sysinfo.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include <sys/robsys.h>
6 #include <sys/mmap.h>
7
8 #include <lib/stdio.h>
9 #include <lib/regfunc.h>
10
11 #define MEM_SIZE        0x00010000 
12 #define MEM_OFFSET      0x20000000
13
14 uint32_t get_msp(void);
15
16 void sysinfo() {
17
18         uint32_t tmp = *MCU_ID;
19         cputs("# ROBSYS 0.1 LOADING...\n");
20         cputs("# DEVICE ID: ");
21
22         if (tmp & 0x414) 
23                 cputs("HIGH DENSITY\n");
24         else {
25                 cputs("UNKNOWN\n");
26         }
27
28         tmp = (tmp >> 16);
29         cputs("# REVISION: ");
30         switch  (tmp) {
31                 case 0x1000:
32                       cputs("REVISION A\n");
33                       break;
34                 case 0x1001:
35                       cputs("REVISION Z\n");
36                       break;
37                 case 0x1003:
38                       cputs("REVISION 1/2/3/X/Y\n");
39                       break;
40                 default:
41                       cputs("UNKNOWN\n");
42         }
43
44         extern char _endofbss;
45         
46         uint32_t current_stack = get_msp();
47         uint32_t stack_usage = (MEM_OFFSET + MEM_SIZE) - current_stack;
48         uint32_t data_bss = &_endofbss - MEM_OFFSET;
49         uint32_t mem_free = MEM_SIZE - stack_usage - data_bss;
50
51         cputs("# TOTAL MEMORY: ");
52         cputs(regtohex(MEM_SIZE));
53         cputchar('\n');
54         cputs("# FREE MEMORY: ");
55         cputs(regtohex(mem_free));
56         cputchar('\n');
57         cputs("# STACK USAGE: ");
58         cputs(regtohex(stack_usage));
59         cputchar('\n');
60
61 }
62
63 /* Get the master stack pointer's position
64  * Here we use a so-called naked function, 
65  * that doesn't automatically push and pop  */
66
67 uint32_t get_msp(void) __attribute__( ( naked ) );
68 uint32_t get_msp(void)
69 {
70   uint32_t msp = 0;
71   __asm__ volatile ("mrs %0, msp\n\t" 
72                   "mov r0, %0 \n\t"
73                   "bx lr     \n\t"  : "=r" (msp) );
74   return(msp);
75 }