| 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;
44 U8 code str_forward[]=STR_FORWARD;
45 U8 code str_backward[]=STR_BACKWARD;
46 U8 code str_set_speed[]=STR_SET_SPEED;
47 U8 code str_get_id[]=STR_GET_ID;
48 U8 code str_get_status0[]=STR_GET_STATUS0;
49 U8 code str_get_status1[]=STR_GET_STATUS1;
50
51 U8 code msg_prompt[]=MSG_PROMPT;
52 U8 code msg_welcome[]=MSG_WELCOME;
53 U8 code msg_help[]=MSG_HELP;
54 U8 code msg_er_cmd_not_found[]=MSG_ER_CMD_NOT_FOUND;
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
75 print_msg(msg_welcome);
76 print_msg(msg_prompt);
77 cmd=FALSE;
78 cmd_type=CMD_NONE;
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:
106 print_msg(msg_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();
122 mci_set_speed(param1);
123 break;
124 case CMD_GET_ID:
125 print_hex16(BOARD_ID);
126 uart_putchar(' ');
127 print_hex16(SOFT_ID);
128 uart_putchar(' ');
129 print_hex16(REV_ID);
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(' ');
137 print_hex16(mci_get_measured_speed());
138 uart_putchar(' ');
139 print_hex16(mci_get_measured_current());
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
148 print_msg(msg_er_cmd_not_found);
149 }
150 cmd_type=CMD_NONE;
151 cmd=FALSE;
152 print_msg(msg_prompt);
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))
229 { cmd_type=CMD_BACKWARD; }
230 else if ( mystrncmp(cmd_str,str_set_speed,i-1))
231 { cmd_type=CMD_SET_SPEED; }
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))
235 { cmd_type=CMD_GET_STATUS0; }
236 else if ( mystrncmp(cmd_str,str_get_status1,i-1))
237 { cmd_type=CMD_GET_STATUS1; }
238 else
239 {
240 if(i_str)
241 {
242 print_msg(msg_er_cmd_not_found);
243 }
244 print_msg(msg_prompt);
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;
343 param1 = param1 + ascii_to_bin(par_str1[i]);
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;
360 param2 = param2 + ascii_to_bin(par_str2[i]);
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
ABORT_CHAR
#define ABORT_CHAR
Definition: ushell_task.h:40
str_get_id
U8 code str_get_id[]
Definition: ushell_task.c:47
convert_param1
void convert_param1(void)
convert param1 to binaire.
Definition: ushell_task.c:336
MSG_ER_CMD_NOT_FOUND
#define MSG_ER_CMD_NOT_FOUND
Definition: ushell_task.h:57
bin_to_ascii
U8 bin_to_ascii(U8 c)
This function returns the ascii value of a quartet.
Definition: ascii.c:47
mc_interface.h
ushell_active
volatile Bool ushell_active
Definition: ushell_task.c:39
str_run
U8 code str_run[]
Definition: ushell_task.c:41
ushell_task_init
void ushell_task_init(void)
This function initializes the hardware/software ressources required for ushell task.
Definition: ushell_task.c:66
mci_set_speed
void mci_set_speed(U16 speed)
Definition: mc_interface.c:214
ascii_to_bin
U8 ascii_to_bin(U8 c)
This function returns the binary value of an ascii digit.
Definition: ascii.c:32
MSG_WELCOME
#define MSG_WELCOME
Definition: ushell_task.h:56
mci_get_measured_current
U16 mci_get_measured_current(void)
Get the current measured in the motor.
Definition: mc_interface.c:179
str_get_status0
U8 code str_get_status0[]
Definition: ushell_task.c:48
CMD_STOP
#define CMD_STOP
Definition: ushell_task.h:25
CMD_SET_SPEED
#define CMD_SET_SPEED
Definition: ushell_task.h:28
config.h
,v
i_str
static U8 i_str
Definition: ushell_task.c:37
str_backward
U8 code str_backward[]
Definition: ushell_task.c:45
CMD_GET_STATUS0
#define CMD_GET_STATUS0
Definition: ushell_task.h:30
uart_putchar
#define uart_putchar
Definition: config.h:85
MSG_PROMPT
#define MSG_PROMPT
Definition: ushell_task.h:55
STR_BACKWARD
#define STR_BACKWARD
Definition: ushell_task.h:48
msg_prompt
U8 code msg_prompt[]
Definition: ushell_task.c:51
mystrncmp
U8 mystrncmp(U8 *str1, U8 code *str2, U8 i)
compares two strings located in flash code area.
Definition: ushell_task.c:285
convert_param2
void convert_param2(void)
convert param2 to binaire.
Definition: ushell_task.c:353
STR_HELP
#define STR_HELP
Definition: ushell_task.h:46
print_msg
void print_msg(U8 code *str)
Display an ASCII code string.
Definition: ushell_task.c:310
mci_direction
Bool mci_direction
User Input parameter to set motor direction.
Definition: mc_interface.c:20
debug_get_value2
U16 debug_get_value2(void)
cmd_type
static U8 cmd_type
Definition: ushell_task.c:31
mci_stop
void mci_stop(void)
mci_stop stop the motor And reset the speed measured value.
Definition: mc_interface.c:81
param1
static U16 param1
Definition: ushell_task.c:34
CR
#define CR
Definition: ushell_task.h:35
CMD_HELP
#define CMD_HELP
Definition: ushell_task.h:23
REV_ID
#define REV_ID
Definition: config.h:78
CMD_GET_STATUS1
#define CMD_GET_STATUS1
Definition: ushell_task.h:31
mci_run
void mci_run(void)
mci_run run the motor with parameter
Definition: mc_interface.c:34
str_forward
U8 code str_forward[]
Definition: ushell_task.c:44
CMD_RUN
#define CMD_RUN
Definition: ushell_task.h:24
mci_run_stop
Bool mci_run_stop
User Input parameter to launch or stop the motor.
Definition: mc_interface.c:21
STR_GET_STATUS1
#define STR_GET_STATUS1
Definition: ushell_task.h:52
str_stop
U8 code str_stop[]
Definition: ushell_task.c:42
mci_backward
void mci_backward(void)
Definition: mc_interface.c:138
par_str2
static U16 par_str2[8]
Definition: ushell_task.c:33
STR_GET_STATUS0
#define STR_GET_STATUS0
Definition: ushell_task.h:51
CMD_BACKWARD
#define CMD_BACKWARD
Definition: ushell_task.h:27
BOARD_ID
#define BOARD_ID
Definition: config.h:76
param2
static U16 param2
Definition: ushell_task.c:35
STR_RUN
#define STR_RUN
Definition: ushell_task.h:44
str_get_status1
U8 code str_get_status1[]
Definition: ushell_task.c:49
ushell_task.h
CMD_FORWARD
#define CMD_FORWARD
Definition: ushell_task.h:26
SOFT_ID
#define SOFT_ID
Definition: config.h:77
debug_get_value1
U16 debug_get_value1(void)
speed_error
S16 speed_error
Error calculation.
Definition: mc_control.c:21
build_cmd
void build_cmd(void)
get the full command line to be interpreted.
Definition: ushell_task.c:161
STR_GET_ID
#define STR_GET_ID
Definition: ushell_task.h:50
mci_forward
void mci_forward(void)
Definition: mc_interface.c:128
BKSPACE_CHAR
#define BKSPACE_CHAR
Definition: ushell_task.h:39
CMD_NONE
#define CMD_NONE
Definition: ushell_task.h:22
mci_get_measured_speed
U8 mci_get_measured_speed(void)
Measured of speed.
Definition: mc_interface.c:169
parse_cmd
void parse_cmd(void)
decodes full command line into command type and arguments
Definition: ushell_task.c:209
MSG_HELP
#define MSG_HELP
Definition: ushell_task.h:58
cmd_str
static U8 cmd_str[26]
Definition: ushell_task.c:36
str_help
U8 code str_help[]
Definition: ushell_task.c:43
CCW
Definition: config_motor.h:34
CMD_GET_ID
#define CMD_GET_ID
Definition: ushell_task.h:29
par_str1
static U16 par_str1[8]
Definition: ushell_task.c:32
STR_SET_SPEED
#define STR_SET_SPEED
Definition: ushell_task.h:49
print_hex16
void print_hex16(U16 value)
Display an U16 with hexa form.
Definition: ushell_task.c:370
STR_STOP
#define STR_STOP
Definition: ushell_task.h:45
msg_welcome
U8 code msg_welcome[]
Definition: ushell_task.c:52
msg_help
U8 code msg_help[]
Definition: ushell_task.c:53
ascii.h
cmd
static U8 cmd
Definition: ushell_task.c:30
ushell_task
void ushell_task(void)
Entry point of the explorer task management.
Definition: ushell_task.c:93
STR_FORWARD
#define STR_FORWARD
Definition: ushell_task.h:47
str_set_speed
U8 code str_set_speed[]
Definition: ushell_task.c:46
msg_er_cmd_not_found
U8 code msg_er_cmd_not_found[]
Definition: ushell_task.c:54