Page MenuHomec4science

main.c
No OneTemporary

File Metadata

Created
Mon, Jan 27, 08:26
/*
* Copyright (c) 2011 Alexandre Tuleu <alexandre.tuleu.2005@polytechnique.org>
* Ecole Polytechnique Fédérale de Lausanne.
*
* this file is part of sbcpd.
*
* sbcpd is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sbcpd 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 Lesser General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with sbcpd. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file main.c
*
* \date Oct 11, 2011
* \author Alexandre Tuleu
*/
#include <stdio.h>
//for all bus transfer definition
#include <sbcpd/sbcpd.h>
//for SBCP definitions
#include <sbcpd/sbcp.h>
#include <sbcpd/si/ft2232h.h>
//Motordriver class 0xea
#define DEVICE_CLASS 0xea
#define UART_BAUDRATE 3310000
int main(int argc, char ** argv){
//first the program shouldinitialize as soon as possible the library
sbcpd_error err;
//first create a new bus defintion
sbcpd_bus_settings * s = sbcpd_bus_settings_create();
//set the minimal packet frame definition.
sbcpd_bus_settings_set_required_frame_definition(s,1,10);
//now open the serial
sbcpd_bus * bus = sbcpd_create_bus(s);//sbcpd_default_oncilla_init(0,&err);
//now open the serial interface
sbcpd_serial_interface * si = sbcpd_ft2232h_create_interface();
if(sbcpd_ft2232h_open(si,UART_BAUDRATE,0,2) < 0 ){
fprintf(stderr,"Unable to open UART.\n");
return 1;
}
if(sbcpd_bus_open(bus,si)){
fprintf(stderr,"Unable to initialize library : %s.\n",
sbcpd_bus_get_last_error_message(bus));
return 1;
}
if(argc <= 1){
fprintf(stderr,"Need at least the id of the board to reach.\n");
return 2;
}
unsigned int id = atoi(argv[1]);
if(id < 0 || id > 0xff){
fprintf(stderr,"Bad id #%d, should be between 0x00 and 0xff.\n",id);
return 3;
}
//ask for a new transfer with a payload of 0
sbcpd_transfer * t = sbcpd_bus_append_transfer(bus,0);
//fill the packet destination
t->out_buffer[SBCP_CLASS_POS] = DEVICE_CLASS;
t->out_buffer[SBCP_ID_POS] = id;
//fill the packet instruction
t->out_buffer[SBCP_INSTRUCTION_POS] = 0x00; //ask for firmware version
//don't need to fill, payload, there is none.
//just don't specify any callback. we want to be blocking :)
t->callback = NULL;
//submission will make sure that our packet is correct for the communication
//layer (header,payload value, checksum)
if(sbcpd_bus_submit_all_transfer(bus) != SBCPD_NO_ERROR){
fprintf(stderr,"Unable to initiate transfer : %s.\n",
sbcpd_bus_get_last_error_message(bus));
return 5;
}
//block indefinetly until completion.
if(sbcpd_bus_wait_for_completion(bus,-1) != SBCPD_NO_ERROR){
fprintf(stderr,"Got a critical error while reading : %s\n",
sbcpd_bus_get_last_error_message(bus));
return 6;
}
if(t->status != SBCPD_TRANSFER_OK){
fprintf(stderr,"Got an error during transfer : \n");
sbcpd_transfer_fprintf(t,stderr);
return 4;
}
//get the data from buffer.
uint16_t firmwareCode = t->in_buffer[SBCP_PAYLOAD_START_POS] << 8;
firmwareCode += t->in_buffer[SBCP_PAYLOAD_START_POS + 1];
printf("Device 0x%x : 0x%x has a firmware value of : 0x%x.\n",
DEVICE_CLASS,
id,
firmwareCode);
//don't forget to release the transfer otherwise bandwith will be blocked !!
if(sbcpd_bus_release_transfer(bus,t) != SBCPD_NO_ERROR){
fprintf(stderr,"Unable to release the transfer : %s\n",
sbcpd_bus_get_last_error_message(bus));
}
return 0;
}

Event Timeline