Skip to content

Commit 3e408d7

Browse files
committed
fix: cache chipId for reset() after security bit is set
1 parent 31fff26 commit 3e408d7

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

src/Samba.cpp

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ using namespace std;
5151

5252
#define min(a, b) ((a) < (b) ? (a) : (b))
5353

54-
Samba::Samba() : _debug(false), _isUsb(false)
54+
Samba::Samba() : _chipId(0), _debug(false), _isUsb(false)
5555
{
5656
}
5757

@@ -63,7 +63,6 @@ bool
6363
Samba::init()
6464
{
6565
uint8_t cmd[3];
66-
uint32_t cid;
6766

6867
_port->timeout(TIMEOUT_QUICK);
6968

@@ -98,7 +97,7 @@ Samba::init()
9897
// Read the chip ID
9998
try
10099
{
101-
cid = chipId();
100+
chipId();
102101
}
103102
catch (SambaError)
104103
{
@@ -108,9 +107,9 @@ Samba::init()
108107
_port->timeout(TIMEOUT_NORMAL);
109108

110109
if (_debug)
111-
printf("chipId=%#08x\n", cid);
110+
printf("chipId=%#08x\n", _chipId);
112111

113-
if (getChipArchitecture(cid) != Unsupported)
112+
if (getChipArchitecture(_chipId) != Unsupported)
114113
return true;
115114

116115
return false;
@@ -530,13 +529,18 @@ Samba::version()
530529
uint32_t
531530
Samba::chipId()
532531
{
532+
// use cached value if present
533+
if (_chipId != 0)
534+
return _chipId;
535+
533536
// Read the ARM reset vector
534537
uint32_t vector = readWord(0x0);
535538

536539
// If the vector is a ARM7TDMI branch, then assume Atmel SAM7 registers
537540
if ((vector & 0xff000000) == 0xea000000)
538541
{
539-
return readWord(0xfffff240);
542+
_chipId = readWord(0xfffff240);
543+
return _chipId;
540544
}
541545

542546
// Else use the Atmel SAM3 or SAM4 or M0+ registers
@@ -547,35 +551,23 @@ Samba::chipId()
547551
// Check if it is Cortex M0+
548552
if (part_no == 0xC600)
549553
{
550-
return readWord(0x41002018); // DSU_DID register
554+
_chipId = readWord(0x41002018); // DSU_DID register
555+
return _chipId;
551556
}
552557

553558
// Else assume M3 or M4
554-
uint32_t cid = readWord(0x400e0740);
555-
if (cid == 0)
556-
cid = readWord(0x400e0940);
557-
return cid;
559+
_chipId = readWord(0x400e0740);
560+
if (_chipId == 0)
561+
_chipId = readWord(0x400e0940);
562+
return _chipId;
558563
}
559564

560565
void
561566
Samba::reset(void)
562567
{
563-
uint32_t chipId;
564-
565568
printf("CPU reset.\n");
566569

567-
// Read the chip ID
568-
try
569-
{
570-
chipId = Samba::chipId();
571-
}
572-
catch (SambaError)
573-
{
574-
printf("Reset failed.\n");
575-
return;
576-
}
577-
578-
switch (getChipArchitecture(chipId))
570+
switch (getChipArchitecture(_chipId))
579571
{
580572
case SAMD20:
581573
case SAMD21:

src/Samba.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class Samba
9696
SAMD21
9797
};
9898

99+
uint32_t _chipId;
99100
bool _debug;
100101
bool _isUsb;
101102
SerialPort::Ptr _port;

0 commit comments

Comments
 (0)