-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoImpl.h
163 lines (132 loc) · 2.7 KB
/
ArduinoImpl.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
#ifndef __ARDUINO_IMPL_H__
#define __ARDUINO_IMPL_H__
#include <Arduino.h>
struct SerialPort
{
bool begin(int baudrate)
{
Serial.begin(baudrate);
return true;
}
int available()
{
return Serial.available();
}
int read(uint8_t* buffer, uint_t count)
{
return Serial.readBytes(buffer, count);
}
int read()
{
return Serial.read();
}
int write(const uint8_t* data, uint_t count)
{
return Serial.write(data, count);
}
int write(uint8_t data)
{
return Serial.write(data);
}
int write(const char* str)
{
return Serial.write(str);
}
void discard()
{
uint8_t buf[256];
for (;;)
{
int n = available();
if (n <= 0)
break;
if (n > sizeof(buf))
{
n = sizeof(buf);
}
read(buf, n);
}
}
};
struct Sys
{
void delay(int ms)
{
::delay(ms);
}
void printf(const char* fmt, ...)
{
}
uint32_t millis()
{
return ::millis();
}
};
template <int resetPin>
struct Avr
{
void begin()
{
pinMode(resetPin, OUTPUT);
SPI.begin();
SPI.setFrequency(300000);
SPI.setHwCs(false);
}
bool setDevice(const uint8_t* params)
{
if (params[0] != 0x14) //ATTINY85
{
return false;
}
if (params[12] != 0) // page size high
{
return false;
}
if (params[13] != 0x40) // page size low
{
return false;
}
return true;
}
bool setDeviceExt(const uint8_t* params)
{
return true;
}
bool enter()
{
digitalWrite(resetPin, LOW);
SPI.transfer((uint8_t)0x00);
for (int i = 0; i < 10; i ++)
{
digitalWrite(resetPin, HIGH);
delayMicroseconds(50);
digitalWrite(resetPin, LOW);
delay(30);
SPI.transfer(0xAC);
SPI.transfer(0x53);
uint8_t r = SPI.transfer(0x00);
SPI.transfer(0x00);
if (r == 0x53)
{
return true;
}
}
return false;
}
void leave()
{
digitalWrite(resetPin, HIGH);
}
uint8_t xfer(uint8_t b1, uint16_t b2b3 = 0, uint8_t b4 = 0)
{
return xfer(b1, b2b3 >> 8, b2b3 & 0xFF, b4);
}
uint8_t xfer(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
{
SPI.transfer(b1);
SPI.transfer(b2);
SPI.transfer(b3);
return SPI.transfer(b4);
}
};
#endif