Page MenuHomec4science

command.c
No OneTemporary

File Metadata

Created
Tue, Nov 26, 19:51

command.c

/*
command.c
Copyright (C) 2011 Rico Moeckel <rico.moeckel at epfl dot ch>,
Michiel D'Haene <michiel.dhaene at gmail dot com>,
EPFL Biorobotics Laboratory (http://biorob.epfl.ch)
EPFL Ecole Polytechnique Federale de Lausanne (http://www.epfl.ch)
ADD LAB from Michiel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
\defgroup command COMMAND
\brief Command handling.
This module is responsible for handling and execution of commands.
All commands that should be automatically executed when found in an SBCP packet
need to be registered to the array sbcp_cmd_tbl[].
\author Rico Moeckel, Michiel D'Haene
\version 1.0
\date 2011-04-14
*/
/*@{*/
/** \file command.c
\brief Command handling.
*/
#include "command.h"
#include <p33Fxxxx.h>
#include "config.h"
#include "sbcp.h"
#include "port.h"
#include "uart.h"
#include "dma_sbcp.h"
#include "led.h"
/*************************************************************************
*
* VARIABLES AND STRUCTS
*
*************************************************************************/
/// \brief command table
///
/// Register your command functions according to their instruction number
/// in this table.
/// IMPORTANT: commands have to be placed as strings in " ".
const tSBCP_CmdTbl sbcp_cmd_tbl[]= {
{command_firmware, "firmware version"}, ///< dealing with LED
{command_get_register, "get a parameter"}, ///< print all entries of command table
{command_set_register, "set a parameter"}, ///< general purpose test function
{command_led, "toggle led"} ///< inquire name and version number of current firmware
};
const int SBCP_COMMAND_TABLE_LENGTH = (sizeof(sbcp_cmd_tbl)/sizeof(tSBCP_CmdTbl)); ///< stores number of entries in command table
/// Type definition of struct storing the local application variables for the command module
//typedef struct {
// unsigned char message[SBCP_MAX_MESSAGE_LENGTH]; ///< buffer for creating output messages
//}tCommand_AppVar;
//static tCommand_AppVar CommandVar; ///< container for local application variables; static makes variable only locally accessable
/*************************************************************************
*
* FUNCTIONS
*
*************************************************************************/
/** \brief Command LED function
Function for handling a LED via the SBCP protocol. This function can act as an example showing how
an microcontroller pin can be read and set via the SBCP protocol.
\param rbuffer:
pointer to the ring buffer containing the received communication packet
\param cmd_string:
command string
\param packet_start:
index of the buffer where the packet starts (first byte of packet)
\param packet_end:
index of the buffer where the packet ends (last byte of packet)
\param parameter_start:
index of the buffer where the first parameter starts (first byte of parameter)
\return no returns
*/
void command_led(const unsigned int *rbuffer)
{
led_toggle(LED_GREEN_1);
sbcp_usb_send_standard_acknowlegement(dma_sbcp.bus_rx_packet);
// USBTXREG = 0x11;
}
/** \brief Command firmware function
Function for reading the firmware name and version number as being stored
in the define SBCP_FIRMWARE_VERSION in file config.h via the SBCP protocol.
\param rbuffer:
Pointer to the ring buffer containing the received communication packet.
\param instruction:
Instruction contained in the packet.
\param payload_start:
Index of the buffer where the packet payload starts (first byte of payload).
\param payload_length:
Number of payload bytes stored in the packet.
\return no returns
*/
void command_firmware(const unsigned int *rbuffer)
{
unsigned char fw[2];
fw[0] = (SBCP_FIRMWARE_VERSION & 0xff00) >> 8;
fw[1] = SBCP_FIRMWARE_VERSION & 0x00ff;
sbcp_usb_send_message(dma_sbcp.bus_rx_packet,0,2,fw);
}
#define NO_ERROR 0x00
#define PARAMETER_PROTECTED 0x10
#define PARAMETER_NOT_AVAILABLE 0x11
#define PARAMETER_NOT_WRITABLE 0x12
#define PARAMETER_NOT_IN_RANGE 0x13
// data frame errors
#define MESSAGE_UNEXPECTED_LENGTH 0x20
/** \brief Command test function
Function dedicated to debugging.
\param rbuffer:
Pointer to the ring buffer containing the received communication packet.
\param instruction:
Instruction contained in the packet.
\param payload_start:
Index of the buffer where the packet payload starts (first byte of payload).
\param payload_length:
Number of payload bytes stored in the packet.
\return no returns
*/
void command_set_register(const unsigned int *rbuffer)
{
unsigned char size = rbuffer[SBCP_POS_PAYLOAD_LENGTH];
unsigned char error_code = NO_ERROR;
unsigned char address;
unsigned int i = 0;
if( (size % 3) != 0){
error_code = MESSAGE_UNEXPECTED_LENGTH;
}
while ( (i < size ) && error_code == NO_ERROR ){
if( rbuffer[SBCP_POS_PAYLOAD_START + i] <= 4){
error_code = PARAMETER_NOT_WRITABLE;
} else {
error_code = PARAMETER_NOT_AVAILABLE;
}
i += 3;
}
if(error_code != NO_ERROR){
sbcp_usb_send_message(dma_sbcp.bus_rx_packet,error_code,0,0);
} else {
sbcp_usb_send_standard_acknowlegement(dma_sbcp.bus_rx_packet);
}
}
/** \brief Command test function
Function dedicated to debugging.
\param buffer:
Pointer to the ring buffer containing the received communication packet.
\param instruction:
Instruction contained in the packet.
\param payload_start:
Index of the buffer where the packet payload starts (first byte of payload).
\param payload_length:
Number of payload bytes stored in the packet.
\return no returns
*/
void command_get_register(const unsigned int *rbuffer)
{
unsigned char size = rbuffer[SBCP_POS_PAYLOAD_LENGTH];
unsigned char error_code = NO_ERROR;
unsigned char address;
unsigned int i = 0;
if(size > DMA_SBCP_MAX_PACKET_SIZE / 2){
error_code = MESSAGE_UNEXPECTED_LENGTH;
}
unsigned char data[DMA_SBCP_MAX_PACKET_SIZE];
unsigned int dataVal;
while( (i < size) && (error_code == NO_ERROR)){
address = rbuffer[SBCP_POS_PAYLOAD_START + i];
switch(address){
case 0 :
dataVal = SBCP_FIRMWARE_VERSION;
break;
case 1 :
dataVal = SBCP_MY_CLASS;
break;
case 2 :
dataVal = SBCP_MY_ID;
break;
case 3 :
dataVal = DMA_PACKET_QUEUE_LENGTH;
break;
case 4 :
dataVal = DMA_SBCP_MAX_PACKET_SIZE;
break;
default :
error_code = PARAMETER_NOT_AVAILABLE;
break;
}
if(error_code == NO_ERROR){
data[2 * i ] = (dataVal & 0xff00) >> 8;
data[2 * i + 1] = (dataVal & 0x00ff) >> 0;
}
i += 1;
}
sbcp_usb_send_message(dma_sbcp.bus_rx_packet,error_code,2 * i,data);
}
/*@}*/

Event Timeline