📄 misc.c
字号:
/* misc.c */#include "sys_conf.h"#include <psos.h>#include <prepc.h>#include <stdio.h>#include <stdarg.h>#include "misc.h"#include "appl.h"/* Log an error message and delete the calling task. * Note: Since this is not an expected call during normal execution, * a call to bpflush() is included to output any buffered data. */voidfail(ULONG rc, const char * fmt, ...){ va_list ap; ULONG tid; char str[256]; bpflush(); /* Flush any buffered printf() data */ va_start(ap, fmt); vsprintf(str, fmt, ap); va_end(ap); t_ident(0, 0, &tid); /* Find out who's calling */ printf("\nFAIL: %s, rc==0x%08x, errno==%d, tid==0x%08x\n", str, rc, errno, tid); exit(rc); /* Could use k_fatal(). Debatable choice. */}/* Output a string to the serial port device. * Purpose: * This routine may be used after pREPC+ resources have * been de-allocated. It is not intended as a replacement * for printf() but rather as a "last resort" method of * printing a disaster message. */static voiddumb_write(const char * fmt, ...){ ULONG rc, pb[4]; va_list ap; char str[256]; va_start(ap, fmt); vsprintf(str, fmt, ap); va_end(ap); pb[0] = strlen(str); pb[1] = (ULONG) str; pb[2] = pb[3] = 0; de_write(DEV_SERIAL, pb, &rc);}/* Task general exit * NOTES: Refer to $PSS_ROOT/include/prepc.h and also to the System * Concepts book, section 2.14.1 "Deleting Tasks That Use Components" * on pages 2-40/41 (separately bound edition). */intexit(int code){ ULONG tid, rc; char str[256]; t_ident(0, 0, &tid); if (code != 0) bprintf("Task 0x%08x exit==%d\n", tid, code);/* Let each optional pSOSystem component release its resources */#if SC_PREPC == YES fclose(0); /* return pREPC resources */#endif#if SC_PHILE == YES close_f(0); /* return pHILE resources */#endif#if SC_PNA == YES close(0); /* return pNA resources */#endif#if SC_PSE == YES pse_close(0); /* return pSE resources */#endif /* Note that we do NOT bpflush() as it is assumed there will * be one of these in the root task. The root task, on the * other hand, should call bpflush() before attempting to exit. */ rc = t_delete(0); /* Delete "me" */ /* Shouldn't get here in current release. Must be some new pSOSystem component present not previously known! */ bpflush(); dumb_write("Is this a new pSOS release???\n"); dumb_write("Task 0x%08x t_delete(0) returned %d???\n", tid, rc); dumb_write("Calling k_fatal!\n"); k_fatal(0x00010000, K_LOCAL);}/* Buffered Print Routines * Purpose: * Normal printf() output causes the task to be blocked * while the data is printed. When the purpose of the * program is to demonstrate the interplay of task priorities, * preemption, and other blocking kernel calls, these * buffered print routines may be used: they do not block * the caller. Instead, all output data is copied to a * single buffer thereby preserving the order of output but * not perturbing the flow of execution. * * Usage: * bpinit(BufSize); Acquire BufSize storage. * bprintf(AsUsual); Add data to buffer. * bpflush(); Prints all bprintf() data. * * Note: * bpinit() and bpflush() are designed to be used ONCE, * typically in the ROOT task during start-up or shutdown. * bprintf(), on the other hand, may be used over and over * by any task at any priority: it is re-entrant. (It may * not, however, be used from ISRs.) */static char * bstart = 0; /* Buffer start address */static int bsize = 0; /* Total buffer size */static char * bptr = 0; /* Current buffer pointer */static int bleft = 0; /* Buffer space remaining *//* bpreset - Reset buffer pointers: Used herein only */static intbpreset(){ ULONG rc, mode; if (rc = t_mode(T_NOPREEMPT, T_NOPREEMPT, & mode)) fail(rc, "bpreset: T_NOPREEMPT"); bptr = bstart; bleft = bsize; *bptr = '\0'; if (rc = t_mode(T_NOPREEMPT, mode, & mode)) fail(rc, "bpreset: PREEMPT Restoration"); return(0);}/* bpinit - Initialize buffered print routines * Purpose: * Use this ONCE at the beginning of a test run to allocate * buffer storage for bprintf() data. */intbpinit(int bufsize){ ULONG rc; bsize = bufsize; if (rc = rn_getseg(0, bsize, RN_NOWAIT, 0, &bstart)) fail(rc, "bpinit: rn_getseg() failed"); bpreset(); return(0);}/* bprintf - Add to buffered data * Purpose: * Use exactly like printf(): the formatted output will be * stored in the internal buffer without blocking the task. */intbprintf(const char * fmt, ...){ int ret; va_list ap; char str[256], * cp; ULONG rc, mode; va_start(ap, fmt); vsprintf(str, fmt, ap); /* Format the data */ va_end(ap); cp = str; if (rc = t_mode(T_NOPREEMPT, T_NOPREEMPT, & mode)) fail(rc, "bprintf: T_NOPREEMPT"); while (*cp) { *bptr++ = *cp++; /* Copy data to buffer. */ if (--bleft == 1) { *bptr = '\0'; /* Flush if full. */ bpflush(); } } *bptr = '\0'; if (rc = t_mode(T_NOPREEMPT, mode, & mode)) fail(rc, "bprintf: PREEMPT Restoration"); return(bleft);}/* bpflush - Print buffered data and flush buffer * Purpose: * Print (to stdout) all buffered data. Typically this * is used at the very end of a test run. */intbpflush(){ puts(bstart); /* Output all buffered data */ bpreset(); /* Reset buffer pointer & count */ return(bleft);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -