Выбрать главу

 rtos_msg_send() receives a task name and a byte as arguments. The function sends the byte to the specified task, where it is placed in the task’s message queue.

 rtos_msg_read() reads the byte located in the task’s message queue.

 rtos_msg_ poll() returns true if there is data in the task’s message queue. This function should be called before reading a byte from the task’s message queue.

 rtos_signal() receives a semaphore name and increments that semaphore.

 rtos_wait() receives a semaphore name and waits for the resource associated with the semaphore to become available. The semaphore count is then decremented so the task can claim the resource.

 rtos_await() receives an expression as an argument, and the task waits until the expression evaluates to true.

 rtos_overrun() receives a task name as an argument, and the function returns true if that task has overrun its allocated time.

 rtos_stats() returns the specified statistics about a specified task. The statistics can be the minimum and maximum task run times and the total task run time. The task name and the type of statistics are specified as arguments to the function.

10.5.1 Preparing for RTOS

In addition to the preceding functions, the #use rtos() preprocessor command must be specified at the beginning of the program before calling any of the RTOS functions. The format of this preprocessor command is:

#use rtos (timer=n, minor_cycle=m)

where timer is between 0 and 4 and specifies the processor timer that will be used by the RTOS, and minor_cycle is the longest time any task will run. The number entered here must be followed by s, ms, us, or ns.

In addition, a statistics option can be specified after the minor_cycle option, in which case the compiler will keep track of the minimum and maximum processor times the task uses at each call and the task’s total time used.

10.5.2 Declaring a Task

A task is declared just like any other C function, but tasks in a multi-tasking application do not have any arguments and do not return any values. Before a task is declared, a #task preprocessor command is needed to specify the task options. The format of this preprocessor command is:

#task(rate=n, max=m, queue=p)

where rate specifies how often the task should be called. The number specified must be followed by s, ms, us, or ns. max specifies how much processor time a task will use in one execution of the task. The time specified here must be equal to or less than the time specified by minor_cycle. queue is optional and if present specifies the number of bytes to be reserved for the task to receive messages from other tasks. The default value is 0.

In the following example, a task called my_ticks is every 20ms and is expected to use no more than 100ms of processor time. This task is specified with no queue option:

#task(rate=20ms, max=100ms)

void my_ticks() {

 ...........

 ...........

}

PROJECT 10.1 — LEDs 

In the following simple RTOS-based project, four LEDs are connected to the lower half of PORTB of a PIC18F452-type microcontroller. The software consists of four tasks, where each task flashes an LED at a different rate:

• Task 1, called task_B0, flashes the LED connected to port RB0 at a rate of 250ms.

• Task 2, called task_B1, flashes the LED connected to port RB1 at a rate of 500ms.

• Task 3, called task_B2, flashes the LED connected to port RB2 once a second.

• Task 4, called task_B3, flashes the LED connected to port RB3 once every two seconds.

Figure 10.7 shows the circuit diagram of the project. A 4MHz crystal is used as the clock. PORTB pins RB0–RB3 are connected to the LEDs through current limiting resistors.

Figure 10.7: Circuit diagram of the project

The software is based on the CCS C compiler, and the program listing (RTOS1.C) is given in Figure 10.8. The main program is at the end of the program, and inside the main program PORTB pins are declared as outputs and RTOS is started by calling function rtos_run().

////////////////////////////////////////////////////////////////////////

//

//                       SIMPLE RTOS EXAMPLE

//                ----------------------------------

//

// This is a simple RTOS example. 4 LEDs are connected to lower half of

// PORTB of a PIC18F452 microcontroller. The program consists of 4

// tasks:

//

// Task task_B0 flashes the LED connected to port RB0 every 250ms.

// Task task_B1 flashes the LED connected to port RB1 every 500ms.

// Task task_B2 flashes the LED connected to port RB2 every second

// Task task_B3 flashes the LED connected to port RB3 every 2 seconds.

//

// The microcontroller is operated from a 4MHz crystal

//

// Programmer: Dogan Ibrahim

// Date: September, 2007

// File: RTOS1.C

//

////////////////////////////////////////////////////////////////////////

#include "C:\NEWNES\PROGRAMS\rtos.h"

#use delay (clock=4000000)

//

// Define which timer to use and minor_cycle for RTOS

//

#use rtos(timer=0, minor_cycle=10ms)

//

// Declare TASK 1 - called every 250ms

//

#task(rate=250ms, max=10ms)

void task_B0() {

 output_toggle(PIN_B0); // Toggle RB0

}

//

// Declare TASK 2 - called every 500ms

//

#task(rate=500ms, max=10ms)

void task_B1() {

 output_toggle(PIN_B1); // Toggle RB1

}

//

// Declare TASK 3 - called every second

//

#task(rate=1s, max=10ms)

void task_B2() {

 output_toggle(PIN_B2); // Toggle RB2

}

//

// Declare TASK 4 - called every 2 seconds

//

#task(rate=2s, max=10ms)

void task_B3() {

 output_toggle(PIN_B3); // Toggle RB3

}