This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OneWireMaster.h
189 lines (163 loc) · 6.56 KB
/
OneWireMaster.h
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
/**
* @file OneWireMaster.h
*
* OneWireMaster class prototype. Designed to act as a master controller
* of a OneWire bus off of a Stellaris Launchpad.
*
* Based off of Petras Saduikis' mbed port of Jim Studt's
* Arduino OneWire library. Some pieces of this code have been taken from
* Dallas Semiconductor's sample code, bearing the copyright below.
* Additionally, though I believe very little of Petras' code remains, I have
* included his copyright block just in case.
*
* Copyright (C) <2012> Cliff Chapman <chapman.cliff@gmail.com>
*
* This file is part of the Stellaris OneWire Library.
*
* The Stellaris OneWire Library is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* The Stellaris OneWire Library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Stellaris OneWire Library.
* If not, see <http://www.gnu.org/licenses/>.
*/
/*
* OneWireCRC. This is a port to mbed of Jim Studt's Adruino One Wire
* library. Please see additional copyrights below this one, including
* references to other copyrights.
*
* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
*
* This file is part of OneWireCRC.
*
* OneWireCRC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OneWireCRC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OneWireCRC. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Dallas Semiconductor
// shall not be used except as stated in the Dallas Semiconductor
// Branding Policy.
//--------------------------------------------------------------------------
#ifndef STELLARIS_ONEWIRE_LIBRARY_CHAPMAN_H
#define STELLARIS_ONEWIRE_LIBRARY_CHAPMAN_H
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "utils/ustdlib.h"
#include "stellaris-pins/DigitalIOPin.h"
#include <vector>
// Maximum number of allowed devices during a search. Default is 50. This is to
// limit the occurance of memory overflow attacks via the bus.
//
// Actually, it's more to limit the Stellaris from freaking out when it
// encouters a poorly wired OneWire bus, a bus held low will result in a stream
// of 0s pumped to the device table. Keep it something sensible to avoid
// blowing the stack
#define OW_MAX_NUM_DEVICES 50
// The Dallas Semiconductor example code for OneWire CRC checking provides two
// methods for computing the CRC of a line of data. This define allows you to
// select which one you would prefer for your code base. Method 0 uses math to
// compute the value, leading to slightly smaller code size but slightly longer
// computation time. Method 1 uses a lookup table, with slightly larger code
// size but smaller computation time. Set the method you'd like to use in this
// define here and the other will be omitted.
#ifndef ONEWIRE_CRC8_CALCULATION_METHOD
#define ONEWIRE_CRC8_CALCULATION_METHOD 1
#endif // ONEWIRE_CRC8_CALCULATION_METHOD
// OneWire bus speed settings
#define OW_SPEED_OVERDRIVE 0
#define OW_SPEED_STANDARD 1
// Standard One Wire command codes
// Used on most OneWire Devices, read the datasheet for more information
#define OW_SEARCH_ROM 0xF0
#define OW_READ_ROM 0x33
#define OW_MATCH_ROM 0x55
#define OW_ALARM_SEARCH 0xEC
#define OW_SKIP_ROM 0xCC
#define OW_OVERDRIVE_SKIP 0x3C
namespace OneWire
{
typedef unsigned char BYTE;
/**
* OneWire Master generic operations
*/
class OneWireMaster
{
public:
OneWireMaster(unsigned int busSpeed);
OneWireMaster
( unsigned int busSpeed
, unsigned long gpioPeriph
, unsigned long gpioPort
, unsigned char gpioPinmask
);
// Standard bus functions
int Reset(void);
BYTE ReadByte(void);
void WriteByte(BYTE data);
int TouchByte(BYTE data);
void Block(BYTE* data, int data_len);
// Wait timer
void WaitUS(unsigned int us);
// Address search/select functions
int Search(void);
void MatchROM(std::vector<BYTE> rom);
void SkipROM(void);
int SkipOverdrive(void);
// CRC check functions
static BYTE CRC8(BYTE* address, BYTE length);
static unsigned short CRC16(unsigned short* data, unsigned short length);
// Container for device addresses
std::vector<std::vector<BYTE> > devices;
private:
// Timing values array, populated based on the bus speed setting
const int* timing;
// GPIO port
DigitalIOPin GPIOPin;
// read/write single bits to the bus
void WriteBit(BYTE bit);
BYTE ReadBit(void);
// Timing functions/constants
static const int standardTime[10];
static const int overdriveTime[10];
};
}
#endif // STELLARIS_ONEWIRE_LIBRARY_CHAPMAN_H