5. Programming Tour

wittyexecutiveRéseaux et Communications

17 févr. 2014 (il y a 3 années et 1 mois)

201 vue(s)

1


Thomas Watteyne @ EDERC 2010

5.
Programming Tour

2


Section 5
-

Overview

5.
Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

3


Section 5
-

Overview

5. Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

4


MSP430


“Heart” of the eZ430
-
RF2500


16
-
bit 16MHz RISC


32kB ROM, 1kB RAM


2 Timers, USCI, 10
-
bit
ADCs


Debug capabilities using
JTAG


Low Power Operation

Thomas Watteyne @ EDERC 2010

5


CC2500


Any frequency on the 2.4
-
2.485GHz band


Not 802.15.4
-
compliant


Wake
-
on
-
radio support


Preamble sampling in
hardware


47 configuration registers


Switch
Tx
/Rx/idle


TXBUF, RXBUF


Tx

power and frequency


Follows a state machine


SmartRF

Studio

Thomas Watteyne @ EDERC 2010

6


Interconnection

Chip Select

Clock

SPI interface

interrupts

Thomas Watteyne @ EDERC 2010

7


eZ430
-
RF2500

2 LEDs

pushbutton

CC2500

chip
antenna

26MHz
crystal
for radio

extension
ports

MSP430

USB programmer:



Power



Debug (JTAG)



Interface (serial)

Thomas Watteyne @ EDERC 2010

8


Extension Pins


P1: GND


P2: VCC_EXT


P3: P2.0

I/O, ACLK, OA010


P4: P2.1

I/O, Timer_A3.INCLK, SMCLK, OA0O


P5: P2.2

I/O, Timer_A3.CCI0B, Timer_A3.TA0, OA


P6: P2.3

I/O, Timer_A3.CCI1B, Timer_A3.TA1, OA


P7: P2.4

I/O, Timer_A3.TA2, OA


P8: P4.3

I/O, Timer_B3.CCI0B, Timer_B3.TB0, OA


P9: P4.4

I/O, Timer_B3.CCI1B, Timer_B3.TB1, OA


P10: P4.5

I/O, Timer_B3.TB2, OA


P11: P4.6

I/O, OA


P12: GND


P13: GDO0

I/O from the CC2500 (configurable)


P14: GDO2

I/O from the CC2500 (configurable)


P15: P3.2

I/O, UC1SOMI


P16: P3.3

I/O, UC1CLK


P17: P3.0

I/O


P18: P3.1

I/O, UC1SIMO

Thomas Watteyne @ EDERC 2010

9


Section 5
-

Overview

5. Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

10


IAR
Embedded Workbench

project file
navigator

compile

open files

Thomas Watteyne @ EDERC 2010

11


Talking with your mote over “USB”


Use Windows Device Manager to
idenfify

the
COM port the eZ430
-
RF2500 is on


Use
PuTTY

to connect to that port

Thomas Watteyne @ EDERC 2010

12


Section 5
-

Overview

5. Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

13


Operations on binary data



A = 0b01101001


~A = 0b
10010110



A |= 0b000000
1
0 => A=0b011010
1
1



A
&
=~0b0000
1
000 => A=0b0110
0
001



A ^= 0b
1
000
1
000 => A=0b
1
110
0
001



A<<2 => A=0b101001
00



A>>2 => A=0b
00
011010

Thomas Watteyne @ EDERC 2010

14


I/O port registers


P1DIR: direction, 0=in, 1=out


P1OUT: set output


P1IN: read input

Thomas Watteyne @ EDERC 2010

16


Active Waiting Loop

#include "io430.h"

#include "in430.h"

int

main( void )

{


WDTCTL = WDTPW + WDTHOLD;


int

i
;


P1DIR |= 0x03;


while (1) {


P1OUT
^
= 0x03;


for (
i
=0;i<10000;i++) {


__
no_operation
();


}


}

}

P1.0 and P1.1
as output

Change Led state
(aka toggle)

Thomas Watteyne @ EDERC 2010

lab1_led_loop

21


Section 5
-

Overview

5. Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

22


Simple
Tx
/Rx

#include "
mrfi.h
"

int

