-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSICC.cpp
169 lines (148 loc) · 4.64 KB
/
SICC.cpp
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
#include "SICC.h"
SICC::SICC(uint8_t SCL, uint8_t SDA, int delay/*=100*/){
DELAY_TIME = delay;
clock_line = SCL;
data_line = SDA;
}
bool SICC::send(char* s, unsigned long timeout/*=-1*/){
startTime = (unsigned long) micros();
isSending(true);
//Write 'start'
digitalWrite(clock_line,1);
digitalWrite(data_line,0);
delayMicroseconds(DELAY_TIME);
bool ack = writeByte(strlen(s));
//Iterate through each character
for (unsigned int i = 0; i < strlen(s) && ack; i++) {
char c = s[i];
ack = writeByte(c);
}
if(!ack) {
sendRestart();
}
//End sequence
digitalWrite(clock_line,0);
digitalWrite(data_line,1);
delayMicroseconds(DELAY_TIME);
//Release wires
digitalWrite(clock_line,1);
digitalWrite(data_line,1);
delayMicroseconds(DELAY_TIME);
if(!ack) {
delay(10);
if((unsigned long) micros()-startTime>timeout) return false;
return SICC::send(s, timeout-(unsigned long)micros()+startTime);
}
return true;
}
bool SICC::receive(char* recvBuf, unsigned long timeout/*=-1*/){
isSending(false);
//Wait for start condition
startTime = (unsigned long) micros();
while(digitalRead(data_line) || !digitalRead(clock_line)) {
if((unsigned long)micros()-startTime>timeout){
recvBuf[0] = 0;
return false;
}
}
while(digitalRead(clock_line)) {
if((unsigned long)micros()-startTime>timeout){
recvBuf[0] = 0;
return false;
}
}
//Receive size of data
byte numChar = readByte();
if(numChar == TIMEOUT_CODE) {
recvBuf[0] = 0;
return false;
}
bool corrupt = numChar == CORRUPTION_CODE;
//Receive bytes
for (int i = 0; i < numChar && !corrupt; i++) {
recvBuf[i] = char(readByte());
if((byte) recvBuf[i] == TIMEOUT_CODE) {
recvBuf[0] = 0;
return false;
}
corrupt = (byte) recvBuf[i] == CORRUPTION_CODE;
}
recvBuf[numChar] = 0;
if(corrupt) {
delay(5);
if((unsigned long)micros()-startTime>timeout){
recvBuf[0] = 0;
return false;
}
return SICC::receive(recvBuf, timeout-micros()+startTime);
}
return true;
}
bool SICC::writeByte(byte c){
for (int j = 0; j < 8; j++) {
byte b = bitRead(c, 7-j);
digitalWrite(clock_line, 0);
digitalWrite(data_line, b);
delayMicroseconds(DELAY_TIME);
digitalWrite(clock_line, 1);
delayMicroseconds(DELAY_TIME);
}
digitalWrite(clock_line,0);
pinMode(data_line,INPUT_PULLUP); //Release data line to let it float for ACK/NACK
delayMicroseconds(DELAY_TIME);
digitalWrite(clock_line,1);
bool ret = !digitalRead(data_line); //Read ACK
delayMicroseconds(DELAY_TIME);
pinMode(data_line,OUTPUT); //Return back to original state
digitalWrite(clock_line,0);
return ret;
}
byte SICC::readByte(){
int a = 0;
for (int i = 0; i < 8; i++) {
startTime = micros(); //Timer for timeouts
//Read bits when clock_line == 1
while (!digitalRead(clock_line)) if(micros()-startTime>BIT_TIMEOUT) return TIMEOUT_CODE;
bool origVal = digitalRead(data_line);
bool prevVal = origVal;
int numFlip = 0;
while (digitalRead(clock_line)){
if(micros()-startTime>BIT_TIMEOUT) return TIMEOUT_CODE;
if(digitalRead(data_line) != prevVal) {
prevVal = !prevVal;
numFlip++;
}
}
if(numFlip > 3) return CORRUPTION_CODE;
a = (a<<1) | origVal;
}
startTime = micros();
pinMode(data_line,OUTPUT); //Control data line to send ACK
digitalWrite(data_line,0); //Send ACK
while(!digitalRead(clock_line))if(micros()-startTime>BIT_TIMEOUT) return TIMEOUT_CODE;
while (digitalRead(clock_line))if(micros()-startTime>BIT_TIMEOUT) return TIMEOUT_CODE;
pinMode(data_line,INPUT_PULLUP); //Release data line to let it float for next byte of data
return a;
}
void SICC::sendRestart(){
pinMode(12,OUTPUT);
digitalWrite(12,HIGH);
bool state = false;
digitalWrite(clock_line, 0);
digitalWrite(data_line, state);
delayMicroseconds(DELAY_TIME);
digitalWrite(clock_line, 1);
for(int i=0;i<10;i++){
digitalWrite(data_line, state=!state);
delayMicroseconds(DELAY_TIME/10);
}
}
void SICC::isSending(bool state){
if(state){
pinMode(clock_line, OUTPUT);
pinMode(data_line, OUTPUT);
return;
}
pinMode(clock_line, INPUT_PULLUP);
pinMode(data_line, INPUT_PULLUP);
}