aboutsummaryrefslogtreecommitdiffstats
path: root/src/simulator
diff options
context:
space:
mode:
Diffstat (limited to 'src/simulator')
-rw-r--r--src/simulator/Kconfig10
-rw-r--r--src/simulator/Makefile3
-rw-r--r--src/simulator/gpio.c45
-rw-r--r--src/simulator/gpio.h37
-rw-r--r--src/simulator/irq.h31
-rw-r--r--src/simulator/main.c102
-rw-r--r--src/simulator/misc.h21
-rw-r--r--src/simulator/pgm.h13
-rw-r--r--src/simulator/timer.h12
9 files changed, 274 insertions, 0 deletions
diff --git a/src/simulator/Kconfig b/src/simulator/Kconfig
new file mode 100644
index 00000000..539cb0aa
--- /dev/null
+++ b/src/simulator/Kconfig
@@ -0,0 +1,10 @@
+# Kconfig settings for compiling and running the firmware on the host
+# processor for simulation purposes.
+
+if MACH_SIMU
+
+config BOARD_DIRECTORY
+ string
+ default "simulator"
+
+endif
diff --git a/src/simulator/Makefile b/src/simulator/Makefile
new file mode 100644
index 00000000..0dbcdab4
--- /dev/null
+++ b/src/simulator/Makefile
@@ -0,0 +1,3 @@
+# Additional simulator build rules
+
+src-y += simulator/main.c simulator/gpio.c
diff --git a/src/simulator/gpio.c b/src/simulator/gpio.c
new file mode 100644
index 00000000..90d684e9
--- /dev/null
+++ b/src/simulator/gpio.c
@@ -0,0 +1,45 @@
+// GPIO functions on simulator.
+//
+// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include "gpio.h" // gpio_out_write
+
+struct gpio_out gpio_out_setup(uint8_t pin, uint8_t val) {
+ return (struct gpio_out){.pin=pin};
+}
+void gpio_out_toggle(struct gpio_out g) {
+}
+void gpio_out_write(struct gpio_out g, uint8_t val) {
+}
+struct gpio_in gpio_in_setup(uint8_t pin, int8_t pull_up) {
+ return (struct gpio_in){.pin=pin};
+}
+uint8_t gpio_in_read(struct gpio_in g) {
+ return 0;
+}
+struct gpio_pwm gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint8_t val) {
+ return (struct gpio_pwm){.pin=pin};
+}
+void gpio_pwm_write(struct gpio_pwm g, uint8_t val) {
+}
+struct gpio_adc gpio_adc_setup(uint8_t pin) {
+ return (struct gpio_adc){.pin=pin};
+}
+uint32_t gpio_adc_sample_time(void) {
+ return 0;
+}
+uint8_t gpio_adc_sample(struct gpio_adc g) {
+ return 0;
+}
+void gpio_adc_clear_sample(struct gpio_adc g) {
+}
+uint16_t gpio_adc_read(struct gpio_adc g) {
+ return 0;
+}
+
+void spi_config(void) {
+}
+void spi_transfer(char *data, uint8_t len) {
+}
diff --git a/src/simulator/gpio.h b/src/simulator/gpio.h
new file mode 100644
index 00000000..84deb7ef
--- /dev/null
+++ b/src/simulator/gpio.h
@@ -0,0 +1,37 @@
+#ifndef __SIMU_GPIO_H
+#define __SIMU_GPIO_H
+
+#include <stdint.h>
+
+struct gpio_out {
+ uint8_t pin;
+};
+struct gpio_out gpio_out_setup(uint8_t pin, uint8_t val);
+void gpio_out_toggle(struct gpio_out g);
+void gpio_out_write(struct gpio_out g, uint8_t val);
+
+struct gpio_in {
+ uint8_t pin;
+};
+struct gpio_in gpio_in_setup(uint8_t pin, int8_t pull_up);
+uint8_t gpio_in_read(struct gpio_in g);
+
+struct gpio_pwm {
+ uint8_t pin;
+};
+struct gpio_pwm gpio_pwm_setup(uint8_t pin, uint32_t cycle_time, uint8_t val);
+void gpio_pwm_write(struct gpio_pwm g, uint8_t val);
+
+struct gpio_adc {
+ uint8_t pin;
+};
+struct gpio_adc gpio_adc_setup(uint8_t pin);
+uint32_t gpio_adc_sample_time(void);
+uint8_t gpio_adc_sample(struct gpio_adc g);
+void gpio_adc_clear_sample(struct gpio_adc g);
+uint16_t gpio_adc_read(struct gpio_adc g);
+
+void spi_config(void);
+void spi_transfer(char *data, uint8_t len);
+
+#endif // gpio.h
diff --git a/src/simulator/irq.h b/src/simulator/irq.h
new file mode 100644
index 00000000..63f51290
--- /dev/null
+++ b/src/simulator/irq.h
@@ -0,0 +1,31 @@
+#ifndef __SIMU_IRQ_H
+#define __SIMU_IRQ_H
+// Definitions for irq enable/disable on host simulator
+
+#include <stdint.h>
+#include "compiler.h" // barrier
+
+extern uint8_t Interrupt_off;
+
+static inline void irq_disable(void) {
+ Interrupt_off = 1;
+ barrier();
+}
+
+static inline void irq_enable(void) {
+ barrier();
+ Interrupt_off = 0;
+}
+
+static inline uint8_t irq_save(void) {
+ uint8_t flag = Interrupt_off;
+ irq_disable();
+ return flag;
+}
+
+static inline void irq_restore(uint8_t flag) {
+ barrier();
+ Interrupt_off = flag;
+}
+
+#endif // irq.h
diff --git a/src/simulator/main.c b/src/simulator/main.c
new file mode 100644
index 00000000..2d43a138
--- /dev/null
+++ b/src/simulator/main.c
@@ -0,0 +1,102 @@
+// Main starting point for host simulator.
+//
+// Copyright (C) 2016 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 "sched.h" // sched_main
+
+uint8_t Interrupt_off;
+
+
+/****************************************************************
+ * Timers
+ ****************************************************************/
+
+uint32_t
+timer_from_ms(uint32_t ms)
+{
+ return 0; // XXX
+}
+
+void
+timer_periodic(void)
+{
+}
+
+uint32_t
+timer_read_time(void)
+{
+ return 0; // XXX
+}
+
+uint8_t
+timer_set_next(uint32_t next)
+{
+ return 0;
+}
+
+uint8_t
+timer_try_set_next(uint32_t next)
+{
+ return 1;
+}
+
+
+/****************************************************************
+ * Turn stdin/stdout into serial console
+ ****************************************************************/
+
+// XXX
+char *
+console_get_input(uint8_t *plen)
+{
+ *plen = 0;
+ return NULL;
+}
+
+void
+console_pop_input(uint8_t len)
+{
+}
+
+// Return an output buffer that the caller may fill with transmit messages
+char *
+console_get_output(uint8_t len)
+{
+ return NULL;
+}
+
+// Accept the given number of bytes added to the transmit buffer
+void
+console_push_output(uint8_t len)
+{
+}
+
+
+/****************************************************************
+ * Startup
+ ****************************************************************/
+
+// Periodically sleep so we don't consume all CPU
+static 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/misc.h b/src/simulator/misc.h
new file mode 100644
index 00000000..c279794e
--- /dev/null
+++ b/src/simulator/misc.h
@@ -0,0 +1,21 @@
+#ifndef __SIMU_MISC_H
+#define __SIMU_MISC_H
+
+#include <stdint.h>
+
+// main.c
+char *console_get_input(uint8_t *plen);
+void console_pop_input(uint8_t len);
+char *console_get_output(uint8_t len);
+void console_push_output(uint8_t len);
+
+static inline size_t alloc_maxsize(size_t reqsize) {
+ return reqsize;
+}
+
+#define HAVE_OPTIMIZED_CRC 0
+static inline uint16_t _crc16_ccitt(char *buf, uint8_t len) {
+ return 0;
+}
+
+#endif // misc.h
diff --git a/src/simulator/pgm.h b/src/simulator/pgm.h
new file mode 100644
index 00000000..e5f3787d
--- /dev/null
+++ b/src/simulator/pgm.h
@@ -0,0 +1,13 @@
+#ifndef __SIMU_PGM_H
+#define __SIMU_PGM_H
+// This header provides wrappers for the AVR specific "PROGMEM"
+// declarations.
+
+#define PROGMEM
+#define PSTR(S) S
+#define READP(VAR) VAR
+#define vsnprintf_P(D, S, F, A) vsnprintf(D, S, F, A)
+#define strcasecmp_P(S1, S2) strcasecmp(S1, S2)
+#define memcpy_P(DST, SRC, SIZE) memcpy((DST), (SRC), (SIZE))
+
+#endif // pgm.h
diff --git a/src/simulator/timer.h b/src/simulator/timer.h
new file mode 100644
index 00000000..f35b8278
--- /dev/null
+++ b/src/simulator/timer.h
@@ -0,0 +1,12 @@
+#ifndef __SIMU_TIMER_H
+#define __SIMU_TIMER_H
+
+#include <stdint.h>
+
+uint32_t timer_from_ms(uint32_t ms);
+void timer_periodic(void);
+uint32_t timer_read_time(void);
+uint8_t timer_set_next(uint32_t next);
+uint8_t timer_try_set_next(uint32_t next);
+
+#endif // timer.h