osthreads.c

来自「KaOS is a real-time operating system tha」· C语言 代码 · 共 78 行

C
78
字号
void sdthread()
{
	os_thread *thread, *temp_thread, *next_thread;
	char c;
	UCHAR *buffer;
	//FILE* file;
	//DIR* dir;
	register UINT progSize, i;
	//UCHAR filename[22] = "test.txt";
	thread = g_currthread;

	DDRB |= 0b00000100;
	
	while (1)
	{
		c = MmcInit();
		if (c == 0)
		{
			PORTB.2 = 1;
		}
		else
		{
			PORTB.2 = 0;
		}
		if (c == 0 && PINB.1 == 0)
		{
			PORTB.2 = 0;
			// we want to load a new program
			temp_thread = g_usedthreads;
			while (temp_thread != NULL)
			{
				// kill any thread but the null/sd thread
				if (temp_thread != thread)
				{
					next_thread = temp_thread->next;
					TerminateThread(temp_thread);
					temp_thread = next_thread;
				}
				else
				{
					temp_thread = temp_thread->next;
				}
			}
			os_reset_stack();
			
			// grab some stack space
			//file = (FILE*)os_alloc_stack(sizeof(FILE));
			
			//dir = (DIR*)os_alloc_stack(sizeof(DIR));
			
			//dir->directoryID = 0;
			
			//fopen(dir, file, filename, FOPEN_CREATE);
			//dread(dir,file);
			buffer = (UCHAR*)os_alloc_stack(512);
			
			// read in the first sector. The first 2 bytes are the size of the program
			mmc_read_sector(0, buffer);
			progSize = (((int)buffer[0]) << 8) | buffer[1];
			
			// just write 0x00 and jmp to 0x01
			buffer[0] = buffer[1] = 0;
			
			// write the first sector
			WriteSector(0, (UINT*)buffer);
			
			for (i = 512; i < progSize; i += 512)
			{
				mmc_read_sector(i, buffer);
		   		WriteSector(i, (UINT*)buffer);
			}
			os_reset_stack();
			#asm("cli");
			temp_thread = CreateThread(1, 255, 150, 150);
			#asm("sei");
		}
	}
}

⌨️ 快捷键说明

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