ac01416abdfbb5f1ef46dc50ff420be7f38e767d
[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
27 #include <drivers/uart.h>
28 #include <drivers/led.h>
29 //#include <drivers/tm1637.h>
30 //#include <drivers/at24c.h>
31 //#include <drivers/tsensor.h>
32 //#include <drivers/mk450_joystick.h>
33 #include <drivers/st7735s.h>
34
35 #include <lib/syscall.h>
36
37
38 process_t p1;
39 process_t p2;
40
41 uint32_t stackp1[500];
42 uint32_t stackp2[500];
43
44 extern int count;
45
46 void switch_usermode() {
47
48         // user mode 
49         //asm volatile ("mov r0, 0x1" "\n\t" 
50         //"msr control, r0" "\n\t"
51         //"isb" "\n\t"); 
52
53         // system init call
54
55 }
56
57 void process1(void) {
58         
59         while(1) {
60                 //uint32_t control = 0xFFFFFFFF;
61                 printf("process 1\n");
62                 //asm volatile("msr control, %0" "\n\t"
63                 //             "dsb" : : "r" (control));
64                 //printf("control: %x", control);       
65                 //for(;;);
66                 _block(100);
67                 theos_switch(&p1, &p2);
68         }
69
70 }
71 void process2(void) {
72         while(1) {
73                 printf("process 2\n");
74                 _block(100);
75                 theos_switch(&p2, &p1);
76         }
77         
78 }
79
80 int test_data_segment = 99;
81
82 void main()
83 {
84
85         /* Load .data segment into SRAM */
86         extern uint32_t * data_lma, data_vma, data_end;
87         int size = (&data_end - &data_vma) * 4;
88         memcpy(&data_vma, &data_lma, size);
89
90         /* Initialize the clock system, */
91         clock_init();
92
93         /* Setup the interrupt vector table */
94         ivt_init();
95
96         /* Initialze basic input and output over serial */
97         uart_init();
98
99         /* TFT screen */
100         // tft_init();
101         
102         /* Set up a very small libc library */
103         init_printf(NULL, putc);
104
105         /* Heap init */
106         //kheap_init();
107         //printf("%p\n", get_kheap());
108         
109         /* Display some basic info at startup */
110         sysinfo();
111
112         /* On board LEDs*/
113         led_init();
114
115
116         /* Real time clock */
117         rtc_init();
118
119
120 //      printf("press any key to start\n");
121 //      asm volatile ("cpsid f"); // doesn't work in qemu
122
123         syscall_init();
124
125         //int ret;
126         //ret = theos_test(0x1, 0x2, 0x3);
127         //ret = theos_uptime();
128
129         //printf("ret: %d\n", ret);
130
131         int size_stack = sizeof(stackp1);
132         
133         p1.stackptr = ((unsigned int) stackp1) + size_stack - 0x1C;
134         p1.stackptr[6] = (uint32_t) process1;
135         p1.stackptr[7] = 0x01000000;
136         p2.stackptr = ((unsigned int) stackp2) + size_stack - 0x1C;
137         p2.stackptr[6] = (uint32_t) process2;
138         p2.stackptr[7] = 0x01000000;
139         
140         theos_init(&p1);
141
142         /* Cortex M* integrated systick, can be replaced
143          * by the more accurate RTC. */
144         
145 //      systick_init();
146
147         //      switch_usermode();      
148
149         //printf("without system call");        
150 //      theos_test(0xA1, 0xA2);
151
152         /* Eeprom Driver
153         eeprom_at24c_init();
154         eeprom_test();
155         */
156         
157         /* LED Segment Driver */
158         //tm1637_init();
159
160         /* ASM Blocking routine */
161         //for (int i = 0; i < 1000; i++)
162         //      _block(10000);
163
164         /* TEMP SENSOR 
165         tsensor_printid();
166         uint16_t temp = tsensor_get_temp();
167         printf("Current temperature: %d °C\n", temp); */
168
169         /* ADC Joystick module */
170         // mk450_init();        
171
172
173         /* Start up terminal */
174         terminal();
175         
176         /* Should not be here, endless loop */
177         for(;;) {
178
179         }
180 }