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