Basic functionality for TM1637
[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("# DEVICE ID: ");
26
27         if (tmp & 0x414) 
28                 cputs("HIGH DENSITY\n");
29         else {
30                 cputs("UNKNOWN\n");
31         }
32
33         tmp = (tmp >> 16);
34         cputs("# REVISION: ");
35         switch  (tmp) {
36                 case 0x1000:
37                       cputs("REVISION A\n");
38                       break;
39                 case 0x1001:
40                       cputs("REVISION Z\n");
41                       break;
42                 case 0x1003:
43                       cputs("REVISION 1/2/3/X/Y\n");
44                       break;
45                 default:
46                       cputs("UNKNOWN\n");
47         }
48
49         extern char _endofbss;
50         
51         uint32_t current_stack = get_msp();
52         uint32_t stack_usage = (SRAM_OFFSET + SRAM_SIZE) - current_stack;
53         uint32_t data_bss = &_endofbss - SRAM_OFFSET;
54         uint32_t mem_free = SRAM_SIZE - stack_usage - data_bss;
55
56         cputs("# TOTAL MEMORY: ");
57         cputs(regtohex(SRAM_SIZE));
58         cputchar('\n');
59         cputs("# FREE MEMORY: ");
60         cputs(regtohex(mem_free));
61         cputchar('\n');
62         cputs("# STACK USAGE: ");
63         cputs(regtohex(stack_usage));
64         cputchar('\n');
65
66 }
67
68 /* Get the master stack pointer's position
69  * Here we use a so-called naked function, 
70  * that doesn't automatically push and pop  */
71
72 uint32_t get_msp(void) __attribute__( ( naked ) );
73 uint32_t get_msp(void)
74 {
75   uint32_t msp = 0;
76   __asm__ volatile ("mrs %0, msp\n\t" 
77                   "mov r0, %0 \n\t"
78                   "bx lr     \n\t"  : "=r" (msp) );
79   return(msp);
80 }