Thanks for your reply.
Although my clock initialization seems a bit fishy, it isn't the cause of the problem.
I found out that the debug code following immediately after the clock init and the wdog init is the cause of the reset. The code is executed from the reset ISR, and if I remove it, everything works smoothly:
if(SRS > 0) /* If reset source wasn't the BDM */
{
uint8 srs = SRS;
if( (srs & SRS_POR_MASK) > 0 ) /* Reset caused by POR */
{
asm NOP;
}
else if( (srs & SRS_PIN_MASK) > 0 ) /* Reset caused by reset pin pulled low */
{
asm NOP;
}
else if( (srs & SRS_COP_MASK) > 0 ) /* Reset caused by COP */
{
asm NOP;
}
else if( (srs & SRS_ILOP_MASK) > 0 ) /* Reset caused by illegal op code */
{
asm NOP;
}
else if( (srs & SRS_ILAD_MASK) > 0 ) /* Reset caused by illegal address access */
{
asm NOP;
}
else if( (srs & SRS_LOC_MASK) > 0 ) /* Reset caused by loss of clock */
{
asm NOP;
}
else /* Reset caused by LVD etc */
{
asm NOP;
}
}
And this is the disassembly:
286: if(SRS > 0) /* If reset source wasn't the BDM */
000e c60000 [4] LDA _SRS
0011 272e [3] BEQ L41 ;abs = 0041
287: {
288: uint8 srs = SRS;
0013 c60000 [4] LDA _SRS
0016 9ee701 [4] STA 1,SP
289:
290: if( (srs & SRS_POR_MASK) > 0 ) /* Reset caused by POR */
0019 2a03 [3] BPL L1E ;abs = 001e
291: {
292: asm NOP;
001b 9d [1] NOP
293: }
001c 2023 [3] BRA L41 ;abs = 0041
001e L1E:
294: else if( (srs & SRS_PIN_MASK) > 0 ) /* Reset caused by reset pin pulled low */
001e a540 [2] BIT #64
0020 2703 [3] BEQ L25 ;abs = 0025
295: {
296: asm NOP;
0022 9d [1] NOP
297: }
0023 201c [3] BRA L41 ;abs = 0041
0025 L25:
298: else if( (srs & SRS_COP_MASK) > 0 ) /* Reset caused by COP */
0025 a520 [2] BIT #32
0027 2703 [3] BEQ L2C ;abs = 002c
299: {
300: asm NOP;
0029 9d [1] NOP
301: }
002a 2015 [3] BRA L41 ;abs = 0041
002c L2C:
302: else if( (srs & SRS_ILOP_MASK) > 0 ) /* Reset caused by illegal op code */
002c a510 [2] BIT #16
002e 2703 [3] BEQ L33 ;abs = 0033
303: {
304: asm NOP;
0030 9d [1] NOP
305: }
0031 200e [3] BRA L41 ;abs = 0041
0033 L33:
306: else if( (srs & SRS_ILAD_MASK) > 0 ) /* Reset caused by illegal address access */
0033 a508 [2] BIT #8
0035 2703 [3] BEQ L3A ;abs = 003a
307: {
308: asm NOP;
0037 9d [1] NOP
309: }
0038 2007 [3] BRA L41 ;abs = 0041
003a L3A:
310: else if( (srs & SRS_LOC_MASK) > 0 ) /* Reset caused by loss of clock */
003a a504 [2] BIT #4
003c 2702 [3] BEQ L40 ;abs = 0040
311: {
312: asm NOP;
003e 9d [1] NOP
313: }
003f 21 [3] SKIP1 L41 ;abs = 0041
0040 L40:
314: else /* Reset caused by LVD etc */
315: {
316: asm NOP;
0040 9d [1] NOP
0041 L41:
317: }
318: }
Now, if the code did a write access to SRS somewhere (address 0x1800), I would have expected a reset. But it is all read-only. So why does the above code reset the mcu? Aren't you allowed to read SRS from the reset ISR? If not, where is this documented? If the BDM is connected, the above code won't be executed, which explains why everything works with the BDM connected.
To clearify, my reset ISR looks like this:
- Set SP.
- Init clock
- Init wdog
- The above debug code to check the cause of the reset.
- JMP to main().
Am I missing something in the documentation or is this a silicon bug?