BLDC control on ATAVRMC303 with ATxMega128A1
ushell_task.c
Go to the documentation of this file.
1 
11 //_____ I N C L U D E S ___________________________________________________
12 
13 #include "config.h"
14 #include "ushell_task.h"
15 #include "ascii.h"
16 #include "lib_mcu/uart/uart_lib.h"
17 #include "mc_interface.h"
18 
19 U16 debug_get_value1(void);
20 U16 debug_get_value2(void);
21 
22 //_____ M A C R O S ________________________________________________________
23 
24 //_____ D E F I N I T I O N S ______________________________________________
25 void convert_param1(void);
26 void print_hex16(U16 value);
27 
28 //_____ D E C L A R A T I O N S ____________________________________________
29 
30 static U8 cmd;
31 static U8 cmd_type;
32 static U16 par_str1[8];
33 static U16 par_str2[8];
34 static U16 param1;
35 static U16 param2;
36 static U8 cmd_str[26];
37 static U8 i_str=0;
38 
39 volatile Bool ushell_active = FALSE;
40 
41 U8 code str_run[]=STR_RUN;
42 U8 code str_stop[]=STR_STOP;
43 U8 code str_help[]=STR_HELP;
50 
53 U8 code msg_help[]=MSG_HELP;
55 
56 extern S16 speed_error;
57 
66 void ushell_task_init(void)
67 {
68 
69  uart_init();
70 
71 #ifdef __GNUC__
72  fdevopen((int (*)(char, FILE*))(uart_putchar),(int (*)(FILE*))uart_getchar); //for printf redirection
73 #endif
74 
77  cmd=FALSE;
79 }
80 
81 
82 
83 
84 
93 void ushell_task(void)
94 {
95  U8 status = 0;
96 
97  if(cmd==FALSE)
98  {
99  build_cmd();
100  }
101  else
102  {
103  switch (cmd_type)
104  {
105  case CMD_HELP:
107  break;
108  case CMD_RUN:
109  mci_run();
110  break;
111  case CMD_STOP:
112  mci_stop();
113  break;
114  case CMD_FORWARD:
115  mci_forward();
116  break;
117  case CMD_BACKWARD:
118  mci_backward();
119  break;
120  case CMD_SET_SPEED:
121  convert_param1();
123  break;
124  case CMD_GET_ID:
126  uart_putchar(' ');
128  uart_putchar(' ');
130  break;
131  case CMD_GET_STATUS0:
132  status = 0;
133  if (mci_direction == CCW) {status |= (1<<3);}
134  if (mci_run_stop == TRUE) {status |= (1<<2);}
135  print_hex16(status);
136  uart_putchar(' ');
138  uart_putchar(' ');
140  ushell_active = TRUE;
141  break;
142  case CMD_GET_STATUS1:
143  print_hex16(0xDEA);
144  uart_putchar(' ');
145  print_hex16(0x123);
146  break;
147  default: //Unknown command
149  }
151  cmd=FALSE;
153  }
154 }
155 
161 void build_cmd(void)
162 {
163 U8 c;
164 
165  if (uart_test_hit()) //Something new of the UART ?
166  {
167  c=uart_getchar();
168  switch (c)
169  {
170  case CR:
171  cmd_str[i_str]=0; //Add NULL char
172  parse_cmd(); //Decode the command
173  i_str=0;
174  break;
175  case ABORT_CHAR: //^c abort cmd
176  i_str=0;
177  printf("\r#");
178  break;
179  case BKSPACE_CHAR: //backspace
180  if(i_str>0)
181  {
182  uart_putchar(c);
183  uart_putchar(' ');
184  uart_putchar(c);
185  }
186  if(i_str>=1)
187  {
188  i_str--;
189  }
190  break;
191 
192  default:
193  cmd_str[i_str++]=c; //append to cmd line
194  break;
195  }
196  }
197 }
198 
199 
200 
209 void parse_cmd(void)
210 {
211  U8 i=0;
212  U8 j;
213 
214  //Get command type
215  for(i=0;cmd_str[i]!=' ' && i<=i_str;i++);
216  cmd=TRUE;
217 
218  //Decode command type
219 
220  if ( mystrncmp(cmd_str,str_run,i-1))
221  { cmd_type=CMD_RUN; }
222  else if ( mystrncmp(cmd_str,str_stop,i-1))
223  { cmd_type=CMD_STOP; }
224  else if ( mystrncmp(cmd_str,str_help,i-1))
225  { cmd_type=CMD_HELP; }
226  else if ( mystrncmp(cmd_str,str_forward,i-1))
227  { cmd_type=CMD_FORWARD; }
228  else if ( mystrncmp(cmd_str,str_backward,i-1))
230  else if ( mystrncmp(cmd_str,str_set_speed,i-1))
232  else if ( mystrncmp(cmd_str,str_get_id,i-1))
233  { cmd_type=CMD_GET_ID; }
234  else if ( mystrncmp(cmd_str,str_get_status0,i-1))
236  else if ( mystrncmp(cmd_str,str_get_status1,i-1))
238  else
239  {
240  if(i_str)
241  {
243  }
245  cmd=FALSE;
246  return;
247  }
248 
249  //Get first arg (if any)
250  if(++i<i_str)
251  {
252  j=0;
253  for(;(cmd_str[i]!=' ')&&(i<i_str);i++)
254  {
255  LSB(par_str1[j])=cmd_str[i];
256  MSB(par_str1[j])=0;
257  j++;
258  }
259  LSB(par_str1[j])=0;MSB(par_str1[j])=0;
260  }
261  else { return; }
262 
263  //Get second arg (if any)
264  if(++i<i_str)
265  {
266  j=0;
267  for(;(cmd_str[i]!=' ')&&(i<i_str);i++)
268  {
269  LSB(par_str2[j])=cmd_str[i];
270  MSB(par_str2[j])=0;
271  j++;
272  }
273  LSB(par_str2[j])=0;MSB(par_str2[j])=0;
274  }
275  else { return; }
276 
277 }
278 
285 U8 mystrncmp(U8 *str1,U8 code *str2,U8 i)
286 {
287  U8 j;
288  for(j=0;j<=i;j++)
289  {
290 #ifndef __GNUC__
291  if(*str1!=*str2)
292 #else
293 // if( pgm_read_byte_near((unsigned int)str1) != pgm_read_byte_near((unsigned int)str2))
294  if( *str1 != pgm_read_byte_near((unsigned int)str2))
295 
296 #endif
297  {
298  return FALSE;
299  }
300  str1++;str2++;
301  }
302  return TRUE;
303 }
304 
310 void print_msg(U8 code *str)
311 {
312 U8 c;
313 #ifndef __GNUC__
314  c=*str++;
315  while(c!=0)
316  {
317  uart_putchar(c);
318  c=*str++;
319  }
320 #else // AVRGCC does not support point to PGM space
321  c=pgm_read_byte_near((unsigned int)str++);
322  while(c!=0)
323  {
324  uart_putchar(c);
325  c=pgm_read_byte_near((unsigned int)str++);
326  }
327 #endif
328 
329 }
330 
336 void convert_param1(void)
337 {
338  U8 i = 0;
339  param1 = 0;
340  while ( par_str1[i] != 0 )
341  {
342  param1 = param1 << 4;
344  i++;
345  }
346 }
347 
353 void convert_param2(void)
354 {
355  U8 i = 0;
356  param2 = 0;
357  while ( par_str2[i] != 0 )
358  {
359  param2 = param2 << 4;
361  i++;
362  }
363 }
364 
370 void print_hex16(U16 value)
371 {
372  U8 c;
373  U8 d4;
374  U8 d3;
375  U8 d2;
376  U8 d1;
377 
378  d4 = (U16)(value >> 12) & 0x0F;
379  d3 = (U16)(value >> 8) & 0x0F;
380  d2 = (U16)(value >> 4) & 0x0F;
381  d1 = (U16)(value) & 0x0F;
382 
383  if (d4 != 0)
384  {
385  c = bin_to_ascii(d4);
386  uart_putchar(c);
387 
388  c = bin_to_ascii(d3);
389  uart_putchar(c);
390 
391  c = bin_to_ascii(d2);
392  uart_putchar(c);
393  }
394  else
395  {
396  if (d3 != 0)
397  {
398  c = bin_to_ascii(d3);
399  uart_putchar(c);
400 
401  c = bin_to_ascii(d2);
402  uart_putchar(c);
403  }
404  else
405  {
406  if (d2 != 0)
407  {
408  c = bin_to_ascii(d2);
409  uart_putchar(c);
410  }
411  }
412  }
413 
414  c = bin_to_ascii(d1);
415  uart_putchar(c);
416 }
417 
418 
419 
#define ABORT_CHAR
Definition: ushell_task.h:40
U8 code str_get_id[]
Definition: ushell_task.c:47
void convert_param1(void)
convert param1 to binaire.
Definition: ushell_task.c:336
#define MSG_ER_CMD_NOT_FOUND
Definition: ushell_task.h:57
U8 bin_to_ascii(U8 c)
This function returns the ascii value of a quartet.
Definition: ascii.c:47
volatile Bool ushell_active
Definition: ushell_task.c:39
U8 code str_run[]
Definition: ushell_task.c:41
void ushell_task_init(void)
This function initializes the hardware/software ressources required for ushell task.
Definition: ushell_task.c:66
void mci_set_speed(U16 speed)
Definition: mc_interface.c:214
U8 ascii_to_bin(U8 c)
This function returns the binary value of an ascii digit.
Definition: ascii.c:32
#define MSG_WELCOME
Definition: ushell_task.h:56
U16 mci_get_measured_current(void)
Get the current measured in the motor.
Definition: mc_interface.c:179
U8 code str_get_status0[]
Definition: ushell_task.c:48
#define CMD_STOP
Definition: ushell_task.h:25
#define CMD_SET_SPEED
Definition: ushell_task.h:28
static U8 i_str
Definition: ushell_task.c:37
U8 code str_backward[]
Definition: ushell_task.c:45
#define CMD_GET_STATUS0
Definition: ushell_task.h:30
#define uart_putchar
Definition: config.h:85
#define MSG_PROMPT
Definition: ushell_task.h:55
#define STR_BACKWARD
Definition: ushell_task.h:48
U8 code msg_prompt[]
Definition: ushell_task.c:51
U8 mystrncmp(U8 *str1, U8 code *str2, U8 i)
compares two strings located in flash code area.
Definition: ushell_task.c:285
void convert_param2(void)
convert param2 to binaire.
Definition: ushell_task.c:353
#define STR_HELP
Definition: ushell_task.h:46
void print_msg(U8 code *str)
Display an ASCII code string.
Definition: ushell_task.c:310
Bool mci_direction
User Input parameter to set motor direction.
Definition: mc_interface.c:20
U16 debug_get_value2(void)
static U8 cmd_type
Definition: ushell_task.c:31
void mci_stop(void)
mci_stop stop the motor And reset the speed measured value.
Definition: mc_interface.c:81
static U16 param1
Definition: ushell_task.c:34
#define CR
Definition: ushell_task.h:35
#define CMD_HELP
Definition: ushell_task.h:23
#define REV_ID
Definition: config.h:78
#define CMD_GET_STATUS1
Definition: ushell_task.h:31
void mci_run(void)
mci_run run the motor with parameter
Definition: mc_interface.c:34
U8 code str_forward[]
Definition: ushell_task.c:44
#define CMD_RUN
Definition: ushell_task.h:24
Bool mci_run_stop
User Input parameter to launch or stop the motor.
Definition: mc_interface.c:21
#define STR_GET_STATUS1
Definition: ushell_task.h:52
U8 code str_stop[]
Definition: ushell_task.c:42
void mci_backward(void)
Definition: mc_interface.c:138
static U16 par_str2[8]
Definition: ushell_task.c:33
#define STR_GET_STATUS0
Definition: ushell_task.h:51
#define CMD_BACKWARD
Definition: ushell_task.h:27
#define BOARD_ID
Definition: config.h:76
static U16 param2
Definition: ushell_task.c:35
#define STR_RUN
Definition: ushell_task.h:44
U8 code str_get_status1[]
Definition: ushell_task.c:49
#define CMD_FORWARD
Definition: ushell_task.h:26
#define SOFT_ID
Definition: config.h:77
U16 debug_get_value1(void)
S16 speed_error
Error calculation.
Definition: mc_control.c:21
void build_cmd(void)
get the full command line to be interpreted.
Definition: ushell_task.c:161
#define STR_GET_ID
Definition: ushell_task.h:50
void mci_forward(void)
Definition: mc_interface.c:128
#define BKSPACE_CHAR
Definition: ushell_task.h:39
#define CMD_NONE
Definition: ushell_task.h:22
U8 mci_get_measured_speed(void)
Measured of speed.
Definition: mc_interface.c:169
void parse_cmd(void)
decodes full command line into command type and arguments
Definition: ushell_task.c:209
#define MSG_HELP
Definition: ushell_task.h:58
static U8 cmd_str[26]
Definition: ushell_task.c:36
U8 code str_help[]
Definition: ushell_task.c:43
#define CMD_GET_ID
Definition: ushell_task.h:29
static U16 par_str1[8]
Definition: ushell_task.c:32
#define STR_SET_SPEED
Definition: ushell_task.h:49
void print_hex16(U16 value)
Display an U16 with hexa form.
Definition: ushell_task.c:370
#define STR_STOP
Definition: ushell_task.h:45
U8 code msg_welcome[]
Definition: ushell_task.c:52
U8 code msg_help[]
Definition: ushell_task.c:53
static U8 cmd
Definition: ushell_task.c:30
void ushell_task(void)
Entry point of the explorer task management.
Definition: ushell_task.c:93
#define STR_FORWARD
Definition: ushell_task.h:47
U8 code str_set_speed[]
Definition: ushell_task.c:46
U8 code msg_er_cmd_not_found[]
Definition: ushell_task.c:54