main(void)

{


BSP_Init
();


P1REN |= 0x04;


P1OUT |= 0x04;


P1IE |= 0x04;


MRFI_Init
();


MRFI_WakeUp
();


MRFI_RxOn
();


__
bis_SR_register
(GIE+LPM4_bits);

}

void
MRFI_RxCompleteISR
()

{


P1OUT ^= 0x02;

}

#
pragma

vector=PORT1_VECTOR

__interrupt void Port_1 (void)

{


P1IFG &= ~0x04;


mrfiPacket_t

packet;


packet.frame
[0]=8+20;


MRFI_Transmit
(&packet, MRFI_TX_TYPE_FORCED);


P1OUT ^= 0x01;

}

Init MSP430 (clock,
leds
, button)

Init CC2500 (pins,
SPI, registers)

Start CC2500
oscill
.
(IDLE state)

CC2500 to RX state

low power mode,
waiting for
interrupts

Executed when
packet received
(“meta” interrupt
declaration)

Executed when
button pushed

clear button interrupt flag

declare packet w. max length

declare useful length

copy to cc2500
TXFIFO and send

Length (1B)

Source (4B)

Destination (4B)

Payload (Length
-
8 bytes long)

Thomas Watteyne @ EDERC 2010

lab2_txrx_simple

23


#include "
mrfi.h
"

int

main(void)

{


BSP_Init
();


P1REN |= 0x04;


P1OUT |= 0x04;


P1IE |= 0x04;


MRFI_Init
();


MRFI_WakeUp
();


MRFI_RxOn
();


__
bis_SR_register
(GIE+LPM4_bits);

}

void
MRFI_RxCompleteISR
()

{


P1OUT ^= 0x02;

}

#
pragma

vector=PORT1_VECTOR

__interrupt void Port_1 (void)

{


P1IFG &= ~0x04;


mrfiPacket_t

packet;


packet.frame
[0]=8+20;


MRFI_Transmit
(&packet, MRFI_TX_TYPE_FORCED);


P1OUT ^= 0x01;

}

Change Channel

#include "radios/family1/
mrfi_spi.h
"

mrfiSpiWriteReg
(CHANNR,0x
10
);

Removing this line
cause continuous
transmissions

Thomas Watteyne @ EDERC 2010

lab2_txrx_simple

24


Continuous Transmission

Thomas Watteyne @ EDERC 2010

25


#include "
mrfi.h
"

int

main(void)

{


BSP_Init
();


P1REN |= 0x04;


P1OUT |= 0x04;


P1IE |= 0x04;


MRFI_Init
();


MRFI_WakeUp
();


MRFI_RxOn
();


__
bis_SR_register
(GIE+LPM4_bits);

}

void
MRFI_RxCompleteISR
()

{


P1OUT ^= 0x02;

}

#
pragma

vector=PORT1_VECTOR

__interrupt void Port_1 (void)

{


P1IFG &= ~0x04;


mrfiPacket_t

packet;


packet.frame
[0]=8+20;


MRFI_Transmit
(&packet, MRFI_TX_TYPE_FORCED);


P1OUT ^= 0x01;

}

Change Transmission Power

#include "radios/family1/
mrfi_spi.h
"

mrfiSpiWriteReg
(PATABLE,0x
FF
);

Thomas Watteyne @ EDERC 2010

lab2_txrx_simple

26


Impact of Transmission Power

setting

power

current

0X84

-
24dBm

16.1mA

0x55

-
16dBm

16.5mA

0x97

-
10dBm

18.3mA

0xA9

-
4dBm

22.6mA

0xFE

0dBm

27.6mA

0xFF

1dBm

27.9mA

Thomas Watteyne @ EDERC 2010

27


Section 5
-

Overview

5. Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

28


Wireless Chat

[1/3]

#include "radios/family1/
mrfi_spi.h
"

#include "
mrfi.h
"

uint8_t
index_output

= 9;

mrfiPacket_t

packetToSend
;

int

main(void)

