Skip to contentSkip to conversation

target/avr merger

PATCH In review Philippe Mathieu-Daudé 8 participants 58 messages Jan 24, 2020 – Jan 31, 2020 Page 2 of 2

Subsystems (2)
lore
Share & tools
lore
#35 lore raw

Re: [PATCH rc2 14/25] target/avr: Add section about AVR into QEMU documentation

In reply to Philippe Mathieu-Daudé
Quoted 65 lines · Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Message-Id: <[email protected]>

---
rc2: Fixed typos, s/sample/Arduino/, removed -serial section (thuth)

---
 qemu-doc.texi | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
qemu-doc.texi +37 -0
index 39f950471f..89df1d325e 100644
@@ -1741,6 +1741,7 @@ differences are mentioned in the following sections.
 * Microblaze System emulator::
 * SH4 System emulator::
 * Xtensa System emulator::
+* AVR System emulator::
 @end menu
 
 @node PowerPC System emulator
@@ -2514,6 +2515,56 @@ so should only be used with trusted guest OS.
 
 @c man end
 
+@node AVR System emulator
+@section AVR System emulator
+@cindex system emulation (AVR)
+
+Use the executable @file{qemu-system-avr} to emulates a AVR 8 bit based machine
+having one for the following cores: avr1, avr2, avr25, avr3, avr31, avr35, avr4,
+avr5, avr51, avr6, avrtiny, xmega2, xmega3, xmega4, xmega5, xmega6 and xmega7.
+
+As for now it supports few Arduino boards for educational and testing purposes.
+These boards use a ATmega controller, which model is limited to USART & 16 bit
+timer devices, enought to run FreeRTOS based applications (like this @url{https://github.com/seharris/qemu-avr-tests/blob/master/free-rtos/Demo/AVR_ATMega2560_GCC/demo.elf,,demo})
+
+Following are examples of possible usages, assuming program.elf is compiled for
+AVR cpu
+@itemize
+
+@item Continuous non interrupted execution
+@example
+qemu-system-avr -kernel program.elf
+@end example
+
+@item Continuous non interrupted execution with serial output into telnet window
+@example
+qemu-system-avr -kernel program.elf -serial tcp::5678,server,nowait -nographic
+@end example
+and then in another shell
+@example
+telnet localhost 5678
+@end example
+
+@item Debugging wit GDB debugger
+@example
+qemu-system-avr -kernel program.elf -s -S
+@end example
+and then in another shell
+@example

qemu-doc.texi so should only be used with trusted guest OS. · line 2563

Quoted 10 lines
qemu-doc.texi +10 -0
+avr-gdb program.elf
+@end example
+and then within GDB shell
+@example
+target remote :1234
+@end example
+
+@item Print out executed instructions
+@example
+qemu-system-avr -kernel program.elf -d in_asm

If you don't have a default board anymore, I think you need to list -M here, too.
And didn't you mention that -kernel is not working anyway? Do you need to replace it with -bios ?

Thomas

#36 lore raw

Re: [PATCH rc2 14/25] target/avr: Add section about AVR into QEMU documentation

Hi Thomas.

I will fix it. thanks.

Michael Rolnik

Quoted 97 lines · Thomas Huth

qemu-doc.texi differences are mentioned in the following

Quoted 18 lines · Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Message-Id: <[email protected]>

---
rc2: Fixed typos, s/sample/Arduino/, removed -serial section (thuth)

---
 qemu-doc.texi | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
qemu-doc.texi +0 -0
index 39f950471f..89df1d325e 100644
@@ -1741,6 +1741,7 @@ differences are mentioned in the following

sections.

Quoted 8 lines
qemu-doc.texi +1 -0
 * Microblaze System emulator::
 * SH4 System emulator::
 * Xtensa System emulator::
+* AVR System emulator::
 @end menu
 
 @node PowerPC System emulator

qemu-doc.texi so should only be used with trusted guest OS. · line 2522

Quoted 8 lines
qemu-doc.texi +5 -0
@@ -2514,6 +2515,56 @@ so should only be used with trusted guest OS.
 
 @c man end
 
+@node AVR System emulator
+@section AVR System emulator
+@cindex system emulation (AVR)
+
+Use the executable @file{qemu-system-avr} to emulates a AVR 8 bit based

machine

qemu-doc.texi so should only be used with trusted guest OS. · line 2523

Quoted 1 line
qemu-doc.texi +1 -0
+having one for the following cores: avr1, avr2, avr25, avr3, avr31,

avr35, avr4,

qemu-doc.texi so should only be used with trusted guest OS. · line 2524

Quoted 1 line
qemu-doc.texi +1 -0
+avr5, avr51, avr6, avrtiny, xmega2, xmega3, xmega4, xmega5, xmega6 and

xmega7.

qemu-doc.texi so should only be used with trusted guest OS. · line 2526

Quoted 2 lines
qemu-doc.texi +2 -0
+
+As for now it supports few Arduino boards for educational and testing

purposes.

qemu-doc.texi so should only be used with trusted guest OS. · line 2527

Quoted 1 line
qemu-doc.texi +1 -0
+These boards use a ATmega controller, which model is limited to USART &

16 bit

qemu-doc.texi so should only be used with trusted guest OS. · line 2528

Quoted 1 line
qemu-doc.texi +1 -0
+timer devices, enought to run FreeRTOS based applications (like this

@url{
https://github.com/seharris/qemu-avr-tests/blob/master/free-rtos/Demo/AVR_ATMega2560_GCC/demo.elf,,demo
})

qemu-doc.texi so should only be used with trusted guest OS. · line 2530

Quoted 2 lines
qemu-doc.texi +2 -0
+
+Following are examples of possible usages, assuming program.elf is

compiled for

qemu-doc.texi so should only be used with trusted guest OS.

Quoted 9 lines
qemu-doc.texi +9 -0
+AVR cpu
+@itemize
+
+@item Continuous non interrupted execution
+@example
+qemu-system-avr -kernel program.elf
+@end example
+
+@item Continuous non interrupted execution with serial output into

telnet window

qemu-doc.texi so should only be used with trusted guest OS. · line 2541

Quoted 2 lines
qemu-doc.texi +2 -0
+@example
+qemu-system-avr -kernel program.elf -serial tcp::5678,server,nowait

-nographic

Quoted 12 lines
qemu-doc.texi +12 -0
+@end example
+and then in another shell
+@example
+telnet localhost 5678
+@end example
+
+@item Debugging wit GDB debugger
+@example
+qemu-system-avr -kernel program.elf -s -S
+@end example
+and then in another shell
+@example

qemu-doc.texi so should only be used with trusted guest OS. · line 2563

Quoted 10 lines
qemu-doc.texi +10 -0
+avr-gdb program.elf
+@end example
+and then within GDB shell
+@example
+target remote :1234
+@end example
+
+@item Print out executed instructions
+@example
+qemu-system-avr -kernel program.elf -d in_asm

If you don't have a default board anymore, I think you need to list -M here, too.
And didn't you mention that -kernel is not working anyway? Do you need to replace it with -bios ?

Thomas

Signature · 2 lines
Best Regards,
Michael Rolnik
#37 lore raw

PATCH 15/25 target/avr: Register AVR support with the rest of QEMU

In reply to Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Add AVR related definitions into QEMU

Message-Id: <[email protected]>

---
 qapi/machine.json          |  3 ++-
 include/disas/dis-asm.h    | 19 +++++++++++++++++++
 include/sysemu/arch_init.h |  1 +
 arch_init.c                |  2 ++
 4 files changed, 24 insertions(+), 1 deletion(-)
qapi/machine.json +2 -1
index b3d30bc816..f2dc385167 100644
@@ -21,11 +21,12 @@
 #        is true even for "qemu-system-x86_64".
 #
 # ppcemb: dropped in 3.1
+# avr: since 5.0
 #
 # Since: 3.0
 ##
 { 'enum' : 'SysEmuTarget',
-  'data' : [ 'aarch64', 'alpha', 'arm', 'cris', 'hppa', 'i386', 'lm32',
+  'data' : [ 'aarch64', 'alpha', 'arm', 'avr', 'cris', 'hppa', 'i386', 'lm32',
              'm68k', 'microblaze', 'microblazeel', 'mips', 'mips64',
              'mips64el', 'mipsel', 'moxie', 'nios2', 'or1k', 'ppc',
              'ppc64', 'riscv32', 'riscv64', 's390x', 'sh4',
include/disas/dis-asm.h +19 -0
index e9c7dd8eb4..79bbc8b498 100644
@@ -211,6 +211,25 @@ enum bfd_architecture
 #define bfd_mach_m32r          0  /* backwards compatibility */
   bfd_arch_mn10200,    /* Matsushita MN10200 */
   bfd_arch_mn10300,    /* Matsushita MN10300 */
+  bfd_arch_avr,       /* Atmel AVR microcontrollers.  */
+#define bfd_mach_avr1       1
+#define bfd_mach_avr2       2
+#define bfd_mach_avr25      25
+#define bfd_mach_avr3       3
+#define bfd_mach_avr31      31
+#define bfd_mach_avr35      35
+#define bfd_mach_avr4       4
+#define bfd_mach_avr5       5
+#define bfd_mach_avr51      51
+#define bfd_mach_avr6       6
+#define bfd_mach_avrtiny    100
+#define bfd_mach_avrxmega1  101
+#define bfd_mach_avrxmega2  102
+#define bfd_mach_avrxmega3  103
+#define bfd_mach_avrxmega4  104
+#define bfd_mach_avrxmega5  105
+#define bfd_mach_avrxmega6  106
+#define bfd_mach_avrxmega7  107
   bfd_arch_cris,       /* Axis CRIS */
 #define bfd_mach_cris_v0_v10   255
 #define bfd_mach_cris_v32      32
include/sysemu/arch_init.h +1 -0
index 62c6fe4cf1..893df26ce2 100644
@@ -24,6 +24,7 @@ enum {
     QEMU_ARCH_NIOS2 = (1 << 17),
     QEMU_ARCH_HPPA = (1 << 18),
     QEMU_ARCH_RISCV = (1 << 19),
+    QEMU_ARCH_AVR = (1 << 20),
 };
 
 extern const uint32_t arch_type;
arch_init.c +2 -0
index 705d0b94ad..6a741165b2 100644
@@ -89,6 +89,8 @@ int graphic_depth = 32;
 #define QEMU_ARCH QEMU_ARCH_UNICORE32
 #elif defined(TARGET_XTENSA)
 #define QEMU_ARCH QEMU_ARCH_XTENSA
+#elif defined(TARGET_AVR)
+#define QEMU_ARCH QEMU_ARCH_AVR
 #endif
 
 const uint32_t arch_type = QEMU_ARCH;
Signature · 1 line
2.21.1
#38 lore raw

PATCH 16/25 target/avr: Add machine none test

In reply to Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Message-Id: <[email protected]>

---
 tests/qtest/machine-none-test.c | 1 +
 1 file changed, 1 insertion(+)
tests/qtest/machine-none-test.c +1 -0
index 5953d31755..3e5c74e73e 100644
@@ -27,6 +27,7 @@ static struct arch2cpu cpus_map[] = {
     /* tested targets list */
     { "arm", "cortex-a15" },
     { "aarch64", "cortex-a57" },
+    { "avr", "avr6-avr-cpu" },
     { "x86_64", "qemu64,apic-id=0" },
     { "i386", "qemu32,apic-id=0" },
     { "alpha", "ev67" },
Signature · 1 line
2.21.1
#39 lore raw

PATCH 17/25 target/avr: Update MAINTAINERS file

In reply to Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Include AVR maintaners in MAINTAINERS file

Message-Id: <[email protected]>

[rth: Squash ordering fixes from f4bug]

---
rc2: Remove hw/misc/avr_mask (renamed as hw/misc/atmel_power)

---
 MAINTAINERS | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
MAINTAINERS +24 -0
index 2c768ed3d8..066515ac8e 100644
@@ -163,6 +163,14 @@ S: Maintained
 F: hw/arm/smmu*
 F: include/hw/arm/smmu*
 
+AVR TCG CPUs
+M: Michael Rolnik <[email protected]>
+R: Sarah Harris <[email protected]>
+S: Maintained
+F: target/avr/
+F: default-configs/avr-softmmu.mak
+F: gdb-xml/avr-cpu.xml
+
 CRIS TCG CPUs
 M: Edgar E. Iglesias <[email protected]>
 S: Maintained
@@ -880,6 +888,22 @@ F: include/hw/*/nrf51*.h
 F: include/hw/*/microbit*.h
 F: tests/qtest/microbit-test.c
 
+AVR Machines
+-------------
+
+Atmel MCU
+M: Michael Rolnik <[email protected]>
+R: Sarah Harris <[email protected]>
+S: Maintained
+F: hw/avr/
+F: hw/char/atmel_usart.c
+F: include/hw/char/atmel_usart.h
+F: hw/timer/atmel_timer16.c
+F: include/hw/timer/atmel_timer16.h
+F: hw/misc/atmel_power.c
+F: include/hw/misc/atmel_power.h
+F: tests/acceptance/machine_avr6.py
+
 CRIS Machines
 -------------
 Axis Dev88
Signature · 1 line
2.21.1
#40 lore raw

PATCH 18/25 hw/core/loader: Let load_elf populate the processor-specific flags

In reply to Philippe Mathieu-Daudé

One some architectures (like AVR) we can determine the cpu type by reading the ELF flags.

Message-Id: <<[email protected]> [PMD: Extracted from bigger patch,
Replaced 'uint32_t *pe_flags' by 'int proc_flags']

---
 include/hw/elf_ops.h |  6 +++++-
 include/hw/loader.h  |  6 ++++--
 hw/core/loader.c     | 15 ++++++++-------
 hw/riscv/boot.c      |  2 +-
 4 files changed, 18 insertions(+), 11 deletions(-)
include/hw/elf_ops.h +5 -1
index e07d276df7..5d0f9587d3 100644
@@ -316,7 +316,8 @@ static int glue(load_elf, SZ)(const char *name, int fd,
                               void *translate_opaque,
                               int must_swab, uint64_t *pentry,
                               uint64_t *lowaddr, uint64_t *highaddr,
-                              int elf_machine, int clear_lsb, int data_swab,
+                              int *proc_flags, int elf_machine,
+                              int clear_lsb, int data_swab,
                               AddressSpace *as, bool load_rom,
                               symbol_fn_t sym_cb)
 {
@@ -389,6 +390,9 @@ static int glue(load_elf, SZ)(const char *name, int fd,
             }
     }
 
+    if (proc_flags) {
+        *proc_flags = (elf_sword)ehdr.e_flags;
+    }
     if (pentry)
         *pentry = (uint64_t)(elf_sword)ehdr.e_entry;
 
include/hw/loader.h +4 -2
index 48a96cd559..cc5ede7b90 100644
@@ -101,6 +101,7 @@ const char *load_elf_strerror(int error);
  * @pentry: Populated with program entry point. Ignored if NULL.
  * @lowaddr: Populated with lowest loaded address. Ignored if NULL.
  * @highaddr: Populated with highest loaded address. Ignored if NULL.
+ * @proc_flags: Populated with ELF processor-specific flags. Ignore if NULL.
  * @bigendian: Expected ELF endianness. 0 for LE otherwise BE
  * @elf_machine: Expected ELF machine type
  * @clear_lsb: Set to mask off LSB of addresses (Some architectures use
@@ -131,8 +132,9 @@ int load_elf_ram_sym(const char *filename,
                      uint64_t (*elf_note_fn)(void *, void *, bool),
                      uint64_t (*translate_fn)(void *, uint64_t),
                      void *translate_opaque, uint64_t *pentry,
-                     uint64_t *lowaddr, uint64_t *highaddr, int big_endian,
-                     int elf_machine, int clear_lsb, int data_swab,
+                     uint64_t *lowaddr, uint64_t *highaddr, int *proc_flags,
+                     int big_endian, int elf_machine,
+                     int clear_lsb, int data_swab,
                      AddressSpace *as, bool load_rom, symbol_fn_t sym_cb);
 
 /** load_elf_ram:
hw/core/loader.c +8 -7
index 5099f27dc8..3bee2f8ae0 100644
@@ -438,7 +438,7 @@ int load_elf_ram(const char *filename,
 {
     return load_elf_ram_sym(filename, elf_note_fn,
                             translate_fn, translate_opaque,
-                            pentry, lowaddr, highaddr, big_endian,
+                            pentry, lowaddr, highaddr, NULL, big_endian,
                             elf_machine, clear_lsb, data_swab, as,
                             load_rom, NULL);
 }
@@ -448,8 +448,9 @@ int load_elf_ram_sym(const char *filename,
                      uint64_t (*elf_note_fn)(void *, void *, bool),
                      uint64_t (*translate_fn)(void *, uint64_t),
                      void *translate_opaque, uint64_t *pentry,
-                     uint64_t *lowaddr, uint64_t *highaddr, int big_endian,
-                     int elf_machine, int clear_lsb, int data_swab,
+                     uint64_t *lowaddr, uint64_t *highaddr, int *proc_flags,
+                     int big_endian, int elf_machine,
+                     int clear_lsb, int data_swab,
                      AddressSpace *as, bool load_rom, symbol_fn_t sym_cb)
 {
     int fd, data_order, target_data_order, must_swab, ret = ELF_LOAD_FAILED;
@@ -490,13 +491,13 @@ int load_elf_ram_sym(const char *filename,
     if (e_ident[EI_CLASS] == ELFCLASS64) {
         ret = load_elf64(filename, fd, elf_note_fn,
                          translate_fn, translate_opaque, must_swab,
-                         pentry, lowaddr, highaddr, elf_machine, clear_lsb,
-                         data_swab, as, load_rom, sym_cb);
+                         pentry, lowaddr, highaddr, proc_flags, elf_machine,
+                         clear_lsb, data_swab, as, load_rom, sym_cb);
     } else {
         ret = load_elf32(filename, fd, elf_note_fn,
                          translate_fn, translate_opaque, must_swab,
-                         pentry, lowaddr, highaddr, elf_machine, clear_lsb,
-                         data_swab, as, load_rom, sym_cb);
+                         pentry, lowaddr, highaddr, proc_flags, elf_machine,
+                         clear_lsb, data_swab, as, load_rom, sym_cb);
     }
 
  fail:
hw/riscv/boot.c +1 -1
index 027303d2a3..746ca1f795 100644
@@ -119,7 +119,7 @@ target_ulong riscv_load_kernel(const char *kernel_filename, symbol_fn_t sym_cb)
     uint64_t kernel_entry, kernel_high;
 
     if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL,
-                         &kernel_entry, NULL, &kernel_high, 0,
+                         &kernel_entry, NULL, &kernel_high, NULL, 0,
                          EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
         return kernel_entry;
     }
Signature · 1 line
2.21.1
#41 lore raw

PATCH 19/25 hw/avr: Add helper to load raw/ELF firmware binaries

In reply to Philippe Mathieu-Daudé

Add avr_load_firmware() function to load firmware in ELF or raw binary format.

---
 hw/avr/boot.h        | 33 ++++++++++++++++++++
 include/elf.h        |  2 ++
 hw/avr/boot.c        | 74 ++++++++++++++++++++++++++++++++++++++++++++
 hw/avr/Makefile.objs |  1 +
 4 files changed, 110 insertions(+)
 create mode 100644 hw/avr/boot.h
 create mode 100644 hw/avr/boot.c
 create mode 100644 hw/avr/Makefile.objs
hw/avr/boot.h new file +33 -0
new file mode 100644
index 0000000000..62bc10c922
@@ -0,0 +1,33 @@
+/*
+ * AVR loader helpers
+ *
+ * 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
+ */
+
+#ifndef HW_AVR_BOOT_H
+#define HW_AVR_BOOT_H
+
+#include "hw/boards.h"
+#include "cpu.h"
+
+/**
+ * avr_load_firmware:   load an image into a memory region
+ *
+ * @cpu:        Handle a AVR CPU object
+ * @ms:         A MachineState
+ * @mr:         Memory Region to load into
+ * @firmware:   Path to the firmware file (raw binary or ELF format)
+ *
+ * Load a firmware supplied by the machine or by the user  with the
+ * '-bios' command line option, and put it in target memory.
+ *
+ * Returns: true on success, false on error.
+ */
+bool avr_load_firmware(AVRCPU *cpu, MachineState *ms,
+                       MemoryRegion *mr, const char *firmware);
+
+#endif
include/elf.h +2 -0
index 3501e0c8d0..53cdfa23b7 100644
@@ -202,6 +202,8 @@ typedef struct mips_elf_abiflags_v0 {
 #define EM_MOXIE           223     /* Moxie processor family */
 #define EM_MOXIE_OLD       0xFEED
 
+#define EM_AVR 83 /* AVR 8-bit microcontroller */
+
 /* This is the info that is needed to parse the dynamic section of the file */
 #define DT_NULL		0
 #define DT_NEEDED	1
hw/avr/boot.c new file +74 -0

74 lines not on this page · raw message

hw/avr/Makefile.objs new file +1 -0
new file mode 100644
index 0000000000..123f174f0e
@@ -0,0 +1 @@
+obj-y += boot.o
Signature · 1 line
2.21.1
#42 lore raw

PATCH 20/25 hw/avr: Add some ATmega microcontrollers

In reply to Philippe Mathieu-Daudé

Add some microcontrollers from the megaAVR family (ATmega series):

- middle range: ATmega168 and ATmega328
- high range: ATmega1280 and ATmega2560

For product comparison:
https://www.microchip.com/wwwproducts/ProductCompare/ATmega168P/ATmega328P
https://www.microchip.com/wwwproducts/ProductCompare/ATmega1280/ATmega2560

Datasheets:
http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf
http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf

Message-Id: <[email protected]>

---
rc2:
- Squashed Kconfig patch (Thomas)
- Correct SRAM base address

---
 hw/avr/atmel_atmega.h |  48 +++++
 hw/avr/atmel_atmega.c | 470 ++++++++++++++++++++++++++++++++++++++++++
 hw/avr/Kconfig        |   5 +
 hw/avr/Makefile.objs  |   1 +
 4 files changed, 524 insertions(+)
 create mode 100644 hw/avr/atmel_atmega.h
 create mode 100644 hw/avr/atmel_atmega.c
 create mode 100644 hw/avr/Kconfig
hw/avr/atmel_atmega.h new file +48 -0
new file mode 100644
index 0000000000..391b8b1bf8
@@ -0,0 +1,48 @@
+/*
+ * QEMU ATmega MCU
+ *
+ * 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
+ */
+
+#ifndef HW_AVR_ATMEL_ATMEGA_H
+#define HW_AVR_ATMEL_ATMEGA_H
+
+#include "hw/char/atmel_usart.h"
+#include "hw/timer/atmel_timer16.h"
+#include "hw/misc/atmel_power.h"
+#include "target/avr/cpu.h"
+
+#define TYPE_ATMEGA_MCU     "ATmega"
+#define TYPE_ATMEGA168_MCU  "ATmega168"
+#define TYPE_ATMEGA328_MCU  "ATmega328"
+#define TYPE_ATMEGA1280_MCU "ATmega1280"
+#define TYPE_ATMEGA2560_MCU "ATmega2560"
+
+#define ATMEGA_MCU(obj) OBJECT_CHECK(AtmegaMcuState, (obj), TYPE_ATMEGA_MCU)
+
+#define POWER_MAX 2
+#define USART_MAX 4
+#define TIMER_MAX 6
+#define GPIO_MAX 12
+
+typedef struct AtmegaMcuState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+    /*< public >*/
+
+    AVRCPU cpu;
+    MemoryRegion flash;
+    MemoryRegion eeprom;
+    MemoryRegion sram;
+    DeviceState *io;
+    AVRMaskState pwr[POWER_MAX];
+    AVRUsartState usart[USART_MAX];
+    AVRTimer16State timer[TIMER_MAX];
+    uint64_t xtal_freq_hz;
+} AtmegaMcuState;
+
+#endif /* HW_AVR_ATMEL_ATMEGA_H */
hw/avr/atmel_atmega.c new file +470 -0

470 lines not on this page · raw message

hw/avr/Kconfig new file +5 -0
new file mode 100644
index 0000000000..da3b10afec
@@ -0,0 +1,5 @@
+config ATMEL_ATMEGA_MCU
+    bool
+    select ATMEL_TIMER16
+    select ATMEL_USART
+    select ATMEL_POWER
hw/avr/Makefile.objs +1 -0
index 123f174f0e..1f73fd5469 100644
@@ -1 +1,2 @@
 obj-y += boot.o
+obj-$(CONFIG_ATMEL_ATMEGA_MCU) += atmel_atmega.o
Signature · 1 line
2.21.1
#43 lore raw

Re: [PATCH rc2 20/25] hw/avr: Add some ATmega microcontrollers

Quoted 24 lines · Philippe Mathieu-Daudé

Add some microcontrollers from the megaAVR family (ATmega series):

- middle range: ATmega168 and ATmega328
- high range: ATmega1280 and ATmega2560

For product comparison:
https://www.microchip.com/wwwproducts/ProductCompare/
ATmega168P/ATmega328P
https://www.microchip.com/wwwproducts/ProductCompare/
ATmega1280/ATmega2560

Datasheets:
http://ww1.microchip.com/downloads/en/DeviceDoc/
ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf
http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2549-8-bit-AVR-
Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf

Message-Id: <[email protected]>

---
rc2:
- Squashed Kconfig patch (Thomas)
- Correct SRAM base address

Quoted 11 lines
---
 hw/avr/atmel_atmega.h |  48 +++++
 hw/avr/atmel_atmega.c | 470 ++++++++++++++++++++++++++++++++++++++++++
 hw/avr/Kconfig        |   5 +
 hw/avr/Makefile.objs  |   1 +
 4 files changed, 524 insertions(+)
 create mode 100644 hw/avr/atmel_atmega.h
 create mode 100644 hw/avr/atmel_atmega.c
 create mode 100644 hw/avr/Kconfig

This patch introduces a terminology confusion that will be difficult to eliminate once the code is in the tree.

"AVR" is the name (or, if someone wishes, the common name prefix) of the family of all MCUs covered in this series. This is reflected in the existence of the directory "hw/avr", and this is, in my opinion fine.

"Atmel" is the company that used to design AVR MCUs (there are hundreds of such MCUs). "Atmel" was acquired several years ago by another company, "Microchip". Trere is no element in AVR MCUs' docimentation that reffers to the producers (except title, copyright, etc). In all recent docs, "Atmel" is complely eradicated.

Therefore, using the name of non-existant company "Atmel" is confusing, and, moreover, completely unnecessary. Please, remove references to "Atmel" from file names, and from the code.

Thanks, Aleksandar

Quoted 567 lines
hw/avr/atmel_atmega.h new file +25 -0
new file mode 100644
index 0000000000..391b8b1bf8
@@ -0,0 +1,48 @@
+/*
+ * QEMU ATmega MCU
+ *
+ * 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
+ */
+
+#ifndef HW_AVR_ATMEL_ATMEGA_H
+#define HW_AVR_ATMEL_ATMEGA_H
+
+#include "hw/char/atmel_usart.h"
+#include "hw/timer/atmel_timer16.h"
+#include "hw/misc/atmel_power.h"
+#include "target/avr/cpu.h"
+
+#define TYPE_ATMEGA_MCU     "ATmega"
+#define TYPE_ATMEGA168_MCU  "ATmega168"
+#define TYPE_ATMEGA328_MCU  "ATmega328"
+#define TYPE_ATMEGA1280_MCU "ATmega1280"
+#define TYPE_ATMEGA2560_MCU "ATmega2560"
+
+#define ATMEGA_MCU(obj) OBJECT_CHECK(AtmegaMcuState, (obj),

TYPE_ATMEGA_MCU)

hw/avr/atmel_atmega.h new file +23 -0
+
+#define POWER_MAX 2
+#define USART_MAX 4
+#define TIMER_MAX 6
+#define GPIO_MAX 12
+
+typedef struct AtmegaMcuState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+    /*< public >*/
+
+    AVRCPU cpu;
+    MemoryRegion flash;
+    MemoryRegion eeprom;
+    MemoryRegion sram;
+    DeviceState *io;
+    AVRMaskState pwr[POWER_MAX];
+    AVRUsartState usart[USART_MAX];
+    AVRTimer16State timer[TIMER_MAX];
+    uint64_t xtal_freq_hz;
+} AtmegaMcuState;
+
+#endif /* HW_AVR_ATMEL_ATMEGA_H */
hw/avr/atmel_atmega.c new file +20 -0
new file mode 100644
index 0000000000..790c374dee
@@ -0,0 +1,470 @@
+/*
+ * QEMU ATmega MCU
+ *
+ * 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
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/module.h"
+#include "qemu/units.h"
+#include "qapi/error.h"
+#include "exec/memory.h"
+#include "exec/address-spaces.h"
+#include "sysemu/sysemu.h"
+#include "hw/qdev-properties.h"
+#include "hw/sysbus.h"
+#include "hw/boards.h" /* FIXME memory_region_allocate_system_memory for

sram */

hw/avr/atmel_atmega.c new file +68 -0
+#include "hw/misc/unimp.h"
+#include "atmel_atmega.h"
+
+enum AtmegaPeripheral {
+    POWER0, POWER1,
+    GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF,
+    GPIOG, GPIOH, GPIOI, GPIOJ, GPIOK, GPIOL,
+    USART0, USART1, USART2, USART3,
+    TIMER0, TIMER1, TIMER2, TIMER3, TIMER4, TIMER5,
+    PERIFMAX
+};
+
+#define GPIO(n)     (n + GPIOA)
+#define USART(n)    (n + USART0)
+#define TIMER(n)    (n + TIMER0)
+#define POWER(n)    (n + POWER0)
+
+typedef struct {
+    uint16_t addr;
+    enum AtmegaPeripheral power_index;
+    uint8_t power_bit;
+    /* timer specific */
+    uint16_t intmask_addr;
+    uint16_t intflag_addr;
+    bool is_timer16;
+} peripheral_cfg;
+
+typedef struct AtmegaMcuClass {
+    /*< private >*/
+    SysBusDeviceClass parent_class;
+    /*< public >*/
+    const char *uc_name;
+    const char *cpu_type;
+    size_t flash_size;
+    size_t eeprom_size;
+    size_t sram_size;
+    size_t io_size;
+    size_t gpio_count;
+    size_t adc_count;
+    const uint8_t *irq;
+    const peripheral_cfg *dev;
+} AtmegaMcuClass;
+
+#define ATMEGA_MCU_CLASS(klass) \
+    OBJECT_CLASS_CHECK(AtmegaMcuClass, (klass), TYPE_ATMEGA_MCU)
+#define ATMEGA_MCU_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(AtmegaMcuClass, (obj), TYPE_ATMEGA_MCU)
+
+static const peripheral_cfg dev168_328[PERIFMAX] = {
+    [USART0]        = {  0xc0, POWER0, 1 },
+    [TIMER2]        = {  0xb0, POWER0, 6, 0x70, 0x37, false },
+    [TIMER1]        = {  0x80, POWER0, 3, 0x6f, 0x36, true },
+    [POWER0]        = {  0x64 },
+    [TIMER0]        = {  0x44, POWER0, 5, 0x6e, 0x35, false },
+    [GPIOD]         = {  0x29 },
+    [GPIOC]         = {  0x26 },
+    [GPIOB]         = {  0x23 },
+}, dev1280_2560[PERIFMAX] = {
+    [USART3]        = { 0x130, POWER1, 2 },
+    [TIMER5]        = { 0x120, POWER1, 5, 0x73, 0x3a, true },
+    [GPIOL]         = { 0x109 },
+    [GPIOK]         = { 0x106 },
+    [GPIOJ]         = { 0x103 },
+    [GPIOH]         = { 0x100 },
+    [USART2]        = {  0xd0, POWER1, 1 },
+    [USART1]        = {  0xc8, POWER1, 0 },
+    [USART0]        = {  0xc0, POWER0, 1 },
+    [TIMER2]        = {  0xb0, POWER0, 6, 0x70, 0x37, false }, /* TODO

async */

hw/avr/atmel_atmega.c new file +112 -0
+    [TIMER4]        = {  0xa0, POWER1, 4, 0x72, 0x39, true },
+    [TIMER3]        = {  0x90, POWER1, 3, 0x71, 0x38, true },
+    [TIMER1]        = {  0x80, POWER0, 3, 0x6f, 0x36, true },
+    [POWER1]        = {  0x65 },
+    [POWER0]        = {  0x64 },
+    [TIMER0]        = {  0x44, POWER0, 5, 0x6e, 0x35, false },
+    [GPIOG]         = {  0x32 },
+    [GPIOF]         = {  0x2f },
+    [GPIOE]         = {  0x2c },
+    [GPIOD]         = {  0x29 },
+    [GPIOC]         = {  0x26 },
+    [GPIOB]         = {  0x23 },
+    [GPIOA]         = {  0x20 },
+};
+
+enum AtmegaIrq {
+    USART0_RXC_IRQ, USART0_DRE_IRQ, USART0_TXC_IRQ,
+    USART1_RXC_IRQ, USART1_DRE_IRQ, USART1_TXC_IRQ,
+    USART2_RXC_IRQ, USART2_DRE_IRQ, USART2_TXC_IRQ,
+    USART3_RXC_IRQ, USART3_DRE_IRQ, USART3_TXC_IRQ,
+    TIMER0_CAPT_IRQ, TIMER0_COMPA_IRQ, TIMER0_COMPB_IRQ,
+        TIMER0_COMPC_IRQ, TIMER0_OVF_IRQ,
+    TIMER1_CAPT_IRQ, TIMER1_COMPA_IRQ, TIMER1_COMPB_IRQ,
+        TIMER1_COMPC_IRQ, TIMER1_OVF_IRQ,
+    TIMER2_CAPT_IRQ, TIMER2_COMPA_IRQ, TIMER2_COMPB_IRQ,
+        TIMER2_COMPC_IRQ, TIMER2_OVF_IRQ,
+    TIMER3_CAPT_IRQ, TIMER3_COMPA_IRQ, TIMER3_COMPB_IRQ,
+        TIMER3_COMPC_IRQ, TIMER3_OVF_IRQ,
+    TIMER4_CAPT_IRQ, TIMER4_COMPA_IRQ, TIMER4_COMPB_IRQ,
+        TIMER4_COMPC_IRQ, TIMER4_OVF_IRQ,
+    TIMER5_CAPT_IRQ, TIMER5_COMPA_IRQ, TIMER5_COMPB_IRQ,
+        TIMER5_COMPC_IRQ, TIMER5_OVF_IRQ,
+    IRQ_COUNT
+};
+
+#define USART_IRQ_COUNT     3
+#define USART_RXC_IRQ(n)    (n * USART_IRQ_COUNT + USART0_RXC_IRQ)
+#define USART_DRE_IRQ(n)    (n * USART_IRQ_COUNT + USART0_DRE_IRQ)
+#define USART_TXC_IRQ(n)    (n * USART_IRQ_COUNT + USART0_TXC_IRQ)
+#define TIMER_IRQ_COUNT     5
+#define TIMER_CAPT_IRQ(n)   (n * TIMER_IRQ_COUNT + TIMER0_CAPT_IRQ)
+#define TIMER_COMPA_IRQ(n)  (n * TIMER_IRQ_COUNT + TIMER0_COMPA_IRQ)
+#define TIMER_COMPB_IRQ(n)  (n * TIMER_IRQ_COUNT + TIMER0_COMPB_IRQ)
+#define TIMER_COMPC_IRQ(n)  (n * TIMER_IRQ_COUNT + TIMER0_COMPC_IRQ)
+#define TIMER_OVF_IRQ(n)    (n * TIMER_IRQ_COUNT + TIMER0_OVF_IRQ)
+
+static const uint8_t irq168_328[IRQ_COUNT] = {
+    [TIMER2_COMPA_IRQ]      = 8,
+    [TIMER2_COMPB_IRQ]      = 9,
+    [TIMER2_OVF_IRQ]        = 10,
+    [TIMER1_CAPT_IRQ]       = 11,
+    [TIMER1_COMPA_IRQ]      = 12,
+    [TIMER1_COMPB_IRQ]      = 13,
+    [TIMER1_OVF_IRQ]        = 14,
+    [TIMER0_COMPA_IRQ]      = 15,
+    [TIMER0_COMPB_IRQ]      = 16,
+    [TIMER0_OVF_IRQ]        = 17,
+    [USART0_RXC_IRQ]        = 19,
+    [USART0_DRE_IRQ]        = 20,
+    [USART0_TXC_IRQ]        = 21,
+}, irq1280_2560[IRQ_COUNT] = {
+    [TIMER2_COMPA_IRQ]      = 14,
+    [TIMER2_COMPB_IRQ]      = 15,
+    [TIMER2_OVF_IRQ]        = 16,
+    [TIMER1_CAPT_IRQ]       = 17,
+    [TIMER1_COMPA_IRQ]      = 18,
+    [TIMER1_COMPB_IRQ]      = 19,
+    [TIMER1_COMPC_IRQ]      = 20,
+    [TIMER1_OVF_IRQ]        = 21,
+    [TIMER0_COMPA_IRQ]      = 22,
+    [TIMER0_COMPB_IRQ]      = 23,
+    [TIMER0_OVF_IRQ]        = 24,
+    [USART0_RXC_IRQ]        = 26,
+    [USART0_DRE_IRQ]        = 27,
+    [USART0_TXC_IRQ]        = 28,
+    [TIMER3_CAPT_IRQ]       = 32,
+    [TIMER3_COMPA_IRQ]      = 33,
+    [TIMER3_COMPB_IRQ]      = 34,
+    [TIMER3_COMPC_IRQ]      = 35,
+    [TIMER3_OVF_IRQ]        = 36,
+    [USART1_RXC_IRQ]        = 37,
+    [USART1_DRE_IRQ]        = 38,
+    [USART1_TXC_IRQ]        = 39,
+    [TIMER4_CAPT_IRQ]       = 42,
+    [TIMER4_COMPA_IRQ]      = 43,
+    [TIMER4_COMPB_IRQ]      = 44,
+    [TIMER4_COMPC_IRQ]      = 45,
+    [TIMER4_OVF_IRQ]        = 46,
+    [TIMER5_CAPT_IRQ]       = 47,
+    [TIMER5_COMPA_IRQ]      = 48,
+    [TIMER5_COMPB_IRQ]      = 49,
+    [TIMER5_COMPC_IRQ]      = 50,
+    [TIMER5_OVF_IRQ]        = 51,
+    [USART2_RXC_IRQ]        = 52,
+    [USART2_DRE_IRQ]        = 53,
+    [USART2_TXC_IRQ]        = 54,
+    [USART3_RXC_IRQ]        = 55,
+    [USART3_DRE_IRQ]        = 56,
+    [USART3_TXC_IRQ]        = 57,
+};
+
+static void connect_peripheral_irq(const AtmegaMcuClass *mc,
+                                   SysBusDevice *sbd,
+                                   DeviceState *dev, int n,
+                                   unsigned peripheral_irq)
+{
+    int irq = mc->irq[peripheral_irq];
+
+    if (!irq) {
+        return;
+    }
+    /* FIXME move that to avr_cpu_set_int() once 'sample' board is

removed */

hw/avr/atmel_atmega.c new file +32 -0
+    assert(irq >= 2);
+    irq -= 2;
+
+    sysbus_connect_irq(sbd, n, qdev_get_gpio_in(dev, irq));
+}
+
+static void connect_power_reduction_gpio(AtmegaMcuState *s,
+                                         const AtmegaMcuClass *mc,
+                                         DeviceState *dev,
+                                         int peripheral_index)
+{
+    unsigned power_index = mc->dev[peripheral_index].power_index;
+    assert(mc->dev[power_index].addr);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->pwr[power_index - POWER0]),
+                       mc->dev[peripheral_index].power_bit,
+                       qdev_get_gpio_in(dev, 0));
+}
+
+static void atmega_realize(DeviceState *dev, Error **errp)
+{
+    AtmegaMcuState *s = ATMEGA_MCU(dev);
+    const AtmegaMcuClass *mc = ATMEGA_MCU_GET_CLASS(dev);
+    DeviceState *cpudev;
+    SysBusDevice *sbd;
+    Error *err = NULL;
+    char *devname;
+    size_t i;
+
+    assert(mc->io_size <= 0x200);
+
+    if (!s->xtal_freq_hz) {
+        error_setg(errp, "\"xtal-frequency-hz\" property must be

provided.");

hw/avr/atmel_atmega.c new file +11 -0
+        return;
+    }
+
+    /* CPU */
+    object_initialize_child(OBJECT(dev), "cpu", &s->cpu, sizeof(s->cpu),
+                            mc->cpu_type, &err, NULL);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+    object_property_set_bool(OBJECT(&s->cpu), true, "realized",

&error_abort);

hw/avr/atmel_atmega.c new file +12 -0
+    cpudev = DEVICE(&s->cpu);
+
+    /* SRAM */
+    memory_region_init_ram(&s->sram, OBJECT(dev), "sram", mc->sram_size,
+                           &error_abort);
+    memory_region_add_subregion(get_system_memory(),
+                                OFFSET_DATA + mc->io_size, &s->sram);
+
+    /* Flash */
+    memory_region_init_rom(&s->flash, OBJECT(dev),
+                           "flash", mc->flash_size, &error_fatal);
+    memory_region_add_subregion(get_system_memory(), OFFSET_CODE,

&s->flash);

hw/avr/atmel_atmega.c new file +74 -1
+
+    /*
+     * I/O
+     *
+     * 0x00 - 0x1f: Registers
+     * 0x20 - 0x5f: I/O memory
+     * 0x60 - 0xff: Extended I/O
+     */
+    s->io = qdev_create(NULL, TYPE_UNIMPLEMENTED_DEVICE);
+    qdev_prop_set_string(s->io, "name", "I/O");
+    qdev_prop_set_uint64(s->io, "size", mc->io_size);
+    qdev_init_nofail(s->io);
+    sysbus_mmio_map_overlap(SYS_BUS_DEVICE(s->io), 0, OFFSET_DATA,
-1234);
+
+    /* Power Reduction */
+    for (i = 0; i < POWER_MAX; i++) {
+        int idx = POWER(i);
+        if (!mc->dev[idx].addr) {
+            continue;
+        }
+        devname = g_strdup_printf("power%zu", i);
+        object_initialize_child(OBJECT(dev), devname,
+                                &s->pwr[i], sizeof(s->pwr[i]),
+                                TYPE_AVR_MASK, &error_abort, NULL);
+        object_property_set_bool(OBJECT(&s->pwr[i]), true, "realized",
+                                 &error_abort);
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->pwr[i]), 0,
+                        OFFSET_DATA + mc->dev[idx].addr);
+        g_free(devname);
+    }
+
+    /* GPIO */
+    for (i = 0; i < GPIO_MAX; i++) {
+        int idx = GPIO(i);
+        if (!mc->dev[idx].addr) {
+            continue;
+        }
+        devname = g_strdup_printf("avr-gpio-%c", 'a' + (char)i);
+        create_unimplemented_device(devname,
+                                    OFFSET_DATA + mc->dev[idx].addr, 3);
+        g_free(devname);
+    }
+
+    /* USART */
+    for (i = 0; i < USART_MAX; i++) {
+        int idx = USART(i);
+        if (!mc->dev[idx].addr) {
+            continue;
+        }
+        devname = g_strdup_printf("usart%zu", i);
+        object_initialize_child(OBJECT(dev), devname,
+                                &s->usart[i], sizeof(s->usart[i]),
+                                TYPE_AVR_USART, &error_abort, NULL);
+        qdev_prop_set_chr(DEVICE(&s->usart[i]), "chardev", serial_hd(i));
+        object_property_set_bool(OBJECT(&s->usart[i]), true, "realized",
+                                 &error_abort);
+        sbd = SYS_BUS_DEVICE(&s->usart[i]);
+        sysbus_mmio_map(sbd, 0, OFFSET_DATA + mc->dev[USART(i)].addr);
+        connect_peripheral_irq(mc, sbd, cpudev, 0, USART_RXC_IRQ(i));
+        connect_peripheral_irq(mc, sbd, cpudev, 1, USART_DRE_IRQ(i));
+        connect_peripheral_irq(mc, sbd, cpudev, 2, USART_TXC_IRQ(i));
+        connect_power_reduction_gpio(s, mc, DEVICE(&s->usart[i]), idx);
+        g_free(devname);
+    }
+
+    /* Timer */
+    for (i = 0; i < TIMER_MAX; i++) {
+        int idx = TIMER(i);
+        if (!mc->dev[idx].addr) {
+            continue;
+        }
+        if (!mc->dev[idx].is_timer16) {
+            create_unimplemented_device("avr-timer8",
+                                        OFFSET_DATA + mc->dev[idx].addr,

5);

hw/avr/atmel_atmega.c new file +30 -0
+            create_unimplemented_device("avr-timer8-intmask",
+                                        OFFSET_DATA
+                                        + mc->dev[idx].intmask_addr, 1);
+            create_unimplemented_device("avr-timer8-intflag",
+                                        OFFSET_DATA
+                                        + mc->dev[idx].intflag_addr, 1);
+            continue;
+        }
+        devname = g_strdup_printf("timer%zu", i);
+        object_initialize_child(OBJECT(dev), devname,
+                                &s->timer[i], sizeof(s->timer[i]),
+                                TYPE_AVR_TIMER16, &error_abort, NULL);
+        object_property_set_uint(OBJECT(&s->timer[i]), s->xtal_freq_hz,
+                                 "cpu-frequency-hz", &error_abort);
+        object_property_set_bool(OBJECT(&s->timer[i]), true, "realized",
+                                 &error_abort);
+        sbd = SYS_BUS_DEVICE(&s->timer[i]);
+        sysbus_mmio_map(sbd, 0, OFFSET_DATA + mc->dev[idx].addr);
+        sysbus_mmio_map(sbd, 1, OFFSET_DATA + mc->dev[idx].intmask_addr);
+        sysbus_mmio_map(sbd, 2, OFFSET_DATA + mc->dev[idx].intflag_addr);
+        connect_peripheral_irq(mc, sbd, cpudev, 0, TIMER_CAPT_IRQ(i));
+        connect_peripheral_irq(mc, sbd, cpudev, 1, TIMER_COMPA_IRQ(i));
+        connect_peripheral_irq(mc, sbd, cpudev, 2, TIMER_COMPB_IRQ(i));
+        connect_peripheral_irq(mc, sbd, cpudev, 3, TIMER_COMPC_IRQ(i));
+        connect_peripheral_irq(mc, sbd, cpudev, 4, TIMER_OVF_IRQ(i));
+        connect_power_reduction_gpio(s, mc, DEVICE(&s->timer[i]), idx);
+        g_free(devname);
+    }
+
+    create_unimplemented_device("avr-twi",          OFFSET_DATA + 0x0b8,

6);
+ create_unimplemented_device("avr-adc", OFFSET_DATA + 0x078,
8);
+ create_unimplemented_device("avr-ext-mem-ctrl", OFFSET_DATA + 0x074,
2);
+ create_unimplemented_device("avr-watchdog", OFFSET_DATA + 0x060,
1);
+ create_unimplemented_device("avr-spi", OFFSET_DATA + 0x04c,
3);
+ create_unimplemented_device("avr-eeprom", OFFSET_DATA + 0x03f,
3);

hw/avr/atmel_atmega.c new file +106 -0
+}
+
+static Property atmega_props[] = {
+    DEFINE_PROP_UINT64("xtal-frequency-hz", AtmegaMcuState,
+                       xtal_freq_hz, 0),
+    DEFINE_PROP_END_OF_LIST()
+};
+
+static void atmega_class_init(ObjectClass *oc, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+
+    dc->realize = atmega_realize;
+    dc->props = atmega_props;
+    /* Reason: Mapped at fixed location on the system bus */
+    dc->user_creatable = false;
+}
+
+static void atmega168_class_init(ObjectClass *oc, void *data)
+{
+    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+    amc->cpu_type = AVR_CPU_TYPE_NAME("avr5");
+    amc->flash_size = 16 * KiB;
+    amc->eeprom_size = 512;
+    amc->sram_size = 1 * KiB;
+    amc->io_size = 256;
+    amc->gpio_count = 23;
+    amc->adc_count = 6;
+    amc->irq = irq168_328;
+    amc->dev = dev168_328;
+};
+
+static void atmega328_class_init(ObjectClass *oc, void *data)
+{
+    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+    amc->cpu_type = AVR_CPU_TYPE_NAME("avr5");
+    amc->flash_size = 32 * KiB;
+    amc->eeprom_size = 1 * KiB;
+    amc->sram_size = 2 * KiB;
+    amc->io_size = 256;
+    amc->gpio_count = 23;
+    amc->adc_count = 6;
+    amc->irq = irq168_328;
+    amc->dev = dev168_328;
+};
+
+static void atmega1280_class_init(ObjectClass *oc, void *data)
+{
+    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+    amc->cpu_type = AVR_CPU_TYPE_NAME("avr6");
+    amc->flash_size = 128 * KiB;
+    amc->eeprom_size = 4 * KiB;
+    amc->sram_size = 8 * KiB;
+    amc->io_size = 512;
+    amc->gpio_count = 86;
+    amc->adc_count = 16;
+    amc->irq = irq1280_2560;
+    amc->dev = dev1280_2560;
+};
+
+static void atmega2560_class_init(ObjectClass *oc, void *data)
+{
+    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+    amc->cpu_type = AVR_CPU_TYPE_NAME("avr6");
+    amc->flash_size = 256 * KiB;
+    amc->eeprom_size = 4 * KiB;
+    amc->sram_size = 8 * KiB;
+    amc->io_size = 512;
+    amc->gpio_count = 54;
+    amc->adc_count = 16;
+    amc->irq = irq1280_2560;
+    amc->dev = dev1280_2560;
+};
+
+static const TypeInfo atmega_mcu_types[] = {
+    {
+        .name           = TYPE_ATMEGA168_MCU,
+        .parent         = TYPE_ATMEGA_MCU,
+        .class_init     = atmega168_class_init,
+    }, {
+        .name           = TYPE_ATMEGA328_MCU,
+        .parent         = TYPE_ATMEGA_MCU,
+        .class_init     = atmega328_class_init,
+    }, {
+        .name           = TYPE_ATMEGA1280_MCU,
+        .parent         = TYPE_ATMEGA_MCU,
+        .class_init     = atmega1280_class_init,
+    }, {
+        .name           = TYPE_ATMEGA2560_MCU,
+        .parent         = TYPE_ATMEGA_MCU,
+        .class_init     = atmega2560_class_init,
+    }, {
+        .name           = TYPE_ATMEGA_MCU,
+        .parent         = TYPE_SYS_BUS_DEVICE,
+        .instance_size  = sizeof(AtmegaMcuState),
+        .class_size     = sizeof(AtmegaMcuClass),
+        .class_init     = atmega_class_init,
+        .abstract       = true,
+    }
+};
+
+DEFINE_TYPES(atmega_mcu_types)
hw/avr/Kconfig new file +5 -0
new file mode 100644
index 0000000000..da3b10afec
@@ -0,0 +1,5 @@
+config ATMEL_ATMEGA_MCU
+    bool
+    select ATMEL_TIMER16
+    select ATMEL_USART
+    select ATMEL_POWER
hw/avr/Makefile.objs +1 -1
index 123f174f0e..1f73fd5469 100644
@@ -1 +1,2 @@
 obj-y += boot.o
+obj-$(CONFIG_ATMEL_ATMEGA_MCU) += atmel_atmega.o
--

2.21.1

#44 lore raw

Re: [PATCH rc2 20/25] hw/avr: Add some ATmega microcontrollers

Quoted 40 lines · Aleksandar Markovic

On Friday, January 24, 2020, Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

Add some microcontrollers from the megaAVR family (ATmega series):

- middle range: ATmega168 and ATmega328
- high range: ATmega1280 and ATmega2560

For product comparison:
https://www.microchip.com/wwwproducts/ProductCompare/ATmega168P/ATmega328P
<https://www.microchip.com/wwwproducts/ProductCompare/ATmega168P/ATmega328P>
https://www.microchip.com/wwwproducts/ProductCompare/ATmega1280/ATmega2560
<https://www.microchip.com/wwwproducts/ProductCompare/ATmega1280/ATmega2560>

Datasheets:
http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf
<http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf>
http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf
<http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf>

<mailto:[email protected]>>
Message-Id: <[email protected] <mailto:[email protected]>>

<mailto:[email protected]>>
---
rc2:
- Squashed Kconfig patch (Thomas)
- Correct SRAM base address
---
 hw/avr/atmel_atmega.h |  48 +++++
 hw/avr/atmel_atmega.c | 470 ++++++++++++++++++++++++++++++++++++++++++ hw/avr/Kconfig        |   5 +
 hw/avr/Makefile.objs  |   1 +
 4 files changed, 524 insertions(+)
 create mode 100644 hw/avr/atmel_atmega.h
 create mode 100644 hw/avr/atmel_atmega.c
 create mode 100644 hw/avr/Kconfig

Quoted 7 lines

This patch introduces a terminology confusion that will be difficult to eliminate once the code is in the tree.

"AVR" is the name (or, if someone wishes, the common name prefix) of the family of all MCUs covered in this series. This is reflected in the existence of the directory "hw/avr", and this is, in my opinion fine.

"Atmel" is the company that used to design AVR MCUs (there are hundreds of such MCUs). "Atmel" was acquired several years ago by another company, "Microchip". Trere is no element in AVR MCUs' docimentation that reffers to the producers (except title, copyright, etc). In all recent docs, "Atmel" is complely eradicated.

Therefore, using the name of non-existant company "Atmel" is confusing, and, moreover, completely unnecessary. Please, remove references to "Atmel" from file names, and from the code.

I simply used "Atmel" because this is how I discovered this chips and always called them by the Atmel brand. The Microchip acquisition is quite recent. When I look for datasheet is I still write "Atmel something" and eventually end on the microchip website.

IOW it was not a deliberate name, simply Microchip could not eradicate "Atmel" from my memory =)

I'll rename these files at "atmega.{c,h}".

Thanks,

Phil.

Quoted 576 lines
    diff --git a/hw/avr/atmel_atmega.h b/hw/avr/atmel_atmega.h
    new file mode 100644
    index 0000000000..391b8b1bf8
    --- /dev/null
    +++ b/hw/avr/atmel_atmega.h
    @@ -0,0 +1,48 @@
    +/*
    + * QEMU ATmega MCU
    + *
    + * 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
    + */
    +
    +#ifndef HW_AVR_ATMEL_ATMEGA_H
    +#define HW_AVR_ATMEL_ATMEGA_H
    +
    +#include "hw/char/atmel_usart.h"
    +#include "hw/timer/atmel_timer16.h"
    +#include "hw/misc/atmel_power.h"
    +#include "target/avr/cpu.h"
    +
    +#define TYPE_ATMEGA_MCU     "ATmega"
    +#define TYPE_ATMEGA168_MCU  "ATmega168"
    +#define TYPE_ATMEGA328_MCU  "ATmega328"
    +#define TYPE_ATMEGA1280_MCU "ATmega1280"
    +#define TYPE_ATMEGA2560_MCU "ATmega2560"
    +
    +#define ATMEGA_MCU(obj) OBJECT_CHECK(AtmegaMcuState, (obj),
    TYPE_ATMEGA_MCU)
    +
    +#define POWER_MAX 2
    +#define USART_MAX 4
    +#define TIMER_MAX 6
    +#define GPIO_MAX 12
    +
    +typedef struct AtmegaMcuState {
    +    /*< private >*/
    +    SysBusDevice parent_obj;
    +    /*< public >*/
    +
    +    AVRCPU cpu;
    +    MemoryRegion flash;
    +    MemoryRegion eeprom;
    +    MemoryRegion sram;
    +    DeviceState *io;
    +    AVRMaskState pwr[POWER_MAX];
    +    AVRUsartState usart[USART_MAX];
    +    AVRTimer16State timer[TIMER_MAX];
    +    uint64_t xtal_freq_hz;
    +} AtmegaMcuState;
    +
    +#endif /* HW_AVR_ATMEL_ATMEGA_H */
    diff --git a/hw/avr/atmel_atmega.c b/hw/avr/atmel_atmega.c
    new file mode 100644
    index 0000000000..790c374dee
    --- /dev/null
    +++ b/hw/avr/atmel_atmega.c
    @@ -0,0 +1,470 @@
    +/*
    + * QEMU ATmega MCU
    + *
    + * 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
    + */
    +
    +#include "qemu/osdep.h"
    +#include "qemu/module.h"
    +#include "qemu/units.h"
    +#include "qapi/error.h"
    +#include "exec/memory.h"
    +#include "exec/address-spaces.h"
    +#include "sysemu/sysemu.h"
    +#include "hw/qdev-properties.h"
    +#include "hw/sysbus.h"
    +#include "hw/boards.h" /* FIXME
    memory_region_allocate_system_memory for sram */
    +#include "hw/misc/unimp.h"
    +#include "atmel_atmega.h"
    +
    +enum AtmegaPeripheral {
    +    POWER0, POWER1,
    +    GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF,
    +    GPIOG, GPIOH, GPIOI, GPIOJ, GPIOK, GPIOL,
    +    USART0, USART1, USART2, USART3,
    +    TIMER0, TIMER1, TIMER2, TIMER3, TIMER4, TIMER5,
    +    PERIFMAX
    +};
    +
    +#define GPIO(n)     (n + GPIOA)
    +#define USART(n)    (n + USART0)
    +#define TIMER(n)    (n + TIMER0)
    +#define POWER(n)    (n + POWER0)
    +
    +typedef struct {
    +    uint16_t addr;
    +    enum AtmegaPeripheral power_index;
    +    uint8_t power_bit;
    +    /* timer specific */
    +    uint16_t intmask_addr;
    +    uint16_t intflag_addr;
    +    bool is_timer16;
    +} peripheral_cfg;
    +
    +typedef struct AtmegaMcuClass {
    +    /*< private >*/
    +    SysBusDeviceClass parent_class;
    +    /*< public >*/
    +    const char *uc_name;
    +    const char *cpu_type;
    +    size_t flash_size;
    +    size_t eeprom_size;
    +    size_t sram_size;
    +    size_t io_size;
    +    size_t gpio_count;
    +    size_t adc_count;
    +    const uint8_t *irq;
    +    const peripheral_cfg *dev;
    +} AtmegaMcuClass;
    +
    +#define ATMEGA_MCU_CLASS(klass) \
    +    OBJECT_CLASS_CHECK(AtmegaMcuClass, (klass), TYPE_ATMEGA_MCU)
    +#define ATMEGA_MCU_GET_CLASS(obj) \
    +    OBJECT_GET_CLASS(AtmegaMcuClass, (obj), TYPE_ATMEGA_MCU)
    +
    +static const peripheral_cfg dev168_328[PERIFMAX] = {
    +    [USART0]        = {  0xc0, POWER0, 1 },
    +    [TIMER2]        = {  0xb0, POWER0, 6, 0x70, 0x37, false },
    +    [TIMER1]        = {  0x80, POWER0, 3, 0x6f, 0x36, true },
    +    [POWER0]        = {  0x64 },
    +    [TIMER0]        = {  0x44, POWER0, 5, 0x6e, 0x35, false },
    +    [GPIOD]         = {  0x29 },
    +    [GPIOC]         = {  0x26 },
    +    [GPIOB]         = {  0x23 },
    +}, dev1280_2560[PERIFMAX] = {
    +    [USART3]        = { 0x130, POWER1, 2 },
    +    [TIMER5]        = { 0x120, POWER1, 5, 0x73, 0x3a, true },
    +    [GPIOL]         = { 0x109 },
    +    [GPIOK]         = { 0x106 },
    +    [GPIOJ]         = { 0x103 },
    +    [GPIOH]         = { 0x100 },
    +    [USART2]        = {  0xd0, POWER1, 1 },
    +    [USART1]        = {  0xc8, POWER1, 0 },
    +    [USART0]        = {  0xc0, POWER0, 1 },
    +    [TIMER2]        = {  0xb0, POWER0, 6, 0x70, 0x37, false }, /*
    TODO async */
    +    [TIMER4]        = {  0xa0, POWER1, 4, 0x72, 0x39, true },
    +    [TIMER3]        = {  0x90, POWER1, 3, 0x71, 0x38, true },
    +    [TIMER1]        = {  0x80, POWER0, 3, 0x6f, 0x36, true },
    +    [POWER1]        = {  0x65 },
    +    [POWER0]        = {  0x64 },
    +    [TIMER0]        = {  0x44, POWER0, 5, 0x6e, 0x35, false },
    +    [GPIOG]         = {  0x32 },
    +    [GPIOF]         = {  0x2f },
    +    [GPIOE]         = {  0x2c },
    +    [GPIOD]         = {  0x29 },
    +    [GPIOC]         = {  0x26 },
    +    [GPIOB]         = {  0x23 },
    +    [GPIOA]         = {  0x20 },
    +};
    +
    +enum AtmegaIrq {
    +    USART0_RXC_IRQ, USART0_DRE_IRQ, USART0_TXC_IRQ,
    +    USART1_RXC_IRQ, USART1_DRE_IRQ, USART1_TXC_IRQ,
    +    USART2_RXC_IRQ, USART2_DRE_IRQ, USART2_TXC_IRQ,
    +    USART3_RXC_IRQ, USART3_DRE_IRQ, USART3_TXC_IRQ,
    +    TIMER0_CAPT_IRQ, TIMER0_COMPA_IRQ, TIMER0_COMPB_IRQ,
    +        TIMER0_COMPC_IRQ, TIMER0_OVF_IRQ,
    +    TIMER1_CAPT_IRQ, TIMER1_COMPA_IRQ, TIMER1_COMPB_IRQ,
    +        TIMER1_COMPC_IRQ, TIMER1_OVF_IRQ,
    +    TIMER2_CAPT_IRQ, TIMER2_COMPA_IRQ, TIMER2_COMPB_IRQ,
    +        TIMER2_COMPC_IRQ, TIMER2_OVF_IRQ,
    +    TIMER3_CAPT_IRQ, TIMER3_COMPA_IRQ, TIMER3_COMPB_IRQ,
    +        TIMER3_COMPC_IRQ, TIMER3_OVF_IRQ,
    +    TIMER4_CAPT_IRQ, TIMER4_COMPA_IRQ, TIMER4_COMPB_IRQ,
    +        TIMER4_COMPC_IRQ, TIMER4_OVF_IRQ,
    +    TIMER5_CAPT_IRQ, TIMER5_COMPA_IRQ, TIMER5_COMPB_IRQ,
    +        TIMER5_COMPC_IRQ, TIMER5_OVF_IRQ,
    +    IRQ_COUNT
    +};
    +
    +#define USART_IRQ_COUNT     3
    +#define USART_RXC_IRQ(n)    (n * USART_IRQ_COUNT + USART0_RXC_IRQ)
    +#define USART_DRE_IRQ(n)    (n * USART_IRQ_COUNT + USART0_DRE_IRQ)
    +#define USART_TXC_IRQ(n)    (n * USART_IRQ_COUNT + USART0_TXC_IRQ)
    +#define TIMER_IRQ_COUNT     5
    +#define TIMER_CAPT_IRQ(n)   (n * TIMER_IRQ_COUNT + TIMER0_CAPT_IRQ)
    +#define TIMER_COMPA_IRQ(n)  (n * TIMER_IRQ_COUNT + TIMER0_COMPA_IRQ)
    +#define TIMER_COMPB_IRQ(n)  (n * TIMER_IRQ_COUNT + TIMER0_COMPB_IRQ)
    +#define TIMER_COMPC_IRQ(n)  (n * TIMER_IRQ_COUNT + TIMER0_COMPC_IRQ)
    +#define TIMER_OVF_IRQ(n)    (n * TIMER_IRQ_COUNT + TIMER0_OVF_IRQ)
    +
    +static const uint8_t irq168_328[IRQ_COUNT] = {
    +    [TIMER2_COMPA_IRQ]      = 8,
    +    [TIMER2_COMPB_IRQ]      = 9,
    +    [TIMER2_OVF_IRQ]        = 10,
    +    [TIMER1_CAPT_IRQ]       = 11,
    +    [TIMER1_COMPA_IRQ]      = 12,
    +    [TIMER1_COMPB_IRQ]      = 13,
    +    [TIMER1_OVF_IRQ]        = 14,
    +    [TIMER0_COMPA_IRQ]      = 15,
    +    [TIMER0_COMPB_IRQ]      = 16,
    +    [TIMER0_OVF_IRQ]        = 17,
    +    [USART0_RXC_IRQ]        = 19,
    +    [USART0_DRE_IRQ]        = 20,
    +    [USART0_TXC_IRQ]        = 21,
    +}, irq1280_2560[IRQ_COUNT] = {
    +    [TIMER2_COMPA_IRQ]      = 14,
    +    [TIMER2_COMPB_IRQ]      = 15,
    +    [TIMER2_OVF_IRQ]        = 16,
    +    [TIMER1_CAPT_IRQ]       = 17,
    +    [TIMER1_COMPA_IRQ]      = 18,
    +    [TIMER1_COMPB_IRQ]      = 19,
    +    [TIMER1_COMPC_IRQ]      = 20,
    +    [TIMER1_OVF_IRQ]        = 21,
    +    [TIMER0_COMPA_IRQ]      = 22,
    +    [TIMER0_COMPB_IRQ]      = 23,
    +    [TIMER0_OVF_IRQ]        = 24,
    +    [USART0_RXC_IRQ]        = 26,
    +    [USART0_DRE_IRQ]        = 27,
    +    [USART0_TXC_IRQ]        = 28,
    +    [TIMER3_CAPT_IRQ]       = 32,
    +    [TIMER3_COMPA_IRQ]      = 33,
    +    [TIMER3_COMPB_IRQ]      = 34,
    +    [TIMER3_COMPC_IRQ]      = 35,
    +    [TIMER3_OVF_IRQ]        = 36,
    +    [USART1_RXC_IRQ]        = 37,
    +    [USART1_DRE_IRQ]        = 38,
    +    [USART1_TXC_IRQ]        = 39,
    +    [TIMER4_CAPT_IRQ]       = 42,
    +    [TIMER4_COMPA_IRQ]      = 43,
    +    [TIMER4_COMPB_IRQ]      = 44,
    +    [TIMER4_COMPC_IRQ]      = 45,
    +    [TIMER4_OVF_IRQ]        = 46,
    +    [TIMER5_CAPT_IRQ]       = 47,
    +    [TIMER5_COMPA_IRQ]      = 48,
    +    [TIMER5_COMPB_IRQ]      = 49,
    +    [TIMER5_COMPC_IRQ]      = 50,
    +    [TIMER5_OVF_IRQ]        = 51,
    +    [USART2_RXC_IRQ]        = 52,
    +    [USART2_DRE_IRQ]        = 53,
    +    [USART2_TXC_IRQ]        = 54,
    +    [USART3_RXC_IRQ]        = 55,
    +    [USART3_DRE_IRQ]        = 56,
    +    [USART3_TXC_IRQ]        = 57,
    +};
    +
    +static void connect_peripheral_irq(const AtmegaMcuClass *mc,
    +                                   SysBusDevice *sbd,
    +                                   DeviceState *dev, int n,
    +                                   unsigned peripheral_irq)
    +{
    +    int irq = mc->irq[peripheral_irq];
    +
    +    if (!irq) {
    +        return;
    +    }
    +    /* FIXME move that to avr_cpu_set_int() once 'sample' board is
    removed */
    +    assert(irq >= 2);
    +    irq -= 2;
    +
    +    sysbus_connect_irq(sbd, n, qdev_get_gpio_in(dev, irq));
    +}
    +
    +static void connect_power_reduction_gpio(AtmegaMcuState *s,
    +                                         const AtmegaMcuClass *mc,
    +                                         DeviceState *dev,
    +                                         int peripheral_index)
    +{
    +    unsigned power_index = mc->dev[peripheral_index].power_index;
    +    assert(mc->dev[power_index].addr);
    +    sysbus_connect_irq(SYS_BUS_DEVICE(&s->pwr[power_index - POWER0]),
    +                       mc->dev[peripheral_index].power_bit,
    +                       qdev_get_gpio_in(dev, 0));
    +}
    +
    +static void atmega_realize(DeviceState *dev, Error **errp)
    +{
    +    AtmegaMcuState *s = ATMEGA_MCU(dev);
    +    const AtmegaMcuClass *mc = ATMEGA_MCU_GET_CLASS(dev);
    +    DeviceState *cpudev;
    +    SysBusDevice *sbd;
    +    Error *err = NULL;
    +    char *devname;
    +    size_t i;
    +
    +    assert(mc->io_size <= 0x200);
    +
    +    if (!s->xtal_freq_hz) {
    +        error_setg(errp, "\"xtal-frequency-hz\" property must be
    provided.");
    +        return;
    +    }
    +
    +    /* CPU */
    +    object_initialize_child(OBJECT(dev), "cpu", &s->cpu,
    sizeof(s->cpu),
    +                            mc->cpu_type, &err, NULL);
    +    if (err) {
    +        error_propagate(errp, err);
    +        return;
    +    }
    +    object_property_set_bool(OBJECT(&s->cpu), true, "realized",
    &error_abort);
    +    cpudev = DEVICE(&s->cpu);
    +
    +    /* SRAM */
    +    memory_region_init_ram(&s->sram, OBJECT(dev), "sram",
    mc->sram_size,
    +                           &error_abort);
    +    memory_region_add_subregion(get_system_memory(),
    +                                OFFSET_DATA + mc->io_size, &s->sram);
    +
    +    /* Flash */
    +    memory_region_init_rom(&s->flash, OBJECT(dev),
    +                           "flash", mc->flash_size, &error_fatal);
    +    memory_region_add_subregion(get_system_memory(), OFFSET_CODE,
    &s->flash);
    +
    +    /*
    +     * I/O
    +     *
    +     * 0x00 - 0x1f: Registers
    +     * 0x20 - 0x5f: I/O memory
    +     * 0x60 - 0xff: Extended I/O
    +     */
    +    s->io = qdev_create(NULL, TYPE_UNIMPLEMENTED_DEVICE);
    +    qdev_prop_set_string(s->io, "name", "I/O");
    +    qdev_prop_set_uint64(s->io, "size", mc->io_size);
    +    qdev_init_nofail(s->io);
    +    sysbus_mmio_map_overlap(SYS_BUS_DEVICE(s->io), 0, OFFSET_DATA,
    -1234);
    +
    +    /* Power Reduction */
    +    for (i = 0; i < POWER_MAX; i++) {
    +        int idx = POWER(i);
    +        if (!mc->dev[idx].addr) {
    +            continue;
    +        }
    +        devname = g_strdup_printf("power%zu", i);
    +        object_initialize_child(OBJECT(dev), devname,
    +                                &s->pwr[i], sizeof(s->pwr[i]),
    +                                TYPE_AVR_MASK, &error_abort, NULL);
    +        object_property_set_bool(OBJECT(&s->pwr[i]), true, "realized",
    +                                 &error_abort);
    +        sysbus_mmio_map(SYS_BUS_DEVICE(&s->pwr[i]), 0,
    +                        OFFSET_DATA + mc->dev[idx].addr);
    +        g_free(devname);
    +    }
    +
    +    /* GPIO */
    +    for (i = 0; i < GPIO_MAX; i++) {
    +        int idx = GPIO(i);
    +        if (!mc->dev[idx].addr) {
    +            continue;
    +        }
    +        devname = g_strdup_printf("avr-gpio-%c", 'a' + (char)i);
    +        create_unimplemented_device(devname,
    +                                    OFFSET_DATA +
    mc->dev[idx].addr, 3);
    +        g_free(devname);
    +    }
    +
    +    /* USART */
    +    for (i = 0; i < USART_MAX; i++) {
    +        int idx = USART(i);
    +        if (!mc->dev[idx].addr) {
    +            continue;
    +        }
    +        devname = g_strdup_printf("usart%zu", i);
    +        object_initialize_child(OBJECT(dev), devname,
    +                                &s->usart[i], sizeof(s->usart[i]),
    +                                TYPE_AVR_USART, &error_abort, NULL);
    +        qdev_prop_set_chr(DEVICE(&s->usart[i]), "chardev",
    serial_hd(i));
    +        object_property_set_bool(OBJECT(&s->usart[i]), true,
    "realized",
    +                                 &error_abort);
    +        sbd = SYS_BUS_DEVICE(&s->usart[i]);
    +        sysbus_mmio_map(sbd, 0, OFFSET_DATA + mc->dev[USART(i)].addr);
    +        connect_peripheral_irq(mc, sbd, cpudev, 0, USART_RXC_IRQ(i));
    +        connect_peripheral_irq(mc, sbd, cpudev, 1, USART_DRE_IRQ(i));
    +        connect_peripheral_irq(mc, sbd, cpudev, 2, USART_TXC_IRQ(i));
    +        connect_power_reduction_gpio(s, mc, DEVICE(&s->usart[i]), idx);
    +        g_free(devname);
    +    }
    +
    +    /* Timer */
    +    for (i = 0; i < TIMER_MAX; i++) {
    +        int idx = TIMER(i);
    +        if (!mc->dev[idx].addr) {
    +            continue;
    +        }
    +        if (!mc->dev[idx].is_timer16) {
    +            create_unimplemented_device("avr-timer8",
    +                                        OFFSET_DATA +
    mc->dev[idx].addr, 5);
    +            create_unimplemented_device("avr-timer8-intmask",
    +                                        OFFSET_DATA
    +                                        +
    mc->dev[idx].intmask_addr, 1);
    +            create_unimplemented_device("avr-timer8-intflag",
    +                                        OFFSET_DATA
    +                                        +
    mc->dev[idx].intflag_addr, 1);
    +            continue;
    +        }
    +        devname = g_strdup_printf("timer%zu", i);
    +        object_initialize_child(OBJECT(dev), devname,
    +                                &s->timer[i], sizeof(s->timer[i]),
    +                                TYPE_AVR_TIMER16, &error_abort, NULL);
    +        object_property_set_uint(OBJECT(&s->timer[i]), s->xtal_freq_hz,
    +                                 "cpu-frequency-hz", &error_abort);
    +        object_property_set_bool(OBJECT(&s->timer[i]), true,
    "realized",
    +                                 &error_abort);
    +        sbd = SYS_BUS_DEVICE(&s->timer[i]);
    +        sysbus_mmio_map(sbd, 0, OFFSET_DATA + mc->dev[idx].addr);
    +        sysbus_mmio_map(sbd, 1, OFFSET_DATA +
    mc->dev[idx].intmask_addr);
    +        sysbus_mmio_map(sbd, 2, OFFSET_DATA +
    mc->dev[idx].intflag_addr);
    +        connect_peripheral_irq(mc, sbd, cpudev, 0, TIMER_CAPT_IRQ(i));
    +        connect_peripheral_irq(mc, sbd, cpudev, 1, TIMER_COMPA_IRQ(i));
    +        connect_peripheral_irq(mc, sbd, cpudev, 2, TIMER_COMPB_IRQ(i));
    +        connect_peripheral_irq(mc, sbd, cpudev, 3, TIMER_COMPC_IRQ(i));
    +        connect_peripheral_irq(mc, sbd, cpudev, 4, TIMER_OVF_IRQ(i));
    +        connect_power_reduction_gpio(s, mc, DEVICE(&s->timer[i]), idx);
    +        g_free(devname);
    +    }
    +
    +    create_unimplemented_device("avr-twi",          OFFSET_DATA +
    0x0b8, 6);
    +    create_unimplemented_device("avr-adc",          OFFSET_DATA +
    0x078, 8);
    +    create_unimplemented_device("avr-ext-mem-ctrl", OFFSET_DATA +
    0x074, 2);
    +    create_unimplemented_device("avr-watchdog",     OFFSET_DATA +
    0x060, 1);
    +    create_unimplemented_device("avr-spi",          OFFSET_DATA +
    0x04c, 3);
    +    create_unimplemented_device("avr-eeprom",       OFFSET_DATA +
    0x03f, 3);
    +}
    +
    +static Property atmega_props[] = {
    +    DEFINE_PROP_UINT64("xtal-frequency-hz", AtmegaMcuState,
    +                       xtal_freq_hz, 0),
    +    DEFINE_PROP_END_OF_LIST()
    +};
    +
    +static void atmega_class_init(ObjectClass *oc, void *data)
    +{
    +    DeviceClass *dc = DEVICE_CLASS(oc);
    +
    +    dc->realize = atmega_realize;
    +    dc->props = atmega_props;
    +    /* Reason: Mapped at fixed location on the system bus */
    +    dc->user_creatable = false;
    +}
    +
    +static void atmega168_class_init(ObjectClass *oc, void *data)
    +{
    +    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
    +
    +    amc->cpu_type = AVR_CPU_TYPE_NAME("avr5");
    +    amc->flash_size = 16 * KiB;
    +    amc->eeprom_size = 512;
    +    amc->sram_size = 1 * KiB;
    +    amc->io_size = 256;
    +    amc->gpio_count = 23;
    +    amc->adc_count = 6;
    +    amc->irq = irq168_328;
    +    amc->dev = dev168_328;
    +};
    +
    +static void atmega328_class_init(ObjectClass *oc, void *data)
    +{
    +    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
    +
    +    amc->cpu_type = AVR_CPU_TYPE_NAME("avr5");
    +    amc->flash_size = 32 * KiB;
    +    amc->eeprom_size = 1 * KiB;
    +    amc->sram_size = 2 * KiB;
    +    amc->io_size = 256;
    +    amc->gpio_count = 23;
    +    amc->adc_count = 6;
    +    amc->irq = irq168_328;
    +    amc->dev = dev168_328;
    +};
    +
    +static void atmega1280_class_init(ObjectClass *oc, void *data)
    +{
    +    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
    +
    +    amc->cpu_type = AVR_CPU_TYPE_NAME("avr6");
    +    amc->flash_size = 128 * KiB;
    +    amc->eeprom_size = 4 * KiB;
    +    amc->sram_size = 8 * KiB;
    +    amc->io_size = 512;
    +    amc->gpio_count = 86;
    +    amc->adc_count = 16;
    +    amc->irq = irq1280_2560;
    +    amc->dev = dev1280_2560;
    +};
    +
    +static void atmega2560_class_init(ObjectClass *oc, void *data)
    +{
    +    AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
    +
    +    amc->cpu_type = AVR_CPU_TYPE_NAME("avr6");
    +    amc->flash_size = 256 * KiB;
    +    amc->eeprom_size = 4 * KiB;
    +    amc->sram_size = 8 * KiB;
    +    amc->io_size = 512;
    +    amc->gpio_count = 54;
    +    amc->adc_count = 16;
    +    amc->irq = irq1280_2560;
    +    amc->dev = dev1280_2560;
    +};
    +
    +static const TypeInfo atmega_mcu_types[] = {
    +    {
    +        .name           = TYPE_ATMEGA168_MCU,
    +        .parent         = TYPE_ATMEGA_MCU,
    +        .class_init     = atmega168_class_init,
    +    }, {
    +        .name           = TYPE_ATMEGA328_MCU,
    +        .parent         = TYPE_ATMEGA_MCU,
    +        .class_init     = atmega328_class_init,
    +    }, {
    +        .name           = TYPE_ATMEGA1280_MCU,
    +        .parent         = TYPE_ATMEGA_MCU,
    +        .class_init     = atmega1280_class_init,
    +    }, {
    +        .name           = TYPE_ATMEGA2560_MCU,
    +        .parent         = TYPE_ATMEGA_MCU,
    +        .class_init     = atmega2560_class_init,
    +    }, {
    +        .name           = TYPE_ATMEGA_MCU,
    +        .parent         = TYPE_SYS_BUS_DEVICE,
    +        .instance_size  = sizeof(AtmegaMcuState),
    +        .class_size     = sizeof(AtmegaMcuClass),
    +        .class_init     = atmega_class_init,
    +        .abstract       = true,
    +    }
    +};
    +
    +DEFINE_TYPES(atmega_mcu_types)
    diff --git a/hw/avr/Kconfig b/hw/avr/Kconfig
    new file mode 100644
    index 0000000000..da3b10afec
    --- /dev/null
    +++ b/hw/avr/Kconfig
    @@ -0,0 +1,5 @@
    +config ATMEL_ATMEGA_MCU
    +    bool
    +    select ATMEL_TIMER16
    +    select ATMEL_USART
    +    select ATMEL_POWER
    diff --git a/hw/avr/Makefile.objs b/hw/avr/Makefile.objs
    index 123f174f0e..1f73fd5469 100644
    --- a/hw/avr/Makefile.objs
    +++ b/hw/avr/Makefile.objs
    @@ -1 +1,2 @@
      obj-y += boot.o
    +obj-$(CONFIG_ATMEL_ATMEGA_MCU) += atmel_atmega.o
    -- 
    2.21.1
#45 lore raw

Re: [PATCH rc2 20/25] hw/avr: Add some ATmega microcontrollers

08:59 Pon, 27.01.2020. Philippe Mathieu-Daudé <[email protected]> је написао/ла:

Quoted 12 lines · Aleksandar Markovic
Quoted 10 lines

On Friday, January 24, 2020, Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

Add some microcontrollers from the megaAVR family (ATmega series):

- middle range: ATmega168 and ATmega328
- high range: ATmega1280 and ATmega2560

For product comparison:

https://www.microchip.com/wwwproducts/ProductCompare/ATmega168P/ATmega328P

Quoted 1 line
Quoted 1 line
    <

https://www.microchip.com/wwwproducts/ProductCompare/ATmega168P/ATmega328P>

Quoted 1 line
Quoted 1 line

https://www.microchip.com/wwwproducts/ProductCompare/ATmega1280/ATmega2560

Quoted 1 line
Quoted 1 line
    <

https://www.microchip.com/wwwproducts/ProductCompare/ATmega1280/ATmega2560>

Quoted 3 lines
Quoted 3 lines

Datasheets:

http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

Quoted 1 line
Quoted 1 line
    <

http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

Quoted 2 lines
Quoted 2 lines

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf

Quoted 1 line
Quoted 1 line
    <

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet.pdf

Quoted 15 lines
Quoted 15 lines

    Signed-off-by: Philippe Mathieu-Daudé <[email protected]
    <mailto:[email protected]>>
    Message-Id: <[email protected]
    <mailto:[email protected]>>
    Signed-off-by: Richard Henderson <[email protected]
    <mailto:[email protected]>>
    ---
    rc2:
    - Squashed Kconfig patch (Thomas)
    - Correct SRAM base address
    ---
      hw/avr/atmel_atmega.h |  48 +++++
      hw/avr/atmel_atmega.c | 470

++++++++++++++++++++++++++++++++++++++++++

Quoted 8 lines
Quoted 8 lines
      hw/avr/Kconfig        |   5 +
      hw/avr/Makefile.objs  |   1 +
      4 files changed, 524 insertions(+)

create mode 100644 hw/avr/atmel_atmega.h
create mode 100644 hw/avr/atmel_atmega.c
create mode 100644 hw/avr/Kconfig

Quoted 4 lines
Quoted 4 lines

This patch introduces a terminology confusion that will be difficult to eliminate once the code is in the tree.

"AVR" is the name (or, if someone wishes, the common name prefix) of

the

Quoted 9 lines
Quoted 9 lines

family of all MCUs covered in this series. This is reflected in the existence of the directory "hw/avr", and this is, in my opinion fine.

"Atmel" is the company that used to design AVR MCUs (there are hundreds of such MCUs). "Atmel" was acquired several years ago by another company, "Microchip". Trere is no element in AVR MCUs' docimentation that reffers to the producers (except title, copyright, etc). In all recent docs, "Atmel" is complely eradicated.

Quoted 6 lines
Quoted 3 lines

Therefore, using the name of non-existant company "Atmel" is confusing, and, moreover, completely unnecessary. Please, remove references to "Atmel" from file names, and from the code.

I simply used "Atmel" because this is how I discovered this chips and always called them by the Atmel brand.

That is understandable.

Quoted 9 lines

The Microchip acquisition is
quite recent. When I look for datasheet is I still write "Atmel something" and eventually end on the microchip website.

IOW it was not a deliberate name, simply Microchip could not eradicate "Atmel" from my memory =)

