ushell_task.c

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 //_____  I N C L U D E S ___________________________________________________
00012 
00013 #ifdef __ICCAVR__ // IAR C Compiler
00014 #include "config.h"
00015 #endif
00016 
00017 #ifdef __GNUC__  // GNU C Compiler
00018 #include "config_for_gcc.h"
00019 #include <avr/io.h>
00020 #include <avr/pgmspace.h>
00021 #endif
00022 
00023 #include "ushell_task.h"
00024 #include "ascii.h"
00025 #include "uart_lib.h"
00026 #include "mc_interface.h"
00027 
00028 U16 debug_get_value1(void);
00029 U16 debug_get_value2(void);
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 #ifdef __GNUC__
00077    fdevopen((int (*)(char, FILE*))(uart_putchar),(int (*)(FILE*))uart_getchar); //for printf redirection
00078 #endif
00079    print_msg(msg_welcome);
00080    print_msg(msg_prompt);
00081    cmd=FALSE;
00082    cmd_type=CMD_NONE;
00083    
00084 
00085 }
00086 
00087 
00088 
00089 
00090 
00099 void ushell_task(void)
00100 {
00101    U8 status = 0;
00102 
00103    if(cmd==FALSE)
00104    {
00105       build_cmd();
00106    }
00107    else
00108    {
00109       switch (cmd_type)
00110       {
00111          case CMD_HELP:
00112             print_msg(msg_help);
00113             break;
00114          case CMD_RUN:
00115             mci_run();
00116             break;
00117          case CMD_STOP:
00118             mci_stop();
00119             break;
00120          case CMD_FORWARD:
00121             mci_forward();
00122             break;
00123          case CMD_BACKWARD:
00124             mci_backward();
00125             break;
00126          case CMD_SET_SPEED:
00127             convert_param1();
00128             mci_set_speed(param1);
00129             break;
00130          case CMD_GET_ID:
00131             print_hex16(BOARD_ID);
00132             putchar(' ');
00133             print_hex16(SOFT_ID);
00134             putchar(' ');
00135             print_hex16(REV_ID);
00136             break;
00137          case CMD_GET_STATUS0:
00138             status = (mci_direction<<3)|(mci_run_stop<<2);
00139             print_hex16(status);
00140             putchar(' ');
00141             print_hex16(mci_get_measured_speed());
00142             putchar(' ');
00143             print_hex16(mci_get_measured_current());
00144             break;
00145          case CMD_GET_STATUS1:
00146             print_hex16(0xDEA);
00147             putchar(' ');
00148             print_hex16(0x123);
00149             break;
00150          default:    //Unknown command
00151             print_msg(msg_er_cmd_not_found);
00152       }
00153       cmd_type=CMD_NONE;
00154       cmd=FALSE;
00155       print_msg(msg_prompt);
00156    }
00157 }
00158 
00164 void build_cmd(void)
00165 {
00166 U8 c;
00167 
00168    if (uart_test_hit())    //Something new of  the UART ?
00169    {
00170       c=uart_getchar();
00171       switch (c)
00172       {
00173          case CR:
00174             cmd_str[i_str]=0;  //Add NULL char
00175             parse_cmd();    //Decode the command
00176             i_str=0;
00177             break;
00178          case ABORT_CHAR:    //^c abort cmd
00179             i_str=0;
00180             printf("\r#");
00181             break;
00182          case BKSPACE_CHAR:   //backspace
00183             if(i_str>0)
00184             {
00185                putchar(c);
00186                putchar(' ');
00187                putchar(c);
00188             }
00189             if(i_str>=1)
00190             {
00191                i_str--;
00192             }
00193             break;
00194 
00195          default:
00196             cmd_str[i_str++]=c;  //append to cmd line
00197             break;
00198       }
00199    }
00200 }
00201 
00202 
00203 
00212 void parse_cmd(void)
00213 {
00214    U8 i=0;
00215    U8 j;
00216 
00217    //Get command type
00218    for(i=0;cmd_str[i]!=' ' && i<=i_str;i++);
00219    cmd=TRUE;
00220 
00221    //Decode command type
00222 
00223    if ( mystrncmp(cmd_str,str_run,i-1))
00224    {  cmd_type=CMD_RUN; }
00225    else if ( mystrncmp(cmd_str,str_stop,i-1))
00226    {  cmd_type=CMD_STOP; }
00227    else if ( mystrncmp(cmd_str,str_help,i-1))
00228    {  cmd_type=CMD_HELP; }
00229    else if ( mystrncmp(cmd_str,str_forward,i-1))
00230    {  cmd_type=CMD_FORWARD; }
00231    else if ( mystrncmp(cmd_str,str_backward,i-1))
00232    {  cmd_type=CMD_BACKWARD; }
00233    else if ( mystrncmp(cmd_str,str_set_speed,i-1))
00234    {  cmd_type=CMD_SET_SPEED; }
00235    else if ( mystrncmp(cmd_str,str_get_id,i-1))
00236    {  cmd_type=CMD_GET_ID; }
00237    else if ( mystrncmp(cmd_str,str_get_status0,i-1))
00238    {  cmd_type=CMD_GET_STATUS0; }
00239    else if ( mystrncmp(cmd_str,str_get_status1,i-1))
00240    {  cmd_type=CMD_GET_STATUS1; }
00241    else
00242    {
00243       if(i_str)
00244       {
00245         print_msg(msg_er_cmd_not_found);
00246       }
00247       print_msg(msg_prompt);
00248       cmd=FALSE;
00249       return;
00250    }
00251 
00252   //Get first arg (if any)
00253    if(++i<i_str)
00254    {
00255       j=0;
00256       for(;(cmd_str[i]!=' ')&&(i<i_str);i++)
00257       {
00258          LSB(par_str1[j])=cmd_str[i];
00259          MSB(par_str1[j])=0;
00260          j++;
00261       }
00262       LSB(par_str1[j])=0;MSB(par_str1[j])=0;
00263    }
00264    else   {  return; }
00265 
00266    //Get second arg (if any)
00267    if(++i<i_str)
00268    {
00269       j=0;
00270       for(;(cmd_str[i]!=' ')&&(i<i_str);i++)
00271       {
00272          LSB(par_str2[j])=cmd_str[i];
00273          MSB(par_str2[j])=0;
00274          j++;
00275       }
00276       LSB(par_str2[j])=0;MSB(par_str2[j])=0;
00277    }
00278    else   { return; }
00279 
00280 }
00281 
00288 #ifdef __ICCAVR__ // IAR C Compiler
00289 U8     mystrncmp(U8 *str1,U8 code *str2,U8 i)
00290 #endif
00291 
00292 #ifdef __GNUC__  // GNU C Compiler
00293 U8     mystrncmp(U8 *str1,U8 *str2,U8 i)
00294 #endif
00295 {
00296    U8 j;
00297    for(j=0;j<=i;j++)
00298    {
00299 #ifndef __GNUC__
00300       if(*str1!=*str2)
00301 #else
00302 //      if( pgm_read_byte_near((unsigned int)str1) != pgm_read_byte_near((unsigned int)str2))
00303       if( *str1 != pgm_read_byte_near((unsigned int)str2))
00304 #endif
00305       {
00306          return FALSE;
00307       }
00308       str1++;str2++;
00309    }
00310    return TRUE;
00311 }
00312 
00318 #ifdef __ICCAVR__ // IAR C Compiler
00319 void   print_msg(U8 code *str)
00320 #endif
00321 
00322 #ifdef __GNUC__  // GNU C Compiler
00323 void   print_msg(U8 *str)
00324 #endif
00325 {
00326 U8 c;
00327 #ifndef __GNUC__
00328    c=*str++;
00329    while(c!=0)
00330    {
00331       putchar(c);
00332       c=*str++;
00333    }
00334 #else    // AVRGCC does not support point to PGM space
00335    c=pgm_read_byte_near((unsigned int)str++);
00336    while(c!=0)
00337    {
00338       putchar(c);
00339       c=pgm_read_byte_near((unsigned int)str++);
00340    }
00341 #endif
00342 
00343 }
00344 
00350 void convert_param1(void)
00351 {
00352    U8 i = 0;
00353    param1 = 0;
00354    while ( par_str1[i] != 0 )
00355    {
00356       param1 = param1 << 4;
00357       param1 = param1 + ascii_to_bin(par_str1[i]);
00358       i++;
00359    }
00360 }
00361 
00367 void convert_param2(void)
00368 {
00369    U8 i = 0;
00370    param2 = 0;
00371    while ( par_str2[i] != 0 )
00372    {
00373       param2 = param2 << 4;
00374       param2 = param2 + ascii_to_bin(par_str2[i]);
00375       i++;
00376    }
00377 }
00378 
00384 void print_hex16(U16 value)
00385 {
00386    U8 c;
00387    U8 d4;
00388    U8 d3;
00389    U8 d2;
00390    U8 d1;
00391 
00392    d4 = (U16)(value >> 12) & 0x0F;
00393    d3 = (U16)(value >> 8) & 0x0F;
00394    d2 = (U16)(value >> 4) & 0x0F;
00395    d1 = (U16)(value) & 0x0F;
00396 
00397    if (d4 != 0)
00398    {
00399       c = bin_to_ascii(d4);
00400       putchar(c);
00401 
00402       c = bin_to_ascii(d3);
00403       putchar(c);
00404 
00405       c = bin_to_ascii(d2);
00406       putchar(c);
00407    }
00408    else
00409    {
00410       if (d3 != 0)
00411       {
00412          c = bin_to_ascii(d3);
00413          putchar(c);
00414 
00415          c = bin_to_ascii(d2);
00416          putchar(c);
00417       }
00418       else
00419       {
00420          if (d2 != 0)
00421          {
00422             c = bin_to_ascii(d2);
00423             putchar(c);
00424          }
00425       }
00426    }
00427 
00428    c = bin_to_ascii(d1);
00429    putchar(c);
00430 }
00431 
00432 
00433 

Generated on Tue Sep 16 18:11:19 2008 for Atmel BLDC Sinusoidal on ATAVRMC100 by  doxygen 1.5.3