{


BSP_Init
();


MRFI_Init
();


P3SEL |= 0x30; // P3.4,5 = USCI_A0 TXD/RXD


UCA0CTL1 = UCSSEL_2; // SMCLK


UCA0BR0 = 0x41; // 9600 from 8Mhz


UCA0BR1 = 0x3;


UCA0MCTL = UCBRS_2;


UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine


IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt


MRFI_WakeUp
();


MRFI_RxOn
();


index_output
=0;


__
bis_SR_register
(GIE+LPM4_bits);

}

Enable UART

Thomas Watteyne @ EDERC 2010

lab2_txrx_chat

29


Wireless Chat

[2/3]

void
MRFI_RxCompleteISR
()

{


uint8_t
i
;


P1OUT ^= 0x02;


mrfiPacket_t

packet;


MRFI_Receive
(&packet);


char output[] = {" "};


for (
i
=9;i<29;i++) {


output[i
-
9]=
packet.frame
[
i
];


if (
packet.frame
[
i
]=='
\
r') {


output[i
-
9]='
\
n';


output[i
-
8]='
\
r';


}


}


TXString
(output, (
sizeof

output));

}

when received a
packet

Copy RXFIFO
into “packet”

write over serial port

newline

(for
PuTTY
)

Thomas Watteyne @ EDERC 2010

lab2_txrx_chat

30


Wireless Chat

[3/3]

#
pragma

vector=USCIAB0RX_VECTOR

__interrupt void USCI0RX_ISR(void)

{


char
rx

= UCA0RXBUF;


uint8_t
i
;


packetToSend.frame
[
index_output
]=
rx
;


index_output
++;


if (
rx
=='
\
r' ||
index_output
==29) {


packetToSend.frame
[0]=28;


MRFI_Transmit
(&
packetToSend
, MRFI_TX_TYPE_FORCED);


index_output

= 9;


for(
i
=9;i<29;i++) {


packetToSend.frame
[
i
]=' ';


}


P1OUT ^= 0x01;


}


P1OUT ^= 0x02;


TXString
(&
rx
, 1);

}

when received
one character
over serial port

copy serial input
buffer to “
rx


Append to the
packet being
constructed

newline or
packet full

copy to TXFIFO,
trigger
Tx

re
-
initialize

echo over serial port

Thomas Watteyne @ EDERC 2010

lab2_txrx_chat

37


Section 5
-

Overview

5. Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

38


Poor Man’s Spectrum Analyzer

[1/2]

#include "
mrfi.h
"

#include "radios/family1/
mrfi_spi.h
"

void
print_rssi
(int8_t
rssi
)

{


char output[] = {" 000 "};


if (
rssi
<0) {output[0]='
-
';
rssi
=
-
rssi
;}


output[1] = '0'+((
rssi
/100)%10);


output[2] = '0'+((
rssi
/10)%10);


output[3] = '0'+ (rssi%10);


TXString
(output, (
sizeof

output)
-
1);

}

void
MRFI_RxCompleteISR
()

{

}

prints out a
signed
int

as
characters

send string over serial

we’re not interested in
actually receiving
packets

Thomas Watteyne @ EDERC 2010

lab3_spectrum_
analyzer

39


int

main(void)