I'll rename these files at "atmega.{c,h}".

Thanks. It is important that we get the naming right from the outset.

Quoted 197 lines

Thanks,

Phil.

Quoted 193 lines

diff --git a/hw/avr/atmel_atmega.h b/hw/avr/atmel_atmega.h new file mode 100644
index 0000000000..391b8b1bf8
--- /dev/null
+++ b/hw/avr/atmel_atmega.h
@@ -0,0 +1,48 @@
+/*
+ * QEMU ATmega MCU
+ *
+ * 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
+ */
+
+#ifndef HW_AVR_ATMEL_ATMEGA_H
+#define HW_AVR_ATMEL_ATMEGA_H
+
+#include "hw/char/atmel_usart.h"
+#include "hw/timer/atmel_timer16.h"
+#include "hw/misc/atmel_power.h"
+#include "target/avr/cpu.h"
+
+#define TYPE_ATMEGA_MCU "ATmega"
+#define TYPE_ATMEGA168_MCU "ATmega168"
+#define TYPE_ATMEGA328_MCU "ATmega328"
+#define TYPE_ATMEGA1280_MCU "ATmega1280"
+#define TYPE_ATMEGA2560_MCU "ATmega2560"
+
+#define ATMEGA_MCU(obj) OBJECT_CHECK(AtmegaMcuState, (obj), TYPE_ATMEGA_MCU)
+
+#define POWER_MAX 2
+#define USART_MAX 4
+#define TIMER_MAX 6
+#define GPIO_MAX 12
+
+typedef struct AtmegaMcuState {
+ /*< private >*/
+ SysBusDevice parent_obj;
+ /*< public >*/
+
+ AVRCPU cpu;
+ MemoryRegion flash;
+ MemoryRegion eeprom;
+ MemoryRegion sram;
+ DeviceState *io;
+ AVRMaskState pwr[POWER_MAX];
+ AVRUsartState usart[USART_MAX];
+ AVRTimer16State timer[TIMER_MAX];
+ uint64_t xtal_freq_hz;
+} AtmegaMcuState;
+
+#endif /* HW_AVR_ATMEL_ATMEGA_H */
diff --git a/hw/avr/atmel_atmega.c b/hw/avr/atmel_atmega.c new file mode 100644
index 0000000000..790c374dee
--- /dev/null
+++ b/hw/avr/atmel_atmega.c
@@ -0,0 +1,470 @@
+/*
+ * QEMU ATmega MCU
+ *
+ * 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
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/module.h"
+#include "qemu/units.h"
+#include "qapi/error.h"
+#include "exec/memory.h"
+#include "exec/address-spaces.h"
+#include "sysemu/sysemu.h"
+#include "hw/qdev-properties.h"
+#include "hw/sysbus.h"
+#include "hw/boards.h" /* FIXME memory_region_allocate_system_memory for sram */ +#include "hw/misc/unimp.h"
+#include "atmel_atmega.h"
+
+enum AtmegaPeripheral {
+ POWER0, POWER1,
+ GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF,
+ GPIOG, GPIOH, GPIOI, GPIOJ, GPIOK, GPIOL,
+ USART0, USART1, USART2, USART3,
+ TIMER0, TIMER1, TIMER2, TIMER3, TIMER4, TIMER5,
+ PERIFMAX
+};
+
+#define GPIO(n) (n + GPIOA)
+#define USART(n) (n + USART0)
+#define TIMER(n) (n + TIMER0)
+#define POWER(n) (n + POWER0)
+
+typedef struct {
+ uint16_t addr;
+ enum AtmegaPeripheral power_index;
+ uint8_t power_bit;
+ /* timer specific */
+ uint16_t intmask_addr;
+ uint16_t intflag_addr;
+ bool is_timer16;
+} peripheral_cfg;
+
+typedef struct AtmegaMcuClass {
+ /*< private >*/
+ SysBusDeviceClass parent_class;
+ /*< public >*/
+ const char *uc_name;
+ const char *cpu_type;
+ size_t flash_size;
+ size_t eeprom_size;
+ size_t sram_size;
+ size_t io_size;
+ size_t gpio_count;
+ size_t adc_count;
+ const uint8_t *irq;
+ const peripheral_cfg *dev;
+} AtmegaMcuClass;
+
+#define ATMEGA_MCU_CLASS(klass) \
+ OBJECT_CLASS_CHECK(AtmegaMcuClass, (klass), TYPE_ATMEGA_MCU)
+#define ATMEGA_MCU_GET_CLASS(obj) \
+ OBJECT_GET_CLASS(AtmegaMcuClass, (obj), TYPE_ATMEGA_MCU)
+
+static const peripheral_cfg dev168_328[PERIFMAX] = {
+ [USART0] = { 0xc0, POWER0, 1 },
+ [TIMER2] = { 0xb0, POWER0, 6, 0x70, 0x37, false },
+ [TIMER1] = { 0x80, POWER0, 3, 0x6f, 0x36, true },
+ [POWER0] = { 0x64 },
+ [TIMER0] = { 0x44, POWER0, 5, 0x6e, 0x35, false },
+ [GPIOD] = { 0x29 },
+ [GPIOC] = { 0x26 },
+ [GPIOB] = { 0x23 },
+}, dev1280_2560[PERIFMAX] = {
+ [USART3] = { 0x130, POWER1, 2 },
+ [TIMER5] = { 0x120, POWER1, 5, 0x73, 0x3a, true },
+ [GPIOL] = { 0x109 },
+ [GPIOK] = { 0x106 },
+ [GPIOJ] = { 0x103 },
+ [GPIOH] = { 0x100 },
+ [USART2] = { 0xd0, POWER1, 1 },
+ [USART1] = { 0xc8, POWER1, 0 },
+ [USART0] = { 0xc0, POWER0, 1 },
+ [TIMER2] = { 0xb0, POWER0, 6, 0x70, 0x37, false }, /*
TODO async */
+ [TIMER4] = { 0xa0, POWER1, 4, 0x72, 0x39, true },
+ [TIMER3] = { 0x90, POWER1, 3, 0x71, 0x38, true },
+ [TIMER1] = { 0x80, POWER0, 3, 0x6f, 0x36, true },
+ [POWER1] = { 0x65 },
+ [POWER0] = { 0x64 },
+ [TIMER0] = { 0x44, POWER0, 5, 0x6e, 0x35, false },
+ [GPIOG] = { 0x32 },
+ [GPIOF] = { 0x2f },
+ [GPIOE] = { 0x2c },
+ [GPIOD] = { 0x29 },
+ [GPIOC] = { 0x26 },
+ [GPIOB] = { 0x23 },
+ [GPIOA] = { 0x20 },
+};
+
+enum AtmegaIrq {
+ USART0_RXC_IRQ, USART0_DRE_IRQ, USART0_TXC_IRQ,
+ USART1_RXC_IRQ, USART1_DRE_IRQ, USART1_TXC_IRQ,
+ USART2_RXC_IRQ, USART2_DRE_IRQ, USART2_TXC_IRQ,
+ USART3_RXC_IRQ, USART3_DRE_IRQ, USART3_TXC_IRQ,
+ TIMER0_CAPT_IRQ, TIMER0_COMPA_IRQ, TIMER0_COMPB_IRQ,
+ TIMER0_COMPC_IRQ, TIMER0_OVF_IRQ,
+ TIMER1_CAPT_IRQ, TIMER1_COMPA_IRQ, TIMER1_COMPB_IRQ,
+ TIMER1_COMPC_IRQ, TIMER1_OVF_IRQ,
+ TIMER2_CAPT_IRQ, TIMER2_COMPA_IRQ, TIMER2_COMPB_IRQ,
+ TIMER2_COMPC_IRQ, TIMER2_OVF_IRQ,
+ TIMER3_CAPT_IRQ, TIMER3_COMPA_IRQ, TIMER3_COMPB_IRQ,
+ TIMER3_COMPC_IRQ, TIMER3_OVF_IRQ,
+ TIMER4_CAPT_IRQ, TIMER4_COMPA_IRQ, TIMER4_COMPB_IRQ,
+ TIMER4_COMPC_IRQ, TIMER4_OVF_IRQ,
+ TIMER5_CAPT_IRQ, TIMER5_COMPA_IRQ, TIMER5_COMPB_IRQ,
+ TIMER5_COMPC_IRQ, TIMER5_OVF_IRQ,
+ IRQ_COUNT
+};
+
+#define USART_IRQ_COUNT 3
+#define USART_RXC_IRQ(n) (n * USART_IRQ_COUNT + USART0_RXC_IRQ)
+#define USART_DRE_IRQ(n) (n * USART_IRQ_COUNT + USART0_DRE_IRQ)
+#define USART_TXC_IRQ(n) (n * USART_IRQ_COUNT + USART0_TXC_IRQ)
+#define TIMER_IRQ_COUNT 5
+#define TIMER_CAPT_IRQ(n) (n * TIMER_IRQ_COUNT + TIMER0_CAPT_IRQ)
+#define TIMER_COMPA_IRQ(n) (n * TIMER_IRQ_COUNT +

