Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 406ef88

Browse files
authoredSep 16, 2019
Merge pull request #52 from facchinm/emulate_16u2_dtr
[MuxTO] Reset 4809 on baudrate change / port open
2 parents e7170e9 + e458d1d commit 406ef88

File tree

5 files changed

+809
-750
lines changed

5 files changed

+809
-750
lines changed
 

‎firmwares/MuxTO/JTAG2.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ enum baud_rate {
3838
// *** Parameter Values ***
3939
constexpr uint8_t PARAM_HW_VER_M_VAL = 0x01;
4040
constexpr uint8_t PARAM_HW_VER_S_VAL = 0x01;
41-
constexpr uint8_t PARAM_FW_VER_M_MIN_VAL = 0x00;
42-
constexpr uint8_t PARAM_FW_VER_M_MAJ_VAL = 0x06;
43-
constexpr uint8_t PARAM_FW_VER_S_MIN_VAL = 0x00;
41+
constexpr uint8_t PARAM_FW_VER_M_MIN_VAL = 0x07;
42+
constexpr uint8_t PARAM_FW_VER_M_MAJ_VAL = 0x01;
43+
constexpr uint8_t PARAM_FW_VER_S_MIN_VAL = 0x07;
4444
constexpr uint8_t PARAM_FW_VER_S_MAJ_VAL = 0x06;
4545
extern uint8_t PARAM_EMU_MODE_VAL;
4646
extern baud_rate PARAM_BAUD_RATE_VAL;

‎firmwares/MuxTO/MuxTO.bin

68 Bytes
Binary file not shown.

‎firmwares/MuxTO/MuxTO.hex

Lines changed: 750 additions & 746 deletions
Large diffs are not rendered by default.

‎firmwares/MuxTO/MuxTO.ino

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
// Includes
1919
#include "sys.h"
20+
#include "lock.h"
2021
#include "updi_io.h"
2122
#include "JICE_io.h"
2223
#include "JTAG2.h"
24+
#include "UPDI_hi_lvl.h"
2325

2426
volatile bool updi_mode = false;
2527
unsigned long baudrate = 115200;
@@ -28,11 +30,15 @@ unsigned long updi_mode_end = 0;
2830
uint8_t stopbits = 1;
2931
uint8_t paritytype = 0;
3032
uint8_t numbits = 8;
33+
int8_t dtr = -1;
34+
int8_t rts = -1;
3135
uint16_t serial_mode = SERIAL_8N1;
3236
bool serialNeedReconfiguration = false;
3337

3438
char support_buffer[64];
3539

40+
struct lock q;
41+
3642
void setup() {
3743
/* Initialize MCU */
3844
pinMode(LED_BUILTIN, OUTPUT);
@@ -41,6 +47,8 @@ void setup() {
4147
JICE_io::init();
4248
UPDI_io::init();
4349
Serial1.begin(baudrate, serial_mode);
50+
lock_init(&q);
51+
JTAG2::sign_on();
4452
}
4553

4654
//#define DEBUG;
@@ -62,17 +70,21 @@ void loop() {
6270
//blink_delay = 1000;
6371

6472
if (int c = Serial1.available()) {
73+
lock(&q);
6574
if (c > Serial.availableForWrite()) {
6675
c = Serial.availableForWrite();
6776
}
77+
unlock(&q);
6878
Serial1.readBytes(support_buffer, c);
6979
Serial.write(support_buffer, c);
7080
}
7181

7282
if (int c = Serial.available()) {
83+
lock(&q);
7384
if (c > Serial1.availableForWrite()) {
7485
c = Serial1.availableForWrite();
7586
}
87+
unlock(&q);
7688
Serial.readBytes(support_buffer, c);
7789
Serial1.write(support_buffer, c);
7890
}
@@ -133,13 +145,18 @@ void loop() {
133145
updi_mode_end = 0;
134146
}
135147

136-
if (Serial.baud() != baudrate || serialNeedReconfiguration) {
148+
if (Serial.baud() != baudrate || serialNeedReconfiguration || Serial.dtr() != dtr) {
149+
dtr = Serial.dtr();
137150
if (Serial.dtr() == 1) {
138151
baudrate = Serial.baud();
139152
Serial1.end();
140153
if (baudrate != 1200) {
141154
Serial1.begin(baudrate, serial_mode);
142155
serialNeedReconfiguration = false;
156+
// Request reset
157+
UPDI::stcs(UPDI::reg::ASI_Reset_Request, UPDI::RESET_ON);
158+
// Release reset (System remains in reset state until released)
159+
UPDI::stcs(UPDI::reg::ASI_Reset_Request, UPDI::RESET_OFF);
143160
}
144161
}
145162
}

‎firmwares/MuxTO/lock.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef _ARCH_LOCK_H_
2+
#define _ARCH_LOCK_H_
3+
4+
struct lock {
5+
uint32_t primask;
6+
};
7+
8+
#define LOCK_INIT (struct lock) {}
9+
10+
static inline void lock_init(struct lock *lock)
11+
{
12+
}
13+
14+
// source:
15+
// http://embeddedfreak.wordpress.com/2009/08/14/cortex-m3-global-interruptexception-control/
16+
//
17+
// confirm this is correct by printing primask before after
18+
19+
// note arm architecture v7m rm... setting PRIMASK raises priority to 0
20+
21+
static inline void lock(struct lock *lock)
22+
{
23+
uint32_t tmp;
24+
asm volatile (
25+
"mrs %0, PRIMASK\n\t"
26+
"cpsid i\n\t"
27+
: "=r" (tmp) );
28+
lock->primask = tmp;
29+
}
30+
31+
static inline void unlock(struct lock *lock)
32+
{
33+
asm volatile (
34+
"msr PRIMASK, %0\n\t"
35+
: : "r" (lock->primask) );
36+
}
37+
38+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.