16
Lab 1: I/O, timers, interrupts on the eZ430-RF2500 UC Berkeley - EE 290Q Thomas Watteyne January 25, 2010 1 The eZ430-RF2500 and its Components 1.1 Crash Course on the MSP430f2274 The heart of this platform is its MSP430 microcontroller, by Texas Instruments. There is a complete family of MSP430 micro-controllers, the variants of which are different in the amount of RAM/ROM and I/O capabilities, mainly. The one you will program is the MSP420f2274, featuring 32KB of Flash Memory (ROM) and 1KB of RAM. The MSP430 is a 16-bit RISC microcontroller. 16-bit means that all registers hold 16 bits; interconnection between the elements of the micro-controller is done using 16-bit buses. RISC – for Reduced Instruction Set Computer – refers to the fact that there are (only) 27 core instructions. 1.1.1 Operation of the MSP430 Fig. 1 shows the internal architecture of the MSP430. The CPU contains 16 registers; its operation goes as follows. A system clock ticks at a programmable rate (e.g. 1MHz), so each μs an instruction is fetched from memory (ROM), copied into the right register and executed 1 . An example execution can be adding two registers and copying the result to a third. In practice, these low- level details are taken care of by the compiler, which translates C into assembler language and binary code. In this tutorial, we only work with the higher-level C. 1.1.2 Programming the MSP430 When you program a mote, you program its microcontroller, i.e. you put the compiled binary code at the right location in the MSP430’s ROM memory. When the board is switched on, the MSP430 starts by fetching the first instruc- tion at a predetermined location in ROM; this is where the programming tool puts your compiled code. 1 Strictly speaking, instructions can take a couple of CPU cycles to execute 1

Lab 1 I.O Timers Interrupts on The

Embed Size (px)

DESCRIPTION

CA

Citation preview

Lab 1: I/O, timers, interrupts on the

eZ430-RF2500

UC Berkeley - EE 290Q

Thomas Watteyne

January 25, 2010

1 The eZ430-RF2500 and its Components

1.1 Crash Course on the MSP430f2274

The heart of this platform is its MSP430 microcontroller, by Texas Instruments.There is a complete family of MSP430 micro-controllers, the variants of whichare different in the amount of RAM/ROM and I/O capabilities, mainly. Theone you will program is the MSP420f2274, featuring 32KB of Flash Memory(ROM) and 1KB of RAM.

The MSP430 is a 16-bit RISC microcontroller. 16-bit means that all registershold 16 bits; interconnection between the elements of the micro-controller is doneusing 16-bit buses. RISC – for Reduced Instruction Set Computer – refers tothe fact that there are (only) 27 core instructions.

1.1.1 Operation of the MSP430

Fig. 1 shows the internal architecture of the MSP430. The CPU contains 16registers; its operation goes as follows. A system clock ticks at a programmablerate (e.g. 1MHz), so each µs an instruction is fetched from memory (ROM),copied into the right register and executed1. An example execution can beadding two registers and copying the result to a third. In practice, these low-level details are taken care of by the compiler, which translates C into assemblerlanguage and binary code. In this tutorial, we only work with the higher-levelC.

1.1.2 Programming the MSP430

When you program a mote, you program its microcontroller, i.e. you put thecompiled binary code at the right location in the MSP430’s ROM memory.When the board is switched on, the MSP430 starts by fetching the first instruc-tion at a predetermined location in ROM; this is where the programming toolputs your compiled code.

1Strictly speaking, instructions can take a couple of CPU cycles to execute

1

Figure 1: The internal architecture of the MSP430.

To configure other components (e.g. to set the transmission power of theradio), you need to program MSP430 in such a way that it configures the radioat the beginning of your program.

1.1.3 Interrupts

