qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: thuth@redhat.com, me@xcancerberox.com.ar, S.E.Harris@kent.ac.uk,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	dovgaluk@ispras.ru, imammedo@redhat.com, mrolnik@gmail.com,
	aleksandar.m.mail@gmail.com
Subject: [PATCH rc1 20/24] hw/avr: Add some Arduino boards
Date: Wed, 22 Jan 2020 14:03:03 -1000	[thread overview]
Message-ID: <20200123000307.11541-21-richard.henderson@linaro.org> (raw)
In-Reply-To: <20200123000307.11541-1-richard.henderson@linaro.org>

From: Philippe Mathieu-Daudé <f4bug@amsat.org>

Arduino boards are build with AVR chipsets.
Add some of the popular boards:

- Arduino Duemilanove
- Arduino Uno
- Arduino Mega

For more information:
  https://www.arduino.cc/en/Main/Products
  https://store.arduino.cc/arduino-genuino/most-popular

Reviewed-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20200120220107.17825-15-f4bug@amsat.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 hw/avr/arduino.c     | 175 +++++++++++++++++++++++++++++++++++++++++++
 hw/avr/Kconfig       |   4 +
 hw/avr/Makefile.objs |   1 +
 3 files changed, 180 insertions(+)
 create mode 100644 hw/avr/arduino.c

