mk450: code clean up and documentation
[cortex-from-scratch] / sysinfo.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/7/20 - ROBIN KRENS      
5  * Initial version
6  * Display some system information, calculate
7  * the amount of SRAM available 
8  * 
9  * */
10
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include <sys/robsys.h>
16 #include <sys/mmap.h>
17
18 #include <lib/tinyprintf.h>
19 #include <lib/regfunc.h>
20
21
22 uint32_t get_msp(void);
23
24 void sysinfo() {
25
26         uint32_t tmp = *MCU_ID;
27         printf("# DEVICE ID: ");
28
29         if (tmp & 0x414) 
30                 printf("HIGH DENSITY\n");
31         else {
32                 printf("UNKNOWN\n");
33         }
34
35         tmp = (tmp >> 16);
36         printf("# REVISION: ");
37         switch  (tmp) {
38                 case 0x1000:
39                       printf("REVISION A\n");
40                       break;
41                 case 0x1001:
42                       printf("REVISION Z\n");
43                       break;
44                 case 0x1003:
45                       printf("REVISION 1/2/3/X/Y\n");
46                       break;
47                 default:
48                       printf("UNKNOWN\n");
49         }
50
51         extern char _endofbss;
52         
53         uint32_t current_stack = get_msp();
54         uint32_t stack_usage = (SRAM_OFFSET + SRAM_SIZE) - current_stack;
55         uint32_t data_bss = (uint32_t) &_endofbss - SRAM_OFFSET;
56         uint32_t mem_free = SRAM_SIZE - stack_usage - data_bss;
57
58         printf("# TOTAL MEMORY: %#x\n", SRAM_SIZE);
59         printf("# FREE MEMORY: %#x\n", mem_free);
60         printf("# STACK USAGE: %#x\n", stack_usage);
61
62 }
63
64 /* Get the master stack pointer's position
65  * Here we use a so-called naked function, 
66  * that doesn't automatically push and pop  */
67
68 uint32_t get_msp(void) __attribute__( ( naked ) );
69 uint32_t get_msp(void)
70 {
71   uint32_t msp = 0;
72   __asm__ volatile ("mrs %0, msp\n\t" 
73                   "mov r0, %0 \n\t"
74                   "bx lr     \n\t"  : "=r" (msp) );
75   return(msp);
76 }