linux: Initial support for running Klipper in a Linux real-time process
Add support for compiling the Klipper micro-controller code as a real-time process capable of running on standard Linux systems. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
99
src/linux/main.c
Normal file
99
src/linux/main.c
Normal file
@@ -0,0 +1,99 @@
|
||||
// Main starting point for micro-controller code running on linux systems
|
||||
//
|
||||
// Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include </usr/include/sched.h> // sched_setscheduler
|
||||
#include <stdio.h> // fprintf
|
||||
#include <string.h> // memset
|
||||
#include <unistd.h> // getopt
|
||||
#include "board/misc.h" // console_sendf
|
||||
#include "command.h" // DECL_CONSTANT
|
||||
#include "internal.h" // console_setup
|
||||
#include "sched.h" // sched_main
|
||||
|
||||
DECL_CONSTANT(MCU, "linux");
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* Real-time setup
|
||||
****************************************************************/
|
||||
|
||||
static int
|
||||
realtime_setup(void)
|
||||
{
|
||||
struct sched_param sp;
|
||||
memset(&sp, 0, sizeof(sp));
|
||||
sp.sched_priority = 1;
|
||||
int ret = sched_setscheduler(0, SCHED_FIFO, &sp);
|
||||
if (ret < 0) {
|
||||
report_errno("sched_setscheduler", ret);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* Restart
|
||||
****************************************************************/
|
||||
|
||||
static char **orig_argv;
|
||||
|
||||
void
|
||||
command_config_reset(uint32_t *args)
|
||||
{
|
||||
if (! sched_is_shutdown())
|
||||
shutdown("config_reset only available when shutdown");
|
||||
int ret = execv(orig_argv[0], orig_argv);
|
||||
report_errno("execv", ret);
|
||||
}
|
||||
DECL_COMMAND_FLAGS(config_reset, HF_IN_SHUTDOWN, "config_reset");
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* Startup
|
||||
****************************************************************/
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
// Parse program args
|
||||
orig_argv = argv;
|
||||
int opt, watchdog = 0, realtime = 0;
|
||||
while ((opt = getopt(argc, argv, "wr")) != -1) {
|
||||
switch (opt) {
|
||||
case 'w':
|
||||
watchdog = 1;
|
||||
break;
|
||||
case 'r':
|
||||
realtime = 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Usage: %s [-w] [-r]\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Initial setup
|
||||
if (watchdog) {
|
||||
int ret = watchdog_setup();
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (realtime) {
|
||||
int ret = realtime_setup();
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ret = console_setup("/tmp/klipper_host_mcu");
|
||||
if (ret)
|
||||
return -1;
|
||||
|
||||
// Main loop
|
||||
sched_main();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user