TIMER0_COMPA_IRQ)

Quoted 1 line
Quoted 1 line

+#define TIMER_COMPB_IRQ(n) (n * TIMER_IRQ_COUNT +

TIMER0_COMPB_IRQ)

Quoted 1 line
Quoted 1 line

+#define TIMER_COMPC_IRQ(n) (n * TIMER_IRQ_COUNT +

TIMER0_COMPC_IRQ)

Quoted 75 lines
Quoted 75 lines

+#define TIMER_OVF_IRQ(n) (n * TIMER_IRQ_COUNT + TIMER0_OVF_IRQ)
+
+static const uint8_t irq168_328[IRQ_COUNT] = {
+ [TIMER2_COMPA_IRQ] = 8,
+ [TIMER2_COMPB_IRQ] = 9,
+ [TIMER2_OVF_IRQ] = 10,
+ [TIMER1_CAPT_IRQ] = 11,
+ [TIMER1_COMPA_IRQ] = 12,
+ [TIMER1_COMPB_IRQ] = 13,
+ [TIMER1_OVF_IRQ] = 14,
+ [TIMER0_COMPA_IRQ] = 15,
+ [TIMER0_COMPB_IRQ] = 16,
+ [TIMER0_OVF_IRQ] = 17,
+ [USART0_RXC_IRQ] = 19,
+ [USART0_DRE_IRQ] = 20,
+ [USART0_TXC_IRQ] = 21,
+}, irq1280_2560[IRQ_COUNT] = {
+ [TIMER2_COMPA_IRQ] = 14,
+ [TIMER2_COMPB_IRQ] = 15,
+ [TIMER2_OVF_IRQ] = 16,
+ [TIMER1_CAPT_IRQ] = 17,
+ [TIMER1_COMPA_IRQ] = 18,
+ [TIMER1_COMPB_IRQ] = 19,
+ [TIMER1_COMPC_IRQ] = 20,
+ [TIMER1_OVF_IRQ] = 21,
+ [TIMER0_COMPA_IRQ] = 22,
+ [TIMER0_COMPB_IRQ] = 23,
+ [TIMER0_OVF_IRQ] = 24,
+ [USART0_RXC_IRQ] = 26,
+ [USART0_DRE_IRQ] = 27,
+ [USART0_TXC_IRQ] = 28,
+ [TIMER3_CAPT_IRQ] = 32,
+ [TIMER3_COMPA_IRQ] = 33,
+ [TIMER3_COMPB_IRQ] = 34,
+ [TIMER3_COMPC_IRQ] = 35,
+ [TIMER3_OVF_IRQ] = 36,
+ [USART1_RXC_IRQ] = 37,
+ [USART1_DRE_IRQ] = 38,
+ [USART1_TXC_IRQ] = 39,
+ [TIMER4_CAPT_IRQ] = 42,
+ [TIMER4_COMPA_IRQ] = 43,
+ [TIMER4_COMPB_IRQ] = 44,
+ [TIMER4_COMPC_IRQ] = 45,
+ [TIMER4_OVF_IRQ] = 46,
+ [TIMER5_CAPT_IRQ] = 47,
+ [TIMER5_COMPA_IRQ] = 48,
+ [TIMER5_COMPB_IRQ] = 49,
+ [TIMER5_COMPC_IRQ] = 50,
+ [TIMER5_OVF_IRQ] = 51,
+ [USART2_RXC_IRQ] = 52,
+ [USART2_DRE_IRQ] = 53,
+ [USART2_TXC_IRQ] = 54,
+ [USART3_RXC_IRQ] = 55,
+ [USART3_DRE_IRQ] = 56,
+ [USART3_TXC_IRQ] = 57,
+};
+
+static void connect_peripheral_irq(const AtmegaMcuClass *mc,
+ SysBusDevice *sbd,
+ DeviceState *dev, int n,
+ unsigned peripheral_irq)
+{
+ int irq = mc->irq[peripheral_irq];
+
+ if (!irq) {
+ return;
+ }
+ /* FIXME move that to avr_cpu_set_int() once 'sample' board is
removed */
+ assert(irq >= 2);
+ irq -= 2;
+
+ sysbus_connect_irq(sbd, n, qdev_get_gpio_in(dev, irq));
+}
+

