forked from ElTangas/jtag2updi
-
Notifications
You must be signed in to change notification settings - Fork 16
/
jtag2updi.cpp
198 lines (188 loc) · 6.13 KB
/
jtag2updi.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
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
/*
* j2updi.cpp
*
* Created: 11-11-2017 22:29:58
* Author : JMR_2
*/
// Includes
#include <util/delay.h>
#include "sys.h"
#include "updi_io.h"
#include "JICE_io.h"
#include "JTAG2.h"
#include "UPDI_lo_lvl.h"
#include "dbg.h"
#ifdef USE_WDT_RESET
#include <avr/wdt.h>
#endif
/* Internal stuff */
namespace {
// Prototypes
void setup();
void loop();
void process_command();
}
int main(void)
{
setup();
loop();
}
/* Internal stuff */
namespace {
inline void setup() {
/* Initialize MCU */
//wdt_disable();
SYS::init();
/* Initialize serial links */
JICE_io::init();
UPDI_io::init();
}
inline void loop() {
#if (defined(USE_HV_PROGRAMMING) && (defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny_Zero_One__) || defined(__AVR_ATmega_Zero__) || defined(__AVR_DA__)))
SYS::setPOWER();
if (SYS::checkHVMODE() > 100) { // if HV or PCHV mode, then apply HV pulse and UPDI enable sequence
SYS::pulseHV();
SYS::updiEnable();
}
SYS::checkOVERLOAD();
#endif
#ifndef DISABLE_HOST_TIMEOUT
uint8_t HostErrorCount=0;
#endif
//wdt_enable(WDTO_500MS);
while (1) {
// Receive command
#ifndef DISABLE_HOST_TIMEOUT
while(!(JTAG2::receive()||(SYS::checkTimeouts() & WAIT_FOR_HOST)));
if (!(SYS::checkTimeouts() & WAIT_FOR_HOST)) {
HostErrorCount=0;
SYS::clearTimeouts(); //clear the timeouts, because WAIT_FOR_TARGET may be set here because of how this is implemented...
#else
while(!(JTAG2::receive()));
#endif
// Process command
#if defined(DEBUG_ON)
DBG::debug('c',JTAG2::packet.body[0]);
DBG::debug('C',JTAG2::ConnectedTo);
#endif
process_command();
// send response
JTAG2::answer();
// some commands need to be executed after sending the answer
JTAG2::delay_exec();
#ifndef DISABLE_HOST_TIMEOUT
} else { // timed out waiting for host communication
// We timed out waiting for the host to send something
// if we thought we were talking with host, that would be a sign that something went wrong
if (JTAG2::ConnectedTo&0x02) {
#if defined(DEBUG_ON)
DBG::debug('t',SYS::checkTimeouts());
DBG::debug('h',HostErrorCount);
#endif
if (HostErrorCount++>3) {
// Time to give up on host, restore default baud rate, and wait for future contact
#if defined(DEBUG_ON)
DBG::debug("Giving up...");
#endif
JTAG2::PARAM_BAUD_RATE_VAL = JTAG2::baud_19200;
JICE_io::set_baud(JTAG2::baud_19200);
JTAG2::ConnectedTo&=0xFD; // no longer talking to host.
if (JTAG2::ConnectedTo&0x01) {
//if this is true, we're still talking to the target! We're better tell it we're done...
JTAG2::leave_progmode();
JTAG2::go();
}
} else { // send a fail and give them a chance to start talking again
JTAG2::set_status(JTAG2::RSP_FAILED);
// send response
JTAG2::answer();
// some commands need to be executed after sending the answer
JTAG2::delay_exec();
}
}
}
SYS::clearTimeouts();
#endif
}
}
inline void 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_ENTER_PROGMODE:
JTAG2::enter_progmode();
break;
case JTAG2::CMND_RESET:
#ifdef DISABLE_HOST_TIMEOUT
JTAG2::enter_progmode();
#else
set_status(JTAG2::RSP_OK);
#endif
break;
case JTAG2::CMND_SIGN_OFF:
// Restore default baud rate before exiting
JTAG2::PARAM_BAUD_RATE_VAL = JTAG2::baud_19200;
if (JTAG2::ConnectedTo&0x01) {
//if this is true, we're talking to the target too! We're better tell it we're done...
JTAG2::leave_progmode();
JTAG2::go();
}
JTAG2::ConnectedTo&=0xFD; // no longer talking to host either, anymore.
set_status(JTAG2::RSP_OK);
#if (defined(USE_HV_PROGRAMMING) && (defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny_Zero_One__) || defined(__AVR_ATmega_Zero__) || defined(__AVR_DA__)))
if (SYS::checkHVMODE() > 200) SYS::cyclePOWER(); // if PCHV mode, power-cycle target
#endif
break;
case JTAG2::CMND_LEAVE_PROGMODE:
JTAG2::leave_progmode();
break;
case JTAG2::CMND_GET_SYNC:
JTAG2::set_status(JTAG2::RSP_OK);
break;
case JTAG2::CMND_GO:
#ifdef DISABLE_HOST_TIMEOUT
JTAG2::leave_progmode();
#endif
JTAG2::go();
break;
case JTAG2::CMND_SET_DEVICE_DESCRIPTOR:
#if (defined(USE_HV_PROGRAMMING) && (defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny_Zero_One__) || defined(__AVR_ATmega_Zero__) || defined(__AVR_DA__)))
if (SYS::checkHVMODE() > 200) SYS::cyclePOWER(); // if PCHV mode, power-cycle target
if (SYS::checkHVMODE() > 100) { // if HV or PCHV mode, then apply HV pulse and UPDI enable sequence
SYS::pulseHV();
SYS::updiEnable();
}
#endif
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;
}
#ifndef DISABLE_TARGET_TIMEOUT
if (SYS::checkTimeouts() & WAIT_FOR_TARGET) {
#ifdef DEBUG_ON
DBG::debug('T',SYS::checkTimeouts());
#endif
// If we got a timeout while waiting for the target during the preceeding command, then warn the host:
JTAG2::set_status(JTAG2::RSP_NO_TARGET_POWER); //this error looks like the best fit
}
#endif
}
}