-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathMuxTO.ino
228 lines (204 loc) · 5.87 KB
/
MuxTO.ino
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
/*
j2updi.cpp
Created: 11-11-2017 22:29:58
Author : JMR_2
Ported to generic Arduino framework by the Arduino Team - 2019
*/
/*
Compile with
fqbn=arduino:samd:muxto:float=default,config=enabled,clock=internal_usb,timer=timer_732Hz,bootloader=4kb,serial=two_uart,usb=cdc
to obtain the binary for Serial-to-USB converter + UPDI programmer on the Arduino Nano Every (MuxTO for brevity)
Since the "sketch" is (almost) pure Arduino it can be ported easily to other architectures (on chips with two serial ports)
The bitbanged UPDI layer is being reworked to make it compatible with chips with just one UART (eg. atmega 32u4)
*/
// Includes
#include "sys.h"
#include "lock.h"
#include "updi_io.h"
#include "JICE_io.h"
#include "JTAG2.h"
#include "UPDI_hi_lvl.h"
volatile bool updi_mode = false;
unsigned long baudrate = 115200;
unsigned long updi_mode_start = 0;
unsigned long updi_mode_end = 0;
uint8_t stopbits = 1;
uint8_t paritytype = 0;
uint8_t numbits = 8;
int8_t dtr = -1;
int8_t rts = -1;
uint16_t serial_mode = SERIAL_8N1;
bool serialNeedReconfiguration = false;
char support_buffer[64];
struct lock q;
void setup() {
/* Initialize MCU */
pinMode(LED_BUILTIN, OUTPUT);
/* Initialize serial links */
JICE_io::init();
UPDI_io::init();
Serial1.begin(baudrate, serial_mode);
lock_init(&q);
JTAG2::sign_on();
}
//#define DEBUG;
//long blink_timer = 0;
//long blink_delay = 1000;
void loop() {
#ifdef DEBUG
if (millis() - blink_timer > blink_delay) {
SYS::toggleLED();
blink_timer = millis();
}
#endif
if (updi_mode == false) {
//blink_delay = 1000;
if (int c = Serial1.available()) {
lock(&q);
if (c > Serial.availableForWrite()) {
c = Serial.availableForWrite();
}
unlock(&q);
Serial1.readBytes(support_buffer, c);
Serial.write(support_buffer, c);
}
if (int c = Serial.available()) {
lock(&q);
if (c > Serial1.availableForWrite()) {
c = Serial1.availableForWrite();
}
unlock(&q);
Serial.readBytes(support_buffer, c);
Serial1.write(support_buffer, c);
}
if (Serial.stopbits() != stopbits) {
serial_mode &= ~HARDSER_STOP_BIT_MASK;
stopbits = Serial.stopbits();
serialNeedReconfiguration = true;
switch (stopbits) {
case 1:
serial_mode |= HARDSER_STOP_BIT_1;
break;
case 2:
serial_mode |= HARDSER_STOP_BIT_2;
break;
}
}
if (Serial.paritytype() != paritytype) {
serial_mode &= ~HARDSER_PARITY_MASK;
paritytype = Serial.paritytype();
serialNeedReconfiguration = true;
switch (paritytype) {
case 0:
serial_mode |= HARDSER_PARITY_NONE;
break;
case 1:
serial_mode |= HARDSER_PARITY_EVEN;
break;
case 2:
serial_mode |= HARDSER_PARITY_ODD;
break;
}
}
if (Serial.numbits() != numbits) {
serial_mode &= ~HARDSER_DATA_MASK;
numbits = Serial.numbits();
serialNeedReconfiguration = true;
switch (numbits) {
case 5:
serial_mode |= HARDSER_DATA_5;
break;
case 6:
serial_mode |= HARDSER_DATA_6;
break;
case 7:
serial_mode |= HARDSER_DATA_7;
break;
case 8:
serial_mode |= HARDSER_DATA_8;
break;
}
}
if (baudrate == 1200 && Serial.dtr() == 0 && (millis() - updi_mode_end > 200)) {
// don't reenter here if you just finished flashing
updi_mode = true;
updi_mode_start = millis();
updi_mode_end = 0;
}
if (Serial.baud() != baudrate || serialNeedReconfiguration || Serial.dtr() != dtr) {
dtr = Serial.dtr();
if (Serial.dtr() == 1) {
baudrate = Serial.baud();
Serial1.end();
if (baudrate != 1200) {
Serial1.begin(baudrate, serial_mode);
serialNeedReconfiguration = false;
// Request reset
UPDI::stcs(UPDI::reg::ASI_Reset_Request, UPDI::RESET_ON);
// Release reset (System remains in reset state until released)
UPDI::stcs(UPDI::reg::ASI_Reset_Request, UPDI::RESET_OFF);
}
}
}
return;
}
if (updi_mode == true) {
// updi_mode cannot last more than 1 minute; in that case, break forcibly
if ((updi_mode_end != 0 && (millis() - updi_mode_end) > 500) || ((millis() - updi_mode_start) > 60000)) {
updi_mode = false;
baudrate = -1;
return;
}
//blink_delay = 100;
// Receive command
if (!JTAG2::receive()) {
return;
}
// Process command
switch (JTAG2::packet.body[0]) {
case JTAG2::CMND_GET_SIGN_ON:
JTAG2::sign_on();
break;
case JTAG2::CMND_GET_PARAMETER:
JTAG2::get_parameter();
break;
case JTAG2::CMND_SET_PARAMETER:
JTAG2::set_parameter();
break;
case JTAG2::CMND_RESET:
case JTAG2::CMND_ENTER_PROGMODE:
JTAG2::enter_progmode();
break;
case JTAG2::CMND_SIGN_OFF:
// Restore default baud rate before exiting
JTAG2::PARAM_BAUD_RATE_VAL = JTAG2::baud_19200;
case JTAG2::CMND_LEAVE_PROGMODE:
JTAG2::leave_progmode();
updi_mode_end = millis();
break;
case JTAG2::CMND_GET_SYNC:
case JTAG2::CMND_GO:
JTAG2::set_status(JTAG2::RSP_OK);
break;
case JTAG2::CMND_SET_DEVICE_DESCRIPTOR:
JTAG2::set_device_descriptor();
break;
case JTAG2::CMND_READ_MEMORY:
JTAG2::read_mem();
break;
case JTAG2::CMND_WRITE_MEMORY:
JTAG2::write_mem();
break;
case JTAG2::CMND_XMEGA_ERASE:
JTAG2::erase();
break;
default:
JTAG2::set_status(JTAG2::RSP_FAILED);
break;
}
// send response
JTAG2::answer();
// some commands need to be executed after sending the answer
JTAG2::delay_exec();
}
}