Pumpkin, Inc.

Pumpkin User Forums

Example of Serial Receive w/Salvo

If you can't make Salvo do what you want it to do, post it here.

Example of Serial Receive w/Salvo

Postby aek » Thu Jan 10, 2008 10:07 am

Here is a simple example of how one can do single-character event-driven reception in Salvo.

The ISR picks up an incoming character and places it into a ring buffer, and signals a semaphore to indicate to the OS that a character has been received.

The task waits (indefinitely) for the semaphore to be signaled. For each signaling of the semaphore, the task becomes eligible, runs when it's the highest-priority task, processes the command, and then returns to the waiting state.

This example is for the MSP430, but is not target-specific in terms of the Salvo components.

code:
#pragma vector=USART1RX_VECTOR

__interrupt void ISRRx1 (void)

{

USART_UART1_inchar();

OSSignalSem(SEM_CMD_CHAR_P);

__low_power_mode_off_on_exit();

}


and
code:
#include "main.h"

#include <ctype.h>

#include <salvo.h>

#define STR_TASK_CMD "task_cmd:"

static

void list_cmds( void )

{

user_msg(STR_TASK_CMD STR_2TABS "Commands: {h|?, +, -, 1, d, f, g, i,");

user_msg(STR_TASK_CMD STR_2TABS " m, p, r, s, v, w, x, z}");

}

static

void explain_cmds( void )

{

user_msg(STR_TASK_CMD STR_2TABS "h|?: Expanded command listing");

user_msg(STR_TASK_CMD STR_2TABS "-: Set DIRECTION for forward");

user_msg(STR_TASK_CMD STR_2TABS "+: Set DIRECTION for backward");

user_msg(STR_TASK_CMD STR_2TABS "1: Single step");

user_msg(STR_TASK_CMD STR_2TABS "d: start task_demo");

user_msg(STR_TASK_CMD STR_2TABS "f: Start/stop task_freq()");

user_msg(STR_TASK_CMD STR_2TABS "g: Start/stop task_gps()");

user_msg(STR_TASK_CMD STR_2TABS "i: Display information");

user_msg(STR_TASK_CMD STR_2TABS "m: Start/stop task_move()");

user_msg(STR_TASK_CMD STR_2TABS "p: Start/stop task_pwm()");

user_msg(STR_TASK_CMD STR_2TABS "r: Restart");

user_msg(STR_TASK_CMD STR_2TABS "s: Start/stop task_step()");

user_msg(STR_TASK_CMD STR_2TABS "v: List software version");

user_msg(STR_TASK_CMD STR_2TABS "w: Start/stop task_wait()");

user_msg(STR_TASK_CMD STR_2TABS "x: Stop tasks, freq(0) & pwm(0)");

user_msg(STR_TASK_CMD STR_2TABS "z: Sleep");

}

/************************************************************

**** ****

** **

task_cmd()

** **

**** ****

************************************************************/

void task_cmd (void)

{

static int cmd;

user_msg(STR_TASK_CMD STR_2TABS "Starting.");

list_cmds();

while (1) {

OS_WaitSem(SEM_CMD_CHAR_P, OSNO_TIMEOUT);

if (cmd = getchar1()) {

switch (tolower((char) cmd)) {

// Help.

case 'h':

case '?':

explain_cmds();

break;

// Set DIRECTION for backwards motion.

case '-':

if (set_direction(DIRECTION_BACKWARD) == 0) {

user_msg(STR_TASK_CMD STR_2TABS "-:" " DIRECTION = backwards.");

}

break;

// Help.

case '+':

if (set_direction(DIRECTION_FORWARD) == 0) {

user_msg(STR_TASK_CMD STR_2TABS "+:" " DIRECTION = forwards.");

}

break;

// Help.

case '1':

step (get_direction());

user_msg(STR_TASK_CMD STR_2TABS "1:" " Stepped once.");

break;

// Start task_demo().

case 'd':

if (OSTaskStopped(TASK_DEMO_P))

{

OSStartTask(TASK_DEMO_P);

user_msg(STR_TASK_CMD STR_2TABS "d:" " Started task_demo().");

}

break;

// Start/stop task_freq().

case 'f':

if (OSTaskStopped(TASK_FREQ_P))

{

OSStartTask(TASK_FREQ_P);

user_msg(STR_TASK_CMD STR_2TABS "f:" " Started task_freq().");

}

else

{

OSStopTask(TASK_FREQ_P);

user_msg(STR_TASK_CMD STR_2TABS "f:" " Stopped task_freq().");

}

break;

// Start/stop task_gps().

case 'g':

if (OSTaskStopped(TASK_GPS_P))

{

OSStartTask(TASK_GPS_P);

user_msg(STR_TASK_CMD STR_2TABS "g:" " Started task_gps().");

}

else

{

OSStopTask(TASK_GPS_P);

user_msg(STR_TASK_CMD STR_2TABS "g:" " Stopped task_gps().");

}

break;

// Information.

case 'i':

sprintf(strTmp, STR_TASK_CMD STR_2TABS "position: %6lu, direction: %+d",

get_position(), get_direction());

user_msg(strTmp);

break;

// Start/stop task_move().

case 'm':

if (OSTaskStopped(TASK_MOVE_P))

{

OSStartTask(TASK_MOVE_P);

user_msg(STR_TASK_CMD STR_2TABS "m:" " Started task_move().");

}

else

{

OSStopTask(TASK_MOVE_P);

user_msg(STR_TASK_CMD STR_2TABS "m:" " Stopped task_move().");

}

break;

// Start/stop task_pwm().

case 'p':

if (OSTaskStopped(TASK_PWM_P))

{

OSStartTask(TASK_PWM_P);

user_msg(STR_TASK_CMD STR_2TABS "p:" " Started task_pwm().");

}

else

{

OSStopTask(TASK_PWM_P);

user_msg(STR_TASK_CMD STR_2TABS "p:" " Stopped task_pwm().");

}

break;

// Reset.

case 'r':

user_msg(STR_TASK_CMD STR_2TABS "r:" " Reset (via WDT) in 1 s.");

OS_Delay(100);

WDTCTL = 0xDEAD;

break;

// Start/stop task_step().

case 's':

if (OSTaskStopped(TASK_STEP_P))

{

OSStartTask(TASK_STEP_P);

user_msg(STR_TASK_CMD STR_2TABS "s:" " Started task_step().");

}

else

{

OSStopTask(TASK_STEP_P);

user_msg(STR_TASK_CMD STR_2TABS "s:" " Stopped task_step().");

}

break;

// Tell version.

case 'v':

user_msg(STR_TASK_CMD STR_2TABS "v: v" STR_VERSION);

break;

// Start/stop task_wait().

case 'w':

if (OSTaskStopped(TASK_WAIT_P))

{

OSStartTask(TASK_WAIT_P);

user_msg(STR_TASK_CMD STR_2TABS "w:" " Started task_wait().");

}

else

{

OSStopTask(TASK_WAIT_P);

user_msg(STR_TASK_CMD STR_2TABS "w:" " Stopped task_wait().");

}

break;

// All Stop.

case 'x':

OSStopTask(TASK_FREQ_P);

OSStopTask(TASK_MOVE_P);

OSStopTask(TASK_PWM_P);

OSStopTask(TASK_STEP_P);

OSStopTask(TASK_WAIT_P);

freq(0);

pwm(0);

user_msg(STR_TASK_CMD STR_2TABS "x:" " Stopped FREQ, MOVE, PWM, STEP & WAIT tasks.");

user_msg(STR_TASK_CMD STR_2TABS " " " freq(0). pwm(0)");

break;

// Go into LPM4 (permanently).

case 'z':

user_msg(STR_TASK_CMD STR_2TABS "z:" " Full shutdown (LPM4) in 2 s.");

// Stop all tasks but this one. OK to

// use <cmd> since we'll sleep before

// exiting this case.

for (cmd=2; cmd <= OSTASKS; cmd++) {

OSStopTask(OSTCBP(cmd));

}

// Wait a while to flush RS-232, etc.

OS_Delay(100);

// Final message.

user_msg(STR_TASK_CMD STR_2TABS "z:" " System is now shut down in LPM4.");

OS_Delay(100);

// Shut everything down.

shutdown();

break;

// Bad command.

default:

sprintf(strTmp, STR_TASK_CMD STR_2TABS "Unknown command: '%c' (0x%x)", cmd, cmd);

user_msg(strTmp);

break;

} /* switch */

} /* if */

} /* while */

}


------------------

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

Return to Coding

Who is online

Users browsing this forum: No registered users and 3 guests

cron