Quoted 8 lines
Quoted 8 lines

+static void connect_power_reduction_gpio(AtmegaMcuState *s,
+ const AtmegaMcuClass *mc,
+ DeviceState *dev,
+ int peripheral_index)
+{
+ unsigned power_index = mc->dev[peripheral_index].power_index;
+ assert(mc->dev[power_index].addr);
+ sysbus_connect_irq(SYS_BUS_DEVICE(&s->pwr[power_index -

POWER0]),

Quoted 32 lines
Quoted 32 lines

+ mc->dev[peripheral_index].power_bit,
+ qdev_get_gpio_in(dev, 0));
+}
+
+static void atmega_realize(DeviceState *dev, Error **errp) +{
+ AtmegaMcuState *s = ATMEGA_MCU(dev);
+ const AtmegaMcuClass *mc = ATMEGA_MCU_GET_CLASS(dev);
+ DeviceState *cpudev;
+ SysBusDevice *sbd;
+ Error *err = NULL;
+ char *devname;
+ size_t i;
+
+ assert(mc->io_size <= 0x200);
+
+ if (!s->xtal_freq_hz) {
+ error_setg(errp, "\"xtal-frequency-hz\" property must be
provided.");
+ return;
+ }
+
+ /* CPU */
+ object_initialize_child(OBJECT(dev), "cpu", &s->cpu,
sizeof(s->cpu),
+ mc->cpu_type, &err, NULL);
+ if (err) {
+ error_propagate(errp, err);
+ return;
+ }
+ object_property_set_bool(OBJECT(&s->cpu), true, "realized",
&error_abort);

Quoted 8 lines
Quoted 8 lines

+ cpudev = DEVICE(&s->cpu);
+
+ /* SRAM */
+ memory_region_init_ram(&s->sram, OBJECT(dev), "sram",
mc->sram_size,
+ &error_abort);
+ memory_region_add_subregion(get_system_memory(),
+ OFFSET_DATA + mc->io_size,

&s->sram);

Quoted 24 lines
Quoted 24 lines

+
+ /* Flash */
+ memory_region_init_rom(&s->flash, OBJECT(dev),
+ "flash", mc->flash_size, &error_fatal);
+ memory_region_add_subregion(get_system_memory(), OFFSET_CODE,
&s->flash);
+
+ /*
+ * I/O
+ *
+ * 0x00 - 0x1f: Registers
+ * 0x20 - 0x5f: I/O memory
+ * 0x60 - 0xff: Extended I/O
+ */
+ s->io = qdev_create(NULL, TYPE_UNIMPLEMENTED_DEVICE);
+ qdev_prop_set_string(s->io, "name", "I/O");
+ qdev_prop_set_uint64(s->io, "size", mc->io_size);
+ qdev_init_nofail(s->io);
+ sysbus_mmio_map_overlap(SYS_BUS_DEVICE(s->io), 0, OFFSET_DATA,
-1234);
+
+ /* Power Reduction */
+ for (i = 0; i < POWER_MAX; i++) {
+ int idx = POWER(i);

Quoted 8 lines
Quoted 8 lines

+ if (!mc->dev[idx].addr) {
+ continue;
+ }
+ devname = g_strdup_printf("power%zu", i);
+ object_initialize_child(OBJECT(dev), devname,
+ &s->pwr[i], sizeof(s->pwr[i]),
+ TYPE_AVR_MASK, &error_abort, NULL);
+ object_property_set_bool(OBJECT(&s->pwr[i]), true,

"realized",

Quoted 21 lines
Quoted 21 lines

+ &error_abort);
+ sysbus_mmio_map(SYS_BUS_DEVICE(&s->pwr[i]), 0,
+ OFFSET_DATA + mc->dev[idx].addr);
+ g_free(devname);
+ }
+
+ /* GPIO */
+ for (i = 0; i < GPIO_MAX; i++) {
+ int idx = GPIO(i);
+ if (!mc->dev[idx].addr) {
+ continue;
+ }
+ devname = g_strdup_printf("avr-gpio-%c", 'a' + (char)i);
+ create_unimplemented_device(devname,
+ OFFSET_DATA +
mc->dev[idx].addr, 3);
+ g_free(devname);
+ }
+
+ /* USART */
+ for (i = 0; i < USART_MAX; i++) {

Quoted 8 lines
Quoted 8 lines

+ int idx = USART(i);
+ if (!mc->dev[idx].addr) {
+ continue;
+ }
+ devname = g_strdup_printf("usart%zu", i);
+ object_initialize_child(OBJECT(dev), devname,
+ &s->usart[i], sizeof(s->usart[i]),
+ TYPE_AVR_USART, &error_abort,

NULL);

Quoted 7 lines
Quoted 7 lines

+ qdev_prop_set_chr(DEVICE(&s->usart[i]), "chardev",
serial_hd(i));
+ object_property_set_bool(OBJECT(&s->usart[i]), true,
"realized",
+ &error_abort);
+ sbd = SYS_BUS_DEVICE(&s->usart[i]);
+ sysbus_mmio_map(sbd, 0, OFFSET_DATA +

mc->dev[USART(i)].addr);

Quoted 1 line
Quoted 1 line

+ connect_peripheral_irq(mc, sbd, cpudev, 0,

USART_RXC_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_peripheral_irq(mc, sbd, cpudev, 1,

USART_DRE_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_peripheral_irq(mc, sbd, cpudev, 2,

USART_TXC_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_power_reduction_gpio(s, mc, DEVICE(&s->usart[i]),

idx);

Quoted 19 lines
Quoted 19 lines

+ g_free(devname);
+ }
+
+ /* Timer */
+ for (i = 0; i < TIMER_MAX; i++) {
+ int idx = TIMER(i);
+ if (!mc->dev[idx].addr) {
+ continue;
+ }
+ if (!mc->dev[idx].is_timer16) {
+ create_unimplemented_device("avr-timer8",
+ OFFSET_DATA +
mc->dev[idx].addr, 5);
+ create_unimplemented_device("avr-timer8-intmask",
+ OFFSET_DATA
+ +
mc->dev[idx].intmask_addr, 1);
+ create_unimplemented_device("avr-timer8-intflag",
+ OFFSET_DATA

Quoted 8 lines
Quoted 8 lines

+ +
mc->dev[idx].intflag_addr, 1);
+ continue;
+ }
+ devname = g_strdup_printf("timer%zu", i);
+ object_initialize_child(OBJECT(dev), devname,
+ &s->timer[i], sizeof(s->timer[i]),
+ TYPE_AVR_TIMER16, &error_abort,

NULL);

Quoted 1 line
Quoted 1 line

+ object_property_set_uint(OBJECT(&s->timer[i]),

s->xtal_freq_hz,

Quoted 11 lines
Quoted 11 lines

+ "cpu-frequency-hz", &error_abort);
+ object_property_set_bool(OBJECT(&s->timer[i]), true,
"realized",
+ &error_abort);
+ sbd = SYS_BUS_DEVICE(&s->timer[i]);
+ sysbus_mmio_map(sbd, 0, OFFSET_DATA + mc->dev[idx].addr);
+ sysbus_mmio_map(sbd, 1, OFFSET_DATA +
mc->dev[idx].intmask_addr);
+ sysbus_mmio_map(sbd, 2, OFFSET_DATA +
mc->dev[idx].intflag_addr);
+ connect_peripheral_irq(mc, sbd, cpudev, 0,

TIMER_CAPT_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_peripheral_irq(mc, sbd, cpudev, 1,

TIMER_COMPA_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_peripheral_irq(mc, sbd, cpudev, 2,

TIMER_COMPB_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_peripheral_irq(mc, sbd, cpudev, 3,

TIMER_COMPC_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_peripheral_irq(mc, sbd, cpudev, 4,

TIMER_OVF_IRQ(i));

Quoted 1 line
Quoted 1 line

+ connect_power_reduction_gpio(s, mc, DEVICE(&s->timer[i]),

idx);

Quoted 143 lines
Quoted 142 lines

+ g_free(devname);
+ }
+
+ create_unimplemented_device("avr-twi", OFFSET_DATA +
0x0b8, 6);
+ create_unimplemented_device("avr-adc", OFFSET_DATA +
0x078, 8);
+ create_unimplemented_device("avr-ext-mem-ctrl", OFFSET_DATA +
0x074, 2);
+ create_unimplemented_device("avr-watchdog", OFFSET_DATA +
0x060, 1);
+ create_unimplemented_device("avr-spi", OFFSET_DATA +
0x04c, 3);
+ create_unimplemented_device("avr-eeprom", OFFSET_DATA +
0x03f, 3);
+}
+
+static Property atmega_props[] = {
+ DEFINE_PROP_UINT64("xtal-frequency-hz", AtmegaMcuState,
+ xtal_freq_hz, 0),
+ DEFINE_PROP_END_OF_LIST()
+};
+
+static void atmega_class_init(ObjectClass *oc, void *data) +{
+ DeviceClass *dc = DEVICE_CLASS(oc);
+
+ dc->realize = atmega_realize;
+ dc->props = atmega_props;
+ /* Reason: Mapped at fixed location on the system bus */
+ dc->user_creatable = false;
+}
+
+static void atmega168_class_init(ObjectClass *oc, void *data) +{
+ AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+ amc->cpu_type = AVR_CPU_TYPE_NAME("avr5");
+ amc->flash_size = 16 * KiB;
+ amc->eeprom_size = 512;
+ amc->sram_size = 1 * KiB;
+ amc->io_size = 256;
+ amc->gpio_count = 23;
+ amc->adc_count = 6;
+ amc->irq = irq168_328;
+ amc->dev = dev168_328;
+};
+
+static void atmega328_class_init(ObjectClass *oc, void *data) +{
+ AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+ amc->cpu_type = AVR_CPU_TYPE_NAME("avr5");
+ amc->flash_size = 32 * KiB;
+ amc->eeprom_size = 1 * KiB;
+ amc->sram_size = 2 * KiB;
+ amc->io_size = 256;
+ amc->gpio_count = 23;
+ amc->adc_count = 6;
+ amc->irq = irq168_328;
+ amc->dev = dev168_328;
+};
+
+static void atmega1280_class_init(ObjectClass *oc, void *data) +{
+ AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+ amc->cpu_type = AVR_CPU_TYPE_NAME("avr6");
+ amc->flash_size = 128 * KiB;
+ amc->eeprom_size = 4 * KiB;
+ amc->sram_size = 8 * KiB;
+ amc->io_size = 512;
+ amc->gpio_count = 86;
+ amc->adc_count = 16;
+ amc->irq = irq1280_2560;
+ amc->dev = dev1280_2560;
+};
+
+static void atmega2560_class_init(ObjectClass *oc, void *data) +{
+ AtmegaMcuClass *amc = ATMEGA_MCU_CLASS(oc);
+
+ amc->cpu_type = AVR_CPU_TYPE_NAME("avr6");
+ amc->flash_size = 256 * KiB;
+ amc->eeprom_size = 4 * KiB;
+ amc->sram_size = 8 * KiB;
+ amc->io_size = 512;
+ amc->gpio_count = 54;
+ amc->adc_count = 16;
+ amc->irq = irq1280_2560;
+ amc->dev = dev1280_2560;
+};
+
+static const TypeInfo atmega_mcu_types[] = {
+ {
+ .name = TYPE_ATMEGA168_MCU,
+ .parent = TYPE_ATMEGA_MCU,
+ .class_init = atmega168_class_init,
+ }, {
+ .name = TYPE_ATMEGA328_MCU,
+ .parent = TYPE_ATMEGA_MCU,
+ .class_init = atmega328_class_init,
+ }, {
+ .name = TYPE_ATMEGA1280_MCU,
+ .parent = TYPE_ATMEGA_MCU,
+ .class_init = atmega1280_class_init,
+ }, {
+ .name = TYPE_ATMEGA2560_MCU,
+ .parent = TYPE_ATMEGA_MCU,
+ .class_init = atmega2560_class_init,
+ }, {
+ .name = TYPE_ATMEGA_MCU,
+ .parent = TYPE_SYS_BUS_DEVICE,
+ .instance_size = sizeof(AtmegaMcuState),
+ .class_size = sizeof(AtmegaMcuClass),
+ .class_init = atmega_class_init,
+ .abstract = true,
+ }
+};
+
+DEFINE_TYPES(atmega_mcu_types)
diff --git a/hw/avr/Kconfig b/hw/avr/Kconfig
new file mode 100644
index 0000000000..da3b10afec
--- /dev/null
+++ b/hw/avr/Kconfig
@@ -0,0 +1,5 @@
+config ATMEL_ATMEGA_MCU
+ bool
+ select ATMEL_TIMER16
+ select ATMEL_USART
+ select ATMEL_POWER
diff --git a/hw/avr/Makefile.objs b/hw/avr/Makefile.objs index 123f174f0e..1f73fd5469 100644
--- a/hw/avr/Makefile.objs
+++ b/hw/avr/Makefile.objs
@@ -1 +1,2 @@
obj-y += boot.o
+obj-$(CONFIG_ATMEL_ATMEGA_MCU) += atmel_atmega.o
--
2.21.1

#46 lore raw

PATCH 21/25 hw/avr: Add some Arduino boards

In reply to Philippe Mathieu-Daudé

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

Message-Id: <[email protected]>

---
rc2:
- Use avr_load_firmware (Aleksandar)
- No default machine on AVR (Richard)
- Add entry in MAINTAINERS (Michael is still the maintainer of hw/avr/)

---
 hw/avr/arduino.c     | 151 +++++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS          |   6 ++
 hw/avr/Kconfig       |   4 ++
 hw/avr/Makefile.objs |   1 +
 4 files changed, 162 insertions(+)
 create mode 100644 hw/avr/arduino.c
hw/avr/arduino.c new file +151 -0
new file mode 100644
index 0000000000..2fb2e96ffe
@@ -0,0 +1,151 @@
+/*
+ * 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 "qapi/error.h"
+#include "hw/boards.h"
+#include "atmel_atmega.h"
+#include "boot.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 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) {
+        if (!avr_load_firmware(&ams->mcu.cpu, machine,
+                               &ams->mcu.flash, machine->firmware)) {
+            exit(1);
+        }
+    }
+}
+
+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";
+    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)
MAINTAINERS +6 -0
index 066515ac8e..07c8912489 100644
@@ -904,6 +904,12 @@ F: hw/misc/atmel_power.c
 F: include/hw/misc/atmel_power.h
 F: tests/acceptance/machine_avr6.py
 
+Arduino
+M: Philippe Mathieu-Daudé <[email protected]>
+S: Maintained
+F: hw/avr/arduino.c
+F: hw/avr/atmel_atmega.*
+
 CRIS Machines
 -------------
 Axis Dev88
hw/avr/Kconfig +4 -0
index da3b10afec..59d9649d27 100644
@@ -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
hw/avr/Makefile.objs +1 -0
index 1f73fd5469..c8a131923c 100644
@@ -1,2 +1,3 @@
 obj-y += boot.o
 obj-$(CONFIG_ATMEL_ATMEGA_MCU) += atmel_atmega.o
+obj-$(CONFIG_ARDUINO) += arduino.o
Signature · 1 line
2.21.1
Reviewed-by on v1 21/25
#47 lore raw

Re: [PATCH rc2 21/25] hw/avr: Add some Arduino boards

Quoted 110 lines · Philippe Mathieu-Daudé

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

Message-Id: <[email protected]>

---
rc2:
- Use avr_load_firmware (Aleksandar)
- No default machine on AVR (Richard)
- Add entry in MAINTAINERS (Michael is still the maintainer of hw/avr/)

---
 hw/avr/arduino.c     | 151 +++++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS          |   6 ++
 hw/avr/Kconfig       |   4 ++
 hw/avr/Makefile.objs |   1 +
 4 files changed, 162 insertions(+)
 create mode 100644 hw/avr/arduino.c
hw/avr/arduino.c new file +76 -0
new file mode 100644
index 0000000000..2fb2e96ffe
@@ -0,0 +1,151 @@
+/*
+ * 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 "qapi/error.h"
+#include "hw/boards.h"
+#include "atmel_atmega.h"
+#include "boot.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 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) {
+        if (!avr_load_firmware(&ams->mcu.cpu, machine,
+                               &ams->mcu.flash, machine->firmware)) {
+            exit(1);
+        }
+    }
+}
+
+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)

hw/avr/arduino.c line 86

Quoted 10 lines
hw/avr/arduino.c new file +10 -0
+{
+    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;
+};

Hi! According to the page this board could be used with Atmega328 too. Maybe you can define both?

The rest of this patch looks good to me, so:

Quoted 103 lines
hw/avr/arduino.c new file +65 -0
+
+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";
+    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)
MAINTAINERS +6 -0
index 066515ac8e..07c8912489 100644
@@ -904,6 +904,12 @@ F: hw/misc/atmel_power.c
 F: include/hw/misc/atmel_power.h
 F: tests/acceptance/machine_avr6.py
 
+Arduino
+M: Philippe Mathieu-Daudé <[email protected]>
+S: Maintained
+F: hw/avr/arduino.c
+F: hw/avr/atmel_atmega.*
+
 CRIS Machines
 -------------
 Axis Dev88
hw/avr/Kconfig +4 -0
index da3b10afec..59d9649d27 100644
@@ -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
hw/avr/Makefile.objs +1 -0
index 1f73fd5469..c8a131923c 100644
@@ -1,2 +1,3 @@
 obj-y += boot.o
 obj-$(CONFIG_ATMEL_ATMEGA_MCU) += atmel_atmega.o
+obj-$(CONFIG_ARDUINO) += arduino.o
#48 lore raw

PATCH 22/25 target/avr: Update build system

In reply to Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Make AVR support buildable

Message-Id: <[email protected]>

---
 configure                       |  7 +++++++
 default-configs/avr-softmmu.mak |  5 +++++
 target/avr/Makefile.objs        | 34 +++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 default-configs/avr-softmmu.mak
 create mode 100644 target/avr/Makefile.objs
configure +7 -0
index 557e4382ea..94e79ca634 100755
@@ -7612,6 +7612,10 @@ case "$target_name" in
     mttcg="yes"
     gdb_xml_files="aarch64-core.xml aarch64-fpu.xml arm-core.xml arm-vfp.xml arm-vfp3.xml arm-neon.xml"
   ;;
+  avr)
+    gdb_xml_files="avr-cpu.xml"
+    target_compiler=$cross_cc_avr
+  ;;
   cris)
   ;;
   hppa)
@@ -7831,6 +7835,9 @@ for i in $ARCH $TARGET_BASE_ARCH ; do
       disas_config "ARM_A64"
     fi
   ;;
+  avr)
+    disas_config "AVR"
+  ;;
   cris)
     disas_config "CRIS"
   ;;
default-configs/avr-softmmu.mak new file +5 -0
new file mode 100644
index 0000000000..80218add98
@@ -0,0 +1,5 @@
+# Default configuration for avr-softmmu
+
+# Boards:
+#
+CONFIG_ARDUINO=y
target/avr/Makefile.objs new file +34 -0
new file mode 100644
index 0000000000..7523e0c6e2
@@ -0,0 +1,34 @@
+#
+#  QEMU AVR CPU
+#
+#  Copyright (c) 2019 Michael Rolnik
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU Lesser General Public
+#  License as published by the Free Software Foundation; either
+#  version 2.1 of the License, or (at your option) any later version.
+#
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  Lesser General Public License for more details.
+#
+#  You should have received a copy of the GNU Lesser General Public
+#  License along with this library; if not, see
+#  <http://www.gnu.org/licenses/lgpl-2.1.html>
+#
+
+DECODETREE = $(SRC_PATH)/scripts/decodetree.py
+decode-y = $(SRC_PATH)/target/avr/insn.decode
+
+target/avr/decode_insn.inc.c: $(decode-y) $(DECODETREE)
+	$(call quiet-command, \
+	  $(PYTHON) $(DECODETREE) -o $@ --decode decode_insn --insnwidth 16 $<, \
+	  "GEN", $(TARGET_DIR)$@)
+
+target/avr/translate.o: target/avr/decode_insn.inc.c
+
+obj-y += translate.o cpu.o helper.o
+obj-y += gdbstub.o
+obj-y += disas.o
+obj-$(CONFIG_SOFTMMU) += machine.o
Signature · 1 line
2.21.1
#49 lore raw

Re: [PATCH rc2 22/25] target/avr: Update build system

Quoted 22 lines · Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Make AVR support buildable

Message-Id: <[email protected]>

---
 configure                       |  7 +++++++
 default-configs/avr-softmmu.mak |  5 +++++
 target/avr/Makefile.objs        | 34 +++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 default-configs/avr-softmmu.mak
 create mode 100644 target/avr/Makefile.objs
configure +0 -0
index 557e4382ea..94e79ca634 100755

configure case "$target_name" in · line 7617

Quoted 6 lines
configure +3 -0
@@ -7612,6 +7612,10 @@ case "$target_name" in
     mttcg="yes"
     gdb_xml_files="aarch64-core.xml aarch64-fpu.xml arm-core.xml arm-vfp.xml arm-vfp3.xml arm-neon.xml"
   ;;
+  avr)
+    gdb_xml_files="avr-cpu.xml"
+    target_compiler=$cross_cc_avr

I don't think you want this here. target_compiler belongs in the tests/tcg/configure.sh config.

A docker target with the compiler would also be nice.

Quoted 65 lines
configure +4 -0
+  ;;
   cris)
   ;;
   hppa)
@@ -7831,6 +7835,9 @@ for i in $ARCH $TARGET_BASE_ARCH ; do
       disas_config "ARM_A64"
     fi
   ;;
+  avr)
+    disas_config "AVR"
+  ;;
   cris)
     disas_config "CRIS"
   ;;
default-configs/avr-softmmu.mak new file +5 -0
new file mode 100644
index 0000000000..80218add98
@@ -0,0 +1,5 @@
+# Default configuration for avr-softmmu
+
+# Boards:
+#
+CONFIG_ARDUINO=y
target/avr/Makefile.objs new file +34 -0
new file mode 100644
index 0000000000..7523e0c6e2
@@ -0,0 +1,34 @@
+#
+#  QEMU AVR CPU
+#
+#  Copyright (c) 2019 Michael Rolnik
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU Lesser General Public
+#  License as published by the Free Software Foundation; either
+#  version 2.1 of the License, or (at your option) any later version.
+#
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  Lesser General Public License for more details.
+#
+#  You should have received a copy of the GNU Lesser General Public
+#  License along with this library; if not, see
+#  <http://www.gnu.org/licenses/lgpl-2.1.html>
+#
+
+DECODETREE = $(SRC_PATH)/scripts/decodetree.py
+decode-y = $(SRC_PATH)/target/avr/insn.decode
+
+target/avr/decode_insn.inc.c: $(decode-y) $(DECODETREE)
+	$(call quiet-command, \
+	  $(PYTHON) $(DECODETREE) -o $@ --decode decode_insn --insnwidth 16 $<, \
+	  "GEN", $(TARGET_DIR)$@)
+
+target/avr/translate.o: target/avr/decode_insn.inc.c
+
+obj-y += translate.o cpu.o helper.o
+obj-y += gdbstub.o
+obj-y += disas.o
+obj-$(CONFIG_SOFTMMU) += machine.o
Signature · 1 line
Alex Bennée
#50 lore raw

PATCH 23/25 tests/boot-serial-test: Test some Arduino boards (AVR based)

In reply to Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

Print out 'T' through serial port

The Arduino Duemilanove is based on a AVR5 CPU, while the Arduino MEGA2560 on a AVR6 CPU.

---
 tests/qtest/boot-serial-test.c | 11 +++++++++++
 tests/qtest/Makefile.include   |  2 ++
 2 files changed, 13 insertions(+)
tests/qtest/boot-serial-test.c +11 -0
index 05c7f44457..07067b76a2 100644
@@ -16,6 +16,15 @@
 #include "qemu/osdep.h"
 #include "libqtest.h"
 
+static const uint8_t bios_avr[] = {
+    0x88, 0xe0,             /* ldi r24, 0x08   */
+    0x80, 0x93, 0xc1, 0x00, /* sts 0x00C1, r24 ; Enable tx */
+    0x86, 0xe0,             /* ldi r24, 0x06   */
+    0x80, 0x93, 0xc2, 0x00, /* sts 0x00C2, r24 ; Set the data bits to 8 */
+    0x84, 0xe5,             /* ldi r24, 0x54   */
+    0x80, 0x93, 0xc6, 0x00, /* sts 0x00C6, r24 ; Output 'T' */
+};
+
 static const uint8_t kernel_mcf5208[] = {
     0x41, 0xf9, 0xfc, 0x06, 0x00, 0x00,     /* lea 0xfc060000,%a0 */
     0x10, 0x3c, 0x00, 0x54,                 /* move.b #'T',%d0 */
@@ -103,6 +112,8 @@ typedef struct testdef {
 
 static testdef_t tests[] = {
     { "alpha", "clipper", "", "PCI:" },
+    { "avr", "arduino-duemilanove", "", "T", sizeof(bios_avr), NULL, bios_avr },
+    { "avr", "arduino-mega-2560-v3", "", "T", sizeof(bios_avr), NULL, bios_avr},
     { "ppc", "ppce500", "", "U-Boot" },
     { "ppc", "40p", "-vga none -boot d", "Trying cd:," },
     { "ppc", "g3beige", "", "PowerPC,750" },
tests/qtest/Makefile.include +2 -0
index e6bb4ab28c..4817b6320f 100644
@@ -65,6 +65,8 @@ check-qtest-i386-y += numa-test
 
 check-qtest-x86_64-y += $(check-qtest-i386-y)
 
+check-qtest-avr-y += boot-serial-test
+
 check-qtest-alpha-y += boot-serial-test
 check-qtest-alpha-$(CONFIG_VGA) += display-vga-test
 
Signature · 1 line
2.21.1
#51 lore raw

PATCH 24/25 tests/acceptance: Test the Arduino MEGA2560 board

In reply to Philippe Mathieu-Daudé

From: Michael Rolnik <[email protected]>

The test is based on
https://github.com/seharris/qemu-avr-tests/tree/master/free-rtos/Demo
demo which. If working correctly, prints 'ABCDEFGHIJKLMNOPQRSTUVWX' out. it also demostrates that timer and IRQ are working

As the path name demonstrates, the FreeRTOS tests target a board based on a ATMega2560 MCU. We have one, the Arduino MEGA2560.

Complementary documentation:

https://feilipu.me/2012/01/15/ethermega-arduino-mega-2560-and-freertos/
https://feilipu.me/2015/11/24/arduino_freertos/ (see 'Compatibility')

Message-Id: <[email protected]>

[rth: Squash multiple avocado fixups from f4bug]
---
 tests/acceptance/machine_avr6.py | 50 ++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 tests/acceptance/machine_avr6.py
tests/acceptance/machine_avr6.py new file +50 -0
new file mode 100644
index 0000000000..b644d2a81c
@@ -0,0 +1,50 @@
+#
+# QEMU AVR
+#
+# Copyright (c) 2019 Michael Rolnik <[email protected]>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import time
+
+from avocado_qemu import Test
+
+class AVR6Machine(Test):
+    timeout = 5
+
+    def test_freertos(self):
+        """
+        :avocado: tags=arch:avr
+        :avocado: tags=machine:arduino-mega-2560-v3
+        """
+        """
+        https://github.com/seharris/qemu-avr-tests/raw/master/free-rtos/Demo/AVR_ATMega2560_GCC/demo.elf
+        constantly prints out 'ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWX'
+        """
+        rom_url = ('https://github.com/seharris/qemu-avr-tests'
+                   '/raw/36c3e67b8755dcf/free-rtos/Demo'
+                   '/AVR_ATMega2560_GCC/demo.elf')
+        rom_hash = '7eb521f511ca8f2622e0a3c5e8dd686efbb911d4'
+        rom_path = self.fetch_asset(rom_url, asset_hash=rom_hash)
+
+        self.vm.add_args('-bios', rom_path)
+        self.vm.add_args('-nographic')
+        self.vm.launch()
+
+        time.sleep(2)
+        self.vm.shutdown()
+
+        self.assertIn('ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWX',
+                self.vm.get_log())
Signature · 1 line
2.21.1
#52 lore raw

PATCH 25/25 .travis.yml: Run the AVR acceptance tests

In reply to Philippe Mathieu-Daudé

We have one test so far, and it is very fast:

  $ avocado --show=app run -t arch:avr tests/acceptance/
  (1/1) tests/acceptance/machine_avr6.py:AVR6Machine.test_freertos: PASS (2.13 s)
  RESULTS    : PASS 1 | ERROR 0 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0 | CANCEL 0
  JOB TIME   : 2.30 s

Message-Id: <[email protected]>

---
 .travis.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
.travis.yml +1 -1
index 6c1038a0f1..2301c9221e 100644
@@ -268,7 +268,7 @@ matrix:
 
     # Acceptance (Functional) tests
     - env:
-        - CONFIG="--python=/usr/bin/python3 --target-list=x86_64-softmmu,mips-softmmu,mips64el-softmmu,aarch64-softmmu,arm-softmmu,s390x-softmmu,alpha-softmmu,ppc-softmmu,ppc64-softmmu,m68k-softmmu,sparc-softmmu"
+        - CONFIG="--python=/usr/bin/python3 --target-list=x86_64-softmmu,mips-softmmu,mips64el-softmmu,aarch64-softmmu,arm-softmmu,s390x-softmmu,alpha-softmmu,ppc-softmmu,ppc64-softmmu,m68k-softmmu,sparc-softmmu,avr-softmmu"
         - TEST_CMD="make check-acceptance"
       after_script:
         - python3 -c 'import json; r = json.load(open("tests/results/latest/results.json")); [print(t["logfile"]) for t in r["tests"] if t["status"] not in ("PASS", "SKIP")]' | xargs cat
Signature · 1 line
2.21.1
#53 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 31 lines · Philippe Mathieu-Daudé

This is the AVR port from Michael release (merge) candidate 2.

Since v1 [1]:
- Addressed Thomas comments
- Fixed a non-critical bug in ATmega (incorrect SRAM base address)
- Added ELF parsing requested by Aleksandar
- Dropped default machine (as with the ARM port)

Change since rc1:

$ git backport-diff -u avr-rc1 -r origin/master..
Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch
[down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/25:[----] [--] 'target/avr: Add outward facing interfaces and core CPU logic' 002/25:[----] [--] 'target/avr: Add instruction helpers'
003/25:[----] [--] 'target/avr: Add instruction translation - Registers definition' 004/25:[----] [--] 'target/avr: Add instruction translation - Arithmetic and Logic Instructions' 005/25:[----] [--] 'target/avr: Add instruction translation - Branch Instructions' 006/25:[----] [--] 'target/avr: Add instruction translation - Data Transfer Instructions' 007/25:[----] [--] 'target/avr: Add instruction translation - Bit and Bit-test Instructions' 008/25:[----] [--] 'target/avr: Add instruction translation - MCU Control Instructions' 009/25:[----] [--] 'target/avr: Add instruction translation - CPU main translation function' 010/25:[----] [--] 'target/avr: Add instruction disassembly function' 011/25:[----] [--] 'hw/char: Add limited support for Atmel USART peripheral' 012/25:[0045] [FC] 'hw/timer: Add limited support for Atmel 16 bit timer peripheral' 013/25:[----] [--] 'hw/misc: Add Atmel power device'

Quoted 8 lines

014/25:[0024] [FC] 'target/avr: Add section about AVR into QEMU documentation' 015/25:[----] [--] 'target/avr: Register AVR support with the rest of QEMU' 016/25:[----] [--] 'target/avr: Add machine none test' 017/25:[0002] [FC] 'target/avr: Update MAINTAINERS file'
018/25:[down] 'hw/core/loader: Let load_elf populate the processor-specific flags'
019/25:[down] 'hw/avr: Add helper to load raw/ELF firmware binaries'
020/25:[0015] [FC] 'hw/avr: Add some ATmega microcontrollers' 021/25:[0040] [FC] 'hw/avr: Add some Arduino boards'

Patchew can be used to quickly visualize the few changes:
https://patchew.org/QEMU/[email protected]/diff/[email protected]/

Quoted 125 lines

022/25:[----] [--] 'target/avr: Update build system'
023/25:[----] [--] 'tests/boot-serial-test: Test some Arduino boards (AVR based)' 024/25:[----] [--] 'tests/acceptance: Test the Arduino MEGA2560 board' 025/25:[----] [--] '.travis.yml: Run the AVR acceptance tests'

Repo: https://gitlab.com/philmd/qemu/commits/avr-rc2

[1] https://www.mail-archive.com/[email protected]/msg672926.html
Supersedes: <[email protected]>

Michael Rolnik (20):
target/avr: Add outward facing interfaces and core CPU logic target/avr: Add instruction helpers
target/avr: Add instruction translation - Registers definition target/avr: Add instruction translation - Arithmetic and Logic
Instructions
target/avr: Add instruction translation - Branch Instructions target/avr: Add instruction translation - Data Transfer Instructions target/avr: Add instruction translation - Bit and Bit-test
Instructions
target/avr: Add instruction translation - MCU Control Instructions target/avr: Add instruction translation - CPU main translation
function
target/avr: Add instruction disassembly function
hw/char: Add limited support for Atmel USART peripheral hw/timer: Add limited support for Atmel 16 bit timer peripheral hw/misc: Add Atmel power device
target/avr: Add section about AVR into QEMU documentation target/avr: Register AVR support with the rest of QEMU target/avr: Add machine none test
target/avr: Update MAINTAINERS file
target/avr: Update build system
tests/boot-serial-test: Test some Arduino boards (AVR based) tests/acceptance: Test the Arduino MEGA2560 board

Philippe Mathieu-Daudé (5):
hw/core/loader: Let load_elf populate the processor-specific flags hw/avr: Add helper to load raw/ELF firmware binaries hw/avr: Add some ATmega microcontrollers
hw/avr: Add some Arduino boards
.travis.yml: Run the AVR acceptance tests

 qemu-doc.texi                    |   51 +
 configure                        |    7 +
 default-configs/avr-softmmu.mak  |    5 +
 qapi/machine.json                |    3 +-
 hw/avr/atmel_atmega.h            |   48 +
 hw/avr/boot.h                    |   33 +
 include/disas/dis-asm.h          |   19 +
 include/elf.h                    |    2 +
 include/hw/char/atmel_usart.h    |   93 +
 include/hw/elf_ops.h             |    6 +-
 include/hw/loader.h              |    6 +-
 include/hw/misc/atmel_power.h    |   46 +
 include/hw/timer/atmel_timer16.h |   94 +
 include/sysemu/arch_init.h       |    1 +
 target/avr/cpu-param.h           |   37 +
 target/avr/cpu-qom.h             |   54 +
 target/avr/cpu.h                 |  259 +++
 target/avr/helper.h              |   29 +
 arch_init.c                      |    2 +
 hw/avr/arduino.c                 |  151 ++
 hw/avr/atmel_atmega.c            |  470 +++++
 hw/avr/boot.c                    |   74 +
 hw/char/atmel_usart.c            |  320 ++++
 hw/core/loader.c                 |   15 +-
 hw/misc/atmel_power.c            |  112 ++
 hw/riscv/boot.c                  |    2 +-
 hw/timer/atmel_timer16.c         |  605 ++++++
 target/avr/cpu.c                 |  826 ++++++++
 target/avr/disas.c               |  246 +++
 target/avr/gdbstub.c             |   84 +
 target/avr/helper.c              |  347 ++++
 target/avr/machine.c             |  121 ++
 target/avr/translate.c           | 2997 ++++++++++++++++++++++++++++++
 tests/qtest/boot-serial-test.c   |   11 +
 tests/qtest/machine-none-test.c  |    1 +
 .travis.yml                      |    2 +-
 MAINTAINERS                      |   30 +
 gdb-xml/avr-cpu.xml              |   49 +
 hw/avr/Kconfig                   |    9 +
 hw/avr/Makefile.objs             |    3 +
 hw/char/Kconfig                  |    3 +
 hw/char/Makefile.objs            |    1 +
 hw/misc/Kconfig                  |    3 +
 hw/misc/Makefile.objs            |    2 +
 hw/timer/Kconfig                 |    3 +
 hw/timer/Makefile.objs           |    2 +
 target/avr/Makefile.objs         |   34 +
 target/avr/insn.decode           |  182 ++
 tests/acceptance/machine_avr6.py |   50 +
 tests/qtest/Makefile.include     |    2 +
 50 files changed, 7539 insertions(+), 13 deletions(-)
 create mode 100644 default-configs/avr-softmmu.mak
 create mode 100644 hw/avr/atmel_atmega.h
 create mode 100644 hw/avr/boot.h
 create mode 100644 include/hw/char/atmel_usart.h
 create mode 100644 include/hw/misc/atmel_power.h
 create mode 100644 include/hw/timer/atmel_timer16.h
 create mode 100644 target/avr/cpu-param.h
 create mode 100644 target/avr/cpu-qom.h
 create mode 100644 target/avr/cpu.h
 create mode 100644 target/avr/helper.h
 create mode 100644 hw/avr/arduino.c
 create mode 100644 hw/avr/atmel_atmega.c
 create mode 100644 hw/avr/boot.c
 create mode 100644 hw/char/atmel_usart.c
 create mode 100644 hw/misc/atmel_power.c
 create mode 100644 hw/timer/atmel_timer16.c
 create mode 100644 target/avr/cpu.c
 create mode 100644 target/avr/disas.c
 create mode 100644 target/avr/gdbstub.c
 create mode 100644 target/avr/helper.c
 create mode 100644 target/avr/machine.c
 create mode 100644 target/avr/translate.c
 create mode 100644 gdb-xml/avr-cpu.xml
 create mode 100644 hw/avr/Kconfig
 create mode 100644 hw/avr/Makefile.objs
 create mode 100644 target/avr/Makefile.objs
 create mode 100644 target/avr/insn.decode
 create mode 100644 tests/acceptance/machine_avr6.py

--
2.21.1

#54 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

This is the AVR port from Michael release (merge) candidate 2.

Since v1 [1]:
- Addressed Thomas comments
- Fixed a non-critical bug in ATmega (incorrect SRAM base address)
- Added ELF parsing requested by Aleksandar
- Dropped default machine (as with the ARM port)

If you don't have a default machine, I'd expect to see a change to tests/qemu-iotests/check in the patchset. Please make sure that "make check" (i.e. "make check-block" in this case) continues to work if you run "configure" just with this single architecture (--target-list=avr-softmmu).

Thanks,
Thomas

Tested-by on the v1 series
#55 lore raw
In reply to Philippe Mathieu-Daudé

The only thing I want to change is instead of -kernel put -bios in qemu-doc.texi file. Should I send a new series?

Quoted 178 lines · Philippe Mathieu-Daudé

This is the AVR port from Michael release (merge) candidate 2.

Since v1 [1]:
- Addressed Thomas comments
- Fixed a non-critical bug in ATmega (incorrect SRAM base address)
- Added ELF parsing requested by Aleksandar
- Dropped default machine (as with the ARM port)

Change since rc1:

$ git backport-diff -u avr-rc1 -r origin/master..
Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch
[down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences,
respectively

001/25:[----] [--] 'target/avr: Add outward facing interfaces and core CPU logic'
002/25:[----] [--] 'target/avr: Add instruction helpers' 003/25:[----] [--] 'target/avr: Add instruction translation - Registers definition'
004/25:[----] [--] 'target/avr: Add instruction translation - Arithmetic and Logic Instructions'
005/25:[----] [--] 'target/avr: Add instruction translation - Branch Instructions'
006/25:[----] [--] 'target/avr: Add instruction translation - Data Transfer Instructions'
007/25:[----] [--] 'target/avr: Add instruction translation - Bit and Bit-test Instructions'
008/25:[----] [--] 'target/avr: Add instruction translation - MCU Control Instructions'
009/25:[----] [--] 'target/avr: Add instruction translation - CPU main translation function'
010/25:[----] [--] 'target/avr: Add instruction disassembly function' 011/25:[----] [--] 'hw/char: Add limited support for Atmel USART peripheral'
012/25:[0045] [FC] 'hw/timer: Add limited support for Atmel 16 bit timer peripheral'
013/25:[----] [--] 'hw/misc: Add Atmel power device' 014/25:[0024] [FC] 'target/avr: Add section about AVR into QEMU documentation'
015/25:[----] [--] 'target/avr: Register AVR support with the rest of QEMU' 016/25:[----] [--] 'target/avr: Add machine none test' 017/25:[0002] [FC] 'target/avr: Update MAINTAINERS file'
018/25:[down] 'hw/core/loader: Let load_elf populate the
processor-specific flags'
019/25:[down] 'hw/avr: Add helper to load raw/ELF firmware binaries'
020/25:[0015] [FC] 'hw/avr: Add some ATmega microcontrollers' 021/25:[0040] [FC] 'hw/avr: Add some Arduino boards' 022/25:[----] [--] 'target/avr: Update build system' 023/25:[----] [--] 'tests/boot-serial-test: Test some Arduino boards (AVR based)'
024/25:[----] [--] 'tests/acceptance: Test the Arduino MEGA2560 board' 025/25:[----] [--] '.travis.yml: Run the AVR acceptance tests'

Repo: https://gitlab.com/philmd/qemu/commits/avr-rc2

[1] https://www.mail-archive.com/[email protected]/msg672926.html
Supersedes: <[email protected]>

Michael Rolnik (20):
target/avr: Add outward facing interfaces and core CPU logic target/avr: Add instruction helpers
target/avr: Add instruction translation - Registers definition target/avr: Add instruction translation - Arithmetic and Logic
Instructions
target/avr: Add instruction translation - Branch Instructions target/avr: Add instruction translation - Data Transfer Instructions target/avr: Add instruction translation - Bit and Bit-test
Instructions
target/avr: Add instruction translation - MCU Control Instructions target/avr: Add instruction translation - CPU main translation
function
target/avr: Add instruction disassembly function
hw/char: Add limited support for Atmel USART peripheral hw/timer: Add limited support for Atmel 16 bit timer peripheral hw/misc: Add Atmel power device
target/avr: Add section about AVR into QEMU documentation target/avr: Register AVR support with the rest of QEMU target/avr: Add machine none test
target/avr: Update MAINTAINERS file
target/avr: Update build system
tests/boot-serial-test: Test some Arduino boards (AVR based) tests/acceptance: Test the Arduino MEGA2560 board

Philippe Mathieu-Daudé (5):
hw/core/loader: Let load_elf populate the processor-specific flags hw/avr: Add helper to load raw/ELF firmware binaries hw/avr: Add some ATmega microcontrollers
hw/avr: Add some Arduino boards
.travis.yml: Run the AVR acceptance tests

 qemu-doc.texi                    |   51 +
 configure                        |    7 +
 default-configs/avr-softmmu.mak  |    5 +
 qapi/machine.json                |    3 +-
 hw/avr/atmel_atmega.h            |   48 +
 hw/avr/boot.h                    |   33 +
 include/disas/dis-asm.h          |   19 +
 include/elf.h                    |    2 +
 include/hw/char/atmel_usart.h    |   93 +
 include/hw/elf_ops.h             |    6 +-
 include/hw/loader.h              |    6 +-
 include/hw/misc/atmel_power.h    |   46 +
 include/hw/timer/atmel_timer16.h |   94 +
 include/sysemu/arch_init.h       |    1 +
 target/avr/cpu-param.h           |   37 +
 target/avr/cpu-qom.h             |   54 +
 target/avr/cpu.h                 |  259 +++
 target/avr/helper.h              |   29 +
 arch_init.c                      |    2 +
 hw/avr/arduino.c                 |  151 ++
 hw/avr/atmel_atmega.c            |  470 +++++
 hw/avr/boot.c                    |   74 +
 hw/char/atmel_usart.c            |  320 ++++
 hw/core/loader.c                 |   15 +-
 hw/misc/atmel_power.c            |  112 ++
 hw/riscv/boot.c                  |    2 +-
 hw/timer/atmel_timer16.c         |  605 ++++++
 target/avr/cpu.c                 |  826 ++++++++
 target/avr/disas.c               |  246 +++
 target/avr/gdbstub.c             |   84 +
 target/avr/helper.c              |  347 ++++
 target/avr/machine.c             |  121 ++
 target/avr/translate.c           | 2997 ++++++++++++++++++++++++++++++
 tests/qtest/boot-serial-test.c   |   11 +
 tests/qtest/machine-none-test.c  |    1 +
 .travis.yml                      |    2 +-
 MAINTAINERS                      |   30 +
 gdb-xml/avr-cpu.xml              |   49 +
 hw/avr/Kconfig                   |    9 +
 hw/avr/Makefile.objs             |    3 +
 hw/char/Kconfig                  |    3 +
 hw/char/Makefile.objs            |    1 +
 hw/misc/Kconfig                  |    3 +
 hw/misc/Makefile.objs            |    2 +
 hw/timer/Kconfig                 |    3 +
 hw/timer/Makefile.objs           |    2 +
 target/avr/Makefile.objs         |   34 +
 target/avr/insn.decode           |  182 ++
 tests/acceptance/machine_avr6.py |   50 +
 tests/qtest/Makefile.include     |    2 +
 50 files changed, 7539 insertions(+), 13 deletions(-)
 create mode 100644 default-configs/avr-softmmu.mak
 create mode 100644 hw/avr/atmel_atmega.h
 create mode 100644 hw/avr/boot.h
 create mode 100644 include/hw/char/atmel_usart.h
 create mode 100644 include/hw/misc/atmel_power.h
 create mode 100644 include/hw/timer/atmel_timer16.h
 create mode 100644 target/avr/cpu-param.h
 create mode 100644 target/avr/cpu-qom.h
 create mode 100644 target/avr/cpu.h
 create mode 100644 target/avr/helper.h
 create mode 100644 hw/avr/arduino.c
 create mode 100644 hw/avr/atmel_atmega.c
 create mode 100644 hw/avr/boot.c
 create mode 100644 hw/char/atmel_usart.c
 create mode 100644 hw/misc/atmel_power.c
 create mode 100644 hw/timer/atmel_timer16.c
 create mode 100644 target/avr/cpu.c
 create mode 100644 target/avr/disas.c
 create mode 100644 target/avr/gdbstub.c
 create mode 100644 target/avr/helper.c
 create mode 100644 target/avr/machine.c
 create mode 100644 target/avr/translate.c
 create mode 100644 gdb-xml/avr-cpu.xml
 create mode 100644 hw/avr/Kconfig
 create mode 100644 hw/avr/Makefile.objs
 create mode 100644 target/avr/Makefile.objs
 create mode 100644 target/avr/insn.decode
 create mode 100644 tests/acceptance/machine_avr6.py

--
2.21.1

Signature · 2 lines
Best Regards,
Michael Rolnik
#56 lore raw
Quoted 1 line · Michael Rolnik
Tested-by Michael Rolnik <[email protected] <mailto:[email protected]>>

Thanks a lot!

Quoted 1 line

The only thing I want to change is instead of -kernel put -bios in qemu-doc.texi file. Should I send a new series?

Please do NOT :)

Richard can do the trivial fixup directly.

Quoted 188 lines · On Fri, Jan 24, 2020 at 2:51 AM Philippe Mathieu-Daudé <[email protected]…

This is the AVR port from Michael release (merge) candidate 2.

Since v1 [1]:
- Addressed Thomas comments
- Fixed a non-critical bug in ATmega (incorrect SRAM base address)
- Added ELF parsing requested by Aleksandar
- Dropped default machine (as with the ARM port)

Change since rc1:

$ git backport-diff -u avr-rc1 -r origin/master..
Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch
[down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/25:[----] [--] 'target/avr: Add outward facing interfaces and core CPU logic'
002/25:[----] [--] 'target/avr: Add instruction helpers' 003/25:[----] [--] 'target/avr: Add instruction translation - Registers definition'
004/25:[----] [--] 'target/avr: Add instruction translation - Arithmetic and Logic Instructions'
005/25:[----] [--] 'target/avr: Add instruction translation - Branch Instructions'
006/25:[----] [--] 'target/avr: Add instruction translation - Data Transfer Instructions'
007/25:[----] [--] 'target/avr: Add instruction translation - Bit and Bit-test Instructions'
008/25:[----] [--] 'target/avr: Add instruction translation - MCU Control Instructions'
009/25:[----] [--] 'target/avr: Add instruction translation - CPU main translation function'
010/25:[----] [--] 'target/avr: Add instruction disassembly function' 011/25:[----] [--] 'hw/char: Add limited support for Atmel USART peripheral'
012/25:[0045] [FC] 'hw/timer: Add limited support for Atmel 16 bit timer peripheral'
013/25:[----] [--] 'hw/misc: Add Atmel power device' 014/25:[0024] [FC] 'target/avr: Add section about AVR into QEMU documentation'
015/25:[----] [--] 'target/avr: Register AVR support with the rest of QEMU'
016/25:[----] [--] 'target/avr: Add machine none test' 017/25:[0002] [FC] 'target/avr: Update MAINTAINERS file' 018/25:[down]      'hw/core/loader: Let load_elf populate the processor-specific flags'
019/25:[down]      'hw/avr: Add helper to load raw/ELF firmware binaries'
020/25:[0015] [FC] 'hw/avr: Add some ATmega microcontrollers' 021/25:[0040] [FC] 'hw/avr: Add some Arduino boards' 022/25:[----] [--] 'target/avr: Update build system' 023/25:[----] [--] 'tests/boot-serial-test: Test some Arduino boards (AVR based)'
024/25:[----] [--] 'tests/acceptance: Test the Arduino MEGA2560 board' 025/25:[----] [--] '.travis.yml: Run the AVR acceptance tests'

Repo: https://gitlab.com/philmd/qemu/commits/avr-rc2

[1] https://www.mail-archive.com/[email protected]/msg672926.html
Supersedes: <[email protected] <mailto:[email protected]>>

Michael Rolnik (20):
  target/avr: Add outward facing interfaces and core CPU logic target/avr: Add instruction helpers
  target/avr: Add instruction translation - Registers definition target/avr: Add instruction translation - Arithmetic and Logic Instructions
  target/avr: Add instruction translation - Branch Instructions target/avr: Add instruction translation - Data Transfer Instructions target/avr: Add instruction translation - Bit and Bit-test Instructions
  target/avr: Add instruction translation - MCU Control Instructions target/avr: Add instruction translation - CPU main translation function
  target/avr: Add instruction disassembly function hw/char: Add limited support for Atmel USART peripheral hw/timer: Add limited support for Atmel 16 bit timer peripheral hw/misc: Add Atmel power device
  target/avr: Add section about AVR into QEMU documentation target/avr: Register AVR support with the rest of QEMU target/avr: Add machine none test
  target/avr: Update MAINTAINERS file
  target/avr: Update build system
  tests/boot-serial-test: Test some Arduino boards (AVR based) tests/acceptance: Test the Arduino MEGA2560 board

Philippe Mathieu-Daudé (5):
  hw/core/loader: Let load_elf populate the processor-specific flags hw/avr: Add helper to load raw/ELF firmware binaries hw/avr: Add some ATmega microcontrollers
  hw/avr: Add some Arduino boards
  .travis.yml: Run the AVR acceptance tests

 qemu-doc.texi                    |   51 +
 configure                        |    7 + default-configs/avr-softmmu.mak  |    5 + qapi/machine.json                |    3 +- hw/avr/atmel_atmega.h            |   48 +
 hw/avr/boot.h                    |   33 + include/disas/dis-asm.h          |   19 +
 include/elf.h                    |    2 + include/hw/char/atmel_usart.h    |   93 + include/hw/elf_ops.h             |    6 +- include/hw/loader.h              |    6 +- include/hw/misc/atmel_power.h    |   46 + include/hw/timer/atmel_timer16.h |   94 + include/sysemu/arch_init.h       |    1 + target/avr/cpu-param.h           |   37 + target/avr/cpu-qom.h             |   54 +
 target/avr/cpu.h                 |  259 +++ target/avr/helper.h              |   29 +
 arch_init.c                      |    2 +
 hw/avr/arduino.c                 |  151 ++ hw/avr/atmel_atmega.c            |  470 +++++ hw/avr/boot.c                    |   74 + hw/char/atmel_usart.c            |  320 ++++ hw/core/loader.c                 |   15 +- hw/misc/atmel_power.c            |  112 ++
 hw/riscv/boot.c                  |    2 +- hw/timer/atmel_timer16.c         |  605 ++++++ target/avr/cpu.c                 |  826 ++++++++ target/avr/disas.c               |  246 +++ target/avr/gdbstub.c             |   84 + target/avr/helper.c              |  347 ++++ target/avr/machine.c             |  121 ++ target/avr/translate.c           | 2997 ++++++++++++++++++++++++++++++ tests/qtest/boot-serial-test.c   |   11 + tests/qtest/machine-none-test.c  |    1 +
 .travis.yml                      |    2 +-
 MAINTAINERS                      |   30 + gdb-xml/avr-cpu.xml              |   49 +
 hw/avr/Kconfig                   |    9 + hw/avr/Makefile.objs             |    3 +
 hw/char/Kconfig                  |    3 + hw/char/Makefile.objs            |    1 +
 hw/misc/Kconfig                  |    3 + hw/misc/Makefile.objs            |    2 +
 hw/timer/Kconfig                 |    3 + hw/timer/Makefile.objs           |    2 + target/avr/Makefile.objs         |   34 + target/avr/insn.decode           |  182 ++ tests/acceptance/machine_avr6.py |   50 + tests/qtest/Makefile.include     |    2 +
 50 files changed, 7539 insertions(+), 13 deletions(-) create mode 100644 default-configs/avr-softmmu.mak
 create mode 100644 hw/avr/atmel_atmega.h
 create mode 100644 hw/avr/boot.h
 create mode 100644 include/hw/char/atmel_usart.h
 create mode 100644 include/hw/misc/atmel_power.h
 create mode 100644 include/hw/timer/atmel_timer16.h
 create mode 100644 target/avr/cpu-param.h
 create mode 100644 target/avr/cpu-qom.h
 create mode 100644 target/avr/cpu.h
 create mode 100644 target/avr/helper.h
 create mode 100644 hw/avr/arduino.c
 create mode 100644 hw/avr/atmel_atmega.c
 create mode 100644 hw/avr/boot.c
 create mode 100644 hw/char/atmel_usart.c
 create mode 100644 hw/misc/atmel_power.c
 create mode 100644 hw/timer/atmel_timer16.c
 create mode 100644 target/avr/cpu.c
 create mode 100644 target/avr/disas.c
 create mode 100644 target/avr/gdbstub.c
 create mode 100644 target/avr/helper.c
 create mode 100644 target/avr/machine.c
 create mode 100644 target/avr/translate.c
 create mode 100644 gdb-xml/avr-cpu.xml
 create mode 100644 hw/avr/Kconfig
 create mode 100644 hw/avr/Makefile.objs
 create mode 100644 target/avr/Makefile.objs
 create mode 100644 target/avr/insn.decode
 create mode 100644 tests/acceptance/machine_avr6.py

--
2.21.1

Signature · 2 lines
Best Regards,
Michael Rolnik
#57 lore raw

Just one patch attached.
Thanks.

Quoted 207 lines · Philippe Mathieu-Daudé
Quoted 1 line · Michael Rolnik
Tested-by Michael Rolnik <[email protected] <mailto:[email protected]>>

Thanks a lot!

Quoted 2 lines

The only thing I want to change is instead of -kernel put -bios in qemu-doc.texi file. Should I send a new series?

Please do NOT :)

Richard can do the trivial fixup directly.

Quoted 53 lines

On Fri, Jan 24, 2020 at 2:51 AM Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

    This is the AVR port from Michael release (merge) candidate 2.

    Since v1 [1]:
    - Addressed Thomas comments
    - Fixed a non-critical bug in ATmega (incorrect SRAM base address)
    - Added ELF parsing requested by Aleksandar
    - Dropped default machine (as with the ARM port)

    Change since rc1:

    $ git backport-diff -u avr-rc1 -r origin/master..
    Key:
    [----] : patches are identical
    [####] : number of functional differences between
    upstream/downstream patch
    [down] : patch is downstream-only
    The flags [FC] indicate (F)unctional and (C)ontextual differences,
    respectively

    001/25:[----] [--] 'target/avr: Add outward facing interfaces and
    core CPU logic'
    002/25:[----] [--] 'target/avr: Add instruction helpers'
    003/25:[----] [--] 'target/avr: Add instruction translation -
    Registers definition'
    004/25:[----] [--] 'target/avr: Add instruction translation -
    Arithmetic and Logic Instructions'
    005/25:[----] [--] 'target/avr: Add instruction translation - Branch
    Instructions'
    006/25:[----] [--] 'target/avr: Add instruction translation - Data
    Transfer Instructions'
    007/25:[----] [--] 'target/avr: Add instruction translation - Bit
    and Bit-test Instructions'
    008/25:[----] [--] 'target/avr: Add instruction translation - MCU
    Control Instructions'
    009/25:[----] [--] 'target/avr: Add instruction translation - CPU
    main translation function'
    010/25:[----] [--] 'target/avr: Add instruction disassembly function'
    011/25:[----] [--] 'hw/char: Add limited support for Atmel USART
    peripheral'
    012/25:[0045] [FC] 'hw/timer: Add limited support for Atmel 16 bit
    timer peripheral'
    013/25:[----] [--] 'hw/misc: Add Atmel power device'
    014/25:[0024] [FC] 'target/avr: Add section about AVR into QEMU
    documentation'
    015/25:[----] [--] 'target/avr: Register AVR support with the rest
    of QEMU'
    016/25:[----] [--] 'target/avr: Add machine none test'
    017/25:[0002] [FC] 'target/avr: Update MAINTAINERS file'
    018/25:[down]      'hw/core/loader: Let load_elf populate the
    processor-specific flags'
Quoted 8 lines
    019/25:[down]      'hw/avr: Add helper to load raw/ELF firmware
    binaries'
    020/25:[0015] [FC] 'hw/avr: Add some ATmega microcontrollers'
    021/25:[0040] [FC] 'hw/avr: Add some Arduino boards'
    022/25:[----] [--] 'target/avr: Update build system'
    023/25:[----] [--] 'tests/boot-serial-test: Test some Arduino boards
    (AVR based)'
    024/25:[----] [--] 'tests/acceptance: Test the Arduino MEGA2560

board'

Quoted 5 lines
    025/25:[----] [--] '.travis.yml: Run the AVR acceptance tests'

    Repo: https://gitlab.com/philmd/qemu/commits/avr-rc2

    [1]

https://www.mail-archive.com/[email protected]/msg672926.html

Quoted 11 lines

Supersedes: <[email protected] <mailto:[email protected]>>

Michael Rolnik (20):
target/avr: Add outward facing interfaces and core CPU logic target/avr: Add instruction helpers
target/avr: Add instruction translation - Registers definition target/avr: Add instruction translation - Arithmetic and Logic
Instructions
target/avr: Add instruction translation - Branch Instructions target/avr: Add instruction translation - Data Transfer

Instructions

Quoted 49 lines

target/avr: Add instruction translation - Bit and Bit-test
Instructions
target/avr: Add instruction translation - MCU Control Instructions target/avr: Add instruction translation - CPU main translation
function
target/avr: Add instruction disassembly function hw/char: Add limited support for Atmel USART peripheral hw/timer: Add limited support for Atmel 16 bit timer peripheral hw/misc: Add Atmel power device
target/avr: Add section about AVR into QEMU documentation target/avr: Register AVR support with the rest of QEMU target/avr: Add machine none test
target/avr: Update MAINTAINERS file
target/avr: Update build system tests/boot-serial-test: Test some Arduino boards (AVR based) tests/acceptance: Test the Arduino MEGA2560 board

Philippe Mathieu-Daudé (5):
hw/core/loader: Let load_elf populate the processor-specific flags hw/avr: Add helper to load raw/ELF firmware binaries hw/avr: Add some ATmega microcontrollers
hw/avr: Add some Arduino boards
.travis.yml: Run the AVR acceptance tests

qemu-doc.texi | 51 +
configure | 7 +
default-configs/avr-softmmu.mak | 5 +
qapi/machine.json | 3 +-
hw/avr/atmel_atmega.h | 48 +
hw/avr/boot.h | 33 +
include/disas/dis-asm.h | 19 +
include/elf.h | 2 +
include/hw/char/atmel_usart.h | 93 +
include/hw/elf_ops.h | 6 +-
include/hw/loader.h | 6 +-
include/hw/misc/atmel_power.h | 46 +
include/hw/timer/atmel_timer16.h | 94 +
include/sysemu/arch_init.h | 1 +
target/avr/cpu-param.h | 37 +
target/avr/cpu-qom.h | 54 +
target/avr/cpu.h | 259 +++
target/avr/helper.h | 29 +
arch_init.c | 2 +
hw/avr/arduino.c | 151 ++
hw/avr/atmel_atmega.c | 470 +++++
hw/avr/boot.c | 74 +
hw/char/atmel_usart.c | 320 ++++
hw/core/loader.c | 15 +-
hw/misc/atmel_power.c | 112 ++

Quoted 8 lines

hw/riscv/boot.c | 2 +-
hw/timer/atmel_timer16.c | 605 ++++++
target/avr/cpu.c | 826 ++++++++
target/avr/disas.c | 246 +++
target/avr/gdbstub.c | 84 +
target/avr/helper.c | 347 ++++
target/avr/machine.c | 121 ++
target/avr/translate.c | 2997

++++++++++++++++++++++++++++++

Quoted 55 lines
      tests/qtest/boot-serial-test.c   |   11 +
      tests/qtest/machine-none-test.c  |    1 +
      .travis.yml                      |    2 +-
      MAINTAINERS                      |   30 +
      gdb-xml/avr-cpu.xml              |   49 +
      hw/avr/Kconfig                   |    9 +
      hw/avr/Makefile.objs             |    3 +
      hw/char/Kconfig                  |    3 +
      hw/char/Makefile.objs            |    1 +
      hw/misc/Kconfig                  |    3 +
      hw/misc/Makefile.objs            |    2 +
      hw/timer/Kconfig                 |    3 +
      hw/timer/Makefile.objs           |    2 +
      target/avr/Makefile.objs         |   34 +
      target/avr/insn.decode           |  182 ++
      tests/acceptance/machine_avr6.py |   50 +
      tests/qtest/Makefile.include     |    2 +
      50 files changed, 7539 insertions(+), 13 deletions(-)

create mode 100644 default-configs/avr-softmmu.mak create mode 100644 hw/avr/atmel_atmega.h create mode 100644 hw/avr/boot.h
create mode 100644 include/hw/char/atmel_usart.h create mode 100644 include/hw/misc/atmel_power.h create mode 100644 include/hw/timer/atmel_timer16.h create mode 100644 target/avr/cpu-param.h create mode 100644 target/avr/cpu-qom.h create mode 100644 target/avr/cpu.h
create mode 100644 target/avr/helper.h create mode 100644 hw/avr/arduino.c
create mode 100644 hw/avr/atmel_atmega.c create mode 100644 hw/avr/boot.c
create mode 100644 hw/char/atmel_usart.c create mode 100644 hw/misc/atmel_power.c create mode 100644 hw/timer/atmel_timer16.c create mode 100644 target/avr/cpu.c
create mode 100644 target/avr/disas.c create mode 100644 target/avr/gdbstub.c create mode 100644 target/avr/helper.c create mode 100644 target/avr/machine.c create mode 100644 target/avr/translate.c create mode 100644 gdb-xml/avr-cpu.xml create mode 100644 hw/avr/Kconfig
create mode 100644 hw/avr/Makefile.objs create mode 100644 target/avr/Makefile.objs create mode 100644 target/avr/insn.decode create mode 100644 tests/acceptance/machine_avr6.py

--
2.21.1

--
Best Regards,
Michael Rolnik

Signature · 2 lines
Best Regards,
Michael Rolnik

From 7a03872e892b0ed95bbf6a34b8da2a67f65aed07 Mon Sep 17 00:00:00 2001
From: Michael Rolnik <[email protected]>
Date: Fri, 24 Jan 2020 01:51:20 +0100
Subject: [PATCH] target/avr: Add section about AVR into QEMU documentation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Message-Id: <[email protected]>

---
 qemu-doc.texi | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
qemu-doc.texi +51 -0
index 2328e7ea47..aaf4e5478e 100644
@@ -1723,6 +1723,7 @@ differences are mentioned in the following sections.
 * Microblaze System emulator::
 * SH4 System emulator::
 * Xtensa System emulator::
+* AVR System emulator::
 @end menu
 
 @node PowerPC System emulator
@@ -2496,6 +2497,56 @@ so should only be used with trusted guest OS.
 
 @c man end
 
+@node AVR System emulator
+@section AVR System emulator
+@cindex system emulation (AVR)
+
+Use the executable @file{qemu-system-avr} to emulates a AVR 8 bit based machine
+having one for the following cores: avr1, avr2, avr25, avr3, avr31, avr35, avr4,
+avr5, avr51, avr6, avrtiny, xmega2, xmega3, xmega4, xmega5, xmega6 and xmega7.
+
+As for now it supports few Arduino boards for educational and testing purposes.
+These boards use a ATmega controller, which model is limited to USART & 16 bit
+timer devices, enought to run FreeRTOS based applications (like this @url{https://github.com/seharris/qemu-avr-tests/blob/master/free-rtos/Demo/AVR_ATMega2560_GCC/demo.elf,,demo})
+
+Following are examples of possible usages, assuming demo.elf is compiled for
+AVR cpu
+@itemize
+
+@item Continuous non interrupted execution
+@example
+qemu-system-avr -machine mega2560 -bios demo.elf
+@end example
+
+@item Continuous non interrupted execution with serial output into telnet window
+@example
+qemu-system-avr -machine mega2560 -bios demo.elf -serial tcp::5678,server,nowait -nographic
+@end example
+and then in another shell
+@example
+telnet localhost 5678
+@end example
+
+@item Debugging wit GDB debugger
+@example
+qemu-system-avr -machine mega2560 -bios demo.elf -s -S
+@end example
+and then in another shell
+@example
+avr-gdb demo.elf
+@end example
+and then within GDB shell
+@example
+target remote :1234
+@end example
+
+@item Print out executed instructions
+@example
+qemu-system-avr -machine mega2560 -bios demo.elf -d in_asm
+@end example
+
+@end itemize
+
 @node QEMU User space emulator
 @chapter QEMU User space emulator
 
Signature · 1 line
2.17.2 (Apple Git-113)