/
TravelBoxReadUniqueId.c
82 lines (67 loc) · 2.35 KB
/
TravelBoxReadUniqueId.c
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
/*
* BJ Devices Travel Box series midi controller library
* @file TravelBoxReadUniqueId.c
*
* @brief This example explain how to read unique device ID and compare it with user provided ID to implement commercial firmware
*
* Software is provided "as is" without express or implied warranty.
* BJ Devices 2016
*/
#include "unique_id.h"
#include "lcd_tb.h"
#include <util/delay.h>
#include <stdio.h>
#include <string.h>
uint64_t swapByteOrder(uint64_t value)
{
uint64_t uvalue = (uint64_t)value;
uint64_t swapped = ( ((0x00000000000000FF) & (uvalue >> 56))
| ((0x000000000000FF00) & (uvalue >> 40))
| ((0x0000000000FF0000) & (uvalue >> 24))
| ((0x00000000FF000000) & (uvalue >> 8))
| ((0x000000FF00000000) & (uvalue << 8))
| ((0x0000FF0000000000) & (uvalue << 24))
| ((0x00FF000000000000) & (uvalue << 40))
| ((0xFF00000000000000) & (uvalue << 56)));
return (uint64_t)swapped;
}
//this can be defined in project settings, makefile or provided through -D compiler option in command line
#define GLOBALY_DEFINED_USER_ID 0x0102030405060708
#define ID_BYTES 8//DS2411 ID contain 8 bytes. 7 bytes data + 1 byte CRC
uint8_t idBuffer[ID_BYTES];//Buffer for data from DS2411
char idStr[ID_BYTES*2+1];//Buffer for string representation of ID
int main(void)
{
uint8_t i;
//LCD initialization
LCDInit(LS_ULINE);
//1-wire interface initialization
initUniqId();
//small delay... just in case :)
_delay_ms(10);
//read unique ID from DS2411. If successfully - print in on screen
if(uniqIdGet(idBuffer))
{
for(i = 0; i < ID_BYTES; ++i)
sprintf(idStr+(i*2), "%02x", idBuffer[i]);
LCDWriteString(idStr);
}
//If error during read - print error message
//Read error may occur when DS2411 is not available. Gen1 devices not have DS2411
else
{
LCDWriteString("ID not available");
return 0;//If you don't want your firmware works on gen 1 devices, just halt execution here
}
uint64_t userId = GLOBALY_DEFINED_USER_ID;
//compare current ID with user-provided ID
//This example use memcmp for comparison. So I should swap bytes in userId variable for correct comparison
userId = swapByteOrder(userId);
if (0 != memcmp((void*)idBuffer, (void*)&userId, ID_BYTES))
{
LCDWriteStringXY(0,1, "ID not matched!");
return 0;//break execution
}
LCDWriteStringXY(0,1, "ID matched!");//ID matched, start firmware
//Start to implement your firmware behavior here
}