diff --git a/hw/avr/arduino.c b/hw/avr/arduino.c
new file mode 100644
index 0000000000..784246baba
--- /dev/null
+++ b/hw/avr/arduino.c
@@ -0,0 +1,175 @@
+/*
+ * QEMU Arduino boards
+ *
+ * Copyright (c) 2019 Philippe Mathieu-Daudé
+ *
+ * This work is licensed under the terms of the GNU GPLv2 or later.
+ * See the COPYING file in the top-level directory.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+/* TODO: Implement the use of EXTRAM */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "qapi/error.h"
+#include "hw/boards.h"
+#include "hw/loader.h"
+#include "elf.h"
+#include "atmel_atmega.h"
+
+typedef struct ArduinoMachineState {
+    /*< private >*/
+    MachineState parent_obj;
+    /*< public >*/
+    AtmegaMcuState mcu;
+} ArduinoMachineState;
+
+typedef struct ArduinoMachineClass {
+    /*< private >*/
+    MachineClass parent_class;
+    /*< public >*/
+    const char *mcu_type;
+    uint64_t xtal_hz;
+} ArduinoMachineClass;
+
+#define TYPE_ARDUINO_MACHINE \
+        MACHINE_TYPE_NAME("arduino")
+#define ARDUINO_MACHINE(obj) \
+        OBJECT_CHECK(ArduinoMachineState, (obj), TYPE_ARDUINO_MACHINE)
+#define ARDUINO_MACHINE_CLASS(klass) \
+        OBJECT_CLASS_CHECK(ArduinoMachineClass, (klass), TYPE_ARDUINO_MACHINE)
+#define ARDUINO_MACHINE_GET_CLASS(obj) \
+        OBJECT_GET_CLASS(ArduinoMachineClass, (obj), TYPE_ARDUINO_MACHINE)
+
+static void load_firmware(const char *firmware, uint64_t flash_size)
+{
+    const char *filename;
+    int bytes_loaded;
+
+    /* Load firmware (contents of flash) trying to auto-detect format */
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware);
+    if (filename == NULL) {
+        error_report("Unable to find %s", firmware);
+        exit(1);
+    }
+
+    bytes_loaded = load_elf(filename, NULL, NULL, NULL, NULL, NULL, NULL,
+                            0, EM_NONE, 0, 0);
+    if (bytes_loaded < 0) {
+        bytes_loaded = load_image_targphys(filename, OFFSET_CODE, flash_size);
+    }
+    if (bytes_loaded < 0) {
+        error_report("Unable to load firmware image %s as ELF or raw binary",
+                     firmware);
+        exit(1);
+    }
+}
+
+static void arduino_machine_init(MachineState *machine)
+{
+    ArduinoMachineClass *amc = ARDUINO_MACHINE_GET_CLASS(machine);
+    ArduinoMachineState *ams = ARDUINO_MACHINE(machine);
+
+    sysbus_init_child_obj(OBJECT(machine), "mcu", &ams->mcu, sizeof(ams->mcu),
+                          amc->mcu_type);
+    object_property_set_uint(OBJECT(&ams->mcu), amc->xtal_hz,
+                             "xtal-frequency-hz", &error_abort);
+    object_property_set_bool(OBJECT(&ams->mcu), true, "realized",
+                             &error_abort);
+
+    if (machine->firmware) {
+        load_firmware(machine->firmware, memory_region_size(&ams->mcu.flash));
+    }
+}
+
+static void arduino_machine_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+
+    mc->init = arduino_machine_init;
+    mc->default_cpus = 1;
+    mc->min_cpus = mc->default_cpus;
+    mc->max_cpus = mc->default_cpus;
+    mc->no_floppy = 1;
+    mc->no_cdrom = 1;
+    mc->no_parallel = 1;
+}
+
+static void arduino_duemilanove_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+    ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
+
+    /* https://www.arduino.cc/en/Main/ArduinoBoardDuemilanove */
+    mc->desc        = "Arduino Duemilanove (ATmega168)",
+    mc->alias       = "2009";
+    amc->mcu_type   = TYPE_ATMEGA168_MCU;
+    amc->xtal_hz    = 16 * 1000 * 1000;
+};
+
+static void arduino_uno_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+    ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
+
+    /* https://store.arduino.cc/arduino-uno-rev3 */
+    mc->desc        = "Arduino UNO (ATmega328P)";
+    mc->alias       = "uno";
+    amc->mcu_type   = TYPE_ATMEGA328_MCU;
+    amc->xtal_hz    = 16 * 1000 * 1000;
+};
+
+static void arduino_mega_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+    ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
+
+    /* https://www.arduino.cc/en/Main/ArduinoBoardMega */
+    mc->desc        = "Arduino Mega (ATmega1280)";
+    mc->alias       = "mega";
+    amc->mcu_type   = TYPE_ATMEGA1280_MCU;
+    amc->xtal_hz    = 16 * 1000 * 1000;
+};
+
+static void arduino_mega2560_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+    ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
+
+    /* https://store.arduino.cc/arduino-mega-2560-rev3 */
+    mc->desc        = "Arduino Mega 2560 (ATmega2560)";
+    mc->alias       = "mega2560";
+    mc->is_default  = true;
+    amc->mcu_type   = TYPE_ATMEGA2560_MCU;
+    amc->xtal_hz    = 16 * 1000 * 1000; /* CSTCE16M0V53-R0 */
+};
+
+static const TypeInfo arduino_machine_types[] = {
+    {
+        .name          = MACHINE_TYPE_NAME("arduino-duemilanove"),
+        .parent        = TYPE_ARDUINO_MACHINE,
+        .class_init    = arduino_duemilanove_class_init,
+    }, {
+        .name          = MACHINE_TYPE_NAME("arduino-uno"),
+        .parent        = TYPE_ARDUINO_MACHINE,
+        .class_init    = arduino_uno_class_init,
+    }, {
+        .name          = MACHINE_TYPE_NAME("arduino-mega"),
+        .parent        = TYPE_ARDUINO_MACHINE,
+        .class_init    = arduino_mega_class_init,
+    }, {
+        .name          = MACHINE_TYPE_NAME("arduino-mega-2560-v3"),
+        .parent        = TYPE_ARDUINO_MACHINE,
+        .class_init    = arduino_mega2560_class_init,
+    }, {
+        .name           = TYPE_ARDUINO_MACHINE,
+        .parent         = TYPE_MACHINE,
+        .instance_size  = sizeof(ArduinoMachineState),
+        .class_size     = sizeof(ArduinoMachineClass),
+        .class_init     = arduino_machine_class_init,
+        .abstract       = true,
+    }
+};
+
+DEFINE_TYPES(arduino_machine_types)
diff --git a/hw/avr/Kconfig b/hw/avr/Kconfig
index da3b10afec..59d9649d27 100644
--- a/hw/avr/Kconfig
+++ b/hw/avr/Kconfig
@@ -3,3 +3,7 @@ config ATMEL_ATMEGA_MCU
     select ATMEL_TIMER16
     select ATMEL_USART
     select ATMEL_POWER
