System Calls cleanup, multiple Processes and context switch
[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("SYSTEM LOADING...\n\n");
28         printf("DEV ID: ");
29
30         if (tmp & 0x414) 
31                 printf("H. DENSITY\n");
32         else {
33                 printf("UNKNOWN\n");
34         }
35
36         /* tmp = (tmp >> 16);
37         printf("REV: ");
38         switch  (tmp) {
39                 case 0x1000:
40                       printf("REVISION A\n");
41                       break;
42                 case 0x1001:
43                       printf("REVISION Z\n");
44                       break;
45                 case 0x1003:
46                       printf("REVISION 1/2/3/X/Y\n");
47                       break;
48                 default:
49                       printf("UNKNOWN\n");
50         } */
51
52         extern char _endofbss;
53
54         uint32_t current_stack = get_msp();
55         uint32_t stack_usage = (SRAM_OFFSET + SRAM_SIZE) - current_stack;
56         uint32_t data_bss = (uint32_t) &_endofbss - SRAM_OFFSET;
57         uint32_t mem_free = SRAM_SIZE - stack_usage - data_bss;
58
59         extern uint32_t KHEAP_SIZE;
60         
61         printf("TOTAL MEM: %#x\n", SRAM_SIZE);
62         printf("FREE MEM: %#x\n", mem_free);
63         printf("STACK USE: %#x\n", stack_usage);
64         printf("HEAP_SIZE: %#x\n\n", &KHEAP_SIZE);
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 }