Newer
Older
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2015 Bitcraze AB
*
* This program 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, in version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* deck_ow.c - Functions to decode the decks oneWire memory content
*/
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define DEBUG_MODULE "DECK_INFO"
#include "deck.h"
#include "ow.h"
#include "crc.h"
#include "debug.h"
#ifdef DEBUG
#define DECK_INFO_DBG_PRINT(fmt, ...) DEBUG_PRINT(fmt, ## __VA_ARGS__)
#else
#define DECK_INFO_DBG_PRINT(...)
#endif
static int count = 0;
static DeckInfo deckInfos[DECK_MAX_COUNT];
static void enumerateDecks(void);
void deckInfoInit()
{
static bool isInit = false;
if (isInit) return;
enumerateDecks();
isInit = true;
}
int deckCount(void)
{
return count;
}
DeckInfo * deckInfo(int i)
{
if (i<count) {
return &deckInfos[i];
}
return NULL;
}
// Dummy driver for decks that do not have a driver implemented
static const DeckDriver dummyDriver;
static const DeckDriver * findDriver(DeckInfo *deck)
{
char name[30];
const DeckDriver *driver = &dummyDriver;
deckTlvGetString(&deck->tlv, DECK_INFO_NAME, name, 30);
if (deck->vid) {
driver = deckFindDriverByVidPid(deck->vid, deck->pid);
} else if (strlen(name)>0) {
driver = deckFindDriverByName(name);
}
if (driver == NULL)
driver = &dummyDriver;
return driver;
}
void printDeckInfo(DeckInfo *info)
{
char name[30] = "NoName";
char rev[10] = "NoRev";
if (deckTlvHasElement(&info->tlv, DECK_INFO_NAME)) {
deckTlvGetString(&info->tlv, DECK_INFO_NAME, name, 30);
}
if (deckTlvHasElement(&info->tlv, DECK_INFO_REVISION)) {
deckTlvGetString(&info->tlv, DECK_INFO_REVISION, rev, 10);
}
DECK_INFO_DBG_PRINT("Deck %02x:%02x %s (Rev. %s)\n", info->vid, info->pid, name, rev);
DECK_INFO_DBG_PRINT("Used pin: %08x\n", (unsigned int)info->usedPins);
if (info->driver == &dummyDriver) {
DEBUG_PRINT("Warning! No driver found for deck.\n");
} else {
DECK_INFO_DBG_PRINT("Driver implements: [ %s%s]\n",
info->driver->init?"init ":"", info->driver->test?"test ":"");
}
}
static bool infoDecode(DeckInfo * info)
{
uint8_t crcHeader;
uint8_t crcTlv;
if (info->header != DECK_INFO_HEADER_ID) {
DEBUG_PRINT("Memory error: wrong header ID\n");
return false;
}
crcHeader = crcSlow(info->raw, DECK_INFO_HEADER_SIZE);
if(info->crc != crcHeader) {
DEBUG_PRINT("Memory error: incorrect header CRC\n");
return false;
}
if(info->raw[DECK_INFO_TLV_VERSION_POS] != DECK_INFO_TLV_VERSION) {
DEBUG_PRINT("Memory error: incorrect TLV version\n");
return false;
}
crcTlv = crcSlow(&info->raw[DECK_INFO_TLV_VERSION_POS], info->raw[DECK_INFO_TLV_LENGTH_POS]+2);
if(crcTlv != info->raw[DECK_INFO_TLV_DATA_POS + info->raw[DECK_INFO_TLV_LENGTH_POS]]) {
DEBUG_PRINT("Memory error: incorrect TLV CRC %x!=%x\n", (unsigned int)crcTlv,
info->raw[DECK_INFO_TLV_DATA_POS + info->raw[DECK_INFO_TLV_LENGTH_POS]]);
return false;
}
info->tlv.data = &info->raw[DECK_INFO_TLV_DATA_POS];
info->tlv.length = info->raw[DECK_INFO_TLV_LENGTH_POS];
return true;
}
static void enumerateDecks(void)
{
uint8_t nDecks = 0;
int i;
bool noError = true;
uint32_t usedPeriph = 0;
uint32_t usedGpio = 0;
owInit();
if (owScan(&nDecks))
{
DEBUG_PRINT("Found %d deck memor%s.\n", nDecks, nDecks>1?"ies":"y");
} else {
DEBUG_PRINT("Error scanning for deck memories, "
"no deck drivers will be initialised\n");
nDecks = 0;
}
for (i = 0; i < nDecks; i++)
{
DECK_INFO_DBG_PRINT("Enumerating deck %i\n", i);
if (owRead(i, 0, sizeof(deckInfos[0].raw), (uint8_t *)&deckInfos[i]))
{
if (infoDecode(&deckInfos[i]))
{
deckInfos[i].driver = findDriver(&deckInfos[i]);
printDeckInfo(&deckInfos[i]);
// Check for Periph and Gpio conflict
if (usedPeriph & deckInfos[i].driver->usedPeriph) {
DEBUG_PRINT("ERROR: Driver Periph usage conflicts with a "
"previously enumerated deck driver. No decks will be "
"initialized!\n");
noError = false;
}
if (usedGpio & deckInfos[i].driver->usedGpio) {
DEBUG_PRINT("ERROR: Driver Gpio usage conflicts with a "
"previously enumerated deck driver. No decks will be "
"initialized!\n");
noError = false;
}
usedPeriph |= deckInfos[i].driver->usedPeriph;
usedGpio |= deckInfos[i].driver->usedGpio;
} else {
#ifdef DEBUG
DEBUG_PRINT("Deck %i has corrupted OW memory. "
"Ignoring the deck in DEBUG mode.\n", i);
deckInfos[i].driver = &dummyDriver;
#else
DEBUG_PRINT("Deck %i has corrupted OW memory. "
"No driver will be initialized!\n", i);
noError = false;
#endif
}
}
else
{
DEBUG_PRINT("Reading deck nr:%d [FAILED]. "
"No driver will be initialized!\n", i);
noError = false;
}
}
if (noError) {
count = nDecks;
}
return;
}
/****** Key/value area handling ********/
static int findType(TlvArea *tlv, int type) {
int pos = 0;
while (pos < tlv->length) {
if (tlv->data[pos] == type) {
return pos;
} else {
pos += tlv->data[pos+1]+2;
}
}
return -1;
}
bool deckTlvHasElement(TlvArea *tlv, int type) {
return findType(tlv, type) >= 0;
}
int deckTlvGetString(TlvArea *tlv, int type, char *string, int length) {
int pos = findType(tlv, type);
int strlength = 0;
if (pos >= 0) {
strlength = tlv->data[pos+1];
if (strlength > (length-1)) {
strlength = length-1;
}
memcpy(string, &tlv->data[pos+2], strlength);
string[strlength] = '\0';
return strlength;
} else {
string[0] = '\0';
return -1;
}
}
char* deckTlvGetBuffer(TlvArea *tlv, int type, int *length) {
int pos = findType(tlv, type);
if (pos >= 0) {
*length = tlv->data[pos+1];
return (char*) &tlv->data[pos+2];
}
return NULL;
}
void deckTlvGetTlv(TlvArea *tlv, int type, TlvArea *output) {
output->length = 0;
output->data = (uint8_t *)deckTlvGetBuffer(tlv, type, &output->length);
}