-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project2.cc
135 lines (110 loc) · 3.78 KB
/
Project2.cc
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
123
124
125
126
127
128
129
130
131
132
133
134
135
//Global includes.
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h> /* for uintptr_t */
#include <hw/inout.h> /* for in*() and out*() functions */
#include <sys/neutrino.h> /* for ThreadCtl() */
#include <sys/mman.h> /* for mmap_device_io() */
//-------- Local Includes ----------//
//Old test class includes
//#include "DataProviderImpl.h"
//#include "PushbuttonScanTester.h"
#include "PulseScanner.h"
#include "PushbuttonScanner.h"
#include "Display.h"
#include "CyclometerController.h"
#include "ResetWatchdog.h"
// DAQ Port addresses
#define DATA_BASE_ADDRESS (0x280)
#define DATA_PORT_A (DATA_BASE_ADDRESS + 8)
#define DATA_PORT_B (DATA_BASE_ADDRESS + 9)
#define DATA_PORT_C (DATA_BASE_ADDRESS + 10)
#define DATA_DIRECTION (DATA_BASE_ADDRESS + 11)
#define PORT_LENGTH (1)
// Sets A and B to outputs and C to input
#define SET_DIRECTION (0b10001001)
// Declare the hardware port handles they are used...
uintptr_t daq_dir_handle;
uintptr_t daq_porta_handle;
uintptr_t daq_portb_handle;
uintptr_t daq_portc_handle;
uintptr_t ctrl_handle;
uintptr_t cmd_handle;
/**
* Get root permissions to get access to hardware...
*/
void getPermissions() {
int privity_err = ThreadCtl(_NTO_TCTL_IO, NULL );
if (privity_err == -1) {
fprintf(stderr, "can't get root permissions\n");
exit(-1);
}
}
/**
* Get a handle to hardware.
*
* @param address - the memory mapped address of the hardware register requested
* @return the uintptr_t handle to the hardware register of 8 bit length
*/
uintptr_t getHandle(int address) {
uintptr_t ret = mmap_device_io(PORT_LENGTH, address);
if (ret == MAP_DEVICE_FAILED) {
fprintf(stderr, "can't get handle on %d\n", address);
exit(-1);
}
return ret;
}
int main(int argc, char *argv[]) {
/* Give this thread root permissions to access the hardware */
getPermissions();
/* Get a handle for the direction register to set Port A and B */
daq_dir_handle = getHandle(DATA_DIRECTION);
/* Get a handle to Port A for controller outputs 1 */
daq_porta_handle = getHandle(DATA_PORT_A);
/* Get a handle to Port B for controller outputs 2 */
daq_portb_handle = getHandle(DATA_PORT_B);
/* Get a handle to Port C for controller inputs */
daq_portc_handle = getHandle(DATA_PORT_C);
/* Get a handle to the command port for interrupts */
cmd_handle = getHandle(DATA_BASE_ADDRESS);
/* Get a handle to the control register... */
ctrl_handle = getHandle(DATA_BASE_ADDRESS + 4);
//throw the reset bit to reset the board.
out8(ctrl_handle, (1 << 7));
//enable Digital IO Interrupts (Ext Trig pin)...
out8(ctrl_handle, (0b00011111));
// set the data directions...
out8(daq_dir_handle, SET_DIRECTION);
//Start the pulse scanner...
PulseScanner pulseScanner(cmd_handle, daq_portb_handle);
//Start the pushbutton scanner...
PushbuttonScanner pushbuttonScanner(daq_portc_handle);
pushbuttonScanner.start();
//Start the CyclometerController
CyclometerController cyclometerController(&pulseScanner);
//Start the ResetWatchdog
ResetWatchdog resetWatchdog(&cyclometerController);
//Start the display thread.
Display display((DataProvider*)&cyclometerController, daq_porta_handle, daq_portb_handle);
//Display display(NULL, daq_porta_handle, daq_portb_handle);
//Start the display
display.start();
for (;;) {
// Loop infinitely because this is an embedded system.
}
// Clean up threads.
// NB: Program will never get here, just for show.
pulseScanner.stop();
pushbuttonScanner.stop();
cyclometerController.stop();
resetWatchdog.stop();
display.stop();
pulseScanner.join();
pushbuttonScanner.join();
cyclometerController.join();
resetWatchdog.join();
display.join();
return EXIT_SUCCESS;
}