A program for a wireless mote is in practice a sequence of very small piecesof code executed when some event happens: e.g. when the button is pressed,turn on the red LED. When an event happens in an electronic element outsidethe MSP430 (e.g. the button is pressed), this element informs the MSP430by changing the electric state of the wire which connects this element to theMSP430. This wire is connected to one of the ports of the MSP430 (e.g. portP1.2 in the case of the button on the eZ430-RF2500). You need to programthe MSP430 in such a way that changing the status on port P1.2 generatesan interrupt. When an interrupt is generated, the MSP430 stops its currentexecution (if any), and starts executing a specific function called the InterruptService Routine (ISR) associated to that particular interrupt. Once this functionis finished (normally an ISR is a very small function), it resumes its normalexecution (if any).

1.1.4 Timers

When writing code, you may want to wait some time before doing something(e.g. when I receive a packet, wait 10ms, and send a reply packet). This can bedone using a timer, a specific component of the MSP430. Physically, a timeris a 16-bit register which is incremented at each clock cycle2, i.e. once every µs

with a 1MHz clock. It starts at 0, and counts up until a programmable value,

2Strictly speaking, timers can be configured to count in up, down, or up/down modes,see [1]

2

A = 0b01101001

∼A = 0b10010110

A —= 0b00000010 ⇒ A=0b01101011

A &= ∼0b00001000 ⇒ A=0b01100001

A ∧= 0b10001000 ⇒ A=0b11100001

A << 2 ⇒ A=0b10100100

A >> 2 ⇒ A=0b00011010

Figure 2: Binary operators used to set/reset individual bits.

upon which is generates a timer interrupt, reset to 0, and starts counting upagain.

1.1.5 I/O Functionalities

The MSP430 has 40 pins:

• 4 have analog functions to power the board;

• 2 are used for testing at the factory;

• 2 are used if an external crystal is used as clock source, which is not thecase on the eZ430-RF2500 platform;

• 32 have digital functions.

The 32 digital pins are grouped into 4 ports of 8 pins each. Each pin has aname in the form Px.y, y represents the position of the pins within port x. Allpins can be generic I/O pins, a number of 8-bit registers are used to configurethem:

• PxDIR.y sets the direction of port Px.y; output if Px.y=1, input if Px.y=0;

• PxOUT.y sets the state of port Px.y when set as output;

• PxIN.y reads the state of port Px.y when set as input;

• PxIE.y enables interrupts on that port;

Each of these registers holds 8 bits, one for each pin. As a result, P1DIR=0b111100003

means that pins P1.1 through P1.4 are input, while P1.5 through P1.8 are out-puts. To set/reset a specific pin, you need to use the binary operators presentedin Fig. 2.

Note that most of the 32 digital pins can also be used for specific functions(SPI interface, input for Analog-to-Digital conversion, . . . ), see [2] for details.

30bx means that x is written in binary; 0xx means that x is written in hexadecimal. We thushave 0x1A=0b00011010. Use Windows Calculator in Scientific mode for quick conversions.

3

1.1.6 Low-Power Operation

As the MSP430 spends its time waiting for interrupts, it is important to reduceits energy consumption during idle periods by shutting down the clocks youare not using. The more clocks you shut down, the less energy you use, butmake sure you leave on the clocks you need. There are four low power modes(LPM1,. . . , LPM4) which shut down different clocks (details in [1]).

In practice, you only need to leave on the auxiliary clock which clocks a timerto wake the MSP430 after some time. This is achieved by entering low-powermode 3, by adding this line at the end of you main function:

bis SR register(LPM3 bits);

You now know enough about the MSP430 for this tutorial, butif you want to work with the MSP430, you are strongly advised toread [1] and [2] (in that order).

1.2 Crash Course on the CC2500

The CC2500 is the radio chip on the eZ430-RF2500. It functions in the 2400-2483.5 MHz frequency band and provides an excellent option for WSN applica-tions because of its low-power characteristics. This chip has 20 pins:

• 2 for connecting a (mandatory) 26MHz external crystal oscillator;

• 2 for connecting the antenna;

• 10 for powering the chip;

