int main(void) { // There are a lot of dependencies in the order of these inits. // Don't change the ordering unless you understand this. // Inits can assume that all memory has been zeroed by either // a hardware reset or a watchdog timer reset. cli(); // system and drivers sys_init(); // system hardware setup - must be first rtc_init(); // real time counter xio_init(); // xmega io subsystem sig_init(); // signal flags st_init(); // stepper subsystem - must precede gpio_init() gpio_init(); // switches and parallel IO pwm_init(); // pulse width modulation drivers - must follow gpio_init() // application structures tg_init(STD_INPUT); // tinyg controller (controller.c) - must be first app init; reqs xio_init() cfg_init(); // config records from eeprom - must be next app init mp_init(); // motion planning subsystem cm_init(); // canonical machine - must follow cfg_init() sp_init(); // spindle PWM and variables // now bring up the interupts and get started PMIC_SetVectorLocationToApplication(); // as opposed to boot ROM PMIC_EnableHighLevel(); // all levels are used, so don't bother to abstract them PMIC_EnableMediumLevel(); PMIC_EnableLowLevel(); sei(); // enable global interrupts rpt_print_system_ready_message();// (LAST) announce system is ready _unit_tests(); // run any unit tests that are enabled tg_canned_startup(); // run any pre-loaded commands while (true) { // if (tg.network == NET_MASTER) { // tg_repeater(); // } else if (tg.network == NET_SLAVE) { // tg_receiver(); // } else { tg_controller(); // NET_STANDALONE // } } }
stat_t _command_dispatch() { #ifdef __AVR stat_t status; // read input line or return if not a completed line // xio_gets() is a non-blocking workalike of fgets() while (true) { if ((status = xio_gets(cs.primary_src, cs.in_buf, sizeof(cs.in_buf))) == STAT_OK) { cs.bufp = cs.in_buf; break; } // handle end-of-file from file devices if (status == STAT_EOF) { // EOF can come from file devices only if (cfg.comm_mode == TEXT_MODE) { fprintf_P(stderr, PSTR("End of command file\n")); } else { rpt_exception(STAT_EOF); // not really an exception } tg_reset_source(); // reset to default source } return (status); // Note: STAT_EAGAIN, errors, etc. will drop through } #endif // __AVR #ifdef __ARM // detect USB connection and transition to disconnected state if it disconnected if (SerialUSB.isConnected() == false) cs.state = CONTROLLER_NOT_CONNECTED; // read input line and return if not a completed line if (cs.state == CONTROLLER_READY) { if (read_line(cs.in_buf, &cs.read_index, sizeof(cs.in_buf)) != STAT_OK) { cs.bufp = cs.in_buf; return (STAT_OK); // This is an exception: returns OK for anything NOT OK, so the idler always runs } } else if (cs.state == CONTROLLER_NOT_CONNECTED) { if (SerialUSB.isConnected() == false) return (STAT_OK); cm_request_queue_flush(); rpt_print_system_ready_message(); cs.state = CONTROLLER_STARTUP; } else if (cs.state == CONTROLLER_STARTUP) { // run startup code cs.state = CONTROLLER_READY; } else { return (STAT_OK); } cs.read_index = 0; #endif // __ARM #ifdef __RX stat_t status; parse_gcode_func_selection(CODE_PARSER); // read input line or return if not a completed line // xio_gets() is a non-blocking workalike of fgets() while (true) { if ((status = xio_gets(cs.primary_src, cs.in_buf, sizeof(cs.in_buf))) == STAT_OK) { cs.bufp = cs.in_buf; break; } // handle end-of-file from file devices if (status == STAT_EOF) { // EOF can come from file devices only //gfilerunning = false; xio_close(cs.primary_src); // macro_func_ptr = command_idle; if (cfg.comm_mode == TEXT_MODE) { fprintf_P(stderr, PSTR("End of command file\n")); } else { rpt_exception(STAT_EOF); // not really an exception } tg_reset_source(); // reset to default source } return (status); // Note: STAT_EAGAIN, errors, etc. will drop through } #endif // __AVR // set up the buffers cs.linelen = strlen(cs.in_buf)+1; // linelen only tracks primary input strncpy(cs.saved_buf, cs.bufp, SAVED_BUFFER_LEN-1); // save input buffer for reporting // dispatch the new text line switch (toupper(*cs.bufp)) { // first char case '!': { cm_request_feedhold(); break; } // include for AVR diagnostics and ARM serial case '%': { cm_request_queue_flush(); break; } case '~': { cm_request_cycle_start(); break; } case NUL: { // blank line (just a CR) if (cfg.comm_mode != JSON_MODE) { text_response(STAT_OK, cs.saved_buf); } break; } case '$': case '?': case 'H': { // text mode input cfg.comm_mode = TEXT_MODE; text_response(text_parser(cs.bufp), cs.saved_buf); break; } case '{': { // JSON input cfg.comm_mode = JSON_MODE; json_parser(cs.bufp); break; } default: { // anything else must be Gcode if (cfg.comm_mode == JSON_MODE) { // run it as JSON... strncpy(cs.out_buf, cs.bufp, INPUT_BUFFER_LEN -8); // use out_buf as temp sprintf((char *)cs.bufp,"{\"gc\":\"%s\"}\n", (char *)cs.out_buf); // '-8' is used for JSON chars json_parser(cs.bufp); } else { //...or run it as text text_response(gc_gcode_parser(cs.bufp), cs.saved_buf); } } } return (STAT_OK); }