BLDC control on ATAVRMC303 with ATxMega128A1
ushell_task.c File Reference

,v More...

#include "config.h"
#include "ushell_task.h"
#include "ascii.h"
#include "lib_mcu/uart/uart_lib.h"
#include "mc_interface.h"
Include dependency graph for ushell_task.c:

Go to the source code of this file.

Functions

void build_cmd (void)
 get the full command line to be interpreted. More...
 
void convert_param1 (void)
 convert param1 to binaire. More...
 
void convert_param2 (void)
 convert param2 to binaire. More...
 
U16 debug_get_value1 (void)
 
U16 debug_get_value2 (void)
 
U8 mystrncmp (U8 *str1, U8 code *str2, U8 i)
 compares two strings located in flash code area. More...
 
void parse_cmd (void)
 decodes full command line into command type and arguments More...
 
void print_hex16 (U16 value)
 Display an U16 with hexa form. More...
 
void print_msg (U8 code *str)
 Display an ASCII code string. More...
 
void ushell_task (void)
 Entry point of the explorer task management. More...
 
void ushell_task_init (void)
 This function initializes the hardware/software ressources required for ushell task. More...
 

Variables

static U8 cmd
 
static U8 cmd_str [26]
 
static U8 cmd_type
 
static U8 i_str =0
 
U8 code msg_er_cmd_not_found [] =MSG_ER_CMD_NOT_FOUND
 
U8 code msg_help [] =MSG_HELP
 
U8 code msg_prompt [] =MSG_PROMPT
 
U8 code msg_welcome [] =MSG_WELCOME
 
static U16 par_str1 [8]
 
static U16 par_str2 [8]
 
static U16 param1
 
static U16 param2
 
S16 speed_error
 Error calculation. More...
 
U8 code str_backward [] =STR_BACKWARD
 
U8 code str_forward [] =STR_FORWARD
 
U8 code str_get_id [] =STR_GET_ID
 
U8 code str_get_status0 [] =STR_GET_STATUS0
 
U8 code str_get_status1 [] =STR_GET_STATUS1
 
U8 code str_help [] =STR_HELP
 
U8 code str_run [] =STR_RUN
 
U8 code str_set_speed [] =STR_SET_SPEED
 
U8 code str_stop [] =STR_STOP
 
volatile Bool ushell_active = FALSE
 

Detailed Description

,v

Copyright (c) 2006 Atmel.

This file manages the µshell task for communication.

Version
1.2

Definition in file ushell_task.c.

Function Documentation

◆ build_cmd()

void build_cmd ( void  )

get the full command line to be interpreted.

Parameters
none
Returns
none

Definition at line 161 of file ushell_task.c.

References ABORT_CHAR, BKSPACE_CHAR, cmd_str, CR, i_str, parse_cmd(), and uart_putchar.

Referenced by ushell_task().

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 }
#define ABORT_CHAR
Definition: ushell_task.h:40
static U8 i_str
Definition: ushell_task.c:37
#define uart_putchar
Definition: config.h:85
#define CR
Definition: ushell_task.h:35
#define BKSPACE_CHAR
Definition: ushell_task.h:39
void parse_cmd(void)
decodes full command line into command type and arguments
Definition: ushell_task.c:209
static U8 cmd_str[26]
Definition: ushell_task.c:36
Here is the call graph for this function:

◆ convert_param1()

void convert_param1 ( void  )

convert param1 to binaire.

Parameters
none
Returns
none

Definition at line 336 of file ushell_task.c.

References ascii_to_bin(), par_str1, and param1.

Referenced by ushell_task().

337 {
338  U8 i = 0;
339  param1 = 0;
340  while ( par_str1[i] != 0 )
341  {
342  param1 = param1 << 4;
344  i++;
345  }
346 }
U8 ascii_to_bin(U8 c)
This function returns the binary value of an ascii digit.
Definition: ascii.c:32
static U16 param1
Definition: ushell_task.c:34
static U16 par_str1[8]
Definition: ushell_task.c:32
Here is the call graph for this function:

◆ convert_param2()

void convert_param2 ( void  )

convert param2 to binaire.

Parameters
none
Returns
none

Definition at line 353 of file ushell_task.c.

References ascii_to_bin(), par_str2, and param2.

354 {
355  U8 i = 0;
356  param2 = 0;
357  while ( par_str2[i] != 0 )
358  {
359  param2 = param2 << 4;
361  i++;
362  }
363 }
U8 ascii_to_bin(U8 c)
This function returns the binary value of an ascii digit.
Definition: ascii.c:32
static U16 par_str2[8]
Definition: ushell_task.c:33
static U16 param2
Definition: ushell_task.c:35
Here is the call graph for this function:

◆ debug_get_value1()

U16 debug_get_value1 ( void  )

◆ debug_get_value2()

U16 debug_get_value2 ( void  )

◆ mystrncmp()

