-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShell.c
117 lines (113 loc) · 2.85 KB
/
Shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "Z-OS.h"
#include "Shell.h"
#include <uart.h>
extern List Threads;
#define ASM_INST(x) {__asm__ volatile (x);}
static Int8 CompareCmd(UInt8* buffer, char* cmd)
{
Int16 i = 0;
while (buffer[i] == cmd[i])
{
i++;
if (cmd[i] == 0 || cmd[i] == '\r' || cmd[i] == '\n') return True;
}
return False;
}
static void ShellThreadProc(void* arg)
{
UInt8 buffer[17] = {0};
UInt8* pBuf = (UInt8*)buffer;
for(;;)
{
int val;
if (!DataRdyUART1()) continue;
val = getcUART1(); //(5, (unsigned int*)buffer, 65535);
if ((*(pBuf++) = (UInt8)val) != '\n')
{
if (pBuf >= (UInt8*)&(buffer[16]))
{
puts("Invalid command.\r\n");
pBuf = (UInt8*)buffer;
}
}
else
{
pBuf = (UInt8*)buffer;
if (CompareCmd(pBuf,"info") || CompareCmd(pBuf, "ver"))
{
puts("Z-OS v1.2\r\nA real-time multitasking operating system for the PIC 24H.\r\nCoded by Zachary Northrup\r\n");
}
else if (CompareCmd(pBuf,"listThreads"))
{
UInt16 i;
puts("Running Threads:\r\n\r\n");
EnterCriticalSection();
for (i = 0; i < Threads.Length; i++)
{
ThreadInternal* thr = GetListItem(&Threads,i);
InternalObject* obj;
if (InternalObjectFromData(thr,&obj)) continue;
if (obj->Name) printf("%s:\r\n",obj->Name);
else puts("Unnamed Thread:\r\n");
printf(" ID: %d\r\n Stack Bottom: 0x%x\r\n Stack Top: 0x%x\r\n Start Address: 0x%x\r\n State: ",
thr->ThreadID,
(int)thr->Stack,
(int)thr->StackPointer,
(int)thr->StartParams.StartFunc);
switch (thr->State)
{
case Running: puts("Running"); break;
case Queued: puts("Queued"); break;
case Waiting: puts("Waiting"); break;
case Suspended: puts("Suspended"); break;
case Stopped: puts("Stopped"); break;
}
puts("\r\n\r\n");
}
ExitCriticalSection();
}
else if (CompareCmd(pBuf, "prgm"))
{
EnterCriticalSection();
ASM_INST("mov #0x0000,W0");
ASM_INST("mov W0,TBLPAG");
ASM_INST("mov #0x0c00,W0");
ASM_INST("mov #0xFFFF, W1");
ASM_INST("tblwtl W1,[W0]");
ASM_INST("tblwth W1,[W0]");
ASM_INST("reset");
}
else if (CompareCmd(pBuf, "reset"))
{
ASM_INST("reset");
}
else if (CompareCmd(pBuf, "freeze"))
{
EnterCriticalSection();
}
else if (CompareCmd(pBuf, "unfreeze"))
{
ExitCriticalSection();
}
else
{
puts("Invalid command.\r\n");
}
memset(buffer,0,16);
}
}
}
void InitShell(void)
{
UInt16 shellThread;
Int16 ret;
IThread* shellInt;
if ((ret = CreateObject(TypeThread,&shellThread,null))) return;
if ((ret = GetInterface(shellThread,CodeIThread,(void**)&shellInt)))
{
ReleaseObject(shellThread);
return;
}
ret = shellInt->Start(shellThread,ShellThreadProc,null);
ReleaseObject(shellThread);
}