+
+config ARDUINO
+    select ATMEL_ATMEGA_MCU
+    select UNIMP
diff --git a/hw/avr/Makefile.objs b/hw/avr/Makefile.objs
index 7c7a4c3f67..f6aab1438d 100644
--- a/hw/avr/Makefile.objs
+++ b/hw/avr/Makefile.objs
@@ -1 +1,2 @@
 obj-$(CONFIG_ATMEL_ATMEGA_MCU) += atmel_atmega.o
+obj-$(CONFIG_ARDUINO) += arduino.o
-- 
2.20.1



  parent reply	other threads:[~2020-01-23  0:25 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-23  0:02 [PATCH rc1 00/24] target/avr merger Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 01/24] target/avr: Add outward facing interfaces and core CPU logic Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 02/24] target/avr: Add instruction helpers Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 03/24] target/avr: Add instruction translation - Registers definition Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 04/24] target/avr: Add instruction translation - Arithmetic and Logic Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 05/24] target/avr: Add instruction translation - Branch Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 06/24] target/avr: Add instruction translation - Data Transfer Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 07/24] target/avr: Add instruction translation - Bit and Bit-test Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 08/24] target/avr: Add instruction translation - MCU Control Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 09/24] target/avr: Add instruction translation - CPU main translation function Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 10/24] target/avr: Add instruction disassembly function Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 11/24] hw/char: Add limited support for Atmel USART peripheral Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 12/24] hw/timer: Add limited support for Atmel 16 bit timer peripheral Richard Henderson
2020-01-23  4:49   ` Thomas Huth
2020-01-23 17:29     ` Philippe Mathieu-Daudé
2020-01-23  0:02 ` [PATCH rc1 13/24] hw/misc: Add Atmel power device Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 14/24] target/avr: Add section about AVR into QEMU documentation Richard Henderson
2020-01-23  4:56   ` Thomas Huth
2020-01-24  0:33     ` Philippe Mathieu-Daudé
2020-01-23  0:02 ` [PATCH rc1 15/24] target/avr: Register AVR support with the rest of QEMU Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 16/24] target/avr: Add machine none test Richard Henderson
2020-01-23  5:02   ` Thomas Huth
2020-01-23  0:03 ` [PATCH rc1 17/24] target/avr: Update MAINTAINERS file Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 18/24] hw/avr: Introduce ATMEL_ATMEGA_MCU config Richard Henderson
2020-01-23  5:04   ` Thomas Huth
2020-01-23 17:27     ` Philippe Mathieu-Daudé
2020-01-23  0:03 ` [PATCH rc1 19/24] hw/avr: Add some ATmega microcontrollers Richard Henderson
2020-01-23 23:40   ` Philippe Mathieu-Daudé
2020-01-23  0:03 ` Richard Henderson [this message]
2020-01-23  0:03 ` [PATCH rc1 21/24] target/avr: Update build system Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 22/24] tests/boot-serial-test: Test some Arduino boards (AVR based) Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 23/24] tests/acceptance: Test the Arduino MEGA2560 board Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 24/24] .travis.yml: Run the AVR acceptance tests Richard Henderson
2020-01-23  5:12 ` [PATCH rc1 00/24] target/avr merger Thomas Huth
2020-01-23 11:56   ` Michael Rolnik
2020-01-23 17:36     ` Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200123000307.11541-21-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=S.E.Harris@kent.ac.uk \
    --cc=aleksandar.m.mail@gmail.com \
    --cc=dovgaluk@ispras.ru \
    --cc=f4bug@amsat.org \
    --cc=imammedo@redhat.com \
    --cc=me@xcancerberox.com.ar \
    --cc=mrolnik@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).