U8 mystrncmp ( U8 *  str1,
U8 code *  str2,
U8  i 
)

compares two strings located in flash code area.

Parameters
*str1
*str2
Returns
status: TRUE for ok, FALSE if strings are not equal

Definition at line 285 of file ushell_task.c.

Referenced by parse_cmd().

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 }

◆ parse_cmd()

void parse_cmd ( void  )

decodes full command line into command type and arguments

This function allows to set the cmd_type to the command type decoded with its respective arguments par_str1 and par_str2

Parameters
none
Returns
none

Definition at line 209 of file ushell_task.c.

References cmd, CMD_BACKWARD, CMD_FORWARD, CMD_GET_ID, CMD_GET_STATUS0, CMD_GET_STATUS1, CMD_HELP, CMD_RUN, CMD_SET_SPEED, CMD_STOP, cmd_str, cmd_type, i_str, msg_er_cmd_not_found, msg_prompt, mystrncmp(), par_str1, par_str2, print_msg(), str_backward, str_forward, str_get_id, str_get_status0, str_get_status1, str_help, str_run, str_set_speed, and str_stop.

Referenced by build_cmd().

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 }
U8 code str_get_id[]
Definition: ushell_task.c:47
U8 code str_run[]
Definition: ushell_task.c:41
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
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 print_msg(U8 code *str)
Display an ASCII code string.
Definition: ushell_task.c:310
static U8 cmd_type
Definition: ushell_task.c:31
#define CMD_HELP
Definition: ushell_task.h:23
#define CMD_GET_STATUS1
Definition: ushell_task.h:31
U8 code str_forward[]
Definition: ushell_task.c:44
#define CMD_RUN
Definition: ushell_task.h:24
U8 code str_stop[]
Definition: ushell_task.c:42
static U16 par_str2[8]
Definition: ushell_task.c:33
#define CMD_BACKWARD
Definition: ushell_task.h:27
U8 code str_get_status1[]
Definition: ushell_task.c:49
#define CMD_FORWARD
Definition: ushell_task.h:26
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
static U8 cmd
Definition: ushell_task.c:30
U8 code str_set_speed[]
Definition: ushell_task.c:46
U8 code msg_er_cmd_not_found[]
Definition: ushell_task.c:54
Here is the call graph for this function:

◆ print_hex16()

void print_hex16 ( U16  value)

Display an U16 with hexa form.

Parameters

Definition at line 370 of file ushell_task.c.

References bin_to_ascii(), and uart_putchar.

Referenced by ushell_task().

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 }
U8 bin_to_ascii(U8 c)
This function returns the ascii value of a quartet.
Definition: ascii.c:47
#define uart_putchar
Definition: config.h:85
Here is the call graph for this function:

◆ print_msg()

void print_msg ( U8 code *  str)

Display an ASCII code string.

Parameters
*strpointer to string located in flash area
Returns
none

Definition at line 310 of file ushell_task.c.

References uart_putchar.

Referenced by parse_cmd(), ushell_task(), and ushell_task_init().

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 }
#define uart_putchar
Definition: config.h:85

◆ ushell_task()

void ushell_task ( void  )

Entry point of the explorer task management.

This function links perform ushell task decoding to access file system functions.

Parameters
none
Returns
none

Definition at line 93 of file ushell_task.c.

References BOARD_ID, build_cmd(), CCW, cmd, CMD_BACKWARD, CMD_FORWARD, CMD_GET_ID, CMD_GET_STATUS0, CMD_GET_STATUS1, CMD_HELP, CMD_NONE, CMD_RUN, CMD_SET_SPEED, CMD_STOP, cmd_type, convert_param1(), mci_backward(), mci_direction, mci_forward(), mci_get_measured_current(), mci_get_measured_speed(), mci_run(), mci_run_stop, mci_set_speed(), mci_stop(), msg_er_cmd_not_found, msg_help, msg_prompt, param1, print_hex16(), print_msg(), REV_ID, SOFT_ID, uart_putchar, and ushell_active.

Referenced by main().

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 }
void convert_param1(void)
convert param1 to binaire.
Definition: ushell_task.c:336
volatile Bool ushell_active
Definition: ushell_task.c:39
void mci_set_speed(U16 speed)
Definition: mc_interface.c:214
U16 mci_get_measured_current(void)
Get the current measured in the motor.
Definition: mc_interface.c:179
#define CMD_STOP
Definition: ushell_task.h:25
#define CMD_SET_SPEED
Definition: ushell_task.h:28
#define CMD_GET_STATUS0
Definition: ushell_task.h:30
#define uart_putchar
Definition: config.h:85
U8 code msg_prompt[]
Definition: ushell_task.c:51
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
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 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
#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
void mci_backward(void)
Definition: mc_interface.c:138
#define CMD_BACKWARD
Definition: ushell_task.h:27
#define BOARD_ID
Definition: config.h:76
#define CMD_FORWARD
Definition: ushell_task.h:26
#define SOFT_ID
Definition: config.h:77
void build_cmd(void)
get the full command line to be interpreted.
Definition: ushell_task.c:161
void mci_forward(void)
Definition: mc_interface.c:128
#define CMD_NONE
Definition: ushell_task.h:22
U8 mci_get_measured_speed(void)
Measured of speed.
Definition: mc_interface.c:169
#define CMD_GET_ID
Definition: ushell_task.h:29
void print_hex16(U16 value)
Display an U16 with hexa form.
Definition: ushell_task.c:370
U8 code msg_help[]
Definition: ushell_task.c:53
static U8 cmd
Definition: ushell_task.c:30
U8 code msg_er_cmd_not_found[]
Definition: ushell_task.c:54
Here is the call graph for this function:

◆ ushell_task_init()

void ushell_task_init ( void  )

This function initializes the hardware/software ressources required for ushell task.

Parameters
none
Returns
none

/

Definition at line 66 of file ushell_task.c.

References cmd, CMD_NONE, cmd_type, msg_prompt, msg_welcome, print_msg(), and uart_putchar.

Referenced by main().

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 }
#define uart_putchar
Definition: config.h:85
U8 code msg_prompt[]
Definition: ushell_task.c:51
void print_msg(U8 code *str)
Display an ASCII code string.
Definition: ushell_task.c:310
static U8 cmd_type
Definition: ushell_task.c:31
#define CMD_NONE
Definition: ushell_task.h:22
U8 code msg_welcome[]
Definition: ushell_task.c:52
static U8 cmd
Definition: ushell_task.c:30
Here is the call graph for this function:

Variable Documentation

◆ cmd

U8 cmd
static

Definition at line 30 of file ushell_task.c.

Referenced by parse_cmd(), ushell_task(), and ushell_task_init().

◆ cmd_str

U8 cmd_str[26]
static

Definition at line 36 of file ushell_task.c.

Referenced by build_cmd(), and parse_cmd().

◆ cmd_type

U8 cmd_type
static

Definition at line 31 of file ushell_task.c.

Referenced by parse_cmd(), ushell_task(), and ushell_task_init().

◆ i_str

U8 i_str =0
static

Definition at line 37 of file ushell_task.c.

Referenced by build_cmd(), and parse_cmd().

◆ msg_er_cmd_not_found

U8 code msg_er_cmd_not_found[] =MSG_ER_CMD_NOT_FOUND

Definition at line 54 of file ushell_task.c.

Referenced by parse_cmd(), and ushell_task().

◆ msg_help

U8 code msg_help[] =MSG_HELP

Definition at line 53 of file ushell_task.c.

Referenced by ushell_task().

◆ msg_prompt

U8 code msg_prompt[] =MSG_PROMPT

Definition at line 51 of file ushell_task.c.

Referenced by parse_cmd(), ushell_task(), and ushell_task_init().

◆ msg_welcome

U8 code msg_welcome[] =MSG_WELCOME

Definition at line 52 of file ushell_task.c.

Referenced by ushell_task_init().

◆ par_str1

U16 par_str1[8]
static

Definition at line 32 of file ushell_task.c.

Referenced by convert_param1(), and parse_cmd().

◆ par_str2

U16 par_str2[8]
static

Definition at line 33 of file ushell_task.c.

Referenced by convert_param2(), and parse_cmd().

◆ param1

U16 param1
static

Definition at line 34 of file ushell_task.c.

Referenced by convert_param1(), and ushell_task().

◆ param2

U16 param2
static

Definition at line 35 of file ushell_task.c.

Referenced by convert_param2().

◆ speed_error

S16 speed_error

Error calculation.

Definition at line 21 of file mc_control.c.

Referenced by mc_control_speed().

◆ str_backward

U8 code str_backward[] =STR_BACKWARD

Definition at line 45 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_forward

U8 code str_forward[] =STR_FORWARD

Definition at line 44 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_get_id

U8 code str_get_id[] =STR_GET_ID

Definition at line 47 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_get_status0

U8 code str_get_status0[] =STR_GET_STATUS0

Definition at line 48 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_get_status1

U8 code str_get_status1[] =STR_GET_STATUS1

Definition at line 49 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_help

U8 code str_help[] =STR_HELP

Definition at line 43 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_run

U8 code str_run[] =STR_RUN

Definition at line 41 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_set_speed

U8 code str_set_speed[] =STR_SET_SPEED

Definition at line 46 of file ushell_task.c.

Referenced by parse_cmd().

◆ str_stop

U8 code str_stop[] =STR_STOP

Definition at line 42 of file ushell_task.c.

Referenced by parse_cmd().

◆ ushell_active

volatile Bool ushell_active = FALSE

Definition at line 39 of file ushell_task.c.

Referenced by main(), and ushell_task().