seminar 5 msp430

Upload: nguyen-dinh

Post on 03-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Seminar 5 MSP430

    1/24

    Programming embedded systems

    Seminar 5

    EmbeddedOperating System

    Dr. Tran Thanh HungDepartment of Automation Technology,

    College of Engineering, Can Tho University

    Email: [email protected]

  • 8/12/2019 Seminar 5 MSP430

    2/24

    Review

    In the previous seminars, we use a superloop to run a task.

    In this seminar, we will use a different way torun a task.

  • 8/12/2019 Seminar 5 MSP430

    3/24

    Outline

    How to create an Embedded OS forembedded applications?

    Periodic functions Implement periodic functions

    Creating Embedded OS Using Embedded OS

  • 8/12/2019 Seminar 5 MSP430

    4/24

    Seminar objectives

    At the end of this seminar, by referring thelecture notes, students will be able to:

    understand issue of periodic function

    use timer to create a simple Embedded OS(EOS)

    implement periodic functions running in EOS

  • 8/12/2019 Seminar 5 MSP430

    5/24

    How to run a task?

    We has used a super loop architecture to run a task void main (void ){

    X_Init() ; //Prepare for Task X()while (1) //super loop

    {X(); //Perform task X()

    }}

    With this architecture, it is very difficult to run the taskat a precise interval of time.

  • 8/12/2019 Seminar 5 MSP430

    6/24

    Periodic function

    There are many applications require to run tasksrepeatedly, at precise intervals

    Keypad need to be scanned every 200ms The display (PC monitor, LCD) must be

    refreshed 60 times every second Music must be sampled at 44.100 times per

    second Clock must update one time per secondIn practice, many embedded systems must be

    able to support this type of periodic function

  • 8/12/2019 Seminar 5 MSP430

    7/24

    Periodic function

    If you need to run the function X() every 50 ms, how doyou implement?

    If you know the function takes 10 ms to complete

    while (1) //super loop {

    X(); //Call the functiondelay(40); //Delay for 40 ms

    }

    This approach will be fine, if:1. We know the precise duration of X()2. This duration never varies

  • 8/12/2019 Seminar 5 MSP430

    8/24

    Periodic function

    while (1) //super loop {

    X(); //Call the function (10 ms duration)delay(40); //Delay for 40 ms

    }

    However,1. It is very difficult to know the duration of X()2. This duration often vary3. This approach is in inefficient: waste in a delay loop

    Is there any way to implement periodic function, runningat precise 50 ms interval ?

  • 8/12/2019 Seminar 5 MSP430

    9/24

    Timer delay: review

    void delay_tA(unsigned int x){ TACCR0 = x; // set delay value of Timer_A

    setbit(TACTL, (TASSEL1 + MC0)) ; //TASSEL = 02, MC = 01while(bit_is_clear(TACTL, TAIFG)) ; //wait Timer_A overflow (TAIFG = 1)

    clearbit(TACTL ,(MC0+TAIFG)); //stop Timer}

    Note: Time for 1 count: T = 1/F = Prescaler /f CLK Instead of waiting for timer overflow, you can setup

    an interrupt from timer

  • 8/12/2019 Seminar 5 MSP430

    10/24

    Example: Timer interrupt setup

    Chy bi tp 4.3 Xem Timer0_A3 t mt breakpoint dng lnh delay_tA

    Chy chng trnh n khi dng delay_tA Chy chng trnh n khi timer trn CPU thc hin lnh u sau khi timer trn? t bit TAIE trong TACTL v GIE trong Core

    Registers\SR t bit TAIE trong TACTL ln 1 Chy chng trnh n khi dng delay_tA Chy chng trnh n khi timer trn CPU thc hin lnh u sau khi timer trn?

  • 8/12/2019 Seminar 5 MSP430

    11/24

    Timer interrupt setup

    void delay_tA(unsigned int x){ TACCR0 = x; // set delay value of Timer_A

    setbit(TACTL, (TASSEL1 + MC0)) ; //TASSEL = 02, MC = 01

    while(bit_is_clear(TACTL, TAIFG)) ; //wait Timer_A overflow (TAIFG = 1)clearbit(TACTL ,(MC0+TAIFG)); //stop Timer}

    Instead of waiting for timer overflow, we can set up an interrupt:

    void timer_interrupt_init (unsigned int x){ TACCR0 = x; // set interval value of Timer_A

    _enable_interrupt();//Enable general interrupt (set GIE)setbit(TACTL, (TASSEL1 + MC0+ TAIE )) ; //Enable timer interrupt

    }

  • 8/12/2019 Seminar 5 MSP430

    12/24

    Implement periodic function

    Instead of writing a program like this:

    void main (void ){

    X_Init() ; //Prepare for Task X()while (1) //super loop

    {X(); //Call the function (10 ms duration)delay(40); //Delay for 40 ms

    }

    We use a better way to do:

  • 8/12/2019 Seminar 5 MSP430

    13/24

    Implement periodic function

    //file timer_interrupt.c

    #include void timer_interrupt_init (unsigned int x){ TACCR0 = x; // set interval value of Timer_A

    _enable_interrupt(); // Enable general interruptsetbit(TACTL, (TASSEL1 + MC0+ TAIE )) ; //Enable timer interrupt

    }#pragma vector = TIMER0_A1_VECTOR

    interrupt void timer_ISR(void){

    X(); //Call the function }

  • 8/12/2019 Seminar 5 MSP430

    14/24

    Implement periodic function

    //file main.c

    void main ( void ){ X_Init() ; //Prepare for Task X()

    timer_interrupt_init (50000); //initialize timer to interrupt every 50 mswhile (1); //empty loop

    }//file timer_interrupt.c#pragma vector = TIMER0_A1_VECTOR

    interrupt void timer_ISR(void){

    X(); //Call the function

    }

  • 8/12/2019 Seminar 5 MSP430

    15/24

    Bi tp 5.1

    S dng cc hm timer_interrupt_init () vtimer_ISR (), vit chng trnh chp tt:

    - LED ni vi P1.7 tn s 10Hz (50ms sng, 50ms

    tt) - LED ni vi P1.6 tn s 2Hz (250ms sng,

    250ms tt) - LED ni vi P1.5 tn s 1Hz (500ms sng,

    500ms tt) - LED ni vi P1.4 tn s 0.5Hz (1s sng, 1s tt)

    Tn s chp tt c ng nh tnh ton?

  • 8/12/2019 Seminar 5 MSP430

    16/24

    Problem with exercise 5.1?

    1. Chy bi tp 5.1 cho n khi xy ra ngt 2. Chy tng bc, xem gi tr TAIFG khi CPU

    ang thc hin timer_ISR Gi tr TAIFG? Khi thc hin xong timer_ISR,

    iu g xy ra?3. Lp li bc 1, xa TAIFG khi CPU ang thc

    hin timer_ISR 4. Lp li bc 2 Khi thc hin xong timer_ISR, iu g xy ra?

  • 8/12/2019 Seminar 5 MSP430

    17/24

    Implement periodic function:Problem

    void main ( void ){ X_Init() ; //Prepare for Task X()

    timer_interrupt_init (50000); //initialize timer to interrupt every 50 mswhile (1); //empty loop

    }#pragma vector = TIMER0_A1_VECTOR

    interrupt void timer_ISR(void){

    X(); //Call the function }

    Sometime you forget to clear the timer flag. This approach is not adequate if the system requiresvery precise timing

    ClearTAIFG

  • 8/12/2019 Seminar 5 MSP430

    18/24

    Timer_A: Compare mode

    Chy tng bc bi tp 5.1 cho n khi CPUthc hin xong hm timer_interrupt_init

    Trong TACTL, xa bit TAIE Trong TACCTL0, t bit CCIE t gi tr TAR nh hn TACCR0 4 n v Chy tng bc (assembly) cho n khi TAR

    bng TACCR0 iu g xy ra sau ?

  • 8/12/2019 Seminar 5 MSP430

    19/24

    Embedded OS: Initialize/******************************************************************************

    //file EOS.c* @fn EOS_init (unsigned int x)* @brief Initialize for Embedded Operating System:* - timer interrupt in next x microsecond

    * - no reload required* @param x : number of microsecond* @return void

    */void EOS_init(unsigned int x){ TACCR0 = x; // set interval value of Timer_A

    _enable_interrupt ();// Enable general interruptsetbit(TACCTL0, CCIE); // Enable timer interrupt (compare)

    setbit(TACTL, (TASSEL1 + MC0)) ; // Start timer

    }

  • 8/12/2019 Seminar 5 MSP430

    20/24

    Embedded OS: Implement

    //file main.c

    void main ( void ){ Your_function _Init() ; //Initialize for Your_function

    EOS_init (50000); //initialize timer 2 to interrupt every 50 ms

    while (1); //empty loop }

    //file EOS.c #pragma vector = TIMER0_A0_VECTOR

    interrupt void EOS_ISR(void){

    Your_function(); //This function will be called every 50 ms}

    Problem ?

  • 8/12/2019 Seminar 5 MSP430

    21/24

    Embedded OS: Implement

    void main ( void ){ Your_function _Init() ; //Initialize for Your_function

    EOS_init (50000); //initialize timer 2 to interrupt every 50 mswhile (1) // {

    LPM0; // Or LPM1: go to sleep}

    }#pragma vector = TIMER0_A0_VECTOR

    interrupt void EOS_ISR(void){

    Your_function(); //This function will be called every 50 ms}

  • 8/12/2019 Seminar 5 MSP430

    22/24

    Saving power

  • 8/12/2019 Seminar 5 MSP430

    23/24

    Bi tp 5.2

    t hm EOS_init() and EOS_ISR() vo fileEOS.c v EOS.h

    S dng h iu hnh nhng (EOS), vit liBi tp 4.4 vi yu cu nh sau:

    - Hin th Gi / Pht / Giy /x ra cc LED ty theocc phm SW0/SW1/SW2/SW3 c bm

    (nh phm) - Tng/gim Gi / Pht / Giy /x (ang c hinth) nu phm SW12/SW15 c bm(khngnh phm)

  • 8/12/2019 Seminar 5 MSP430

    24/24

    Lu khi s dng EOS

    Gii hn ca hm EOS_int (thi gian tia gia 2 ln ngt)

    Thi gian thc hin EOS_ISR phi nhhn thi gian gia 2 ln ngt (Phi thotkhi chng trnh phc v ngt cngnhanh cng tt)