• 6 for digital communication with the MSP430 (to be detailed in sec-tion 1.3)

The chip contains 47 registers to configure operating frequency, modulationscheme, baud rate, transmission power, etc. Because these registers are erasedduring power down, the MSP430 should configure all of them at startup. 13commands allow the MSP430 to control the state of the CC2500 (transmit,power down, receive, . . . ). The CC2500 follows a state diagram, as detailedin [3].

In practice, Texas Instruments provides some code which hides the low-leveldetails of the CC2500 behind a higher level API. These drivers are part ofthe SimpliciTI project, which can be found online for free. We will use theseoff-the-shelf drivers in this tutorial.

You now know enough about the CC2500 for this tutorial, butif you want to work with the CC2500, you are strongly advised toread [3] (after having read the documents about the MSP430).

4

antenna

green LED (P1.1)

red LED (P1.0)

CC2500

extension pins

MSP430f2274

button (P1.2)

26MHz crystal for CC2500

Figure 3: The components of the eZ430-RF2500.

1.3 The eZ430-RF2500 Board

1.3.1 Overview

Fig. 3 shows the different components on the eZ430-RF2500. In particular, someof pins of the MSP430 are exported as extension pins P1 through P18. Notethat some of these pins may be unused pins of the MSP430, others are alreadyused, but duplicated as extension ports for debugging purposes.

1.3.2 Interconnecting the MSP430 with the CC2500

As shown in Fig. 4, 6 wires interconnect the MSP430 with the CC2500. 4of them form the SPI link, a serial link which enables digital communication.There is a hardware SPI modem on each side of the link, the configuration ofwhich is handled by the drivers.

The remaining two links are wires used by the CC2500 to wake-up theMSP430. That is, the MSP430 configures its ports P2.6 and P2.7 as input,with interrupts. It then configures the CC2500 to trigger GDO0 or GDO2 ona given event (typically, when receiving a packet). This enables the MSP430 toenter LPM3. Note that these wires are also routed to extension port pins P13and P14. The drivers are used in such a way that only GDO0 is used for properoperation. You can thus configure GDO2 as you wish, and monitor its statewith an oscilloscope on extension port pin P14.

Refer to [4] for details about the eZ430-RF2500 platform.

5

Clock

Chip SelectSPI SIMO

SPI

MIS

O

ext. P14ext. P13

Interrupt (GDO2)

Interrupt (GDO0)

Figure 4: MSP430 and CC2500 are interconnected by an SPI 4-wire link (plain)and two interrupt lines (dotted).

2 Keeping Code Organized

During this series of labs, we will come across different types of files:

• *.c files contain the C code which – once compiled – runs on the eZ430-RF2500 boards;

• *.py files contain Python code which we will run on the computer tocommunicate with the connected eZ430-RF2500 dongle;

• *.dep, *.ewd, *.ewp files are created by IAR for each project;

• *.eww is a Workspace file which groups multiple projects. In this lab, wewill have only one Workspace which groups all the projects.

Create a directory source code anywhere on your hard drive which willstore all the source code. We will use the following structure:

• drivers/ contain the drivers for the radio as downloaded from TI’s web-site. We will only use those when using the radio4.

• c files/ contains all the C code which – once compiled – runs on theeZ430-RF2500 boards; it should only contain *.c files;

• python/ contains the Python scripts; it should only contain *.py files;

• iar v4.21/ contains the IAR-specific *.dep, *.ewd, *.ewp and *.eww

files.

4Download from http://www.eecs.berkeley.edu/∼watteyne/290Q/labs/drivers.zip

6

You will need to repeat these steps each time you create a new project inIAR:

• Connect the eZ430-RF2500 programming board to the computer and openIAR;

• Choose Project, Create new project;

• Choose C > main; click OK;

• Create a directory on your desktop in which you save your project;

• Go to Project > Option (Alt+F7), in General Option, choose Device=MSP430F2274;in Debugger, choose Driver=FET Debugger.

