-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneWire.cpp
More file actions
248 lines (199 loc) · 5.39 KB
/
OneWire.cpp
File metadata and controls
248 lines (199 loc) · 5.39 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* OneWire.cpp
*
* Created on: 10.04.2020
* Author: Daniel
*/
#include "OneWire.h"
namespace SD_Utils {
OneWire_ErrorState OneWire::ReadScratchpad(uint8_t *scratch) {
this->Init();
this->WriteByte(0xcc);
this->WriteByte(0xbe);
for(uint8_t idx = 0; idx < 9; idx++)
this->ReadByte(&scratch[idx]);
return NoError;
}
void OneWire::WriteBit(bool bit) {
// set data-port as out
this->InitDataPortAsWrite();
// data-port = low
HAL_GPIO_WritePin(this->DataPort, this->DataPin, GPIO_PIN_RESET);
if (bit)
{
// wait 15 us
DS_Delay_us(8);
// release data-port (set as inport)
this->InitDataPortAsRead();
// wait 45 us
DS_Delay_us(48);
} else
{
// wait 60 us
DS_Delay_us(60);
// release data-port (set as inport)
this->InitDataPortAsRead();
}
}
OneWire_ErrorState OneWire::WriteByte(uint8_t value) {
for (size_t i = 0; i < 8; i++){
uint8_t shifted = (value >> i);
uint8_t bit = (shifted & 0b1);
this->WriteBit(bit);
}
return NoError;
}
bool OneWire::ReadBit() {
bool data = 0;
// set data-port as outport
this->InitDataPortAsWrite();
// port low
HAL_GPIO_WritePin(this->DataPort, this->DataPin, GPIO_PIN_RESET);
// wait min. 1us
DS_Delay_us(1);
// release data-port (set as inport)
this->InitDataPortAsRead();
DS_Delay_us(5);
// data = read port
data = HAL_GPIO_ReadPin(this->DataPort, this->DataPin);
// wait 59us
DS_Delay_us(54);
return data;
}
OneWire_ErrorState OneWire::ReadByte(uint8_t *buf) {
for(uint8_t idx = 0; idx < 8; idx++){
int bit = this->ReadBit();
*buf = *buf >> 1;
*buf |= bit << 7;
}
return NoError;
}
void OneWire::InitDataPortAsRead() {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = this->DataPin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = this->ExternalPullUp ? GPIO_NOPULL : GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(this->DataPort, &GPIO_InitStruct);
}
void OneWire::InitDataPortAsWrite() {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = this->DataPin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
// GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = this->ExternalPullUp ? GPIO_NOPULL : GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(this->DataPort, &GPIO_InitStruct);
}
void OneWire::Init() {
this->InitDataPortAsWrite();
HAL_GPIO_WritePin(this->DataPort, this->DataPin, GPIO_PIN_SET);
DS_Delay_us(10);
// set data-port low
HAL_GPIO_WritePin(this->DataPort, this->DataPin, GPIO_PIN_RESET);
// wait 480 us
DS_Delay_us(480);
// release data-port (data-port DDR as input)
this->InitDataPortAsRead();
this->IsPresent = HAL_GPIO_ReadPin(this->DataPort, this->DataPin);
this->SetErrorState(this->IsPresent ? NoError : NoSlavePresence);
if(!this->IsPresent) {
return;
}
// wait 60 us
DS_Delay_us(60);
// read pin ? 0 return 0 : return 1
this->IsPresent = HAL_GPIO_ReadPin(this->DataPort, this->DataPin) == GPIO_PIN_RESET;
this->SetErrorState(this->IsPresent ? NoError : NoSlavePresence);
if(!this->IsPresent) {
return;
}
DS_Delay_us(360);
}
/* IO's */
/* Presence */
void OneWire::InitPresencePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
this->PresencePort = GPIOx;
this->PresencePin = GPIO_Pin;
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = this->PresencePin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(this->PresencePort, &GPIO_InitStruct);
this->UsingPresencePin = true;
}
void OneWire::SetPresencePin() {
if (!this->UsingPresencePin)
return;
HAL_GPIO_WritePin(this->PresencePort, this->PresencePin, (GPIO_PinState)this->IsPresent);
}
void OneWire::SetPresencePin(bool on) {
this->IsPresent = on;
this->SetPresencePin();
}
/* Error State Flow */
void OneWire::SetErrorState(OneWire_ErrorState state) {
if(this->ErrorState == state)
return;
this->ErrorState = state;
switch ( this->ErrorState ) {
case NoError:
this->SetErrorPin(false);
this->SetPresencePin(true);
break;
case NoSlavePresence:
this->SetErrorPin(true);
this->SetPresencePin(false);
break;
case TimeOut:
this->SetErrorPin(true);
this->SetPresencePin(true);
break;
default:
break;
}
}
/* Error */
void OneWire::InitErrorPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
this->ErrorPort = GPIOx;
this->ErrorPin = GPIO_Pin;
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = this->ErrorPin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(this->ErrorPort, &GPIO_InitStruct);
this->UsingErrorPin = true;
}
void OneWire::SetErrorPin() {
if (!this->UsingErrorPin)
return;
HAL_GPIO_WritePin(this->ErrorPort, this->ErrorPin, (GPIO_PinState)this->IsError);
}
void OneWire::SetErrorPin(bool on) {
this->IsError = on;
this->SetErrorPin();
}
void OneWire::InitIO(){
// set data-port as output
this->InitDataPortAsWrite();
// set data-port to low
HAL_GPIO_WritePin(this->DataPort, this->DataPin, GPIO_PIN_RESET);
}
OneWire::OneWire(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
DS_Delay_init();
this->DataPin = GPIO_Pin;
this->DataPort = GPIOx;
this->InitIO();
}
OneWire::OneWire() {
DS_Delay_init();
this->DataPin = ONEWIRE_DATA_Pin;
this->DataPort = ONEWIRE_DATA_GPIO_Port;
this->InitIO();
}
OneWire::~OneWire() {
// TODO deinit all pins
}
} /* namespace SD_Utils */