⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 initialize.c

📁 KaOS is a real-time operating system that has been implemented with the basic real-time constraints
💻 C
字号:
//********************************************************** 
//Set it all up
void os_init_api(void)
{
    SleepThreadPtr      = SleepThread;
    CreateThreadPtr     = CreateThread;
    SuspendThreadPtr    = SuspendThread;
    ResumeThreadPtr     = ResumeThread;
    TerminateThreadPtr  = TerminateThread;
    FindThreadPtr       = FindThread;
	
    CreateSemaphorePtr  = CreateSemaphore;
    WaitSemaphorePtr    = WaitSemaphore;
    SignalSemaphorePtr  = SignalSemaphore;
	
    SendMessagePtr      = SendMessage;
    ReceiveMessagePtr   = ReceiveMessage;
}
void os_init_structures(void)
{
    register char i;
    os_thread *temp_thread, *prev_thread;
    os_semaphore *temp_semaphore, *prev_semaphore;
	os_threadsem *temp_threadsem; 
	os_message *temp_message;

	g_threadcount = 0;

	// initialize list of threads
    g_freethreads = &g_allthreads[0];        
    prev_thread = NULL;
    temp_thread = g_freethreads;
	for (i = 1; i < THREAD_COUNT; ++i)
	{
        temp_thread->next = &g_allthreads[i];
		temp_thread->prev = prev_thread;
		temp_thread->threadID = i;
    	
		prev_thread = temp_thread;
		temp_thread = temp_thread->next;
  	}
  	temp_thread->threadID = i;
  	temp_thread->prev = prev_thread;
  	temp_thread->next = NULL;
    
  	// initialize list of semaphores
    g_freesemaphores = &g_allsemaphores[0];        
    prev_semaphore = NULL;
    temp_semaphore = g_freesemaphores;
	for (i = 1; i < SEMAPHORE_COUNT; ++i)
	{
        temp_semaphore->next = &g_allsemaphores[i];
		temp_semaphore->prev = prev_semaphore;
    	
		prev_semaphore = temp_semaphore;
		temp_semaphore = temp_semaphore->next;
  	}
	temp_semaphore->prev = prev_semaphore;
  	temp_semaphore->next = NULL;
    
  	// initialize list of thread/semaphore holders
    g_freethreadsems = &g_allthreadsems[0];
    temp_threadsem = g_freethreadsems;
	for (i = 1; i < THREADSEM_COUNT; ++i)
	{
        temp_threadsem->next = &g_allthreadsems[i];
		temp_threadsem = temp_threadsem->next;
  	}
  	temp_threadsem->next = NULL;
    
  	// initialize list of message holders
    g_freemessages = &g_allmessages[0];
    temp_message = g_freemessages;
	for (i = 1; i < MESSAGE_COUNT; ++i)
	{
        temp_message->next = &g_allmessages[i];
		temp_message = temp_message->next;
  	}
  	temp_threadsem->next = NULL;
    
	// allocate the stack
	g_stkCurrent = g_stkStart = &g_stkBlock[0];
	g_stkEnd = g_stkStart + TOTAL_STACK_SIZE;
}
void os_initialize(void)
{
    UCHAR *sd_swstack, *sd_hwstack;

    // set up UART   
    UCSRB = 0x18;
    UBRRL = 51;
    /*UCSRB = 0x18;
    UBRRL = 0x40;  //set baud rate to 1200
    UBRRH = UBRRH | 0x03; 
    UCSRA=0x02;
    UCSRB=0x18;
    UCSRC=0x86;
    UBRRH=0x01;
    UBRRL=0xCD;*/

    // set up Timer0 for scheduling every 10ms
    TCNT0 = 0x00;
    OCR0  = 15.6 * 10;
    TIMSK = 0x02;
    TCCR0 = 0b00001101;
    
    DDRC = 0xFF;

    os_init_api();
	os_init_structures();

    // create some uart semaphores
	g_uart_t_semaphore = CreateSemaphore(1);
	g_uart_r_semaphore = CreateSemaphore(1);

    // create the SD card thread
	g_currthread = CreateThread(sdthread, 255, 150, 150);
	
	// We never want to free the sd threads stack
	g_stkStart += 300;

	sd_swstack = g_currthread->swstack;
	sd_hwstack = g_currthread->hwstack;

    #asm
        ; Load new HW stack pointer, low first
        OUT	SPL,R18
        OUT	SPH,R19

        ; Load new data stack pointer, low first
        MOV			R28,R16
        MOV			R29,R17
    #endasm


    // OUT SREG, R0		will enable interrupts, so CPU control will
    // yield here to the other thread. It will return when control
    // is switched back to the null thread.
    #asm
        POP		R0
        OUT		SREG,R0
        POP		R31
        POP		R30
        POP		R27
        POP		R26
        POP		R25
        POP		R24
        POP		R23
        POP		R22
        POP		R21
        POP		R20
        POP		R19
        POP		R18
        POP		R17
        POP		R16
        POP		R15
        POP		R14
        POP		R13
        POP		R12
        POP		R11
        POP		R10
        POP		R9
        POP		R8
        POP		R7
        POP		R6
        POP		R5
        POP		R4
        POP		R3
        POP		R2
        POP		R1
        POP		R0

        RETI
    #endasm
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -