ushell_task.c

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 //_____  I N C L U D E S ___________________________________________________
00018 
00019 #include "config.h"
00020 #include "ushell_task.h"
00021 #include "ascii.h"
00022 #include "lib_mcu/uart/uart_lib.h"
00023 #include "mc_interface.h"
00024 
00025 void mci_run(void);
00026 void mci_stop(void);
00027 void mci_forward(void);
00028 void mci_backward(void);
00029 void mci_set_speed(U16);
00030 
00031 //_____ M A C R O S ________________________________________________________
00032 
00033 //_____ D E F I N I T I O N S ______________________________________________
00034 void convert_param1(void);
00035 void print_hex16(U16 value);
00036 
00037 //_____ D E C L A R A T I O N S ____________________________________________
00038 
00039 static U8 cmd;
00040 static U8 cmd_type;
00041 static U16 par_str1[8];
00042 static U16 par_str2[8];
00043 static U16 param1;
00044 static U16 param2;
00045 static U8 cmd_str[26];
00046 static U8 i_str=0;
00047 
00048 U8 code str_run[]=STR_RUN;
00049 U8 code str_stop[]=STR_STOP;
00050 U8 code str_help[]=STR_HELP;
00051 U8 code str_forward[]=STR_FORWARD;
00052 U8 code str_backward[]=STR_BACKWARD;
00053 U8 code str_set_speed[]=STR_SET_SPEED;
00054 U8 code str_get_id[]=STR_GET_ID;
00055 U8 code str_get_status0[]=STR_GET_STATUS0;
00056 U8 code str_get_status1[]=STR_GET_STATUS1;
00057 
00058 U8 code msg_prompt[]=MSG_PROMPT;
00059 U8 code msg_welcome[]=MSG_WELCOME;
00060 U8 code msg_help[]=MSG_HELP;
00061 U8 code msg_er_cmd_not_found[]=MSG_ER_CMD_NOT_FOUND;
00062 
00063 extern S16 speed_error;
00064 
00073 void ushell_task_init(void)
00074 {
00075    uart_init();
00076    print_msg(msg_welcome);
00077    print_msg(msg_prompt);
00078    cmd=FALSE;
00079    cmd_type=CMD_NONE;
00080 }
00081 
00082 
00083 
00084 
00085 
00094 void ushell_task(void)
00095 {
00096    U8 status = 0;
00097 
00098    if(cmd==FALSE)
00099    {
00100       build_cmd();
00101    }
00102    else
00103    {
00104       switch (cmd_type)
00105       {
00106          case CMD_HELP:
00107             print_msg(msg_help);
00108             break;
00109          case CMD_RUN:
00110             mci_run();
00111             break;
00112          case CMD_STOP:
00113             mci_stop();
00114             break;
00115          case CMD_FORWARD:
00116             mci_forward();
00117             break;
00118          case CMD_BACKWARD:
00119             mci_backward();
00120             break;
00121          case CMD_SET_SPEED:
00122             convert_param1();
00123             mci_set_speed(param1);
00124             break;
00125          case CMD_GET_ID:
00126             print_hex16(BOARD_ID);
00127             putchar(' ');
00128             print_hex16(SOFT_ID);
00129             putchar(' ');
00130             print_hex16(REV_ID);
00131             break;
00132          case CMD_GET_STATUS0:
00133             status = (mci_direction<<3)|(mci_run_stop<<2);
00134             print_hex16(status);
00135             putchar(' ');
00136             print_hex16(mci_get_measured_speed());
00137             putchar(' ');
00138             print_hex16(mci_get_measured_current());
00139             break;
00140          case CMD_GET_STATUS1:
00141             print_hex16(0xDEA);
00142             putchar(' ');
00143             print_hex16(0x123);
00144             break;
00145          default:    //Unknown command
00146             print_msg(msg_er_cmd_not_found);
00147       }
00148       cmd_type=CMD_NONE;
00149       cmd=FALSE;
00150       print_msg(msg_prompt);
00151    }
00152 }
00153 
00159 void build_cmd(void)
00160 {
00161 U8 c;
00162 
00163    if (uart_test_hit())    //Something new of  the UART ?
00164    {
00165       c=uart_getchar();
00166       switch (c)
00167       {
00168          case CR:
00169             cmd_str[i_str]=0;  //Add NULL char
00170             parse_cmd();    //Decode the command
00171             i_str=0;
00172             break;
00173          case ABORT_CHAR:    //^c abort cmd
00174             i_str=0;
00175             printf("\r#");
00176             break;
00177          case BKSPACE_CHAR:   //backspace
00178             if(i_str>0)
00179             {
00180                putchar(c);
00181                putchar(' ');
00182                putchar(c);
00183             }
00184             if(i_str>=1)
00185             {
00186                i_str--;
00187             }
00188             break;
00189 
00190          default:
00191             cmd_str[i_str++]=c;  //append to cmd line
00192             break;
00193       }
00194    }
00195 }
00196 
00197 
00198 
00207 void parse_cmd(void)
00208 {
00209    U8 i=0;
00210    U8 j;
00211 
00212    //Get command type
00213    for(i=0;cmd_str[i]!=' ' && i<=i_str;i++);
00214    cmd=TRUE;
00215 
00216    //Decode command type
00217 
00218    if ( mystrncmp(cmd_str,str_run,i-1))
00219    {  cmd_type=CMD_RUN; }
00220    else if ( mystrncmp(cmd_str,str_stop,i-1))
00221    {  cmd_type=CMD_STOP; }
00222    else if ( mystrncmp(cmd_str,str_help,i-1))
00223    {  cmd_type=CMD_HELP; }
00224    else if ( mystrncmp(cmd_str,str_forward,i-1))
00225    {  cmd_type=CMD_FORWARD; }
00226    else if ( mystrncmp(cmd_str,str_backward,i-1))
00227    {  cmd_type=CMD_BACKWARD; }
00228    else if ( mystrncmp(cmd_str,str_set_speed,i-1))
00229    {  cmd_type=CMD_SET_SPEED; }
00230    else if ( mystrncmp(cmd_str,str_get_id,i-1))
00231    {  cmd_type=CMD_GET_ID; }
00232    else if ( mystrncmp(cmd_str,str_get_status0,i-1))
00233    {  cmd_type=CMD_GET_STATUS0; }
00234    else if ( mystrncmp(cmd_str,str_get_status1,i-1))
00235    {  cmd_type=CMD_GET_STATUS1; }
00236    else
00237    {
00238       if(i_str)
00239       {
00240         print_msg(msg_er_cmd_not_found);
00241       }
00242       print_msg(msg_prompt);
00243       cmd=FALSE;
00244       return;
00245    }
00246 
00247   //Get first arg (if any)
00248    if(++i<i_str)
00249    {
00250       j=0;
00251       for(;(cmd_str[i]!=' ')&&(i<i_str);i++)
00252       {
00253          LSB(par_str1[j])=cmd_str[i];
00254          MSB(par_str1[j])=0;
00255          j++;
00256       }
00257       LSB(par_str1[j])=0;MSB(par_str1[j])=0;
00258    }
00259    else   {  return; }
00260 
00261    //Get second arg (if any)
00262    if(++i<i_str)
00263    {
00264       j=0;
00265       for(;(cmd_str[i]!=' ')&&(i<i_str);i++)
00266       {
00267          LSB(par_str2[j])=cmd_str[i];
00268          MSB(par_str2[j])=0;
00269          j++;
00270       }
00271       LSB(par_str2[j])=0;MSB(par_str2[j])=0;
00272    }
00273    else   { return; }
00274 
00275 }
00276 
00283 U8 mystrncmp(U8 *str1,U8 code *str2,U8 i)
00284 {
00285    U8 j;
00286    for(j=0;j<=i;j++)
00287    {
00288 #ifndef AVRGCC
00289       if(*str1!=*str2)
00290 #else
00291       if( pgm_read_byte_near((unsigned int)str1) != pgm_read_byte_near((unsigned int)str2))
00292 #endif
00293       {
00294          return FALSE;
00295       }
00296       str1++;str2++;
00297    }
00298    return TRUE;
00299 }
00300 
00306 void print_msg(U8 code *str)
00307 {
00308 U8 c;
00309 #ifndef AVRGCC
00310    c=*str++;
00311    while(c!=0)
00312    {
00313       putchar(c);
00314       c=*str++;
00315    }
00316 #else    // AVRGCC does not support point to PGM space
00317    c=pgm_read_byte_near((unsigned int)str++);
00318    while(c!=0)
00319    {
00320       putchar(c);
00321       c=pgm_read_byte_near((unsigned int)str++);
00322    }
00323 #endif
00324 
00325 }
00326 
00332 void convert_param1(void)
00333 {
00334    U8 i = 0;
00335    param1 = 0;
00336    while ( par_str1[i] != 0 )
00337    {
00338       param1 = param1 << 4;
00339       param1 = param1 + ascii_to_bin(par_str1[i]);
00340       i++;
00341    }
00342 }
00343 
00349 void convert_param2(void)
00350 {
00351    U8 i = 0;
00352    param2 = 0;
00353    while ( par_str2[i] != 0 )
00354    {
00355       param2 = param2 << 4;
00356       param2 = param2 + ascii_to_bin(par_str2[i]);
00357       i++;
00358    }
00359 }
00360 
00366 void print_hex16(U16 value)
00367 {
00368    U8 c;
00369    c = (U16)(value >> 12) & 0x0F;
00370    if (c != 0)
00371    {
00372       c = bin_to_ascii(c);
00373       putchar(c);
00374    }
00375 
00376    c = (U16)(value >> 8) & 0x0F;
00377    if (c != 0)
00378    {
00379       c = bin_to_ascii(c);
00380       putchar(c);
00381    }
00382 
00383    c = (U16)(value >> 4) & 0x0F;
00384    if (c != 0)
00385    {
00386       c = bin_to_ascii(c);
00387       putchar(c);
00388    }
00389 
00390    c = (U16)(value) & 0x0F;
00391    c = bin_to_ascii(c);
00392    putchar(c);
00393 }
00394 
00395 
00396 

Generated on Wed Jul 12 16:55:11 2006 for Atmel BLDC Sensorless on ATAVRMC100 by  doxygen 1.4.7