{


int8_t
rssi
;


uint8_t channel;


BSP_Init
();


MRFI_Init
();


P3SEL |= 0x30;


UCA0CTL1 = UCSSEL_2;


UCA0BR0 = 0x41;


UCA0BR1 = 0x3;


UCA0MCTL = UCBRS_2;


UCA0CTL1 &= ~UCSWRST;


MRFI_WakeUp
();


__
bis_SR_register
(GIE);


while(1) {


for (channel=0;channel<200;channel++) {


MRFI_RxIdle
();


mrfiSpiWriteReg
(
CHANNR,channel
);


MRFI_RxOn
();


rssi
=
MRFI_Rssi
();


print_rssi
(
rssi
);


}


TXString
("
\
n",1);


}

}

Poor Man’s Spectrum Analyzer

[2/2]

change channel

Enable UART

switch radio on

read current
noise level

Thomas Watteyne @ EDERC 2010

lab3_spectrum_
analyzer

40


Poor Man’s Spectrum Analyzer

spectrum.py

Thomas Watteyne @ EDERC 2010

41


Section 5
-

Overview

5. Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010

42


Wonderful Wireless Waterfall


Receiver listens


When button pressed sender sends a burst of
packets


100 packets with counter from 1 to 100


20 packets with counter to 101


When receiving a packet


If packet counter <101, increment a counter and
store the RSSI


For the first packet with counter==101, display
“average RSSI


channel success probability”

Thomas Watteyne @ EDERC 2010

43


Wonderful Wireless Waterfall
[1/4]

#include "
mrfi.h
"

uint8_t counter,
num_received
,
bool_counting
;

int16_t
cumulative_rssi
;

mrfiPacket_t

packet;

void
print_probability
(int16_t
cumulative_rssi
, uint8_t number)

{


char output[] = {" 000 0.00
\
n"};


if (
cumulative_rssi
<0) {


output[0]='
-
';


cumulative_rssi
=
-
cumulative_rssi
;


}


output[1] = '0'+((
cumulative_rssi
/100)%10);


output[2] = '0'+((
cumulative_rssi
/10)%10);


output[3] = '0'+ (cumulative_rssi%10);


output[5] = '0'+((number/100)%10);


output[7] = '0'+((number/10)%10);


output[8] = '0'+ (number%10);


TXString
(output, (
sizeof

output)
-
1);

}

e.g. “
-
55 0.75”

Prepare string

transmit string
over serial

lab4_pdr

Thomas Watteyne @ EDERC 2010

44


Wonderful Wireless Waterfall

[2/4]

int

main(void)

{


BSP_Init
();


P1REN |= 0x04;


P1IE |= 0x04;


MRFI_Init
();


mrfiSpiWriteReg
(PATABLE,0x50);


P3SEL |= 0x30;


UCA0CTL1 = UCSSEL_2;


UCA0BR0 = 0x41;


UCA0BR1 = 0x3;


UCA0MCTL = UCBRS_2;


UCA0CTL1 &= ~UCSWRST;


MRFI_WakeUp
();


MRFI_RxOn
();


__
bis_SR_register
(GIE+LPM4_bits);

}

when button is
pressed

enable serial
communication

Wait for interrupts in
low power mode

Thomas Watteyne @ EDERC 2010

lab4_pdr

45


Wonderful Wireless Waterfall

[3/4]

void
MRFI_RxCompleteISR
()

{


P1OUT ^= 0x02;


MRFI_Receive
(&packet);


counter =
packet.frame
[9];


if (counter==101) {


if (
bool_counting

== 1) {


print_probability
(
cumulative_rssi
/
num_received,num_received
);


}


bool_counting
=0;


num_received
=0;


cumulative_rssi
=0;


} else {


bool_counting
=1;


num_received
++;


cumulative_rssi
+=(int8_t)
packet.rxMetrics
[0];


}

}

When I receive a
packet

Thomas Watteyne @ EDERC 2010

lab4_pdr

46


Wonderful Wireless Waterfall

[4/4]

#
pragma

vector=PORT1_VECTOR

__interrupt void
interrupt_button

(void)

{


P1IFG &= ~0x04;


P1OUT ^= 0x01;


mrfiPacket_t

packet;


packet.frame
[0]=8+3;


for (counter=1;counter<101;counter++){


packet.frame
[9]=counter;


MRFI_Transmit
(&packet, MRFI_TX_TYPE_FORCED);


}


for (counter=0;counter<20;counter++){


packet.frame
[9]=101;


MRFI_Transmit
(&packet, MRFI_TX_TYPE_FORCED);


}

}

when button is
pressed

Thomas Watteyne @ EDERC 2010

lab4_pdr

47


Wonderful Wireless Waterfall

pdr_vs_rssi.py

Thomas Watteyne @ EDERC 2010

48


Section 5
-

Overview

5.
Programming Tour

5.1


Crash
-
course on the Hardware

5.2


Development Environment

5.3


Blinking the LEDs

5.4


Enabling Wireless

5.5


Wireless Chat

5.6


The Importance of CRC

5.7


Poor Man’s Spectrum Analyzer

5.8


Wonderful Wireless Waterfall

Thomas Watteyne @ EDERC 2010