-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardwareController.c
More file actions
194 lines (157 loc) · 3.63 KB
/
Copy pathhardwareController.c
File metadata and controls
194 lines (157 loc) · 3.63 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "general.h"
#include "hardwareController.h"
#include "temperature.h"
#include "hal.h"
#include "errorHandling.h"
#include "crc.h"
#define MAX_NUMBER_ROMS 10
static ROM romList[MAX_NUMBER_ROMS];
static int numberOfROMs;
static void jumpToBranch(int index, uint64_t romBits);
static void detectSensorsRecursively(int index, uint64_t romBits);
static void initRomList(void);
static int getNumberOfRoms(void);
static int addRomToList(ROM rom);
static ROM getRomFromList(int index);
/**
* Initialisierung
**/
void initHardwareController() {
initHAL();
reset();
}
int resetHardware() {
return reset();
}
/**
* Führt den Selct slave cmd aus, und selektiert den Sensor mit dem angegebenen ROM
**/
int selectSlave(ROM slaveRom) {
// reset
int err = reset();
if (err) {
return err;
}
// match rom senden
writeByte(MATCH_ROM);
BYTE * P_RomBytes = (BYTE *) &slaveRom;
writeBytes(sizeof(slaveRom), P_RomBytes);
return 0;
}
/**
* Messung durchfuehren
**/
int measure() {
// send convert command senden
writeByte(SEND_CONVERT);
// push pull mode setzen
setPinMode(PUSH_PULL, DATA_PIN);
setPin(1, DATA_PIN);
wait(750000);
// open drain mode setzen
setPinMode(OPEN_DRAIN, DATA_PIN);
setPin(1, DATA_PIN);
return 0;
}
/**
* Ausführung des skip roms
**/
int skipRom() {
int e = reset();
if (e != EOK) {
return e;
}
writeByte(SKIP_ROM);
return EOK;
}
/**
* Auslesen des Scratch Pads
**/
int readScratch(ScratchPad *SpadOut) {
//Send Read CMD
writeByte(READ_SCRATCH);
// Direktes Lesen in Scratchpad Pointer
readByte(9, (BYTE *) SpadOut);
if(!isValidCRC(sizeof(*SpadOut), (BYTE*) SpadOut)){
return INVALIDCRC;
}else{
return 0;
}
}
int detectSensors(int size, ROM romList[size], int *numberOfElements) {
initRomList();
resetHardware();
writeByte(SEARCH_ROM);
int index = 0;
uint64_t romBits = 0;
detectSensorsRecursively(index, romBits);
for(int i = 0; i < getNumberOfRoms() && i < size;i++){
romList[i] = getRomFromList(i);
}
*numberOfElements = getNumberOfRoms();
return 0;
}
static void jumpToBranch(int index, uint64_t romBits){
resetHardware();
writeByte(SEARCH_ROM);
readBit();
readBit();
for(int i = 0; i < index; i++){
writeBit((romBits & (1 << i))>>i);
readBit();
readBit();
}
}
static void detectSensorsRecursively(int index, uint64_t romBits) {
// Abbruchbedingung
if(index >= 64){
addRomToList(*((ROM *)(&romBits)));
return;
}
int bit, bitKomplement;
bit = readBit();
bitKomplement = readBit();
if (bit == !bitKomplement) {
writeBit(bit); // Bestätigung des Bits
romBits |= ((uint64_t)bit << index);
detectSensorsRecursively(index + 1, romBits);
}
else if ((bit == 0) && (bitKomplement == 0)) { // Uneinheitliches Bit der Sensoren
writeBit(0); // Auswaehlen aller Sensoren, die 0 gesendet haben
romBits |= (0 << index);
detectSensorsRecursively(index + 1, romBits);
jumpToBranch(index, romBits); // Zurueckspringen zum Entscheidungspunkt
writeBit(1); // Auswaehlen aller Sensoren, die 1 gesendet haben
romBits |= (1 << index);
detectSensorsRecursively(index + 1, romBits);
}
else if ((bit == 1) && (bitKomplement == 1)) { // Fehlerfall
return;
}
}
static void initRomList() {
numberOfROMs = 0;
for (int i = 0; i < MAX_NUMBER_ROMS; i++) {
romList[i] = createROM();
}
}
static int getNumberOfRoms() {
return numberOfROMs;
}
static int addRomToList(ROM rom) {
if (numberOfROMs > MAX_NUMBER_ROMS) {
return -1;
}
romList[numberOfROMs] = rom;
numberOfROMs++;
return EOK;
}
static ROM getRomFromList(int index) {
if (index >= numberOfROMs) {
return createROM();
}
return romList[index];
}