7

Listing 1: Switching on both LEDs (Section 3).

1 #inc lude ” io430 . h”2 i n t main ( void )3 4 WDTCTL = WDTPW + WDTHOLD;5 P1DIR |= 0x03 ;6 P1OUT |= 0x03 ;7 whi l e ( 1 ) ;8

3 led steady: A Steady LED

Goal. This example switches on both LEDs at node startup. It shows youhow to use the port registers PxDIR and PxOUT.

3.1 Running the Code

• create a new project called led steady;

• in IAR, copy the code presented in Listing 1 (p.8);

• compile and download the code onto the board (Ctrl+D).

• Let the code execute (F5), you should now see both LEDs on.

Some keys for understanding the code:

• Line 1: io430.h contains all the macros used for translating humanreadable values (e.g. P1DIR) into actual memory location. Right click onio430.h and choose Open "io430.h" to see its content.

• Line 4: The MSP430 has a watchdog timer which resets the board if it isnot reset before it elapses. This way, if you code hangs, the board restartsand continues functioning. For our simple examples, we disactivate thisfunction by writing the correct values into register WDTCTL.

• Line 5 declares P1.0 and P1.1 as output pins. This is done by turningbits 0 and 1 to 1 in register P1DIR;

• Line 6 sets the output state of pins P1.0 and P1.1 to logic 1 (physicallysomewhere between 2.2V an 3V). This causes the LEDs, connected tothose pins, to light.

• Line 7 loops, leaving the board running.

8

Listing 2: Blinking LEDs using an active waiting loop (Section 4).

1 #inc lude ” io430 . h”2 #inc lude ” in430 . h”3 i n t main ( void )4 5 WDTCTL = WDTPW + WDTHOLD;6 i n t i ;7 P1DIR |= 0x03 ;8 whi l e (1 ) 9 P1OUT ˆ= 0x03 ;

10 f o r ( i =0; i <10000; i++) 11 no ope r a t i on ( ) ;12 13 14

4 led loop: Active Waiting Loop

Goal. This example shows a first way of measuring time.no operation(); instructs the MSP430 to do nothing during one

cycle; by repeating this many times, time can be measured. As this isneither accurate nor energy-efficient, a more elegant technique will beshown in Section 6.

4.1 Running the Code

• create a new project called led loop;

• in IAR, copy the code presented in Listing 2 (p.9);

• compile and download the code onto the board (Ctrl+D).

• Let the code execute (F5), both LEDs should blink.

Some keys for understanding the code:

• Line 9: the operator ∧ = causes 1s to become 0s and vice-versa (akatoggling). In case of our LEDs, it causes their on/off state to change;

• Line 10: no operation(); cause the MSP430 to do nothing for onecycle.

4.2 Measuring Current Consumption

Using the oscilloscope together with the 1Ω resistor, you can visualize the cur-rent consumption of the board.

9

Figure 5: Use the 1Ω jumper toswitch on the target board...

board

osc.

Figure 6: ...and read the currentconsumption of the board.

no lEDs (MSP430 running) 3.12mAred+green LED 9.12-3.12=6.00mAred LED 7.04-3.12=3.92mAgreen LED 5.20-3.12=2.08mA

Table 1: Current consumed by the LEDs (Section 3).

• Exit the debug mode in IAR, disconnect the board and connect it to thebattery unit. Use the 1Ω resistor jumper to switch on the board (Fig. 5);

• Connect the oscilloscope onto the resistor as in Fig. 6. What you read isthe current consumption in amps of the board (U = R · I, with R = 1Ω).

• You can see how the energy consumption increases when LEDs switch on.

• By replacing line 6 by P1OUT |= 0x01 and P1OUT |= 0x02 you can mea-sure the consumption of the LEDs independently. Make sure you findsomething close to Table 1.

4.3 Measuring Time

We want to measure time precisely with the oscilloscope. This can, in this case,be done by energy consumption. A more generic solution is to use extension pinP6 represented in Fig. 7, which is connected to P2.3 on the MSP430.

