Commit | Line | Data |
---|---|---|
58baeec1 MM |
1 | /* |
2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). | |
3 | Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | |
4 | Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. | |
cd011f58 AW |
6 | */ |
7 | ||
8 | ||
0325af12 AW |
9 | #include "libs/Kernel.h" |
10 | #include "SimpleShell.h" | |
11 | #include "libs/nuts_bolts.h" | |
12 | #include "libs/utils.h" | |
423df6df | 13 | #include "libs/SerialMessage.h" |
838b33b4 | 14 | #include "libs/StreamOutput.h" |
3fceb8eb | 15 | #include "modules/robot/Conveyor.h" |
172d42d9 | 16 | #include "DirHandle.h" |
0f0b1656 | 17 | #include "mri.h" |
582559c6 | 18 | #include "version.h" |
8293d443 | 19 | #include "PublicDataRequest.h" |
0325af12 | 20 | |
47339e4a | 21 | #include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h" |
5647f709 | 22 | #include "modules/robot/RobotPublicAccess.h" |
47339e4a | 23 | |
ecc610a4 | 24 | //extern "C" caddr_t _sbrk(int incr); |
6187a020 JM |
25 | extern unsigned int g_maximumHeapAddress; |
26 | ||
ecc610a4 JM |
27 | #include <malloc.h> |
28 | #include <mri.h> | |
29 | #include <stdio.h> | |
30 | #include <stdint.h> | |
31 | ||
32 | extern "C" uint32_t __end__; | |
33 | extern "C" uint32_t __malloc_free_list; | |
34 | extern "C" uint32_t _sbrk(int size); | |
35 | ||
36 | ||
37 | // Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556 | |
38 | static void heapWalk(StreamOutput* stream, bool verbose) | |
39 | { | |
40 | uint32_t chunkNumber = 1; | |
41 | // The __end__ linker symbol points to the beginning of the heap. | |
42 | uint32_t chunkCurr = (uint32_t)&__end__; | |
43 | // __malloc_free_list is the head pointer to newlib-nano's link list of free chunks. | |
44 | uint32_t freeCurr = __malloc_free_list; | |
45 | // Calling _sbrk() with 0 reserves no more memory but it returns the current top of heap. | |
46 | uint32_t heapEnd = _sbrk(0); | |
47 | // accumulate totals | |
48 | uint32_t freeSize= 0; | |
49 | uint32_t usedSize= 0; | |
50 | ||
51 | stream->printf("Used Heap Size: %lu\n", heapEnd - chunkCurr); | |
52 | ||
53 | // Walk through the chunks until we hit the end of the heap. | |
54 | while (chunkCurr < heapEnd) | |
55 | { | |
56 | // Assume the chunk is in use. Will update later. | |
57 | int isChunkFree = 0; | |
58 | // The first 32-bit word in a chunk is the size of the allocation. newlib-nano over allocates by 8 bytes. | |
59 | // 4 bytes for this 32-bit chunk size and another 4 bytes to allow for 8 byte-alignment of returned pointer. | |
60 | uint32_t chunkSize = *(uint32_t*)chunkCurr; | |
61 | // The start of the next chunk is right after the end of this one. | |
62 | uint32_t chunkNext = chunkCurr + chunkSize; | |
63 | ||
64 | // The free list is sorted by address. | |
65 | // Check to see if we have found the next free chunk in the heap. | |
66 | if (chunkCurr == freeCurr) | |
67 | { | |
68 | // Chunk is free so flag it as such. | |
69 | isChunkFree = 1; | |
70 | // The second 32-bit word in a free chunk is a pointer to the next free chunk (again sorted by address). | |
71 | freeCurr = *(uint32_t*)(freeCurr + 4); | |
72 | } | |
73 | ||
74 | // Skip past the 32-bit size field in the chunk header. | |
75 | chunkCurr += 4; | |
76 | // 8-byte align the data pointer. | |
77 | chunkCurr = (chunkCurr + 7) & ~7; | |
78 | // newlib-nano over allocates by 8 bytes, 4 bytes for the 32-bit chunk size and another 4 bytes to allow for 8 | |
79 | // byte-alignment of the returned pointer. | |
80 | chunkSize -= 8; | |
81 | if(verbose) | |
82 | stream->printf(" Chunk: %lu Address: 0x%08lX Size: %lu %s\n", chunkNumber, chunkCurr, chunkSize, isChunkFree ? "CHUNK FREE" : ""); | |
83 | ||
84 | if(isChunkFree) freeSize += chunkSize; | |
85 | else usedSize += chunkSize; | |
86 | ||
87 | chunkCurr = chunkNext; | |
88 | chunkNumber++; | |
89 | } | |
90 | stream->printf("Allocated: %lu, Free: %lu\r\n", usedSize, freeSize); | |
91 | } | |
92 | ||
93 | ||
0325af12 AW |
94 | void SimpleShell::on_module_loaded(){ |
95 | this->current_path = "/"; | |
96 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); | |
ead17727 | 97 | this->reset_delay_secs= 0; |
c4e56997 JM |
98 | |
99 | this->register_for_event(ON_SECOND_TICK); | |
100 | this->register_for_event(ON_GCODE_RECEIVED); | |
ead17727 JM |
101 | } |
102 | ||
5895ead3 | 103 | void SimpleShell::on_second_tick(void*) { |
ead17727 | 104 | // we are timing out for the reset |
5895ead3 JM |
105 | if (this->reset_delay_secs > 0) { |
106 | if(--this->reset_delay_secs == 0){ | |
ead17727 JM |
107 | system_reset(false); |
108 | } | |
109 | } | |
0325af12 AW |
110 | } |
111 | ||
c4e56997 JM |
112 | void SimpleShell::on_gcode_received(void *argument) { |
113 | Gcode *gcode = static_cast<Gcode*>(argument); | |
114 | ||
115 | if (gcode->has_m) { | |
116 | if (gcode->m == 20) { // list sd card | |
117 | gcode->mark_as_taken(); | |
118 | gcode->stream->printf("Begin file list\r\n"); | |
119 | ls_command("/sd", gcode->stream); | |
120 | gcode->stream->printf("End file list\r\n"); | |
121 | } | |
122 | } | |
123 | } | |
124 | ||
0325af12 AW |
125 | // When a new line is received, check if it is a command, and if it is, act upon it |
126 | void SimpleShell::on_console_line_received( void* argument ){ | |
b6c86164 | 127 | SerialMessage new_message = *static_cast<SerialMessage*>(argument); |
7f613782 JM |
128 | |
129 | // ignore comments | |
130 | if(new_message.message[0] == ';') return; | |
131 | ||
b6c86164 | 132 | string possible_command = new_message.message; |
0325af12 | 133 | |
3add9a23 AW |
134 | //new_message.stream->printf("Received %s\r\n", possible_command.c_str()); |
135 | ||
0325af12 | 136 | // We don't compare to a string but to a checksum of that string, this saves some space in flash memory |
7b49793d | 137 | unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient |
cc63083e | 138 | |
0325af12 | 139 | // Act depending on command |
d9664db4 AG |
140 | if (check_sum == ls_command_checksum) |
141 | this->ls_command( get_arguments(possible_command), new_message.stream ); | |
142 | else if (check_sum == cd_command_checksum) | |
143 | this->cd_command( get_arguments(possible_command), new_message.stream ); | |
144 | else if (check_sum == pwd_command_checksum) | |
145 | this->pwd_command( get_arguments(possible_command), new_message.stream ); | |
146 | else if (check_sum == cat_command_checksum) | |
147 | this->cat_command( get_arguments(possible_command), new_message.stream ); | |
b34c7b9b L |
148 | else if (check_sum == break_command_checksum) |
149 | this->break_command(get_arguments(possible_command),new_message.stream ); | |
d9664db4 AG |
150 | else if (check_sum == reset_command_checksum) |
151 | this->reset_command(get_arguments(possible_command),new_message.stream ); | |
152 | else if (check_sum == dfu_command_checksum) | |
2742fca9 | 153 | this->dfu_command(get_arguments(possible_command),new_message.stream ); |
ed7c5844 JM |
154 | else if (check_sum == help_command_checksum) |
155 | this->help_command(get_arguments(possible_command),new_message.stream ); | |
582559c6 JM |
156 | else if (check_sum == version_command_checksum) |
157 | this->version_command(get_arguments(possible_command),new_message.stream ); | |
5647f709 JM |
158 | else if (check_sum == get_command_checksum) |
159 | this->get_command(get_arguments(possible_command),new_message.stream ); | |
991d98cc JM |
160 | else if (check_sum == set_temp_command_checksum) |
161 | this->set_temp_command(get_arguments(possible_command),new_message.stream ); | |
6187a020 JM |
162 | else if (check_sum == mem_command_checksum) |
163 | this->mem_command(get_arguments(possible_command),new_message.stream ); | |
164 | ||
0325af12 AW |
165 | } |
166 | ||
0325af12 AW |
167 | // Convert a path indication ( absolute or relative ) into a path ( absolute ) |
168 | string SimpleShell::absolute_from_relative( string path ){ | |
169 | if( path[0] == '/' ){ return path; } | |
58baeec1 | 170 | if( path[0] == '.' ){ return this->current_path; } |
0325af12 AW |
171 | return this->current_path + path; |
172 | } | |
173 | ||
174 | // Act upon an ls command | |
175 | // Convert the first parameter into an absolute path, then list the files in that path | |
838b33b4 | 176 | void SimpleShell::ls_command( string parameters, StreamOutput* stream ){ |
0325af12 AW |
177 | string folder = this->absolute_from_relative( parameters ); |
178 | DIR* d; | |
179 | struct dirent* p; | |
180 | d = opendir(folder.c_str()); | |
181 | if(d != NULL) { | |
ed7c5844 JM |
182 | while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); } |
183 | closedir(d); | |
0325af12 | 184 | } else { |
b6c86164 | 185 | stream->printf("Could not open directory %s \r\n", folder.c_str()); |
0325af12 AW |
186 | } |
187 | } | |
188 | ||
189 | // Change current absolute path to provided path | |
838b33b4 | 190 | void SimpleShell::cd_command( string parameters, StreamOutput* stream ){ |
0325af12 AW |
191 | string folder = this->absolute_from_relative( parameters ); |
192 | if( folder[folder.length()-1] != '/' ){ folder += "/"; } | |
193 | DIR *d; | |
0325af12 | 194 | d = opendir(folder.c_str()); |
58baeec1 MM |
195 | if(d == NULL) { |
196 | stream->printf("Could not open directory %s \r\n", folder.c_str() ); | |
0325af12 | 197 | }else{ |
ed7c5844 JM |
198 | this->current_path = folder; |
199 | closedir(d); | |
0325af12 AW |
200 | } |
201 | } | |
202 | ||
b7250484 L |
203 | // Responds with the present working directory |
204 | void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){ | |
205 | stream->printf("%s\r\n", this->current_path.c_str()); | |
206 | } | |
207 | ||
0325af12 | 208 | // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output ) |
838b33b4 | 209 | void SimpleShell::cat_command( string parameters, StreamOutput* stream ){ |
58baeec1 MM |
210 | |
211 | // Get parameters ( filename and line limit ) | |
0325af12 AW |
212 | string filename = this->absolute_from_relative(shift_parameter( parameters )); |
213 | string limit_paramater = shift_parameter( parameters ); | |
214 | int limit = -1; | |
f7e6f459 MM |
215 | if( limit_paramater != "" ) |
216 | { | |
217 | char* e = NULL; | |
218 | limit = strtol(limit_paramater.c_str(), &e, 10); | |
219 | if (e <= limit_paramater.c_str()) | |
220 | limit = -1; | |
221 | } | |
58baeec1 MM |
222 | |
223 | // Open file | |
0325af12 | 224 | FILE *lp = fopen(filename.c_str(), "r"); |
9ed670c5 | 225 | if(lp == NULL) { |
58baeec1 MM |
226 | stream->printf("File not found: %s\r\n", filename.c_str()); |
227 | return; | |
9ed670c5 | 228 | } |
0325af12 AW |
229 | string buffer; |
230 | int c; | |
58baeec1 MM |
231 | int newlines = 0; |
232 | ||
0325af12 AW |
233 | // Print each line of the file |
234 | while ((c = fgetc (lp)) != EOF){ | |
58baeec1 | 235 | buffer.append((char *)&c, 1); |
68b7afb4 | 236 | if( char(c) == '\n' ){ |
58baeec1 | 237 | newlines++; |
d728799b | 238 | stream->puts(buffer.c_str()); |
58baeec1 | 239 | buffer.clear(); |
68b7afb4 | 240 | } |
0325af12 | 241 | if( newlines == limit ){ break; } |
58baeec1 | 242 | }; |
0325af12 AW |
243 | fclose(lp); |
244 | ||
245 | } | |
246 | ||
6187a020 JM |
247 | // show free memory |
248 | void SimpleShell::mem_command( string parameters, StreamOutput* stream){ | |
ecc610a4 | 249 | bool verbose= shift_parameter( parameters ).find_first_of("Vv") != string::npos ; |
6187a020 JM |
250 | unsigned long heap= (unsigned long)_sbrk(0); |
251 | unsigned long m= g_maximumHeapAddress - heap; | |
ecc610a4 JM |
252 | stream->printf("Unused Heap: %lu bytes\r\n", m); |
253 | ||
254 | heapWalk(stream, verbose); | |
6187a020 JM |
255 | } |
256 | ||
01f35bcc JM |
257 | static uint32_t getDeviceType() { |
258 | #define IAP_LOCATION 0x1FFF1FF1 | |
259 | uint32_t command[1]; | |
260 | uint32_t result[5]; | |
261 | typedef void (*IAP)(uint32_t*, uint32_t*); | |
262 | IAP iap = (IAP) IAP_LOCATION; | |
263 | ||
264 | __disable_irq(); | |
265 | ||
266 | command[0] = 54; | |
267 | iap(command, result); | |
268 | ||
269 | __enable_irq(); | |
270 | ||
271 | return result[1]; | |
272 | } | |
273 | ||
582559c6 JM |
274 | // print out build version |
275 | void SimpleShell::version_command( string parameters, StreamOutput* stream){ | |
276 | Version vers; | |
01f35bcc JM |
277 | uint32_t dev= getDeviceType(); |
278 | const char* mcu= (dev&0x00100000)?"LPC1769":"LPC1768"; | |
279 | stream->printf("Build version: %s, Build date: %s, MCU: %s, System Clock: %ldMHz\r\n", vers.get_build(), vers.get_build_date(), mcu, SystemCoreClock / 1000000); | |
582559c6 JM |
280 | } |
281 | ||
77983aa1 L |
282 | // Reset the system |
283 | void SimpleShell::reset_command( string parameters, StreamOutput* stream){ | |
ead17727 | 284 | stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n"); |
5895ead3 | 285 | this->reset_delay_secs= 5; // reboot in 5 seconds |
2742fca9 JM |
286 | } |
287 | ||
288 | // go into dfu boot mode | |
289 | void SimpleShell::dfu_command( string parameters, StreamOutput* stream){ | |
ed7c5844 JM |
290 | stream->printf("Entering boot mode...\r\n"); |
291 | system_reset(true); | |
77983aa1 L |
292 | } |
293 | ||
0f0b1656 L |
294 | // Break out into the MRI debugging system |
295 | void SimpleShell::break_command( string parameters, StreamOutput* stream){ | |
296 | stream->printf("Entering MRI debug mode...\r\n"); | |
297 | __debugbreak(); | |
298 | } | |
299 | ||
8293d443 | 300 | // used to test out the get public data events |
5647f709 JM |
301 | void SimpleShell::get_command( string parameters, StreamOutput* stream){ |
302 | int what= get_checksum(shift_parameter( parameters )); | |
b55cfff1 | 303 | void *returned_data; |
c4e56997 | 304 | |
b55cfff1 JM |
305 | if(what == get_temp_command_checksum) { |
306 | string type= shift_parameter( parameters ); | |
307 | bool ok= this->kernel->public_data->get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data ); | |
308 | ||
309 | if(ok) { | |
310 | struct pad_temperature temp= *static_cast<struct pad_temperature*>(returned_data); | |
311 | stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm); | |
312 | }else{ | |
313 | stream->printf("%s is not a known temperature device\r\n", type.c_str()); | |
314 | } | |
c4e56997 | 315 | |
b55cfff1 JM |
316 | }else if(what == get_pos_command_checksum) { |
317 | bool ok= this->kernel->public_data->get_value( robot_checksum, current_position_checksum, &returned_data ); | |
318 | ||
319 | if(ok) { | |
320 | double *pos= static_cast<double *>(returned_data); | |
321 | stream->printf("Position X: %f, Y: %f, Z: %f\r\n", pos[0], pos[1], pos[2]); | |
c4e56997 | 322 | |
b55cfff1 JM |
323 | }else{ |
324 | stream->printf("get pos command failed\r\n"); | |
325 | } | |
326 | } | |
8293d443 JM |
327 | } |
328 | ||
77047e76 JM |
329 | // used to test out the get public data events |
330 | void SimpleShell::set_temp_command( string parameters, StreamOutput* stream){ | |
991d98cc JM |
331 | string type= shift_parameter( parameters ); |
332 | string temp= shift_parameter( parameters ); | |
333 | double t= temp.empty() ? 0.0 : strtod(temp.c_str(), NULL); | |
334 | bool ok= this->kernel->public_data->set_value( temperature_control_checksum, get_checksum(type), &t ); | |
335 | ||
336 | if(ok) { | |
337 | stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t); | |
338 | }else{ | |
339 | stream->printf("%s is not a known temperature device\r\n", type.c_str()); | |
340 | } | |
77047e76 JM |
341 | } |
342 | ||
235a7435 | 343 | void SimpleShell::help_command( string parameters, StreamOutput* stream ){ |
ed7c5844 | 344 | stream->printf("Commands:\r\n"); |
582559c6 | 345 | stream->printf("version\r\n"); |
ecc610a4 | 346 | stream->printf("mem [-v]\r\n"); |
ed7c5844 JM |
347 | stream->printf("ls [folder]\r\n"); |
348 | stream->printf("cd folder\r\n"); | |
c4e56997 | 349 | stream->printf("pwd\r\n"); |
ed7c5844 JM |
350 | stream->printf("cat file [limit]\r\n"); |
351 | stream->printf("play file [-q]\r\n"); | |
352 | stream->printf("progress - shows progress of current play\r\n"); | |
353 | stream->printf("abort - abort currently playing file\r\n"); | |
c4e56997 JM |
354 | stream->printf("reset - reset smoothie\r\n"); |
355 | stream->printf("dfu - enter dfu boot loader\r\n"); | |
356 | stream->printf("break - break into debugger\r\n"); | |
ed7c5844 JM |
357 | stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n"); |
358 | stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n"); | |
359 | stream->printf("config-load [<file_name>]\r\n"); | |
5647f709 | 360 | stream->printf("get temp [bed|hotend]\r\n"); |
991d98cc | 361 | stream->printf("set_temp bed|hotend 185\r\n"); |
b55cfff1 | 362 | stream->printf("get pos\r\n"); |
235a7435 JM |
363 | } |
364 |