Linearg posted on 11:34:00 |
1. Official Program Guide In the installation directory:/docs/VxWorks/GUIDE/index.html Ii. Common databases: # I nclude "tasklib. H"/* task */ # I nclude "msgqlib. H"/* Message Queue */ # I nclude "semlib. H"/* semaphore */ # I nclude "iolib. H"/* IO */ # I nclude "wdlib. H"/* Watch Dog */ # I nclude "loglib. H"/* information output */ # I nclude "socket. H"/* network socket */ Iii. Io system: iolib. h 1. the I/O devices in the system, including the keyboard, serial port, and files, are accessed using unified interfaces. The first step is to get the file descriptor first, then read/write or set, and finally close the descriptor. Creat: create a file Open: get the file or device descriptor Read: reads files or devices. Write: Write a file or device IOCTL: set parameters Close: Close file descriptor Remove: delete an object 2. memory files Memdrv ()-initialize a pseudo-memory device Memdevcreate ()-create a pseudo-memory device Memdevcreatedir ()-creates a set of pseudo-memory devices Memdevdelete ()-delete a pseudo-memory device Init (){ Uchar_t buffer [1024]; Int FD; Memdrv (); Memdevcreate ("/MEM/mem1", buffer, sizeof (buffer )); If (FD = open ("/MEM/mem1", o_rdwr, 0644 ))! = Error ){ Write (FD, & Data, sizeof (data )); ...... Close (FD ); } Memdevdelete ("/MEM/mem1 "); } 3. implement multiple Io listeners using the select function: selectlib. h When waiting for multiple Io, we can use the select function. FD is the file descriptor: Int select ( Int width,/* The maximum FD, or fd_setsize (2048 )*/ Fd_set * preadfds,/* read FD Set */ Fd_set * pwritefds,/* FD set written */ Fd_set * p1_tfds,/* not supported by VxWorks, null */ Struct timeval * ptimeout/* wait time, null = forever */ ) There are several macros: Fd_set (FD, & fdset) sets the listening bit of FD Fd_clr (FD, & fdset) clears the listening bit of FD Fd_zero (& fdset) clears all listening bits Fd_isset (FD, & fdset) Whether FD has data For example, Max indicates the maximum value: Init (){ Struct fd_set readfds; Int FDS [4]; Int width; FDS [0] = open (...); ......; FDS [3] = open (...);/* Open Io */ Width = max (FDS [0],..., FDS [3]) + 1;/* maximum FD value + 1 */ /* Forever {*/ Fd_zero (& readfds);/* set the fd_set structure */ Fd_set (FDS [0], & readfds); ......; fd_set (FDS [3], & readfds ); If (select (width, & readfds, null) = Error) {/* listener */ Close (FDS [0]); ...... close (FDS [3]); Return; } For (I = 0; I If (fd_isset (FDS [I], & readfds )){ ...;/* Perform read/write operations */ } } /*}*/ } 4. Multi-task environment programming: 1. Task Control: tasklib. h Taskspawn ()-create a task Taskinit ()-initialize the task. You can specify the stack and PCB address by yourself. Taskactivate ()-activate an initialized task Exit ()-end in task (ANSI) Taskdelete ()-delete a task Taskdeleteforce ()-force Delete, even if it is protected Tasksuspend ()-pending a task Taskresume ()-resume a pending task Taskrestart ()-restart a task Taskpriorityset ()-change the task priority Taskpriorityget ()-read task priority Tasklock ()-Disable Task Scheduling Taskunlock ()-allows Task Scheduling Tasksafe ()-protect tasks from being deleted Taskunsafe ()-unprotect Taskdelay ()-latency Taskidself ()-Get the ID of the current task Taskidverify ()-whether the task id exists Tasktcb ()-Get the address of the task control block (TCB) Taskoptionsset ()-Change Task options Taskoptionsget ()-Get the current task Option Taskregsget ()-obtain the register information in task TCB. Taskregsset ()-set register information in task TCB Taskname ()-Get the Task Name Tasknametoid ()-ID obtained by name Taskiddefault ()-set the default task id Taskisready ()-whether the task is ready Taskissuincluded ()-whether the task is suspended Taskidlistget ()-Get the list of active tasks 2. Task mutex-semaphores: semlib. h Semgive ()-release a semaphore Semtake ()-getting a semaphore will block Semflush ()-changes all tasks blocked on this semaphore to ready state Semdelete ()-delete a semaphore 1) binary semaphore: sembcreate It can be used for task synchronization and mutex, but is often used for task synchronization. 2) mutex semaphores: semmcreate Semaphores dedicated to task mutex to protect critical resources 3) Count semaphores: semccreate Multi-instance Resource Access Control 3. task synchronization 1) Message Queue: msgqlib. h Message Queue Msgqcreate ()-create a message queue Msgqdelete ()-delete a Message Queue Msgqsend ()-send messages Msgqreceive ()-receives messages and blocks messages after the call Msgqnummsgs ()-Get the number of messages in the Message Queue Init (){ /* Create A Message Queue */ If (msgqid = msgqcreate (8, 1, msg_q_fifo) = NULL ){ Printf ("Message Queue create failed! /N "); } } Tasksend (){ If (OK! = Msgqsend (msgqid, "A", 1, no_wait, msg_pri_normal )){ Printf ("Message send failed! "); } } Taskreceive (){ Uchar_t ch; Msgqreceive (msgqid, & Ch, 1, wait_forever);/* the task will be blocked */ Printf ("received from msgq: % C", CH ); } 2) pipe: iolib. h. The system includes the pipe driver by default. Pipedevcreate ()-create an MPS queue Pipedevdelete ()-delete MPs queue Because the pipeline is Io, you can use select listeners. Message Queues are not Io and cannot use select Init (){ /* Create an MPS queue */ If (pipedevcreate ("/pipe/mypipe", 8, 1 )! = OK ){ Printf ("/pipe/mypipe create fialed! /N "); } /* Create mutex semaphores */ If (semmid = semmcreate (sem_q_fifo) = NULL) { Printf ("mutex semaphore create failed! /N "); } } Tasksend (){ Int PD;/* pipe descriptor */ If (Pd = open ("/pipe/mypipe", o_wronly, 0644) = Error ){ Printf ("Open pipe failed! "); } If (semtake (semmid, no_wait) = Error ){ Printf ("pipe in use! "); } Write (PD, "A", 1 ); Semgive (semmid ); Close (PD ); } Taskreceive (){ Int PD;/* pipe descriptor */ Uchar_t ch; If (Pd = open ("/pipe/mypipe", o_rdonly, 0644) = Error ){ Printf ("Open pipe failed! "); } If (read (PD, & Ch, 1)> 0) {/* the task will be blocked */ Printf ("received from pipe: % C", CH ); } } 3) binary semaphores Init (){ /* Create a binary semaphore */ If (sembid = sembcreate (sem_q_fifo, sem_empty) = NULL ){ Printf ("binary semaphore create failed! /N "); } } Tasksend (){ Semgive (sembid ); } Taskreceive (){ Semtake (sembid, wait_forever);/* the task will be blocked */ } 4) Event: eventlib The ID of the target task to be specified when an event is sent Eventreceive ()-Wait for event Eventsend ()-send events Eventclear ()-Clear the event of the current task. Tasksend (){ If (OK! = Eventsend (taskreceiveid, 0x00000001 )){ Printf ("event send failed! "); } } Taskreceive (){ Uint32 EV; If (OK! = Eventreceive (0x00ffffff, events_wait_any, wait_forever, & eV )){ Printf ("eventreceive error! /N "); } Else { Ev & = 0x00000001; If (EV ){ Printf ("event % d received! ", Ev ); } } } 5. Watch Dog: wdlib. h The system provides a soft watchdog timer, which is easy to use: Wdcreate ()-create a watchdog Wddelete ()-delete Wdstart ()-start Wdcancel ()-Stop Init (){ /* Create a watchdog */ If (wdid = wdcreate () = NULL ){ Printf ("Watch Dog create failed! /N "); } } Task (){ If (OK! = Wdstart (wdid, sysclkrateget () * 5, proc_wd, 0 )){ Printf ("Watch Dog start failed! /N "); } } Int proc_wd (int param ){ Logmsg (......); } 6. Network Programming: socklib. h Use a standard BSD socket and TCP or UDP for communication. Socket ()-open socket BIND ()-bind to port and address Listen ()-listener Mode Accept ()-allow the connection of the other party Connect ()-active remote connection Connectwithtimeout ()-connect function of the timeout Function Sendto ()-send Send ()-send Sendmsg ()-send Recvfrom ()-receive Recv ()-receive Recvmsg ()-receive Setsockopt ()-set socket Parameters Getsockopt ()-obtain the socket Parameter Getsockname ()-obtain the socket name Getpeername ()-Get the name of the connected point Shutdown ()-close connection VII. Exception Handling 1. Error code: errnolib. h 32-bit signed integer, 1 ~ 500 occupied by the system and available in other programs. For example # Define memory_leak 0x20005 Errnoget ()-Get the error code of the current task Errnooftaskget ()-Get the error code of the specified task Errnoset ()-set the error number of the current task Errnooftaskset ()-set the error number of the specified task 2. Signal: siglib. h Signal ()-entry function of the specified signal Raise ()-send a signal to the current task Kill ()-send a signal to a specified task Task1 (){ Signal (30, proc_sig);/* sign up for the 30th signal */ /* Raise (30 );*/ } Task2 (){ Kill (task1id, 30 ); } Void proc_sig (int param ){ Logmsg ("error message ..."); } VIII. Interruption: IV. H 0x0 ~ of x86 ~ 0xf: The value 0x20 ~ in VxWorks ~ 0x2f interrupted Take interruption on the 9th as an example: Initialization interruption: Intconnect (inum_to_ivec (9 + 0x20), int9handler, 0);/* bind the interrupt function */ Sysintenablepic (9);/* enable interruption on the 9th */ Interrupt function prototype: Void int9handler (int param);/* do not call the blocking function in the interrupt function */ |