📄 demo.c
字号:
/*********************************************** MODULE HEADER *********
*
* PROCESS NAME: KERNEL
*
*
* FILENAME:
*
* DEMO.C
*
* DESCRIPTION
* -----------
*
* Contains code to test the uC/OS-II kernel port on the LSI Logic ZSP
*
*
* REVISION HISTORY
* ----------------
*
* DATE NAME REASON/CHANGE ID
* ---- ---- ----------------
* 02/22/01 S.Wright ORG
*
*
*
* Copyright LSI Logic, 2001
*
***********************************************************************/
/*********************************************** INCLUDE FILES ********/
#include "includes.h"
/*********************************************** LOCAL DEFINES ********/
#ifndef NULL
#define NULL 0
#endif
#define QUEUE_SIZE 4
#define NUM_TASKS 3
#define STACK_SIZE 256
/*
address of the memory-mapped LEDs
*/
#define LEDS *((INT16U *) 0xFF00)
/*
used to toggled LED1
*/
#define LED_0_ON_1_ON 0x0003
#define LED_0_ON_1_OFF 0x0001
/*
used to display errors
*/
#define LEDS_ON_ALL 0xFFFF
#define LEDS_ON_A 0xA000
#define LEDS_ON_5 0x5000
#define LEDS_ON_6 0x6000
#define LEDS_ON_9 0x9000
/*
task priorities
*/
#define TASK_0_PRIORITY 7
#define TASK_1_PRIORITY 21
#define TASK_2_PRIORITY 14
/*********************************************** LOCAL STRUCTURES *****/
typedef struct _PCB_STRUCT
{
OS_EVENT *InputQueue;
OS_EVENT *OutputQueue;
void *QueuePointers[QUEUE_SIZE];
BOOLEAN StartItAll;
INT16U MyPriorityLevel;
} PCB_STRUCT;
/*********************************************** GLOBAL FUNCTIONS *****/
int main (void);
/*********************************************** LOCAL FUNCTIONS ******/
static void Task (void *Data);
static void Task2 (void *Data);
/*********************************************** GLOBAL DATA **********/
/*********************************************** LOCAL DATA ***********/
static INT32U MessageStorage;
static INT16U Stacks[NUM_TASKS][STACK_SIZE];
static PCB_STRUCT PCB[NUM_TASKS];
volatile INT16U TimerLED;
/*********************************************** GLOBAL FUNCTION *******
*
* FUNCTION:
*
* int main (void)
*
* PARAMETERS:
*
* none
*
* RETURN VALUES:
*
* INT (nothing returned -- function never returns...)
*
* DESCRIPTION:
*
* creates IPC objects, installs tasks, inits OS
*
* ERROR HANDLING:
*
* various
*
* NOTES:
*
***********************************************************************/
int main (void)
{
INT8U ErrorVal;
LEDS = LED_0_ON_1_OFF;
/*
move the interrupt vector table to 0x0000
*/
asm (" bits %smode, 7");
OSInit ();
/*
create Task0's input QUEUE
*/
PCB[0].InputQueue = OSQCreate ((void * *) &(PCB[0].QueuePointers[0]), QUEUE_SIZE);
if (PCB[0].InputQueue == (OS_EVENT *) NULL)
{
/*
oops...
*/
while (1)
;
}
/*
create Task1's input QUEUE
*/
PCB[1].InputQueue = OSQCreate ((void * *) &(PCB[1].QueuePointers[0]), QUEUE_SIZE);
if (PCB[1].InputQueue == (OS_EVENT *) NULL)
{
/*
oops...
*/
while (1)
;
}
/*
setup the output queues
*/
PCB[0].OutputQueue = PCB[1].InputQueue;
PCB[1].OutputQueue = PCB[0].InputQueue;
/*
init the rest of the PCB data
*/
PCB[0].StartItAll = TRUE;
PCB[1].StartItAll = FALSE;
PCB[0].MyPriorityLevel = TASK_0_PRIORITY;
PCB[1].MyPriorityLevel = TASK_1_PRIORITY;
PCB[2].MyPriorityLevel = TASK_2_PRIORITY;
/*
create Task[0]
*/
ErrorVal = OSTaskCreate (Task, (void *) &PCB[0], &(Stacks[0][STACK_SIZE]), TASK_0_PRIORITY);
if (ErrorVal != OS_NO_ERR)
{
/*
oops...
*/
while (1)
;
}
/*
create Task[1]
*/
ErrorVal = OSTaskCreate (Task, (void *) &PCB[1], &(Stacks[1][STACK_SIZE]), TASK_1_PRIORITY);
if (ErrorVal != OS_NO_ERR)
{
/*
oops...
*/
while (1)
;
}
/*
create Task2
*/
ErrorVal = OSTaskCreate (Task2, (void *) &PCB[2], &(Stacks[2][STACK_SIZE]), TASK_2_PRIORITY);
if (ErrorVal != OS_NO_ERR)
{
while (1)
;
}
OSInitTimer ();
/*
Let's Rock!!
*/
OSStart ();
return (0);
}
/*********************************************** LOCAL FUNCTION ********
*
* FUNCTION:
*
* static void Task (void *Data)
*
* PARAMETERS:
*
* void *Data
*
* pointer to a PCB_STRUCT
*
* RETURN VALUES:
*
* none (never returns)
*
* DESCRIPTION:
*
* Sends a message back-and-forth between two instances. Task told
* to StartItAll "primes the pump" by sending the first message. Each
* message reception results in a toggling of LED2.
*
* ERROR HANDLING:
*
* uC/OS calls are checked for success -- errors displayed to LEDs
*
* NOTES:
*
***********************************************************************/
static void Task (void *Data)
{
PCB_STRUCT *MyPCB;
INT8U ErrorVal;
INT32U *MessageData;
INT16U MyPriorityLevel;
volatile INT16U LED_Shadow;
accum_a Temp;
MyPCB = (PCB_STRUCT *) Data;
/*
"pre-calc" the priority level for error displays
*/
MyPriorityLevel = PCB->MyPriorityLevel;
MyPriorityLevel <<= 8;
MyPriorityLevel &= 0x0F00;
/*
does this task start the processing??
*/
if (MyPCB->StartItAll)
{
/*
send the first message
*/
MessageStorage = 0;
ErrorVal = OSQPost (MyPCB->OutputQueue, (void *) &MessageStorage);
if (ErrorVal != OS_NO_ERR)
{
LED_Shadow = (LEDS_ON_A | MyPriorityLevel | 0 | ErrorVal);
LEDS = ~LED_Shadow;
/*
oops...
*/
while (1)
;
}
else
{
LED_Shadow = LEDS_ON_ALL;
LEDS = ~LED_Shadow;
}
}
while (1)
{
ErrorVal = OS_NO_ERR;
MessageData = (INT32U *) OSQPend (MyPCB->InputQueue, 0, &ErrorVal);
if (ErrorVal != OS_NO_ERR)
{
/*
LEDS = LEDS_ON_5;
*/
LED_Shadow = (LEDS_ON_5 | MyPriorityLevel | 0 | ErrorVal);
LEDS = ~LED_Shadow;
/*
oops...
*/
while (1)
;
}
else
{
/*
is the data ODD...
*/
if (*MessageData % 2)
{
/*
turn LED ON
*/
LED_Shadow = LED_0_ON_1_ON | (TimerLED & 0x8000);
LEDS = ~LED_Shadow;
}
/*
...or EVEN??
*/
else
{
/*
turn LED OFF
*/
LED_Shadow = LED_0_ON_1_OFF | (TimerLED & 0x8000);
LEDS = ~LED_Shadow;
}
MessageStorage++;
ErrorVal = OSQPost (MyPCB->OutputQueue, (void *) &MessageStorage);
if (ErrorVal != OS_NO_ERR)
{
LED_Shadow = (LEDS_ON_6 | MyPriorityLevel | 0 | ErrorVal);
LEDS = ~LED_Shadow;
/*
oops...
*/
while (1)
;
}
}
}
}
/*********************************************** LOCAL FUNCTION ********
*
* FUNCTION:
*
* static void Task2 (void *Data)
*
* PARAMETERS:
*
* void *Data:
*
* pointer to a PCB_STRUCT
*
* RETURN VALUES:
*
* none (never returns)
*
* DESCRIPTION:
*
* continually suspends on a 20000 us timeout -- toggling LED16 upon
* each wake-up
*
* ERROR HANDLING:
*
* none
*
* NOTES:
*
***********************************************************************/
static void Task2 (void *Data)
{
PCB_STRUCT *MyPCB;
INT32U Ticks;
/*
do something with Data so the compiler doesn't complain
*/
MyPCB = (PCB_STRUCT *) Data;
/*
set-up to toggle LED16
*/
TimerLED = 0x8000;
/*
make the usToTicks*() call now and save the return value so we're not
constantly making the call.
*/
Ticks = usToTicks (20000);
while (1)
{
/*
sleep...
*/
OSTimeDly (Ticks);
/*
...then toggle LED16
*/
if (TimerLED == 0x0000)
TimerLED = 0x8000;
else
TimerLED = 0x0000;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -