System Calls cleanup, multiple Processes and context switch
[cortex-from-scratch] / main.c
diff --git a/main.c b/main.c
index 4104960..4eb79f9 100644 (file)
--- a/main.c
+++ b/main.c
 
 #include <sys/robsys.h> 
 #include <sys/mmap.h>
+#include <sys/process.h>
 
 #include <lib/regfunc.h>
 #include <lib/pool.h>
 #include <lib/stdio.h>
 #include <lib/string.h>
 #include <lib/tinyprintf.h>
+#include <lib/syscall.h>
 
 #include <drivers/uart.h>
 #include <drivers/led.h>
 //#include <drivers/mk450_joystick.h>
 #include <drivers/st7735s.h>
 
-mem_pool_t kheap_pool;
+
+/* Example of multitasking */
+process_t p1;
+process_t p2;
+
+uint32_t stackp1[500];
+uint32_t stackp2[500];
+
+void process1(void);
+void process2(void);
+
+
 
 void main()
 {
 
+       /* Load .data segment into SRAM */
+       extern uint32_t * data_lma, data_vma, data_end;
+       int size = (&data_end - &data_vma) * 4;
+       memcpy(&data_vma, &data_lma, size);
+
        /* Initialize the clock system, */
        clock_init();
 
@@ -48,71 +66,43 @@ void main()
        /* TFT screen */
        // tft_init();
        
-       /* Cortex M* integrated systick, can be replaced
-        * by the more accurate RTC.
-       systick_init();
-       */
-       
        /* Set up a very small libc library */
        init_printf(NULL, putc);
 
+       /* Heap init */
+       //kheap_init();
+       //printf("%p\n", get_kheap());
+       
        /* Display some basic info at startup */
        sysinfo();
 
        /* On board LEDs*/
        led_init();
 
+
        /* Real time clock */
        rtc_init();
 
-       extern uint32_t * _beginofheap;
-       //printf("%p", &_beginofheap);
-       kpool_init(&kheap_pool, 0x10, 10, (uint32_t *) &_beginofheap);
-
-//     printf("%p\n", &kheap_pool);
-
-       char * string = (char *) kalloc(&kheap_pool);
-       char * string2 = (char *) kalloc(&kheap_pool);
-       char * string3 = (char *) kalloc(&kheap_pool);
-
-
-       memset(string, 0xFF, 0x10);
-       memset(string2, 0xEE, 0x10);
-       memset(string3, 0xDD, 0x10);
-
-       printf("%p\n", string);
-       printf("%p\n", string2);
-       printf("%p\n", string3);
-
-       kfree(&kheap_pool, string);
+       /* Initialize SVC handler for system calls*/
+       syscall_init();
 
-       char * string6 = (char *) kalloc(&kheap_pool);
+       /* System call test */
+       int uptime = theos_uptime();
+       printf("UPTIME: %d\n", uptime);
 
+       /* Multi processes test */
+//     int size_stack = sizeof(stackp1);
+//     p1.stackptr = ((unsigned int) stackp1) + size_stack - 0x1C;
+//     p1.stackptr[6] = (uint32_t) process1;
+//     p1.stackptr[7] = 0x01000000;
+//     p2.stackptr = ((unsigned int) stackp2) + size_stack - 0x1C;
+//     p2.stackptr[6] = (uint32_t) process2;
+//     p2.stackptr[7] = 0x01000000;
+//     theos_init(&p1);
 
-       memset(string6, 0xCC, 0x10);
-//     char * string7 = (char *) kalloc(&kheap_pool);
-       printf("%p\n", string6);
-//     printf("%p\n", string7);
-
-
-       kfree(&kheap_pool, string2);
-
-       char * string7 = (char *) kalloc(&kheap_pool);
-       memset(string7, 0xBB, 0x10);
-
-
-       char * string8 = (char *) kalloc(&kheap_pool);
-       memset(string8, 0xAA, 0x10);
-
-       char * string9 = (char *) kalloc(&kheap_pool);
-       memset(string9, 0x99, 0x10);
-       //free(string);
-
-       kfree(&kheap_pool, string3);
-
-       //char * string2 = (char *) alloc();
-       
-       //string2 = "taalb";
+       /* Cortex M* integrated systick, can be replaced
+        * by the more accurate RTC. */
+       //systick_init();
 
        /* Eeprom Driver
        eeprom_at24c_init();
@@ -134,7 +124,6 @@ void main()
        /* ADC Joystick module */
        // mk450_init();        
 
-
        /* Start up terminal */
        terminal();
        
@@ -143,3 +132,21 @@ void main()
 
        }
 }
+
+void process1(void) {
+       
+       while(1) {
+               printf("process 1\n");
+               _block(0xFFFFF);
+               theos_switch(&p1, &p2);
+       }
+
+}
+void process2(void) {
+       while(1) {
+               printf("process 2\n");
+               _block(0xFFFFF);
+               theos_switch(&p2, &p1);
+       }
+       
+}