AFHBA404
AFHBA404 connects ACQ2106 to PCI-Express
afhba-llcontrol.c
Go to the documentation of this file.
1 
4 /* afhba-llcontrol-common.h D-TACQ ACQ400 FMC DRIVER
5  * Project: AFHBA404
6  * Created: 5 Mar 2018 / User: pgm
7  * ------------------------------------------------------------------------- *
8  * Copyright (C) 2018 Peter Milne, D-TACQ Solutions Ltd *
9  * <peter dot milne at D hyphen TACQ dot com> *
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of Version 2 of the GNU General Public License *
13  * as published by the Free Software Foundation; *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the Free Software *
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
23  *
24  * TODO
25  * TODO
26 /* ------------------------------------------------------------------------- */
27 
28 #define _GNU_SOURCE
29 #include <sched.h>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <sched.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39 #include <sys/mman.h>
40 #include <sys/types.h>
41 
42 #include "afhba-llcontrol.h"
43 #define HB_FILE "/dev/rtm-t.%lu"
44 #define LOG_FILE "afhba.%d.log"
45 
46 #define HB1 "/dev/rtm-t.%d.data/hb01"
47 
48 #define HB_LEN 0x100000 /* 1MB HOST BUFFERSW */
49 
50 #include <time.h>
51 
52 #define NS 1000000000
53 #define US 1000000
54 #define NSUS (NS/US)
55 
56 #ifndef DEF_NCHAN
57 #define DEF_NCHAN 16
58 #endif
60 int spadlongs = 16;
61 
62 
63 unsigned difftime_us(void)
64 /* return delta time in usec */
65 {
66  static struct timespec ts0;
67  struct timespec ts1;
68  unsigned dt;
69 
70  if (clock_gettime(CLOCK_MONOTONIC, &ts1) != 0){
71  perror("clock_gettime()");
72  exit(1);
73  }
74  if (ts0.tv_sec != 0){
75  struct timespec tsd;
76  tsd.tv_sec = ts1.tv_sec - ts0.tv_sec;
77  tsd.tv_nsec = ts1.tv_nsec - ts0.tv_nsec;
78  dt = tsd.tv_sec*US + tsd.tv_nsec/NSUS;
79  }else{
80  dt = 0;
81  }
82  ts0 = ts1;
83  return dt;
84 }
85 
86 void* get_mapping(dev_t devnum, int *pfd) {
87  char fname[80];
88  int fd;
89  void *host_buffer;
90 
91  sprintf(fname, HB_FILE, devnum);
92  fd = open(fname, O_RDWR);
93  if (fd < 0){
94  perror(fname);
95  exit(errno);
96  }
97 
98  host_buffer = mmap(0, HB_LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
99  if (host_buffer == (caddr_t)-1 ){
100  perror( "mmap" );
101  exit(errno);
102  }
103  memset(host_buffer, 0, HB_LEN);
104  if(pfd) *pfd = fd;
105  return host_buffer;
106 }
107 
108 void clear_mapping(int fd, void* hb)
109 {
110  munmap(hb, HB_LEN);
111  close(fd);
112 }
113 void setAffinity(unsigned cpu_mask)
114 {
115  int cpu = 0;
116  cpu_set_t cpu_set;
117  CPU_ZERO(&cpu_set);
118  for (cpu = 0; cpu < 32; ++cpu){
119  if ((1<<cpu) &cpu_mask){
120  CPU_SET(cpu, &cpu_set);
121  }
122  }
123  printf("setAffinity: %d,%d,%d,%d\n",
124  CPU_ISSET(0, &cpu_set), CPU_ISSET(1, &cpu_set),
125  CPU_ISSET(2, &cpu_set), CPU_ISSET(3, &cpu_set)
126  );
127 
128  int rc = sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set);
129  if (rc != 0){
130  perror("sched_set_affinity");
131  exit(1);
132  }
133 }
134 
135 
137 
138 void goRealTime(void)
139 {
140  if (sched_fifo_priority < 1){
141  return;
142  }else{
143  struct sched_param p = {};
144  p.sched_priority = sched_fifo_priority;
145 
146 
147 
148  int rc = sched_setscheduler(0, SCHED_FIFO, &p);
149 
150  if (rc){
151  perror("failed to set RT priority");
152  }
153  }
154 }
155 
sched_fifo_priority
int sched_fifo_priority
Definition: afhba-llcontrol.c:136
afhba-llcontrol.h
goRealTime
void goRealTime(void)
Definition: afhba-llcontrol.c:138
HB_LEN
#define HB_LEN
Definition: afhba-llcontrol.c:48
NSUS
#define NSUS
Definition: afhba-llcontrol.c:54
difftime_us
unsigned difftime_us(void)
Definition: afhba-llcontrol.c:63
HB_FILE
#define HB_FILE
Definition: afhba-llcontrol.c:43
DEF_NCHAN
#define DEF_NCHAN
Definition: afhba-llcontrol.c:57
nchan
int nchan
Definition: afhba-llcontrol.c:59
get_mapping
void * get_mapping(dev_t devnum, int *pfd)
Definition: afhba-llcontrol.c:86
setAffinity
void setAffinity(unsigned cpu_mask)
Definition: afhba-llcontrol.c:113
US
#define US
Definition: afhba-llcontrol.c:53
spadlongs
int spadlongs
Definition: afhba-llcontrol.c:60
clear_mapping
void clear_mapping(int fd, void *hb)
Definition: afhba-llcontrol.c:108