We will configure P2.3 to be output and toggle its state together with thestate of the LEDs. Therefore:

• add line P2DIR |= 0x08; after line 7 to declare P2.3 as output;

• add line P2OUT ∧= 0x08; after line 9 to toggle P2.3 after toggling theLEDs;

• connect a probe of your oscilloscope to extension port P6, ground onextension port P12;

10

Figure 7: The extension pins on the eZ430-RF2500 board, taken from [4]. Notethat the pins with even number (shown on the right) are located on the edge ofthe board, and are thus accessible more easily.

threshold value for i measured toggle duration1000 6.72ms10000 67.2ms20000 134.0ms30000 201.0ms

Table 2: Duration when using an active waiting loop (Section 4).

• power on the board, you’re now able to read the duration between twotoggles.

• reprogram your board with waiting values between 1000 and 30000; verifythat the times obtained are close to the ones presented in Table 2.

11

Listing 3: Toggle LEDs state using the button (Section 5).

1 #inc lude ” io430 . h”2 #inc lude ” in430 . h”3 i n t main ( void )4 5 WDTCTL = WDTPW + WDTHOLD;6 P1DIR |= 0x03 ;7 P1DIR &= ˜0x04 ;8 P1REN |= 0x04 ;9 P1IE |= 0x04 ;

10 b i s S R r e g i s t e r (GIE ) ;11 whi l e ( 1 ) ;12 13 #pragma vec to r=PORT1 VECTOR14 i n t e r r u p t void Port 1 ( void )15 16 P1IFG &= ˜0x04 ;17 P1OUT ˆ= 0x03 ;18

5 led button: Button-Driven Toggle Through

Interrupts

Goal. The goal of this section is to start using interrupts through thebutton on the board. You will program the board so that the LEDs changestate when the button is pressed. You will also measure the energy consumedwhen the board sits idle, and when it enters a low-power mode.

5.1 Running the Code

• create a new project called led button;

• in IAR, copy the code presented in Listing 3 (p.12);

• compile and download the code onto the board (Ctrl+D).

• Let the code execute (F5), press the button on the board, the LEDs’ stateshould change.

Some keys for understanding the code:

• Lines 6 and 7 declare P1.0 and P1.1 as outputs (for the LEDs), and P1.2as input (for the button);

12

• Line 8 activates an internal resistor on P1.2. This is needed for a buttonfor the signal to be cleaner when the button is pressed; i.e. otherwise, theP1.2 constantly floats between high and low state. This is only needed forbuttons.

• Line 9 enables interrupts on P1.2.

• Line 10 enables interrupts globally.

• Line 13 and 14 declare that this function should be called when aninterrupt of type PORT1 VECTOR happens; the name Port 1 chosen has noimportance.

• Line 16 resets the interrupt flag generated (mandatory otherwise thefunction will be called again right after it finishes execution).

5.2 Low-Power Modes

As such, the board sits idle while waiting for an interrupt to happen with theMSP430 on (which continuously executed line 11). After measuring this current,you will change the code so as to enter low-power mode instead of sitting idle.

• Using the battery unit, the resistor jumper and the oscilloscope, measurethe current consumed in this mode (make sure that the LEDs are off whenyou measure).

• Change line 10 by bis SR register(GIE+LPM0 bits);. This instructsthe MSP430 to enable the interrupts globally, and to enter LPM0 modeimmediately. Only an interrupt can wake the MSP430.

• Remove line 11 which can never be reached.

• Measure the current now, make sure that LEDs are again off.

• repeat with LPM3 and LPM4. You should obtain results close to the onesin Table 3.

Note that, because we are not using any clock in this example, we shoulduse LPM4 as it is the most energy-efficient.

13

