📄 test.c
字号:
#include <cyg/kernel/kapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <cyg/io/io.h>
#include <cyg/infra/diag.h>
#include "wd.h"
/* now declare (and allocate space for) some kernel objects,
like the two threads we will use */
cyg_thread thread_s[2]; /* space for two thread objects */
char stack[2][4096]; /* space for two 4K stacks */
/* now the handles for the threads */
cyg_handle_t simple_threadA, simple_threadB;
/* and now variables for the procedure which is the thread */
cyg_thread_entry_t SimpleProgram;
/* and now a mutex to protect calls to the C library */
cyg_mutex_t cliblock;
cyg_io_handle_t ttyHandl;
void HelloWorld(void) {
int err;
char outputString[] = "Hello World!\r\n";
cyg_uint32 outputLen = sizeof(outputString);
cyg_mutex_lock(&cliblock); {
err = cyg_io_write(ttyHandl, outputString, &outputLen);
if (err) {
diag_printf("ERROR writing to device masc.\n");
}
} cyg_mutex_unlock(&cliblock);
}
/* we install our own startup routine which sets up threads */
void cyg_user_start(void) {
int err;
diag_printf("Entering twothreads' cyg_user_start() function\n");
cyg_mutex_init(&cliblock);
cyg_thread_create(4, SimpleProgram, (cyg_addrword_t) 0,
"Thread A", (void *) stack[0], 4096,
&simple_threadA, &thread_s[0]);
diag_printf("Thread A created\n");
cyg_thread_create(4, SimpleProgram, (cyg_addrword_t) 1,
"Thread B", (void *) stack[1], 4096,
&simple_threadB, &thread_s[1]);
diag_printf("Thread B created\n");
cyg_thread_resume(simple_threadA);
cyg_thread_resume(simple_threadB);
diag_printf("Threads started\n");
WD_Enable();
err = cyg_io_lookup("/dev/masc", &ttyHandl);
if (err) {
diag_printf("ERROR opening device masc.\n");
}
}
/* this is a simple program which runs in a thread */
void SimpleProgram(cyg_addrword_t data) {
int err;
int message = (int) data;
int delay;
cyg_tick_count_t t = 0;
char str[100];
cyg_uint32 strLen;
cyg_mutex_lock(&cliblock); {
diag_printf("Beginning execution; thread data is %d\n", message);
} cyg_mutex_unlock(&cliblock);
cyg_thread_delay(500);
for (;;) {
delay = 2 + (rand() % 500) + data*1000;
sprintf(str, "Thread %d: and now a delay of %d clock ticks\r\n",
message, delay);
strLen = strlen(str);
if (data == 1) {
t = cyg_current_time();
} else {
WD_Kick();
}
/* note: printf() must be protected by a
call to cyg_mutex_lock() */
cyg_mutex_lock(&cliblock); {
err = cyg_io_write(ttyHandl, str, &strLen);
if (err) {
diag_printf("ERROR writing to device masc.\n");
}
} cyg_mutex_unlock(&cliblock);
if (data == 1) {
cyg_mutex_lock(&cliblock); {
diag_printf("Time to print %d: %d\n", strLen, cyg_current_time()-t);
t = cyg_current_time();
strLen = 1;
} cyg_mutex_unlock(&cliblock);
cyg_io_read(ttyHandl, str, &strLen);
cyg_mutex_lock(&cliblock); {
diag_printf("Time to read %d: %d\n", strLen, cyg_current_time()-t);
if (str[0] == 'r') {
diag_printf("System reset ordered!\n");
WD_ForceSystemReset();
}
} cyg_mutex_unlock(&cliblock);
if (str[0] == 'h') {
HelloWorld();
}
}
cyg_thread_delay(delay);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -