12.07.2015 Views

Chapter 1 Sample Code Real-Time Programming

Chapter 1 Sample Code Real-Time Programming

Chapter 1 Sample Code Real-Time Programming

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1.02 Compiler-Independent Data Types• Not use C’s short, int, and long data types because they areinherently nonportableListing 1.1 Portable data typestypedef unsigned char BOOLEAN;typedef unsigned char INT8U;typedef signed char INT8S;typedef unsigned int INT16U;typedef signed int INT16S;typedef unsigned long INT32U;typedef signed long INT32S;typedef float FP32;typedef double FP64;#define BYTE INT8S#define UBYTE INT8U#define WORD INT16S#define UWORD INT16U#define LONG INT32S#define ULONG INT32U•For a 32-bit processor, the uC/OS-II will declare theunsigned int to unsigned short71.03 Global Variables• A global variable must be allocated storage space in RAM and must bereferenced by other modules using the C keyword extern.• The following teaches how to use only single declaration in the headerfile• In all .H files that define global variables as follows#ifdef xxx_GLOBALS#define xxx_EXT#else#define xxx_EXT extern#endif8


1.05 PC-Based Services• 1.05.01 Character-based Display– The display functions perform direct writes to video RAM forperformance reasons– On a VGA monitor, video memory starts at absolute memorylocation 0xB8000 (B800:0000)– 25*80 = 2000 characters, each character requires two bytes. Thefirst byte is displayed character, the second byte is display attribute7 6 4 3 0BlinksbitBackgroundcolorForegroundcolorPC_DispClrScr() To clear the screenPC_DispClrLine() To clear a single row (or line)PC_DispChar() To display a single ASCII character anywhere on the screenPC_DispStr() To display an ASCII string anywhere on the screen11• 1.05.02 Elapsed <strong>Time</strong> Measurement– <strong>Time</strong> measurement is performed by using the PC’s 82C54 timernumber 2– PC_ElapsedStart(): begin to measure the function– PC_ElapsedStop(): return the measure result– PC_ElapsedInit(): comput the overhead ofthis two functionsINT16U time;PC_ElapsedInit();..PC_ElapsedStart();PC_DispChar(40, 24, ‘A’, DISP_FGND_WHITE);time = PC_ElapsedStop();12


1.05.03 Miscellaneous• PC_DOSSaveReturn(): save the DOS environment• PC_DOSReturn(): return to the DOS• PC_GetDate<strong>Time</strong>: obtain the PC’s current date and timewith the format MM-DD-YY HH:MM:SS• PC_SetTickRate(): use to change the tick rate– Under DOS, a tick occurs 18.20648 times per second (use the 8054default value 65535)– If the 8254 value is set to 5965, the tick rate would have been avery nice 200 Hz. In the uCOS-II, it use this value to do the tickclock– The DOS tick handler requires to call in every 11 time out131.07 Example 1• Consist 13 tasks– Two internal tasks: the idle task and a task that determines CPUusage– Create 11 tasks: the TaskStart() task is created by main()– Other 10 tasks is create by TaskStart() task. This 10 tasks are basedon the same codevoid main (void){PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); (1)OSInit(); (2)PC_DOSSaveReturn(); (3)PC_VectSet(uCOS, OSCtxSw); // INT 80H 200HZ (4)RandomSem = OSSemCreate(1); (5)OSTaskCreate(TaskStart, (6)(void *)0,(void *)&TaskStartStk[TASK_STK_SIZE-1],0);OSStart(); (7)}– OSInit () creates two tasks : an idle task, which executes when noother task is ready to run and a statistic task, which computes CPUusage14


Listing 1.6 Saving the DOS Environmentvoid PC_DOSSaveReturn (void){PC_ExitFlag = FALSE; (1)OSTickDOSCtr = 8; (2)PC_TickISR = PC_VectGet(VECT_TICK); (3)OS_ENTER_CRITICAL();PC_VectSet(VECT_DOS_CHAIN, PC_TickISR); (4)OS_EXIT_CRITICAL();setjmp(PC_JumpBuf); (5)if (PC_ExitFlag == TRUE) {OS_ENTER_CRITICAL();PC_SetTickRate(18); (6)PC_VectSet(VECT_TICK, PC_TickISR); (7)OS_EXIT_CRITICAL();PC_DispClrScr(DISP_FGND_WHITE+ DISP_BGND_BLACK); (8)exit(0); (9)}}Listing 1.7 Setting up to return to DOSvoid PC_DOSReturn (void){PC_ExitFlag = TRUE; (1)longjmp(PC_JumpBuf, 1); //jump to the after of setjmp () location (2)}15Listing 1.3 Example #1, TaskStart()void TaskStart (void *data){#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */OS_CPU_SR cpu_sr;#endifchar s[100];INT16S key;pdata = pdata; (1) /* Prevent compiler warning */TaskStartDispInit(); (2) /* Initialize the display */OS_ENTER_CRITICAL(); (3)PC_VectSet(0x08, OSTickISR); (4)PC_SetTickRate(200); (5)OS_EXIT_CRITICAL(); (6)OSStatInit(); (7)TaskStartCreateTasks()(8) Create 10 identical tasks;for (;;) {TaskStartDisp();// Display the number of tasks created;if (key was pressed) {if (key pressed was the ESCAPE key) {PC_DOSReturn();}}Delay for 1 Second;}}If you run code in an embedded application, you should always enable the ticker within the first task16


Example #1 Determining the PC’s speedvoid OSStatInit (void){OS<strong>Time</strong>Dly(2); (1)OS_ENTER_CRITICAL();OSIdleCtr = 0L; (2)OS_EXIT_CRITICAL();OS<strong>Time</strong>Dly(OS_TICKS_PER_SEC); (3)OS_ENTER_CRITICAL();OSIdleCtrMax = OSIdleCtr; (4)OSStatRdy = TRUE; (5)OS_EXIT_CRITICAL();}• OSIdleCtrMax contains the largest value of the OSIdleCtl• Use OSStatTask() to compute the CPU utilization, whichexecutes every second17Listing 1.4 Example #1 TaskStartCreateTasks()static void TaskStartCreateTasks (void){INT8U i;for (i = 0; i < N_TASKS; i++) { /* Create N_TASKS identical tasks */TaskData[i] = '0' + i; /* Each task will display its own letter */OSTaskCreate(Task,(void *)&TaskData[i],&TaskStk[i][TASK_STK_SIZE - 1],i + 1);}}18


Listing 1.5 Example #1 Task that displays a numberat random locations on the screenvoid Task (void *data){UBYTE x;UBYTE y;UBYTE err;for (;;) {OSSemPend(RandomSem, 0, &err); (1)x = random(80); (2)y = random(16);OSSemPost(RandomSem); (3)PC_DispChar(x, y + 5, *(char *)data, DISP_FGND_LIGHT_GRAY); (4)OS<strong>Time</strong>Dly(1); (5)}}• TaskStart () creates all the 10 identical tasks, and nocontext switch occurs because TaskStart() has a priority of0 (the highest priority)19Homework 1: compiler the uCOS-II under80x86 platform and run the Example 120

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!