MySmartUSB-MK3-Interface

MySmartUSB MK3 Interface
git clone git://xatko.vsos.ethz.ch/MySmartUSB-MK3-Interface.git
Log | Files | Refs

commit d88f641b7d86f96bba16f41029a7671b9af80681
parent d79e2ec5ec3ac7e3216ed7606337881bc4b91b4f
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Thu, 13 Dec 2012 23:18:17 +0100

Programmer Mode command added

Diffstat:
ctrl.c | 7-------
include/cmd.h | 1+
include/dev.h | 1+
include/option.h | 6+++++-
src/cmd.c | 43+++++++++++++++++++++++++++++++++++++++++++
src/cmd_dispatch.c | 15+++++++++------
src/dev.c | 9+++++++++
7 files changed, 68 insertions(+), 14 deletions(-)

diff --git a/ctrl.c b/ctrl.c @@ -13,11 +13,4 @@ #include <unistd.h> int main(int argc, char **argv){ return engine_autorun(argc,argv); - /* - int fd=openDevice("/dev/ttyUSB0"); - sendMagicPacket(fd,'m'); - readMagicPacket(fd); - closeDevice(fd); - */ - return 0; } diff --git a/include/cmd.h b/include/cmd.h @@ -22,3 +22,4 @@ extern GENERATE_CALLBACK(cmd_status); extern GENERATE_CALLBACK(cmd_board_power); extern GENERATE_CALLBACK(cmd_reset_line); extern GENERATE_CALLBACK(cmd_set_emul_mode); +extern GENERATE_CALLBACK(cmd_set_prog_mode); diff --git a/include/dev.h b/include/dev.h @@ -12,6 +12,7 @@ extern int closeDevice(int dev); extern int autoConfigBaud(int fd); extern int openDevice(char *dev); +extern speed_t getBaudRate(int fd); extern int setBaudRate(int fd, speed_t baud, unsigned int baud_select); extern int setBaudRateExt(int fd, speed_t baud, unsigned int baud_select, int extra); enum BAUD_SELECT{ diff --git a/include/option.h b/include/option.h @@ -10,6 +10,7 @@ #pragma once #include <common.h> #include <cmd.h> +#include <termios.h> #include <stdio.h> extern struct conf configuration; @@ -21,7 +22,10 @@ struct conf{ struct array arr; } cmdlist; unsigned short int need_mymode; - char oldmode; + struct{ + char mode; + speed_t baud; + } oldmode; unsigned short int print_usage; unsigned short int log_level; unsigned short int color; diff --git a/src/cmd.c b/src/cmd.c @@ -151,3 +151,46 @@ GENERATE_CALLBACK(cmd_set_emul_mode){ } return 0; } + +GENERATE_CALLBACK(cmd_set_prog_mode){ + char mode; + if(strcmp(cmd->data[0],"ISP")==0||*cmd->data[0]=='i'){ + mode='i'; + } + else if(strcmp(cmd->data[0],"HVP")==0||*cmd->data[0]=='p'){ + mode='p'; + } + else if(strcmp(cmd->data[0],"HVS")==0||*cmd->data[0]=='s'){ + mode='s'; + } + else if(strcmp(cmd->data[0],"TPI")==0||*cmd->data[0]=='t'){ + mode='t'; + } + else if(strcmp(cmd->data[0],"TPIHV")==0||*cmd->data[0]=='T'){ + mode='T'; + } + else{ + configuration.print_usage=1; + char str[50]; + sprintf(str,"Mode %s unknown",cmd->data[0]); + do_log("cmd_set_prog_mode",str,LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); + return -1; + } + char str[50]; + sprintf(str,"Setting mode to %c",mode); + do_log("cmd_set_prog_mode",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL); + char send[]="@prog progMode "; + send[strlen(send)]=mode; + if(sendString(fd,send)<0){ + do_log("cmd_set_prog_mode","Couldn't send magic packet",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); + return -2; + } + char *res=readString(fd,0); + if(!checkIfOk(res)){ + do_log("cmd_set_prog_mode","Setting Programming mode failed",LOG_LEVEL_ERROR,LOG_TYPE_NORMAL); + free(res); + return -3; + } + free(res); + return 0; +} diff --git a/src/cmd_dispatch.c b/src/cmd_dispatch.c @@ -26,6 +26,7 @@ struct command_def commands[]={ {.name="Boardpower", .opt={.usage="[1/0]", .arg_length=1, .short_option="-bP", .long_option="--board-power"}, .request_mymode=0, .callback=&cmd_board_power}, {.name="Set Emulation Mode", .opt={.usage="[d/UART,s/STK500,a/AVR910,q/Quiet,m/MyMode,p/ProgMode]", .arg_length=1, .short_option="-sE", .long_option="--set-emulation"}, .request_mymode=0, .callback=&cmd_set_emul_mode}, {.name="Rescue Clock", .opt={.usage="[1,0]", .arg_length=1, .short_option="-rC", .long_option="--rescue-clock"}, .request_mymode=0, .callback=&cmd_set_emul_mode}, + {.name="Programming Mode", .opt={.usage="[i/ISP, p/HVP, s/HVS, t/TPI, T/TPIHV]", .arg_length=1, .short_option="-pM", .long_option="--programming-mode"}, .request_mymode=1, .callback=&cmd_set_prog_mode}, {.name=NULL} }; int executeCommand(struct command_exec *cmd, int fd){ @@ -79,12 +80,12 @@ struct command_exec *newCommand(struct command_def *cd){ int prepareCommandExecution(int fd, struct conf *config){ - config->oldmode=0; + config->oldmode.mode=0; if(config->need_mymode){ sendMagicPacket(fd,'i'); - config->oldmode=readMagicPacket(fd); - if(config->oldmode!='m'){ - fsync(fd); + config->oldmode.mode=readMagicPacket(fd); + if(config->oldmode.mode!='m'){ + config->oldmode.baud=getBaudRate(fd); sendMagicPacket(fd,'m'); usleep(5000); setBaudRateExt(fd,B500000, BAUD_INPUT|BAUD_OUTPUT, TCSADRAIN); @@ -100,8 +101,10 @@ int prepareCommandExecution(int fd, struct conf *config){ return 0; } int finishCommandExecution(int fd, struct conf *config){ - if(config->oldmode>0){ - sendMagicPacket(fd,config->oldmode); + if(config->oldmode.mode>0){ + sendMagicPacket(fd,config->oldmode.mode); + usleep(5000); + setBaudRate(fd, config->oldmode.baud, BAUD_INPUT|BAUD_OUTPUT); } return 0; } diff --git a/src/dev.c b/src/dev.c @@ -90,6 +90,15 @@ int setBaudRateExt(int fd, speed_t baud, unsigned int baud_select, int extra){ return 0; } +speed_t getBaudRate(int fd){ + struct termios options; + if(tcgetattr(fd, &options)<0){ + do_log("getBaudRate","Couldn't load Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL); + return -1; + } + return cfgetispeed(&options); + +} int openDevice(char *dev){ int fd = open(dev, O_RDWR | O_NOCTTY ); if(fd<0){