aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-07-11 13:46:07 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-07-11 14:35:24 -0400
commit5294b3cd2de7487dfa4624934f17a1746efd7a20 (patch)
tree21af00d4d5843b25056c877c8e8238c542633bd8 /src
parent00ea4428a302c67c4c460a087402d177185831da (diff)
downloadkutter-5294b3cd2de7487dfa4624934f17a1746efd7a20.tar.gz
kutter-5294b3cd2de7487dfa4624934f17a1746efd7a20.tar.xz
kutter-5294b3cd2de7487dfa4624934f17a1746efd7a20.zip
simulator: Rework dummy simulator code to user timer_irq / serial_irq
Change the simulator to use the generic timer_irq.c and serial_irq.c code for (dummy) timer and io handling. This is just to make the code a better example for other developers (most micro-controllers will use the timer_irq.c and serial_irq.c code). Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src')
-rw-r--r--src/generic/serial_irq.h2
-rw-r--r--src/simulator/Kconfig8
-rw-r--r--src/simulator/Makefile3
-rw-r--r--src/simulator/main.c109
-rw-r--r--src/simulator/serial.c43
-rw-r--r--src/simulator/timer.c109
6 files changed, 165 insertions, 109 deletions
diff --git a/src/generic/serial_irq.h b/src/generic/serial_irq.h
index 470c9dfa..9aa96ab1 100644
--- a/src/generic/serial_irq.h
+++ b/src/generic/serial_irq.h
@@ -1,6 +1,8 @@
#ifndef __GENERIC_SERIAL_IRQ_H
#define __GENERIC_SERIAL_IRQ_H
+#include <stdint.h> // uint32_t
+
// callback provided by board specific code
void serial_enable_tx_irq(void);
diff --git a/src/simulator/Kconfig b/src/simulator/Kconfig
index 9065bb00..bb74fad6 100644
--- a/src/simulator/Kconfig
+++ b/src/simulator/Kconfig
@@ -15,4 +15,12 @@ config SIMULATOR_SELECT
select HAVE_GPIO_SPI
select HAVE_GPIO_HARD_PWM
+config CLOCK_FREQ
+ int
+ default 20000000
+
+config SERIAL_BAUD
+ int
+ default 250000
+
endif
diff --git a/src/simulator/Makefile b/src/simulator/Makefile
index 34cfa4c8..6447a38b 100644
--- a/src/simulator/Makefile
+++ b/src/simulator/Makefile
@@ -2,5 +2,6 @@
dirs-y += src/simulator src/generic
-src-y += simulator/main.c simulator/gpio.c
+src-y += simulator/main.c simulator/gpio.c simulator/timer.c simulator/serial.c
src-y += generic/crc16_ccitt.c generic/alloc.c
+src-y += generic/timer_irq.c generic/serial_irq.c
diff --git a/src/simulator/main.c b/src/simulator/main.c
index e0384611..6817bbc5 100644
--- a/src/simulator/main.c
+++ b/src/simulator/main.c
@@ -1,122 +1,15 @@
// Main starting point for host simulator.
//
-// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include "board/misc.h" // timer_from_us
-#include "board/irq.h" // irq_disable
#include "sched.h" // sched_main
-
-/****************************************************************
- * Interrupts
- ****************************************************************/
-
-irqstatus_t Interrupt_off;
-
-void
-irq_disable(void)
-{
- Interrupt_off = 1;
- barrier();
-}
-
-void
-irq_enable(void)
-{
- barrier();
- Interrupt_off = 0;
-}
-
-irqstatus_t
-irq_save(void)
-{
- irqstatus_t flag = Interrupt_off;
- irq_disable();
- return flag;
-}
-
-void
-irq_restore(irqstatus_t flag)
-{
- barrier();
- Interrupt_off = flag;
-}
-
-void
-irq_wait(void)
-{
-}
-
-void
-irq_poll(void)
-{
-}
-
-
-/****************************************************************
- * Timers
- ****************************************************************/
-
-uint32_t
-timer_from_us(uint32_t us)
-{
- return 0; // XXX
-}
-
-uint8_t
-timer_is_before(uint32_t time1, uint32_t time2)
-{
- return (int32_t)(time1 - time2) < 0;
-}
-
-uint32_t
-timer_read_time(void)
-{
- return 0; // XXX
-}
-
-void
-timer_kick(void)
-{
-}
-
-
-/****************************************************************
- * Turn stdin/stdout into serial console
- ****************************************************************/
-
-// Encode and transmit a "response" message
-void
-console_sendf(const struct command_encoder *ce, va_list args)
-{
-}
-
-
-/****************************************************************
- * Startup
- ****************************************************************/
-
-// Periodically sleep so we don't consume all CPU
-void
-simu_pause(void)
-{
- // XXX - should check that no timers are present.
- usleep(1);
-}
-DECL_TASK(simu_pause);
-
// Main entry point for simulator.
int
main(void)
{
- // Make stdin non-blocking
- fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
-
sched_main();
return 0;
}
diff --git a/src/simulator/serial.c b/src/simulator/serial.c
new file mode 100644
index 00000000..3c7d008d
--- /dev/null
+++ b/src/simulator/serial.c
@@ -0,0 +1,43 @@
+// Example code for interacting with serial_irq.c
+//
+// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include <fcntl.h> // fcntl
+#include <unistd.h> // STDIN_FILENO
+#include "board/serial_irq.h" // serial_get_tx_byte
+#include "sched.h" // DECL_INIT
+
+void
+serial_init(void)
+{
+ // Make stdin/stdout non-blocking
+ fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
+ fcntl(STDOUT_FILENO, F_SETFL, fcntl(STDOUT_FILENO, F_GETFL, 0) | O_NONBLOCK);
+}
+DECL_INIT(serial_init);
+
+static void
+do_uart(void)
+{
+ for (;;) {
+ uint8_t data;
+ int ret = serial_get_tx_byte(&data);
+ if (ret)
+ break;
+ else
+ write(STDOUT_FILENO, &data, sizeof(data));
+
+ // XXX - Normally the code would check if input data is
+ // available and call serial_rx_byte()
+ }
+}
+
+void
+serial_enable_tx_irq(void)
+{
+ // Normally this would enable the hardware irq, but we just call
+ // do_uart() directly in this demo code.
+ do_uart();
+}
diff --git a/src/simulator/timer.c b/src/simulator/timer.c
new file mode 100644
index 00000000..f30d463a
--- /dev/null
+++ b/src/simulator/timer.c
@@ -0,0 +1,109 @@
+// Example code for running timers in a polling mode
+//
+// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include <time.h> // struct timespec
+#include <unistd.h> // usleep
+#include "autoconf.h" // CONFIG_CLOCK_FREQ
+#include "board/irq.h" // irq_disable
+#include "board/misc.h" // timer_from_us
+#include "board/timer_irq.h" // timer_dispatch_many
+#include "command.h" // DECL_CONSTANT
+#include "sched.h" // DECL_INIT
+
+// Helper function that returns the system time as a 32bit counter
+static uint32_t
+get_system_time(void)
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ double t = (double)ts.tv_sec + (double)ts.tv_nsec * .000000001;
+ return (uint32_t)(t * CONFIG_CLOCK_FREQ);
+}
+
+
+/****************************************************************
+ * Timers
+ ****************************************************************/
+
+static uint32_t next_wake_time;
+
+// Return the current time (in absolute clock ticks).
+uint32_t
+timer_read_time(void)
+{
+ return get_system_time();
+}
+
+// Activate timer dispatch as soon as possible
+void
+timer_kick(void)
+{
+ next_wake_time = timer_read_time();
+}
+
+// Invoke timers - called below from irq_poll()
+static void
+do_timer_dispatch(void)
+{
+ next_wake_time = timer_dispatch_many();
+}
+
+void
+timer_init(void)
+{
+ timer_kick();
+}
+DECL_INIT(timer_init);
+
+
+/****************************************************************
+ * Interrupts
+ ****************************************************************/
+
+// Disable hardware interrupts
+void
+irq_disable(void)
+{
+}
+
+// Enable hardware interrupts
+void
+irq_enable(void)
+{
+}
+
+// Disable hardware interrupts in not already disabled
+irqstatus_t
+irq_save(void)
+{
+ return 0;
+}
+
+// Restore hardware interrupts to state from flag returned by irq_save()
+void
+irq_restore(irqstatus_t flag)
+{
+}
+
+// Atomically enable hardware interrupts and sleep processor until next irq
+void
+irq_wait(void)
+{
+ // XXX - sleep to prevent excessive cpu usage in simulator
+ usleep(1);
+
+ irq_poll();
+}
+
+// Check if an interrupt is active (used only on architectures that do
+// not have hardware interrupts)
+void
+irq_poll(void)
+{
+ uint32_t now = timer_read_time();
+ if (!timer_is_before(now, next_wake_time))
+ do_timer_dispatch();
+}