### Analogy Transfer Setup and Cleanup Functions Source: https://doc.xenomai.org/v3/html/xeno3prm/transfer_8h_source Provides function prototypes for managing the lifecycle of Analogy data transfers. This includes presetup, setup, precleanup, and cleanup operations, all operating on the 'a4l_device_context'. ```c void a4l_presetup_transfer(struct a4l_device_context * cxt); int a4l_setup_transfer(struct a4l_device_context * cxt); int a4l_precleanup_transfer(struct a4l_device_context * cxt); int a4l_cleanup_transfer(struct a4l_device_context * cxt); ``` -------------------------------- ### Xenomai C: Define and Register Setup Calls Source: https://doc.xenomai.org/v3/html/xeno3prm/setup_8h_source These C macros define and register various types of setup calls, such as core, boilerplate, copperplate, interface, post, and user setup routines. They utilize `__attribute__((constructor))` to ensure execution during program bootstrap and register the setup functions with the system using `__register_setup_call`. ```c #define __bootstrap_ctor __attribute__ ((constructor(220))) #define __setup_call(__name, __id) \ static __setup_ctor void __declare_ ## __name(void) \ { \ __register_setup_call(&(__name), __id); \ } #define core_setup_call(__name) __setup_call(__name, 0) #define boilerplate_setup_call(__name) __setup_call(__name, 1) #define copperplate_setup_call(__name) __setup_call(__name, 2) #define interface_setup_call(__name) __setup_call(__name, 3) #define post_setup_call(__name) __setup_call(__name, 4) #define user_setup_call(__name) __setup_call(__name, 5) ``` -------------------------------- ### Main Function and CAN Socket Setup (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/can-rtt_8c-example This C code snippet demonstrates the main function of the Xenomai CAN RTT example. It parses command-line arguments, sets up real-time scheduling parameters, initializes CAN sockets (both RX and TX), binds them to specific network interfaces, and configures filters. It also sets up signal handlers and a message queue for statistics. Dependencies include system calls for argument parsing, socket creation, I/O control, socket options, binding, signal handling, and message queue operations. ```c int main(int argc, char *argv[]) { struct sched_param param = { .sched_priority = 1 }; Pthread_attr_t thattr; struct mq_attr mqattr; struct sockaddr_can rxaddr, txaddr; struct can_filter rxfilter[1]; struct rtt_stat rtt_stat; char mqname[32]; char *txdev, *rxdev; struct can_ifreq ifr; int ret, opt; struct option long_options[] = { { "id", required_argument, 0, 'i'}, { "cycle", required_argument, 0, 'c'}, { "repeater", no_argument, 0, 'r'}, { 0, 0, 0, 0}, }; while ((opt = getopt_long(argc, argv, "ri:c:", long_options, NULL)) != -1) { switch (opt) { case 'c': cycle = atoi(optarg); break; case 'i': can_id = strtoul(optarg, NULL, 0); break; case 'r': repeater = 1; break; default: fprintf(stderr, "Unknown option %c\n", opt); exit(-1); } } printf("%d %d\n", optind, argc); if (optind + 2 != argc) { xenomai_usage(); exit(0); } txdev = argv[optind]; rxdev = argv[optind + 1]; /* Create and configure RX socket */ if ((rxsock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("RX socket failed"); return -1; } namecpy(ifr.ifr_name, rxdev); printf("RX rxsock=%d, ifr_name=%s\n", rxsock, ifr.ifr_name); if (ioctl(rxsock, SIOCGIFINDEX, &ifr) < 0) { perror("RX ioctl SIOCGIFINDEX failed"); goto failure1; } /* We only want to receive our own messages */ rxfilter[0].can_id = can_id; rxfilter[0].can_mask = 0x3ff; if (setsockopt(rxsock, SOL_CAN_RAW, CAN_RAW_FILTER, &rxfilter, sizeof(struct can_filter)) < 0) { perror("RX setsockopt CAN_RAW_FILTER failed"); goto failure1; } memset(&rxaddr, 0, sizeof(rxaddr)); rxaddr.can_ifindex = ifr.ifr_ifindex; rxaddr.can_family = AF_CAN; if (bind(rxsock, (struct sockaddr *)&rxaddr, sizeof(rxaddr)) < 0) { perror("RX bind failed\n"); goto failure1; } /* Create and configure TX socket */ if (strcmp(rxdev, txdev) == 0) { txsock = rxsock; } else { if ((txsock = socket(PF_CAN, SOCK_RAW, 0)) < 0) { perror("TX socket failed"); goto failure1; } namecpy(ifr.ifr_name, txdev); printf("TX txsock=%d, ifr_name=%s\n", txsock, ifr.ifr_name); if (ioctl(txsock, SIOCGIFINDEX, &ifr) < 0) { perror("TX ioctl SIOCGIFINDEX failed"); goto failure2; } /* Suppress definiton of a default receive filter list */ if (setsockopt(txsock, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0) < 0) { perror("TX setsockopt CAN_RAW_FILTER failed"); goto failure2; } memset(&txaddr, 0, sizeof(txaddr)); txaddr.can_ifindex = ifr.ifr_ifindex; txaddr.can_family = AF_CAN; if (bind(txsock, (struct sockaddr *)&txaddr, sizeof(txaddr)) < 0) { perror("TX bind failed\n"); goto failure2; } } signal(SIGTERM, catch_signal); signal(SIGINT, catch_signal); signal(SIGHUP, catch_signal); printf("Round-Trip-Time test %s -> %s with CAN ID 0x%x\n", argv[optind], argv[optind + 1], can_id); printf("Cycle time: %d us\n", cycle); printf("All RTT timing figures are in us.\n"); /* Create statistics message queue */ snprintf(mqname, sizeof(mqname), "/rtcan_rtt-%d", getpid()); mqattr.mq_flags = 0; mqattr.mq_maxmsg = 100; mqattr.mq_msgsize = sizeof(struct rtt_stat); mq = mq_open(mqname, O_RDWR | O_CREAT | O_EXCL, 0600, &mqattr); if (mq == (mqd_t)-1) { perror("opening mqueue failed"); goto failure2; } /* Create receiver RT-thread */ Pthread_attr_init(&thattr); ``` -------------------------------- ### Define setup_descriptor struct in C Source: https://doc.xenomai.org/v3/html/xeno3prm/setup_8h_source Defines the setup_descriptor structure that encapsulates initialization callbacks and metadata for Xenomai setup modules. It includes function pointers for tuning, option parsing, help display, and initialization, along with reserved fields for internal management of setup descriptor registration and state tracking. ```c struct setup_descriptor { const char *name; int (*tune)(void); int (*parse_option)(int optnum, const char *optarg); void (*help)(void); int (*init)(void); const struct option *options; struct { int id; int opt_start; int opt_end; struct pvholder next; int done; } __reserved; }; ``` -------------------------------- ### XDDP RT/NRT Thread Communication Setup in C Source: https://doc.xenomai.org/v3/html/xeno3prm/xddp-echo_8c-example Demonstrates the initialization and configuration of XDDP-based communication between Xenomai real-time threads and Linux POSIX threads. Real-time threads use sockets bound to XDDP ports while non-real-time threads access corresponding pseudo-device files (/dev/rtp). The example includes header includes for RTDM IPC functionality, thread declarations, and port configuration constants. ```C #include #include #include #include #include #include #include #include #include #include #include pthread_t rt, nrt; #define XDDP_PORT 0 /* [0..CONFIG-XENO_OPT_PIPE_NRDEV - 1] */ static const char *msg[] = { "Surfing With The Alien", "Lords of Karma", "Banana Mango", "Psycho Monkey", "Luminous Flesh Giants", "Moroccan Sunset", "Satch Boogie", "Flying In A Blue Dream", "Ride", "Summer Song", "Speed Of Light", "Crystal Planet" }; ``` -------------------------------- ### C: Xenomai RTIPC XDDP Main Function and Thread Setup Source: https://doc.xenomai.org/v3/html/xeno3prm/xddp-echo_8c-example This C code demonstrates the main function for the Xenomai RTIPC XDDP example. It sets up signal masks, initializes thread attributes for both real-time (SCHED_FIFO) and regular (SCHED_OTHER) threads, creates the threads using pthread_create, and then manages their lifecycle by waiting for termination signals and joining the threads. ```c int main(int argc, char **argv) { struct sched_param rtparam = { .sched_priority = 42 }; pthread_attr_t rtattr, regattr; sigset_t set; int sig; sigemptyset(&set); sigaddset(&set, SIGINT); sigaddset(&set, SIGTERM); sigaddset(&set, SIGHUP); pthread_sigmask(SIG_BLOCK, &set, NULL); pthread_attr_init(&rtattr); pthread_attr_setdetachstate(&rtattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(&rtattr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&rtattr, SCHED_FIFO); pthread_attr_setschedparam(&rtattr, &rtparam); errno = pthread_create(&rt, &rtattr, &realtime_thread, NULL); if (errno) fail("pthread_create"); pthread_attr_init(®attr); pthread_attr_setdetachstate(®attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(®attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(®attr, SCHED_OTHER); errno = pthread_create(&nrt, ®attr, ®ular_thread, NULL); if (errno) fail("pthread_create"); __STD(sigwait(&set, &sig)); pthread_cancel(rt); pthread_cancel(nrt); pthread_join(rt, NULL); pthread_join(nrt, NULL); return 0; } ``` -------------------------------- ### Define constructor priority macros in C Source: https://doc.xenomai.org/v3/html/xeno3prm/setup_8h_source Defines three constructor priority levels for controlling Xenomai initialization order: __setup_ctor (priority 200) for setup calls, __early_ctor (priority 210) for early initialization with unspecified order relative to setup, and implicit __bootstrap_ctor for bootstrap code. These macros ensure deterministic initialization sequencing across different Xenomai interface layers. ```c #define __setup_ctor __attribute__ ((constructor(200))) #define __early_ctor __attribute__ ((constructor(210))) ``` -------------------------------- ### Xenomai Main: Task Creation and Initialization Source: https://doc.xenomai.org/v3/html/xeno3prm/cross-link_8c-example This C code demonstrates the main function for initializing and starting Xenomai real-time tasks. It includes opening RTIPC devices, configuring them using ioctl, creating tasks with rt_task_create, and starting them with rt_task_start. Signal handling for graceful termination is also implemented. ```c int err = 0; signal(SIGTERM, catch_signal); signal(SIGINT, catch_signal); /* open rtser0 */ write_fd = open( WRITE_FILE, 0); if (write_fd < 0) { printf(MAIN_PREFIX "can't open %s (write), %s\n", WRITE_FILE, strerror(errno)); goto error; } write_state |= STATE_FILE_OPENED; printf(MAIN_PREFIX "write-file opened\n"); /* writing write-config */ err = ioctl(write_fd, RTSER_RTIOC_SET_CONFIG, &write_config); if (err) { printf(MAIN_PREFIX "error while RTSER_RTIOC_SET_CONFIG, %s\n", strerror(errno)); goto error; } printf(MAIN_PREFIX "write-config written\n"); /* open rtser1 */ read_fd = open( READ_FILE, 0 ); if (read_fd < 0) { printf(MAIN_PREFIX "can't open %s (read), %s\n", READ_FILE, strerror(errno)); goto error; } read_state |= STATE_FILE_OPENED; printf(MAIN_PREFIX "read-file opened\n"); /* writing read-config */ err = ioctl(read_fd, RTSER_RTIOC_SET_CONFIG, &read_config); if (err) { printf(MAIN_PREFIX "error while ioctl, %s\n", strerror(errno)); goto error; } printf(MAIN_PREFIX "read-config written\n"); /* create write_task */ err = rt_task_create(&write_task, "write_task", 0, 50, 0); if (err) { printf(MAIN_PREFIX "failed to create write_task, %s\n", strerror(-err)); goto error; } write_state |= STATE_TASK_CREATED; printf(MAIN_PREFIX "write-task created\n"); /* create read_task */ err = rt_task_create(&read_task, "read_task", 0, 51, 0); if (err) { printf(MAIN_PREFIX "failed to create read_task, %s\n", strerror(-err)); goto error; } read_state |= STATE_TASK_CREATED; printf(MAIN_PREFIX "read-task created\n"); /* start write_task */ printf(MAIN_PREFIX "starting write-task\n"); err = rt_task_start(&write_task, &write_task_proc, NULL); if (err) { printf(MAIN_PREFIX "failed to start write_task, %s\n", strerror(-err)); goto error; } /* start read_task */ printf(MAIN_PREFIX "starting read-task\n"); err = rt_task_start(&read_task,&read_task_proc,NULL); if (err) { printf(MAIN_PREFIX "failed to start read_task, %s\n", strerror(-err)); goto error; } for (;;) pause(); return 0; error: cleanup_all(); return err; } ``` -------------------------------- ### Xenomai xntimer Start Function Source: https://doc.xenomai.org/v3/html/xeno3prm/include_2cobalt_2kernel_2timer_8h_source Prototype for the function that starts an xntimer with a specified initial value. ```c int xntimer_start(struct xntimer *timer, xnticks_t value, ``` -------------------------------- ### Initialize and Start Real-Time Task (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/group__rtdm__task Initializes and starts a new real-time task. It requires a task structure, name, the task procedure function, an argument for the procedure, priority, and period. Returns an integer status code. ```c int rtdm_task_init (rtdm_task_t *task, const char *name, rtdm_task_proc_t task_proc, void *arg, int priority, nanosecs_rel_t period) ``` -------------------------------- ### Macros Documentation Source: https://doc.xenomai.org/v3/html/xeno3prm/globals_defs_s This section lists and describes various macros available in the Xenomai API, categorized by their starting letter and associated header files. ```APIDOC ## Macros Documentation This section provides documentation for various macros available within the Xenomai API. ### Macros starting with 's' #### SIOCGCANBAUDRATE * **File**: can.h * **Description**: Gets the CAN baud rate. #### SIOCGCANCTRLMODE * **File**: can.h * **Description**: Gets the CAN controller mode. #### SIOCGCANCUSTOMBITTIME * **File**: can.h * **Description**: Gets the CAN custom bit time. #### SIOCGCANSTATE * **File**: can.h * **Description**: Gets the CAN controller state. #### SIOCGIFINDEX * **File**: can.h * **Description**: Gets the interface index. #### SIOCSCANBAUDRATE * **File**: can.h * **Description**: Sets the CAN baud rate. #### SIOCSCANCTRLMODE * **File**: can.h * **Description**: Sets the CAN controller mode. #### SIOCSCANCUSTOMBITTIME * **File**: can.h * **Description**: Sets the CAN custom bit time. #### SIOCSCANMODE * **File**: can.h * **Description**: Sets the CAN mode. #### SO_RCVTIMEO * **File**: ipc.h * **Description**: Socket option for receive timeout. #### SO_SNDTIMEO * **File**: ipc.h * **Description**: Socket option for send timeout. #### SOL_CAN_RAW * **File**: can.h * **Description**: Socket level for CAN RAW protocol. ### Macros starting with 'a' * Documentation for macros starting with 'a' would be listed here if available. ### Macros starting with 'b' * Documentation for macros starting with 'b' would be listed here if available. ### Macros starting with 'c' * Documentation for macros starting with 'c' would be listed here if available. ### Macros starting with 'f' * Documentation for macros starting with 'f' would be listed here if available. ### Macros starting with 'g' * Documentation for macros starting with 'g' would be listed here if available. ### Macros starting with 'i' * Documentation for macros starting with 'i' would be listed here if available. ### Macros starting with 'n' * Documentation for macros starting with 'n' would be listed here if available. ### Macros starting with 'p' * Documentation for macros starting with 'p' would be listed here if available. ### Macros starting with 'r' * Documentation for macros starting with 'r' would be listed here if available. ### Macros starting with 't' * Documentation for macros starting with 't' would be listed here if available. ### Macros starting with 'u' * Documentation for macros starting with 'u' would be listed here if available. ### Macros starting with 'x' * Documentation for macros starting with 'x' would be listed here if available. ``` -------------------------------- ### Define base_setup_data struct in C Source: https://doc.xenomai.org/v3/html/xeno3prm/setup_8h_source Defines the base_setup_data structure containing CPU affinity settings, memory locking flags, sanity checks, verbosity levels, trace levels, and program name reference. This structure serves as a foundation for system-level setup configuration in Xenomai applications. ```c struct base_setup_data { cpu_set_t cpu_affinity; int no_mlock; int no_sanity; int verbosity_level; int trace_level; const char *arg0; }; ``` -------------------------------- ### Define 32-bit Syscall Thunk Installation Macro - C Source: https://doc.xenomai.org/v3/html/xeno3prm/include_2asm-generic_2xenomai_2syscall32_8h_source Defines __COBALT_CALL32emu_THUNK macro that installs a 32-bit syscall thunk. Uses __syshand32emu__ to create a compatible handler wrapper for the specified syscall name and registers it with appropriate offset. ```C #define __COBALT_CALL32emu_THUNK(__name) \ __COBALT_CALL32emu_ENTRY(__name, __syshand32emu__(__name)) ``` -------------------------------- ### Task Creation Source: https://doc.xenomai.org/v3/html/xeno3prm/include_2alchemy_2task_8h_source Functions for creating and starting real-time tasks. ```APIDOC ## rt_task_create ### Description Create a task with Alchemy personality. ### Method N/A (C function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **task** (*RT_TASK*) - Pointer to the task descriptor. - **name** (*const char**) - Name of the task. - **stksize** (*int*) - Stack size for the task. - **prio** (*int*) - Base priority of the task. - **mode** (*int*) - Task creation mode. ### Request Example N/A ### Response #### Success Response (0) Returns 0 on success. #### Response Example ``` 0 ``` ## rt_task_spawn ### Description Create and start a real-time task. ### Method N/A (C function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **task** (*RT_TASK*) - Pointer to the task descriptor. - **name** (*const char**) - Name of the task. - **stksize** (*int*) - Stack size for the task. - **prio** (*int*) - Base priority of the task. - **mode** (*int*) - Task creation mode. - **entry** (*void(*)(void *arg)*) - Task entry point function. - **arg** (*void *arg*) - Argument for the task entry point function. ### Request Example N/A ### Response #### Success Response (0) Returns 0 on success. #### Response Example ``` 0 ``` ## rt_task_shadow ### Description Turn caller into a real-time task. ### Method N/A (C function) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **task** (*RT_TASK*) - Pointer to the task descriptor. - **name** (*const char**) - Name of the task. - **prio** (*int*) - Base priority of the task. - **mode** (*int*) - Task mode. ### Request Example N/A ### Response #### Success Response (0) Returns 0 on success. #### Response Example ``` 0 ``` ``` -------------------------------- ### Configure Periodic Task Execution in C Source: https://doc.xenomai.org/v3/html/xeno3prm/driver_8h_source Inline function that sets up periodic task scheduling with optional absolute start time. Validates period parameter (converts negative to zero) and start date (converts zero to infinite). Returns error code from underlying periodic setup function. ```C static inline int rtdm_task_set_period(rtdm_task_t *task, nanosecs_abs_t start_date, nanosecs_rel_t period) { if (period < 0) period = 0; if (start_date == 0) start_date = XN_INFINITE; return xnthread_set_periodic(task, start_date, XN_ABSOLUTE, period); } ``` -------------------------------- ### Xenomai Initialization Functions and Variables (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/xenomai_2init_8h_source This C code snippet declares functions for initializing the Xenomai system, handling command-line arguments, and providing usage and version information. It also exposes Xenomai version string and auto-bootstrap flag. The header includes boilerplate setup and ancillaries, and supports C++ linkage. ```c /* * Copyright (C) 2008 Philippe Gerum . * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #ifndef _XENOMAI_INIT_H #define _XENOMAI_INIT_H #include #include #ifdef __cplusplus extern "C" { #endif void xenomai_init(int *argcp, char *const **argvp); void xenomai_init_dso(int *argcp, char *const **argvp); int xenomai_main(int argc, char *const argv[]); void xenomai_usage(void); void application_usage(void); void application_version(void); extern const char *xenomai_version_string; extern const int xenomai_auto_bootstrap; #ifdef __cplusplus } #endif #endif /* _XENOMAI_INIT_H */ ``` -------------------------------- ### Initialize Real-Time Scheduler Parameters and Signal Handling Source: https://doc.xenomai.org/v3/html/xeno3prm/xddp-label_8c-example The main() function entry point that sets up real-time scheduling parameters (priority 42), initializes thread attributes for real-time execution (joinable, explicit scheduling), and blocks SIGINT, SIGTERM, SIGHUP signals. Prepares the environment for launching real-time and regular threads. ```C int main(int argc, char **argv) { struct sched_param rtparam = { .sched_priority = 42 }; pthread_attr_t rtattr, regattr; sigset_t set; int sig; sigemptyset(&set); sigaddset(&set, SIGINT); sigaddset(&set, SIGTERM); sigaddset(&set, SIGHUP); pthread_sigmask(SIG_BLOCK, &set, NULL); pthread_attr_init(&rtattr); pthread_attr_setdetachstate(&rtattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(&rtattr, PTHREAD_EXPLICIT_SCHED); ``` -------------------------------- ### Get Channel Index (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/rt2x00_8h_source Function to determine the index of a channel within its respective range (OFDM, UNII-low, HiperLAN2, UNII-high). It returns an index relative to the start of the range or -EINVAL if the channel is invalid or not within any defined range. ```c static inline int rt2x00_get_channel_index(const u8 channel) { if (OFDM_CHANNEL(channel)) return (channel - 1); if (channel % 4) return -EINVAL; if (UNII_LOW_CHANNEL(channel)) return ((channel - CHANNEL_UNII_LOW_MIN) / 4); else if (HIPERLAN2_CHANNEL(channel)) return ((channel - CHANNEL_HIPERLAN2_MIN) / 4); else if (UNII_HIGH_CHANNEL(channel)) return ((channel - CHANNEL_UNII_HIGH_MIN) / 4); return -EINVAL; } ``` -------------------------------- ### Xenomai Execution Time Statistics Macros (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/stat_8h_source Provides macros for managing execution time statistics within Xenomai. These include setting and getting the current account, finalizing accounts, retrieving start and total times, and resetting statistics. The functionality is enabled when CONFIG_XENO_OPT_STATS is defined. ```c #define xnstat_exectime_set_current(sched, new_account) \ 58({ 59 xnstat_exectime_t *__prev; 60 __prev = (xnstat_exectime_t *) \ 61 atomic_long_xchg((atomic_long_t *)&(sched)->current_account, 62 (long)(new_account)); 63 __prev; 64}) /* Return the currently active accounting entity. */ #define xnstat_exectime_get_current(sched) ((sched)->current_account) /* Finalize an account (no need to accumulate the exectime, just mark the 70 switch date and set the new account). */ #define xnstat_exectime_finalize(sched, new_account) \ 72do { 73 (sched)->last_account_switch = xnclock_core_read_raw(); 74 (sched)->current_account = (new_account); 75} while (0) /* Obtain content of xnstat_exectime_t */ #define xnstat_exectime_get_start(account) ((account)->start) #define xnstat_exectime_get_total(account) ((account)->total) /* Obtain last account switch date of considered sched */ #define xnstat_exectime_get_last_switch(sched) ((sched)->last_account_switch) /* Reset statistics from inside the accounted entity (e.g. after CPU 85 migration). */ #define xnstat_exectime_reset_stats(stat) \ 87do { 88 (stat)->total = 0; 89 (stat)->start = xnclock_core_read_raw(); 90} while (0) /* Account the exectime of the current account until now, switch to 135 new_account, and return the previous one. */ #define xnstat_exectime_switch(sched, new_account) \ 136( 137 xnstat_exectime_update(sched, xnstat_exectime_now()); 138 xnstat_exectime_set_current(sched, new_account); 139) /* Account the exectime of the current account until given start time, switch 143 to new_account, and return the previous one. */ #define xnstat_exectime_lazy_switch(sched, new_account, date) \ 144( 145 xnstat_exectime_update(sched, date); 146 xnstat_exectime_set_current(sched, new_account); 147) ``` -------------------------------- ### Analogy Header File Inclusion (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/analogy_8h Includes necessary header files for using the Analogy library in C. This demonstrates the standard setup for interacting with Xenomai's analogy facilities, requiring system types and specific Xenomai headers. ```c #include #include #include #include "boilerplate/list.h" ``` -------------------------------- ### Open Analogy Device - C Source: https://doc.xenomai.org/v3/html/xeno3prm/group__analogy__lib__descriptor1 Opens an Analogy device and initializes its descriptor. This function is essential before calling other I/O functions. It takes a device descriptor pointer and the device filename as input. Returns 0 on success, or an error code like -EINVAL or -EFAULT on failure. ```c int a4l_open(a4l_desc_t * _dsc_, const char * _fname_); ``` -------------------------------- ### Start newly created Xenomai thread Source: https://doc.xenomai.org/v3/html/xeno3prm/group__cobalt__core__thread Starts a newly initialized thread with specified start attributes. Returns 0 on success or an error code on failure. The thread must be initialized via xnthread_init before calling this function. ```C int xnthread_start(struct xnthread *thread, const struct xnthread_start_attr *attr) ``` -------------------------------- ### Analogy Library - Subdevice Configuration Function (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/sync_8c This function allows for the configuration of a subdevice within the Analogy library. It takes a descriptor, subdevice index, and type as arguments, with variable arguments for specific configuration parameters. ```c int | a4l_config_subd (a4l_desc_t *dsc, unsigned int idx_subd, unsigned int type,...) | Configure a subdevice. ``` -------------------------------- ### Define AI START and STOP Selection Register Macros Source: https://doc.xenomai.org/v3/html/xeno3prm/ni__stc_8h_source Defines macros for AI_START_STOP_Select_Register (register 62) controlling analog input trigger start and stop signal selection, synchronization, and edge sensitivity. Includes 5-bit select fields for START and STOP triggers, plus polarity, sync, and edge-detection flags for trigger configuration. ```C #define AI_START_STOP_Select_Register 62 #define AI_START_Polarity _bit15 #define AI_STOP_Polarity _bit14 #define AI_STOP_Sync _bit13 #define AI_STOP_Edge _bit12 #define AI_STOP_Select(a) (((a) & 0x1f)<<7) #define AI_START_Sync _bit6 #define AI_START_Edge _bit5 #define AI_START_Select(a) ((a) & 0x1f) ``` -------------------------------- ### Analogy Library - Include Statements Source: https://doc.xenomai.org/v3/html/xeno3prm/range_8c This code snippet includes necessary header files for the analogy library. These headers are essential for accessing the library's functions and data structures. ```C #include #include "internal.h" #include ``` -------------------------------- ### GET clock_getres - Get Clock Resolution Source: https://doc.xenomai.org/v3/html/xeno3prm/group__cobalt__api__time Retrieves the resolution of a specified clock (CLOCK_REALTIME or CLOCK_MONOTONIC). Returns the duration of one system clock tick for supported clocks. ```APIDOC ## GET clock_getres ### Description Get the resolution of the specified clock. This service returns the resolution of the clock at the specified address if it is not NULL. For both CLOCK_REALTIME and CLOCK_MONOTONIC, the resolution is the duration of one system clock tick. ### Method GET ### Endpoint /clock_getres ### Parameters #### Query Parameters - **clock_id** (clockid_t) - Required - Clock identifier, either CLOCK_REALTIME or CLOCK_MONOTONIC - **tp** (struct timespec*) - Required - Address where the resolution of the specified clock will be stored on success ### Response #### Success Response (0) - **tp** (struct timespec) - Contains the resolution of the specified clock #### Error Response (-1) - **errno** - Set to EINVAL if clock_id is invalid ### Response Example ``` { "status": 0, "tp": { "tv_sec": 0, "tv_nsec": 1000000 } } ``` ### Tags unrestricted ``` -------------------------------- ### MITE Device Setup and Teardown Functions Source: https://doc.xenomai.org/v3/html/xeno3prm/mite_8h_source Functions to initialize and de-initialize MITE devices. `a4l_mite_setup` configures a MITE device, potentially using IODWBSR_1, and `a4l_mite_unsetup` releases its resources. These are crucial for managing hardware interactions. ```c int a4l_mite_setup(struct mite_struct *mite, int use_iodwbsr_1); void a4l_mite_unsetup(struct mite_struct *mite); ``` -------------------------------- ### Xenomai Task Spawning (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/include_2trank_2native_2task_8h_source Creates and starts a real-time task, including specifying an entry point function and its argument. It requires task details, entry point, and argument. ```c #include int rt_task_spawn(RT_TASK *task, const char *name, int stksize, int prio, int mode, void (*entry)(void *arg), void *arg); ``` -------------------------------- ### Analogy Driver Registration and Management (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/analogy_2driver_8h_source Defines the structure for an Analogy driver and provides functions for registering, unregistering, and locating drivers. It includes hooks for initialization and cleanup, and conditional support for procfs integration. ```c #ifndef _COBALT_RTDM_ANALOGY_DRIVER_H #define _COBALT_RTDM_ANALOGY_DRIVER_H #include #include #include #include struct seq_file; struct a4l_link_desc; struct a4l_device; /* Analogy driver descriptor */ struct a4l_driver { /* List stuff */ struct list_head list; /* Visible description stuff */ struct module *owner; unsigned int flags; char *board_name; char *driver_name; int privdata_size; /* Init/destroy procedures */ int (*attach) (struct a4l_device *, struct a4l_link_desc *); int (*detach) (struct a4l_device *); }; /* Driver list related functions */ int a4l_register_drv(struct a4l_driver * drv); int a4l_unregister_drv(struct a4l_driver * drv); int a4l_lct_drv(char *pin, struct a4l_driver ** pio); #ifdef CONFIG_PROC_FS int a4l_rdproc_drvs(struct seq_file *p, void *data); #endif /* CONFIG_PROC_FS */ #endif /* !_COBALT_RTDM_ANALOGY_DRIVER_H */ ``` -------------------------------- ### Open Analogy Device - a4l_open() Source: https://doc.xenomai.org/v3/html/xeno3prm/group__analogy__lib__descriptor1 Opens an Analogy device and initializes the device descriptor structure. Returns a file descriptor associated with a context that enables asynchronous transfers. The descriptor must be validated before use. ```C int a4l_open(a4l_desc_t *dsc, const char *fname); ``` -------------------------------- ### Define Xenomai Timer Start Trace Event Source: https://doc.xenomai.org/v3/html/xeno3prm/cobalt-core_8h_source Defines the TRACE_EVENT for `cobalt_timer_start`. This event records the start of a timer, including timer details, value, interval, and mode. It supports conditional compilation for statistics. ```c #define cobalt_print_timer_mode(mode) \ __print_symbolic(mode, \ { XN_RELATIVE, "rel" }, \ { XN_ABSOLUTE, "abs" }, \ { XN_REALTIME, "rt" }) TRACE_EVENT(cobalt_timer_start, TP_PROTO(struct xntimer *timer, xnticks_t value, xnticks_t interval, xntmode_t mode), TP_ARGS(timer, value, interval, mode), TP_STRUCT__entry( __field(struct xntimer *, timer) #ifdef CONFIG_XENO_OPT_STATS __string(name, timer->name) #endif __field(xnticks_t, value) __field(xnticks_t, interval) __field(xntmode_t, mode) ), TP_fast_assign( __entry->timer = timer; #ifdef CONFIG_XENO_OPT_STATS __wrap_assign_str(name, timer->name); #endif __entry->value = value; __entry->interval = interval; __entry->mode = mode; ), TP_printk("timer=%p(%s) value=%Lu interval=%Lu mode=%s", __entry->timer, #ifdef CONFIG_XENO_OPT_STATS __get_str(name), #else "(anon)", #endif __entry->value, __entry->interval, cobalt_print_timer_mode(__entry->mode)) ); ``` -------------------------------- ### Mutex Acquire Example (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/group__alchemy__mutex An example demonstrating how to acquire a mutex with a relative timeout. This function, `rt_mutex_acquire`, is a variant of `rt_mutex_acquire_timed`. It allows blocking indefinitely or returning immediately if the mutex is already locked. ```c int rt_mutex_acquire ( RT_MUTEX * _mutex_ , RTIME _timeout_ ) ``` -------------------------------- ### Include Analogy Driver Headers (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/analogy_2driver_8h This snippet shows the necessary header includes for using the Xenomai analogy driver facilities in a Linux environment. It includes standard Linux list functionality and specific Xenomai RTDM headers for context and buffer management. ```c #include #include #include #include ``` -------------------------------- ### Start Xenomai Thread Execution Source: https://doc.xenomai.org/v3/html/xeno3prm/include_2cobalt_2kernel_2thread_8h_source Initiates execution of an initialized thread with specified start attributes. The thread must have been previously initialized via xnthread_init. Returns status code indicating success or failure of thread startup. ```C int xnthread_start(struct xnthread *thread, const struct xnthread_start_attr *attr); ``` -------------------------------- ### Analogy Library - Include Directives Source: https://doc.xenomai.org/v3/html/xeno3prm/sync_8c These are the essential include directives required for using the Analogy library functions. They import necessary standard libraries and Xenomai-specific headers. ```c #include #include #include #include "internal.h" ``` -------------------------------- ### Xenomai Task Creation and Control Source: https://doc.xenomai.org/v3/html/xeno3prm/include_2alchemy_2task_8h_source Functions for creating, starting, and managing the state of Xenomai real-time tasks. These include creating tasks with specific properties, starting their execution, and controlling their lifecycle through suspension and resumption. ```c int rt_task_create(RT_TASK *task, const char *name, int stksize, int prio, int mode); int rt_task_start(RT_TASK *task, void(*entry)(void *arg), void *arg); int rt_task_suspend(RT_TASK *task); int rt_task_resume(RT_TASK *task); int rt_task_shadow(RT_TASK *task, const char *name, int prio, int mode); int rt_task_spawn(RT_TASK *task, const char *name, int stksize, int prio, int mode, void(*entry)(void *arg), void *arg); ``` -------------------------------- ### Analogy Device I/O Control Functions Source: https://doc.xenomai.org/v3/html/xeno3prm/device_8h_source Implements I/O control functions for Analogy devices. `a4l_ioctl_devcfg` handles device configuration via ioctl. `a4l_ioctl_devinfo` retrieves device information using ioctl. ```c int a4l_ioctl_devcfg(struct a4l_device_context * cxt, void *arg); int a4l_ioctl_devinfo(struct a4l_device_context * cxt, void *arg); ``` -------------------------------- ### Main Function: Thread Management and Setup Source: https://doc.xenomai.org/v3/html/xeno3prm/xddp-stream_8c-example The `main` function in this C code sets up and manages the real-time and regular threads. It configures thread attributes for specific scheduling policies (SCHED_FIFO for real-time, SCHED_OTHER for regular), blocks certain signals, creates the threads, waits for a termination signal, and then cleans up by joining the threads. ```c int main(int argc, char **argv) { struct sched_param rtparam = { .sched_priority = 42 }; pthread_attr_t rtattr, regattr; sigset_t set; int sig; sigemptyset(&set); sigaddset(&set, SIGINT); sigaddset(&set, SIGTERM); sigaddset(&set, SIGHUP); pthread_sigmask(SIG_BLOCK, &set, NULL); pthread_attr_init(&rtattr); pthread_attr_setdetachstate(&rtattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(&rtattr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&rtattr, SCHED_FIFO); pthread_attr_setschedparam(&rtattr, &rtparam); errno = pthread_create(&rt, &rtattr, &realtime_thread, NULL); if (errno) fail("pthread_create"); pthread_attr_init(®attr); pthread_attr_setdetachstate(®attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(®attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(®attr, SCHED_OTHER); errno = pthread_create(&nrt, ®attr, ®ular_thread, NULL); if (errno) fail("pthread_create"); __STD(sigwait(&set, &sig)); pthread_cancel(rt); pthread_cancel(nrt); pthread_join(rt, NULL); pthread_join(nrt, NULL); return 0; } ``` -------------------------------- ### Get Next List Entry (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/cobalt_2kernel_2list_8h_source Defines a macro 'list_next_entry' to get the next entry in a linked list. It takes the current entry and the member name used for list traversal. This macro is essential for iterating through linked lists. ```c #ifndef list_next_entry #define list_next_entry(__item, __member) \ list_entry((__item)->__member.next, typeof(*(__item)), __member) #endif ``` -------------------------------- ### Configure CAN Raw Filter (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/group__rtdm__can Installs a CAN raw filter list to control frame reception for a bound socket. An empty list can be installed for write-only sockets. This option replaces any existing filter list. ```C #define CAN_RAW_FILTER 0x1 // Example usage with setsockopt: // int sock = socket(PF_CAN, SOCK_RAW, CAN_RAW); // struct can_filter rfilter[1]; // rfilter[0].can_id = CAN_ID | CAN_ERR_FLAG; // rfilter[0].can_mask = CAN_MASK; // setsockopt(sock, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); ``` -------------------------------- ### Start Xenomai Alarm Source: https://doc.xenomai.org/v3/html/xeno3prm/group__alchemy__alarm Starts a previously created Xenomai alarm. It takes the alarm descriptor, an initial delay (value), and an optional interval for periodic alarms. Returns zero on success, or an error code if the alarm is not properly set up. ```c int rt_alarm_start (RT_ALARM *alarm, RTIME value, RTIME interval) ``` -------------------------------- ### Analogy Device Context and Macros Source: https://doc.xenomai.org/v3/html/xeno3prm/device_8h_source Defines functions and macros for managing the device context. `a4l_set_dev` sets the device context. `a4l_get_dev` is a macro to retrieve the device from a context, simplifying access. ```c void a4l_set_dev(struct a4l_device_context *cxt); #define a4l_get_dev(x) ((x)->dev) ``` -------------------------------- ### PHY Configuration and Diagnostics by Model - e1000 Source: https://doc.xenomai.org/v3/html/xeno3prm/e1000e_2e1000_8h_source Extern declarations for PHY model-specific setup, diagnostics, and link configuration for 82577, m88, and ife PHY models. Includes copper link setup, cable length detection, and force speed/duplex operations. ```C extern s32 e1000_copper_link_setup_82577(struct e1000_hw *hw); extern s32 e1000_get_phy_info_82577(struct e1000_hw *hw); extern s32 e1000_phy_force_speed_duplex_82577(struct e1000_hw *hw); extern s32 e1000_get_cable_length_82577(struct e1000_hw *hw); extern s32 e1000_get_phy_info_ife(struct e1000_hw *hw); extern s32 e1000_phy_force_speed_duplex_ife(struct e1000_hw *hw); extern bool e1000_check_phy_82574(struct e1000_hw *hw); ``` -------------------------------- ### Create and Join Threads with Real-Time Scheduling (C) Source: https://doc.xenomai.org/v3/html/xeno3prm/iddp-sendrecv_8c-example Demonstrates creating two threads, a server and a client, with explicit real-time scheduling policies (SCHED_FIFO) and priorities. It also shows how to block signals, wait for specific signals, and then cancel and join the created threads for proper cleanup. ```c #include #include #include #include // Assume server and client functions are defined elsewhere void *server(void *arg); void *client(void *arg); // Assume fail function is defined elsewhere void fail(const char *msg); int main(int argc, char **argv) { struct sched_param svparam = {.sched_priority = 71 }; struct sched_param clparam = {.sched_priority = 70 }; pthread_attr_t svattr, clattr; sigset_t set; int sig; pthread_t svtid, cltid; sigemptyset(&set); sigaddset(&set, SIGINT); sigaddset(&set, SIGTERM); sigaddset(&set, SIGHUP); pthread_sigmask(SIG_BLOCK, &set, NULL); pthread_attr_init(&svattr); pthread_attr_setdetachstate(&svattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(&svattr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&svattr, SCHED_FIFO); pthread_attr_setschedparam(&svattr, &svparam); errno = pthread_create(&svtid, &svattr, &server, NULL); if (errno) fail("pthread_create"); pthread_attr_init(&clattr); pthread_attr_setdetachstate(&clattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(&clattr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&clattr, SCHED_FIFO); pthread_attr_setschedparam(&clattr, &clparam); errno = pthread_create(&cltid, &clattr, &client, NULL); if (errno) fail("pthread_create"); __STD(sigwait(&set, &sig)); pthread_cancel(svtid); pthread_cancel(cltid); pthread_join(svtid, NULL); pthread_join(cltid, NULL); return 0; } ```