# MSP430 Illegal Instruction

A few questions here along the same lines, I'm looking at the datasheet for the MSP430x2xx family (I'm using the MSP430F235 FWIW). It says there in section 2.2.4 that "illegal instruction fetch" is an interrupt source for the Reset vector. Then separately under 2.2.1 under the topic of Non-Maskable Interrupts (NMI), "An access violation to the flash memory" is cited as a source.

(1) Can anyone explain to me the difference between (and/or causes of) these two apparently distinct event sources (with different apparently different consequences)?

(2) Can I write an ISR for the NMI source, and make it act like a soft reset by reinitializing the stack pointer and "recursively" calling main or something like that, so that the register and RAM (global variable) state isn't altered?

(3) Assuming I can't intercept the one that results in a Reset vector (which I presume also comes along with a reinitialization of all the registers?), can I at least detect at startup that it happened by checking some status bit to decide what to do about it, and if so which status register/bit would that be?

## 1 Answer

Illegal instruction means that it read an opcode, but that opcode does not match to any valid machine instruction.

Access violation means that you tried to use a type of memory access that is not supported by the device. For a flash, that usually means you tried to write to it without using the proper procedure for writing or erasing a page.

Access violation can also mean that you tried to execute a privileged instruction while in a user mode, though I'm not sure MSP430 supports this.

Another example of access violation is trying to fetch opcodes from a region of memory marked not-executable, though I doubt that the MSP430 has this kind of memory protection support.

Can I write an ISR for the NMI source, and make it act like a soft reset by reinitializing the stack pointer and "recursively" calling main or something like that, so that the register and RAM (global variable) state isn't altered?

I'd look for an app note or something in the datasheet saying how to do that. Generally, jumping to main() does not work. You usually want to disable all interrupts, initialize the stack pointer, and jump to the reset vector. You may need to reset processor mode registers or control registers as well. This way, the code that sets things up before calling main() gets run.

Assuming I can't intercept the one that results in a Reset vector (which I presume also comes along with a reinitialization of all the registers?), can I at least detect at startup that it happened by checking some status bit to decide what to do about it, and if so which status register/bit would that be?

I know the Atmel AVR has a register you can read that tells you why you were reset. Not sure about the MSP430.