11.07.2015 Views

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

MSP430 IAR C/C++ Compiler reference guide - Rice University

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Primitives for interrupts, concurrency, and OS-related programmingInterrupt functions for the <strong>MSP430</strong>X architectureWhen compiling for the <strong>MSP430</strong>X architecture, all interrupt functions are automaticallyplaced in the segment ISR_CODE, which must be located in the lower 64 Kbytes ofmemory. If you are using a ready-made linker command file for an <strong>MSP430</strong>X device,the segment will be correctly located.In the Small data model, all functions save only 16 bits of the 20-bit registers on entryand exit. If you have assembler routines that use the upper 4 bits of the registers, youmust use either the __save_reg20 keyword on all your interrupt functions,alternatively the --save_reg20 compiler option.This will ensure that the interrupt functions will save and restore all 20 bits of any 20-bitregisters that are used. The drawback is that the entry and leave sequences will becomeslower and consume more stack space.Note: If a __save_reg20 function, compiled using either the --lock_R4 or the--lock_R5 option, calls another function that is not __save_reg20 declared and doesnot lock R4/R5, the upper four bits of R4/R5 might be destroyed. For this reason, it is notrecommended to use different settings of the --lock_R4/R5 option for differentmodules.MONITOR FUNCTIONSA monitor function causes interrupts to be disabled during execution of the function. Atfunction entry, the status register is saved and interrupts are disabled. At function exit,the original status register is restored, and thereby the interrupt status that existed beforethe function call is also restored.To define a monitor function, you can use the __monitor keyword. For <strong>reference</strong>information, see __monitor, page 195.Avoid using the __monitor keyword on large functions, since the interrupt willotherwise be turned off for a significant period of time.Example of implementing a semaphore in CIn the following example, a semaphore is implemented using one static variable and twomonitor functions. A semaphore can be locked by one process, and is used forpreventing processes from simultaneously using resources that can only be used by oneprocess at a time, for example a USART. The __monitor keyword assures that the lockoperation is atomic; in other words it cannot be interrupted./* When the_lock is non-zero, someone owns the lock. */static volatile unsigned int the_lock = 0;/* get_lock -- Try to lock the lock.* Return 1 on success and 0 on failure. */26<strong>MSP430</strong> <strong>IAR</strong> C/<strong>C++</strong> <strong>Compiler</strong>Reference Guide

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!