summaryrefslogtreecommitdiff
path: root/dds_io_sub/diasio.cpp
blob: 9209fcb7831c3a4d52459bf24c5449e6d7f2693f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 ============================================================================
 Name        : diasio.cpp
 Author      : Manuel Traut
 Version     :
 Copyright   : Your copyright notice
 Description : DAIS IO Class
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/io.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>

#include "diasio.h"

unsigned int DIASIO::instance = 0;
bool DIASIO::stop_wd = false;
int DIASIO::m_Bereich1;
int DIASIO::m_Bereich2;
int DIASIO::m_Bereich3;

static void* watchDogTriggern(void* arg) 
{
	struct timespec req;
	unsigned int out = 0;
	
	req.tv_sec  = 0;
	req.tv_nsec = 300000;
	while(!DIASIO::stop_wd){
		outw( out, DIASIO::m_Bereich2 + 0x0c );
		nanosleep(&req, NULL);
	}
	return NULL;
}


DIASIO::DIASIO(){
	if(instance == 0){
		init();
	}
	instance++;
}

DIASIO::~DIASIO(){
	instance--;
	if(instance == 0){
		stop_wd = true;
		pthread_join(watchdog, NULL);
		S_AdresseOut -= m_Bereich2;
	}
}

void DIASIO::set(TyuInt16 value){
	outw( value, S_AdresseOut );
}

TyuInt16 DIASIO::get(){
	return inw( S_AdresseOut );
}

void DIASIO::init(){
	S_AdresseOut += m_Bereich2;

	char a_buffer[17];
	
	pthread_attr_t watchdog_attr;
		
	iopl( 3 );

	sprintf( a_buffer, "/dev/diasio_card%i", 0 );
	m_Deviceio = open( a_buffer, O_RDWR );

	if( -1 == m_Deviceio ) {
		fprintf(stderr, "Kein Zugriff auf IO-Karte %s", a_buffer);
		fflush(stderr);
		exit(0);
	}
   
	//PCI-Kartenadressen holen
	m_Bereich1 = 0;
	if( 0 != ioctl( m_Deviceio, D_TCDIASIOC_IOADRESSELESEN, &m_Bereich1 ) )
	{
		fprintf(stderr, "couldn't get bereich1\n");
		fflush(stderr);
		close( m_Deviceio );
		m_Deviceio = -1;
		exit(0);
	}

	m_Bereich2 = 1;
	if( 0 != ioctl( m_Deviceio, D_TCDIASIOC_IOADRESSELESEN, &m_Bereich2 ) )
	{
		fprintf(stderr, "couldn't get bereich2\n");
		fflush(stderr);
		close( m_Deviceio );
		m_Deviceio = -1;
		exit(0);
	}

	m_Bereich3 = 2;
	if( 0 != ioctl( m_Deviceio, D_TCDIASIOC_IOADRESSELESEN, &m_Bereich3 ) )
	{
		fprintf(stderr, "couldn't get bereich3\n");
		fflush(stderr);
		close( m_Deviceio );
		m_Deviceio = -1;
		exit(0);
	}

	fprintf(stderr, "1: 0x%x, 2: 0x%x, 3: 0x%x\n", m_Bereich1, m_Bereich2, m_Bereich3);
	fflush(stderr);

	pthread_attr_init(&watchdog_attr);
	if ( pthread_create(&watchdog, &watchdog_attr, watchDogTriggern, NULL) )
		perror("watchdog thread creation failed");
}