Active mode 3.28mA(active: CPU and all clock)LPM0 mode 2.88mA(active: SMCLK, ACLK; disabled: CPU, MCLK5)LPM3 mode 2.72mA(active: ACLK; disabled: CPU, MCLK, SMCLK)LPM4 mode 2.72mA(disabled: CPU and all clock)

Table 3: Current consumed by the LPM modes (Section 5).

6 led timer: Timer-Driven Toggle Through Timer

Interrupts

Goal. We will explore a energy-efficient way of measuring time by usingtimers. A timer is a register which counts up to a certain number at eachclock tick. During this, the MSP430 can switch to a low-power mode. Eachtime the timer threshold is reached, a timer interrupt wakes the MSP430,which toggles the two LEDs.

6.1 Running the Code

• create a new project called led timer;

• in IAR, copy the code presented in Listing 4 (p.15);

• compile and download the code onto the board (Ctrl+D).

• Let the code execute (F5), both LEDs should blink.

Some keys for understanding the code:

• Line 7 switches on the ACLK by sourcing it to the VLO, a very low powercrystal oscillator inside the MSP430, different from the more accurate andenergy-hungry DCO (Digitally Controlled Oscillator). The DCO drivesthe MCLK and the VLO the ACLK. ACLK is used for the timer; MCLKfor executing code. When there is no code to be executed (i.e. whenwaiting for interrupts), a low-power mode (in which DCO is switched off)can be used.

• Line 8 enables interrupts for Timer A.

• Line 9 sets the value up to which Timer A will count.

• Line 10 tells Timer A to count up (MC 1) each time ACLK ticks (TASSEL 1).

14

Listing 4: Blink LEDs using timer interrupts (Section 6).

1 #inc lude ” io430 . h”2 #inc lude ” in430 . h”3 i n t main ( void )4 5 WDTCTL = WDTPW + WDTHOLD;6 P1DIR |= 0x03 ;7 BCSCTL3 |= LFXT1S 2 ;8 TACCTL0 = CCIE ;9 TACCR0 = 1000 ;

10 TACTL = MC 1+TASSEL 1 ;11 b i s S R r e g i s t e r (GIE+LPM3 bits ) ;12 13 #pragma vec to r=TIMERA0 VECTOR14 i n t e r r u p t void Timer A ( void )15 16 P1OUT ˆ= 0x03 ;17

• Line 11 enables interrupts globally and enters LPM3. Note that LPM3leaves only ACLK running, which is exactly what we need because ourTime A runs off ACLK.

6.2 Measuring Time

As in Section 4, we use extension pin P6 to measure time exactly. Therefore:

• add line P2DIR |= 0x08; after line 6 to declare P2.3 as output;

• add line P2OUT ∧= 0x08; after line 16 to toggle P2.3 after toggling theLEDs;

• connect a probe of your oscilloscope to extension port P6, ground onextension port P1. Power on the board, you’re now able to read theduration between two toggles.

• reprogram your board with TACCR0 values between 1000 and 30000;verify that the times obtained are close to the ones presented in Table 4.

Table 4 informs you that 1000 ACLK cycles take 95.00ms, one ACLK cyclethus takes 95µs, the VLO on the MSP430 thus runs at 10.5kHz6.

6In theory, the VLO runs at 12kHz; the exact value depends on the voltage of the batteriesand on the temperature.

15

TACCR0 value measured toggle duration500 47.40ms1000 95.00ms10000 950.0ms20000 1890ms

Table 4: Duration when using timers (Section 6).

References

[1] MSP430x2xx Family User’s Guide, 2008, SLAU144E [available online].

[2] MSP430x22x2, MSP430x22x4 Mixed Signal Microcontroller, 13 May 2009,SLAS504C [available online].

[3] CC2500, CC2500, Low-Cost Low-Power 2.4 GHz RF Transceiver (Rev. B),Texas Instruments, Inc., 19 May 2009, data Sheet SWRS040C [availableonline].

[4] eZ430-RF2500 Development Tool User’s Guide, Texas Instruments, April2009, SLAU227E [available online].

16