📄 app.c
字号:
/*
*********************************************************************************************************
* EXAMPLE CODE
*
* (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may NOT be used to develop a similar product.
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* EXAMPLE CODE
*
* NXP LPC2378
* on the
* IAR LPC2378-SK Evaluation Board
*
* Filename : app.c
* Version : V1.00
* Programmer(s) : Brian Nagel
*********************************************************************************************************
*/
#include <includes.h>
/*
*********************************************************************************************************
* #DEFINE CONSTANTS
*********************************************************************************************************
*/
#define COLOR_GREEN1 0x0F0
#define COLOR_GREEN2 0x0C0
#define COLOR_GREEN3 0x080
#define COLOR_BLACK 0x000
#define COLOR_WHITE 0xFFF
#define COLOR_RED 0xF00
#define COLOR_BLUE 0x00F
#define COLOR_ORANGE 0xF84
#define COLOR_TASK1 0xF00
#define COLOR_TASK2 0x0F0
#define COLOR_TASK3 0x00F
#define COLOR_TASK4 0xF84
#define COLOR_TASK5 0xFF0
#define COLOR_TASK6 0x0FF
#define COLOR_TASK7 0xF0F
#define COLOR_TASK8 0x888
#define COLOR_TASK9 0x48F
#define COLOR_TASK9 0x48F
#define COLOR_TASK10 0x4F8
#define CPU_USAGE_NUM_TASKS 8
#define CPU_USAGE_NUM_SAMPLES 35
#define CPU_USAGE_BUF_SIZE 200
/*
*********************************************************************************************************
* LOCAL VARIABLES
*********************************************************************************************************
*/
static OS_STK AppTaskStartStk[APP_TASK_START_STK_SIZE];
static OS_STK AppTaskUserIFStk[APP_TASK_USER_IF_STK_SIZE];
static OS_STK AppTaskKbdStk[APP_TASK_KBD_STK_SIZE];
static OS_STK AppTaskFactorialStk[APP_TASK_FACTORIAL_STK_SIZE];
static CPU_INT08U AppUserIFState; /* User I/F state */
static CPU_INT08U AppUserIFScroll; /* User I/F scroll */
static OS_EVENT *AppUserIFMbox1;
static OS_EVENT *AppUserIFMbox2;
static CPU_INT08U AppTaskCPUUsageIdx;
static CPU_FP32 AppTaskCPUUsage[CPU_USAGE_NUM_TASKS][CPU_USAGE_BUF_SIZE];
static CPU_INT32U AppTaskCPUUsageColors[CPU_USAGE_NUM_TASKS] = {COLOR_TASK1, COLOR_TASK2,
COLOR_TASK3, COLOR_TASK4,
COLOR_TASK5, COLOR_TASK6,
COLOR_TASK7, COLOR_TASK8};
extern FontType_t Terminal_6_8_6;
extern FontType_t Terminal_9_12_6;
extern FontType_t Terminal_18_24_12;
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void AppTaskStart (void *p_arg);
static void AppTaskUserIF (void *p_arg);
static void AppTaskKbd (void *p_arg);
static void AppTaskFactorial (void *p_arg);
static void AppTaskCreate (void);
static void AppDispScr_SignOn (void);
static void AppDispScr_VersionTickRate (void);
static void AppDispScr_CPU (void);
static void AppDiscScr_TaskNames (void);
static void AppDispScr_TaskStacks (void);
static void AppDispScr_TaskCPU (void);
static void AppDispScr_Transition (void);
#if OS_VIEW_MODULE > 0
static void AppTerminalRx (CPU_INT08U rx_data);
#endif
static CPU_INT32U AppRecursiveFactorial (CPU_INT08U i);
/*
*********************************************************************************************************
* main()
*
* Description : This is the standard entry point for C code. It is assumed that your code will call
* main() once you have performed all necessary 68HC12 and C initialization.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
void main (void)
{
CPU_INT08U err;
BSP_IntDisAll(); /* Disable all interrupts until we are ready to accept them */
OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel" */
OSTaskCreateExt(AppTaskStart, /* Create the start task */
(void *)0,
(OS_STK *)&AppTaskStartStk[APP_TASK_START_STK_SIZE - 1],
APP_TASK_START_PRIO,
APP_TASK_START_PRIO,
(OS_STK *)&AppTaskStartStk[0],
APP_TASK_START_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
#if OS_TASK_NAME_SIZE > 13
OSTaskNameSet(APP_TASK_START_PRIO, "Start Task", &err);
#endif
OSStart(); /* Start multitasking (i.e. give control to uC/OS-II) */
}
/*
*********************************************************************************************************
* STARTUP TASK
*
* Description : This is an example of a startup task. As mentioned in the book's text, you MUST
* initialize the ticker only once multitasking has started.
*
* Arguments : p_arg is the argument passed to 'AppTaskStart()' by 'OSTaskCreate()'.
*
* Returns : none
*
* Notes : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not
* used. The compiler should not generate any code for this statement.
*********************************************************************************************************
*/
static void AppTaskStart (void *p_arg)
{
CPU_INT16U adc;
CPU_INT32U idx;
CPU_INT32U jdx;
(void)p_arg;
BSP_Init(); /* Initialize BSP functions */
#if OS_TASK_STAT_EN > 0
OSStatInit(); /* Determine CPU capacity */
#endif
#if OS_VIEW_MODULE > 0
OSView_Init(38400); /* OSView Init, baud rate = 38400 */
OSView_TerminalRxSetCallback(AppTerminalRx);
OSView_RxIntEn(); /* Enable Rx Interrupts */
#endif
#if uC_PROBE_OS_PLUGIN > 0
OSProbe_Init();
OSProbe_SetDelay(200);
#endif
AppUserIFState = 0;
AppUserIFScroll = 0;
AppUserIFMbox1 = OSMboxCreate((void *)0); /* Create MBOX for communication between Kbd and UserIF */
AppUserIFMbox2 = OSMboxCreate((void *)0); /* Create MBOX for communication between Kbd and UserIF */
AppTaskCreate(); /* Create application tasks */
for (idx = 0; idx < CPU_USAGE_NUM_TASKS; idx++) {
for (jdx = 0; jdx < CPU_USAGE_BUF_SIZE; jdx++) {
AppTaskCPUUsage[idx][jdx] = 0;
}
}
AppTaskCPUUsageIdx = 0;
while (DEF_TRUE) { /* Task body, always written as an infinite loop. */
adc = ADC_GetStatus(1); /* Get current ADC reading */
LCD_LightSet((adc >> 4) + 0x40); /* Set the LCD backlight */
OSTimeDlyHMSM(0, 0, 0, 250); /* Delay for 250 ms */
for (idx = 0; idx < CPU_USAGE_NUM_TASKS; idx++) {
if (AppTaskCPUUsageIdx == CPU_USAGE_BUF_SIZE - 1) {
AppTaskCPUUsage[idx][0] = OSProbe_TaskCPUUsage[idx];
} else {
AppTaskCPUUsage[idx][AppTaskCPUUsageIdx + 1] = OSProbe_TaskCPUUsage[idx];
}
}
if (AppTaskCPUUsageIdx == CPU_USAGE_BUF_SIZE - 1) {
AppTaskCPUUsageIdx = 0;
} else {
AppTaskCPUUsageIdx++;
}
}
}
/*
*********************************************************************************************************
* USER INTERFACE TASK
*
* Description : This task updates the LCD screen based on messages passed to it by AppTaskKbd().
*
* Arguments : p_arg is the argument passed to 'AppStartUserIF()' by 'OSTaskCreate()'.
*
* Returns : none
*********************************************************************************************************
*/
static void AppTaskUserIF (void *p_arg)
{
CPU_INT08U *msg;
CPU_INT08U err;
CPU_INT32U nstate;
CPU_INT32U pstate;
CPU_INT08U scroll;
(void)p_arg;
GLCD_PowerUpInit(NULL); /* Initialize the LCD module */
GLCD_DrawRectangle (0, 0, 132, 132, COLOR_WHITE);
AppDispScr_SignOn(); /* Display sign-on screen */
OSTimeDlyHMSM(0, 0, 2, 0);
AppDispScr_Transition();
nstate = 1;
pstate = 0;
while (DEF_TRUE) { /* Task body, always written as an infinite loop. */
msg = (CPU_INT08U *)(OSMboxPend(AppUserIFMbox1, OS_TICKS_PER_SEC / 10, &err));
if (err == OS_NO_ERR) {
if (msg != (CPU_INT08U *)0) {
nstate = *msg;
}
}
msg = (CPU_INT08U *)(OSMboxPend(AppUserIFMbox2, 1, &err));
if (err == OS_NO_ERR) {
if (msg != (CPU_INT08U *)0) {
scroll = *msg;
GLCD_SendCmd(VSCSAD, &scroll, 1);
}
}
if (nstate != pstate) {
AppDispScr_Transition();
GLCD_DrawRectangle (0, 0, 132, 132, COLOR_WHITE);
}
switch (nstate) {
case 1:
AppDispScr_VersionTickRate();
break;
case 2:
AppDispScr_CPU();
break;
case 3:
AppDiscScr_TaskNames();
break;
case 4:
AppDispScr_TaskStacks();
break;
case 5:
AppDispScr_TaskCPU();
break;
case 0:
default:
AppDispScr_SignOn();
break;
}
pstate = nstate;
}
}
/*
*********************************************************************************************************
* KEYBOARD RESPONSE TASK
*
* Description : This task monitors the state of the push buttons and passes messages to AppTaskUserIF()
*
* Arguments : p_arg is the argument passed to 'AppStartKbd()' by 'OSTaskCreate()'.
*
* Returns : none
*********************************************************************************************************
*/
static void AppTaskKbd (void *p_arg)
{
CPU_BOOLEAN b1; /* State of Push Button #1 */
CPU_BOOLEAN b1_prev;
CPU_BOOLEAN b2; /* State of Push Button #2 */
CPU_BOOLEAN b2_prev;
CPU_INT08U joystick;
CPU_INT08U joystick_prev;
CPU_INT08U key;
CPU_INT08U scroll;
CPU_INT08U ctr;
CPU_BOOLEAN left;
CPU_BOOLEAN right;
(void)p_arg;
key = 1;
scroll = 0;
ctr = 0;
while (DEF_TRUE) {
b1 = PB_GetStatus(1);
b2 = PB_GetStatus(2);
joystick = Joystick_GetStatus();
if (( b1 == DEF_TRUE && b1_prev == DEF_FALSE && b2 != DEF_TRUE) ||
((joystick & JOYSTICK_LEFT) == JOYSTICK_LEFT && ((joystick_prev & JOYSTICK_LEFT) == 0 && (joystick & JOYSTICK_RIGHT) == 0 ))) {
left = DEF_TRUE;
} else {
left = DEF_FALSE;
}
if (( b2 == DEF_TRUE && b2_prev == DEF_FALSE && b1 != DEF_TRUE) ||
((joystick & JOYSTICK_RIGHT) == JOYSTICK_RIGHT && ((joystick_prev & JOYSTICK_RIGHT) == 0 && (joystick & JOYSTICK_LEFT) == 0 ))) {
right = DEF_TRUE;
} else {
right = DEF_FALSE;
}
if (left) {
if (key == 0) {
key = 5;
} else {
key--;
}
AppUserIFState = key;
OSMboxPost(AppUserIFMbox1, &AppUserIFState);
}
if (right) {
if (key == 5) {
key = 0;
} else {
key++;
}
AppUserIFState = key;
OSMboxPost(AppUserIFMbox1, &AppUserIFState);
}
if ((ctr & 0x03) == 0x03) {
if ((joystick & JOYSTICK_UP) == JOYSTICK_UP) {
if (scroll == 31) {
scroll = 0;
} else {
scroll++;
}
AppUserIFScroll = scroll;
OSMboxPost(AppUserIFMbox2, &AppUserIFScroll);
} else if ((joystick & JOYSTICK_DOWN) == JOYSTICK_DOWN) {
if (scroll == 0) {
scroll = 31;
} else {
scroll--;
}
AppUserIFScroll = scroll;
OSMboxPost(AppUserIFMbox2, &AppUserIFScroll);
}
}
b1_prev = b1;
b2_prev = b2;
joystick_prev = joystick;
ctr++;
OSTimeDly(OS_TICKS_PER_SEC / 10);
}
}
/*
*********************************************************************************************************
* CREATE APPLICATION TASKS
*
* Description: This function creates the application tasks.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -