310 likes | 536 Vues
NesC 프로그래밍 실습 I. 1. Booting, LED 2. Timer 사용 (Blink) 3. Timer 2 개 이상. Basic : Lesson.1. Requirements : 1) 모든 LED 를 초기화한다 . 2) 모든 LED 를 ON 한다 . ** Red, Yellow, Green LED (in tos/interface/) - make telosb docs
E N D
NesC 프로그래밍 실습 I 1. Booting, LED 2. Timer 사용 (Blink) 3. Timer 2개 이상
Basic : Lesson.1 • Requirements : 1) 모든 LED를 초기화한다. 2) 모든 LED를 ON 한다. ** Red, Yellow, Green LED (in tos/interface/) - make telosb docs ( in /doc/nesdoc/telosb/ )
Test.nc configuration Test{ } implementation{ components Main, TestM, LedsC; Main.StdControl -> TestM; TestM.Leds ->LedsC; }
TestM.nc module TestM{ provides { interface StdControl; } uses { interface Leds; } } implementation{ command result_t StdControl.init() { call Leds.init(); return SUCCESS; } command result_t StdControl.start() { call Leds.redOn(); call Leds.greenOn(); call Leds.yellowOn(); return SUCCESS; } command result_t StdControl.stop() { return SUCCESS; } }
Basic : Lesson. 2 TimerC.timer[unique(“Timer”)]
Test.nc configuration Test{ } implementation{ components Main, TestM, LedsC, TimerC; Main.StdControl -> TestM.StdControl; Main.StdControl -> TimerC. StdControl TestM.Leds ->LedsC.Leds; TestM.Timer -> TimerC.Timer[unique(“Timer”)] }
TestM.nc command result_t StdControl.start() { call Timer.start(TIMER_REPEAT, 1000); return SUCCESS; } commandresult_t StdControl.stop() { call Timer.stop(); return SUCCESS; } eventresult_t Timer.fired(){ call Led.redToggle(); return SUCCESS; } } module TestM{ provides { interface StdControl; } uses { interface Leds; interface Timer; } } implementation{ command result_t StdControl.init() { call Leds.init(); return SUCCESS; }
Basic : Lesson. 3 • Timer 2개 이상인 경우 Test.nc 에서 TimerC.Timer[unique(“Timer”)] TimerC.Timer2[unique(“Timer”)] TestM.nc 에서 Interface Timer as Timer2 event추가
Test.nc configuration Test{ } implementation{ components Main, TestM, LedsC, TimerC; Main.StdControl -> TestM.StdControl; Main.StdControl -> TimerC. StdControl TestM.Leds -> LedsC.Leds; TestM.Timer -> TimerC.Timer[unique(“Timer”)] TestM.Timer2 -> TimerC.Timer[unique(“Timer”)] }
TestM.nc command result_t StdControl.start() { call Timer.start(TIMER_REPEAT, 1000); call Timer2.start(TIMER_REPEAT,2000); return SUCCESS; } command result_t StdControl.stop() { call Timer.stop(); return SUCCESS; } Event result_t Timer.fired(){ call Led.redToggle(); return SUCCESS; } Event result_t Timer2.fired(){ call Led.redToggle(); return SUCCESS; } } module TestM{ provides { interface StdControl; } uses { interface Leds; interface Timer; interface Timer as Timer2 } } Implementation { command result_t StdControl.init() { call Leds.init(); return SUCCESS; }
NesC 프로그래밍 실습 II 4. MSP430GeneralIO 5. MSP430Inturrupt 6. UART 통신
Basic : Lesson. 4 • Requirements : • MSP430GeneralIOC component를 사용하여 LED를 제어하는 • 프로그램 작성 • - MSP430GeneralIO interface 사용 • (in tos/platform/msp430/) • - Msp430GeneralIOC Component 이용 • (in tos/platform/msp430/)
Test.nc configurationTest{ } implementation{ components Main, TestM, LedsC, MSP430GeneralIOC; Main.StdControl -> TestM.StdControl; TestM.Leds ->LedsC.Leds; TestM.MSP430GeneralIO -> MSP430GeneralIOC.Port54 }
TestM.nc module TestM { provides { interface StdControl; } uses { interface MSP430GeneralIO; } } implementation { command result_t StdControl.init() { call Leds.init(); call MSP430GeneralIO.makeOutput(); call MSP430GeneralIO.setHigh(); return SUCCESS; } commandresult_t StdControl.start() { call MSP430GeneralIO.setLow(); return SUCCESS; } commandresult_t StdControl.stop() { return SUCCESS; } }
Basic : Lesson. 5 Requirements : 1) LED를 초기화한다. 2) User 버튼(Port27)을 누르면, Red LED가 Toggle 한다. Msp430InterruptC Component 이용 (in tos/platform/msp430/)
chattering call MSP430Interrupt.enable(); call MSP430Interrupt.disable; call MSP430Interrupt.clear(); call Leds.redToggle(); // do something call MSP430Interrupt.enable();
Test.nc configurationTest{ } implementation{ components Main, TestM, LedsC, MSP430InterruptC; Main.StdControl -> TestM.StdControl; TestM.Leds ->LedsC.Leds; TestM.MSP430Interrupt -> MSP430InterruptC.Port27 }
TestM.nc command result_t StdControl.start() { return SUCCESS; } command result_t StdControl.stop() { return SUCCESS; } async event void MSP430Interrupt.fired(){ call MSP430Interrupt.disable(); call MSP430Interrupt.clear(); call Leds.redToggle(); call MSP430Interrupt.enable(); } } module TestM{ provides { interface StdControl; } uses { interface Leds; interface MSP430Interrupt; } } implementation{ command result_t StdControl.init() { call Leds.init(); call MSP430Interrupt.enable(); call MSP430Interrupt.edge(TRUE); return SUCCESS; }
Basic : Lesson. 6 • Requirements : • UART component 를 사용하여 인터럽트 발생시 0x47를 전송하는 • 프로그램 작성 • -ByteComm interface 사용 • (in tos/interface/) • -Msp430InterruptC Component 이용 • (in tos/platform/msp430/)
Test.nc configurationTest{ } implementation{ components Main, TestM, LedsC, MSP430InterruptC, UART; Main.StdControl -> TestM.StdControl; Main.StdControl -> UART.Control; TestM.Leds ->LedsC.Leds; TestM.MSP430Interrupt -> MSP430InterruptC.Port27 TestM.ByteComm ->UART.ByteComm; }
TestM.nc async event void MSP430Interrupt.fired(){ call MSP430Interrupt.disable(); call MSP430Interrupt.clear(); call Leds.redToggle(); call MSP430Interrupt.enable(); // do something call ByteComm.txByte(0x47); // 0x47 means 'G' } async event result_t ByteComm.rxByteReady(uint8_t data, bool error, uint16_t strength){ // RX occurs, do something call ByteComm.txByte(data); call Leds.yellowToggle(); return SUCCESS; } async event result_t ByteComm.txByteReady(bool success){ // Second, third, fourth..... byte tx via UART // such as call ByteComm.txByte(0x47); call Leds.greenToggle(); return SUCCESS; } async event result_t ByteComm.txDone(){ return SUCCESS; } module TestM { provides { interface StdControl; } uses { interface Leds; interface ByteComm; interface MSP430Interrupt; } } implementation { command result_t StdControl.init() { call Leds.init(); call MSP430Interrupt.enable(); call MSP430Interrupt.edge(FALSE); return SUCCESS; } command result_t StdControl.start() { return SUCCESS; } command result_t StdControl.stop() { return SUCCESS; }
NesC 프로그래밍 실습 III 7. GenericComm
Basic : Lesson. 7 • Requirements : • 인터럽트 발생하면 데이터 송수신 • - GenericComm component 사용 • (in tos/system/) • - Msp430InterruptC Component 이용 • (in tos/platform/msp430/) • - AM packet (in tos/platform/telos/) • - make 파일에 PFLAGS += -v 옵션을 주면 컴파일하는 파일을 모두 보여줌
Test.nc configurationTest{ } implementation{ components Main, TestM, LedsC, MSP430InterruptC, GenericComm; Main.StdControl -> TestM.StdControl; TestM.Leds ->LedsC.Leds; TestM.MSP430Interrupt -> MSP430InterruptC.Port27 TestM.SendMsg -> GenericComm.SendMsg[1]; TestM.ReceiveMsg -> GenericComm.ReceiveMsg[1]; }
TestM.nc module TestM { provides { interface StdControl; } uses { interface Leds; interface MSP430Interrupt; interface SendMsg; interface ReceiveMsg; } } implementation { command result_t StdControl.init() { call Leds.init(); call MSP430Interrupt.enable(); call MSP430Interrupt.edge(FALSE); return SUCCESS; } command result_t StdControl.start() { return SUCCESS; } command result_t StdControl.stop() { return SUCCESS; } TOS_Msg test_msg; async event void MSP430Interrupt.fired(){ call MSP430Interrupt.disable(); call MSP430Interrupt.clear(); call Leds.redToggle(); call MSP430Interrupt.enable(); call SendMsg(TOS_BCAST_ADDR, 28, &test_msg); } event result_t SendMsg,sendDone(TOS_MsgPtr msg,result_t success){ call Leds.yellowToggle(); return SUCCESS; } event TOS_MsgPtr ReceiveMsg.receive(TOS_MsgPtr m){ call Leds.greenToggle(); return m; }
SendMsg 2개 이상인 경우 • TestM.SendMsg -> GenericComm.SendMsg[1]; • TestM.ReceiveMsg -> GenericComm.ReceiveMsg[1]; • TestM.DeltaSendMsg -> GenericComm.SendMsg[2]; • TestM.DeltaReceiveMsg -> GenericComm.ReceiveMsg[2];
Test.nc configurationTest{ } implementation{ components Main, TestM, LedsC, MSP430InterruptC, GenericComm; Main.StdControl -> TestM.StdControl; TestM.Leds ->LedsC.Leds; TestM.MSP430Interrupt -> MSP430Interrupt.Port27 TestM.SendMsg -> GenericComm.SendMsg[1]; TestM.ReceiveMsg -> GenericComm.ReceiveMsg[1]; TestM.DeltaSendMsg -> GenericComm.SendMsg[2]; TestM.DeltaReceiveMsg -> GenericComm.ReceiveMsg[2]; }
TestM.nc command result_t StdControl.stop() { return SUCCESS; } TOS_Msg test_msg; async event void MSP430Interrupt.fired(){ call MSP430Interrupt.disable(); call MSP430Interrupt.clear(); call Leds.redOn(); call MSP430Interrupt.enable(); call SendMsg(TOS_BCAST_ADDR, 28, &test_msg); // call DeltaSendMsg(TOS_BCAST_ADDR, 28, &test_msg); } event result_t SendMsg,sendDone(TOS_MsgPtr msg,result_t success){ call Leds.redOff(); return SUCCESS; } event TOS_MsgPtr ReceiveMsg.receive(TOS_MsgPtr m){ call Leds.greenToggle(); return m; } event result_t DeltaSendMsg,sendDone(TOS_MsgPtr msg,result_t success){ call Leds.redOff(); return SUCCESS; } event TOS_MsgPtr DeltaReceiveMsg.receive(TOS_MsgPtr m){ call Leds.yellowToggle(); return m; } module TestM { provides { interface StdControl; } uses { interface Leds; interface MSP430Interrupt; interface SendMsg; interface ReceiveMsg; interface SendMsg as DeltaSendMsg; interface ReceiveMsg as DeltaSendMsg; } } implementation { command result_t StdControl.init() { call Leds.init(); call MSP430Interrupt.enable(); call MSP430Interrupt.edge(FALSE); return SUCCESS; } command result_t StdControl.start() { return SUCCESS; }