System Calls cleanup, multiple Processes and context switch
[cortex-from-scratch] / main.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  * $DESCRIPTION$
8  * Main initialize basic components of the board
9  * and jumps to a terminal
10  *
11  * */
12
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <stdint.h>
16
17 #include <sys/robsys.h> 
18 #include <sys/mmap.h>
19 #include <sys/process.h>
20
21 #include <lib/regfunc.h>
22 #include <lib/pool.h>
23 #include <lib/stdio.h>
24 #include <lib/string.h>
25 #include <lib/tinyprintf.h>
26 #include <lib/syscall.h>
27
28 #include <drivers/uart.h>
29 #include <drivers/led.h>
30 //#include <drivers/tm1637.h>
31 //#include <drivers/at24c.h>
32 //#include <drivers/tsensor.h>
33 //#include <drivers/mk450_joystick.h>
34 #include <drivers/st7735s.h>
35
36
37 /* Example of multitasking */
38 process_t p1;
39 process_t p2;
40
41 uint32_t stackp1[500];
42 uint32_t stackp2[500];
43
44 void process1(void);
45 void process2(void);
46
47
48
49 void main()
50 {
51
52         /* Load .data segment into SRAM */
53         extern uint32_t * data_lma, data_vma, data_end;
54         int size = (&data_end - &data_vma) * 4;
55         memcpy(&data_vma, &data_lma, size);
56
57         /* Initialize the clock system, */
58         clock_init();
59
60         /* Setup the interrupt vector table */
61         ivt_init();
62
63         /* Initialze basic input and output over serial */
64         uart_init();
65
66         /* TFT screen */
67         // tft_init();
68         
69         /* Set up a very small libc library */
70         init_printf(NULL, putc);
71
72         /* Heap init */
73         //kheap_init();
74         //printf("%p\n", get_kheap());
75         
76         /* Display some basic info at startup */
77         sysinfo();
78
79         /* On board LEDs*/
80         led_init();
81
82
83         /* Real time clock */
84         rtc_init();
85
86         /* Initialize SVC handler for system calls*/
87         syscall_init();
88
89         /* System call test */
90         int uptime = theos_uptime();
91         printf("UPTIME: %d\n", uptime);
92
93         /* Multi processes test */
94 //      int size_stack = sizeof(stackp1);
95 //      p1.stackptr = ((unsigned int) stackp1) + size_stack - 0x1C;
96 //      p1.stackptr[6] = (uint32_t) process1;
97 //      p1.stackptr[7] = 0x01000000;
98 //      p2.stackptr = ((unsigned int) stackp2) + size_stack - 0x1C;
99 //      p2.stackptr[6] = (uint32_t) process2;
100 //      p2.stackptr[7] = 0x01000000;
101 //      theos_init(&p1);
102
103         /* Cortex M* integrated systick, can be replaced
104          * by the more accurate RTC. */
105         //systick_init();
106
107         /* Eeprom Driver
108         eeprom_at24c_init();
109         eeprom_test();
110         */
111         
112         /* LED Segment Driver */
113         //tm1637_init();
114
115         /* ASM Blocking routine */
116         //for (int i = 0; i < 1000; i++)
117         //      _block(10000);
118
119         /* TEMP SENSOR 
120         tsensor_printid();
121         uint16_t temp = tsensor_get_temp();
122         printf("Current temperature: %d °C\n", temp); */
123
124         /* ADC Joystick module */
125         // mk450_init();        
126
127         /* Start up terminal */
128         terminal();
129         
130         /* Should not be here, endless loop */
131         for(;;) {
132
133         }
134 }
135
136 void process1(void) {
137         
138         while(1) {
139                 printf("process 1\n");
140                 _block(0xFFFFF);
141                 theos_switch(&p1, &p2);
142         }
143
144 }
145 void process2(void) {
146         while(1) {
147                 printf("process 2\n");
148                 _block(0xFFFFF);
149                 theos_switch(&p2, &p1);
150         }
151         
152 }