hw/avr: Introduce the Arduino boards
v2 cover hw/avr: Introduce the Arduino boards
Hi,
This series add the arduino boards, aiming at removing the 'sample' board that doesn't follow any specification.
Since v1:
- Addressed Igor comments
- Addressed Aleksandar comments
- Fixed UART issue (was due to IRQ shifted by 2 in CPU)
TODO after merge is:
- Extract Timer8 common parts from Timer16
- Add GPIOs
- Connect LED to GPIO on Arduino
- Plug to Scratch (See http://s4a.cat/).
(I plan to purpose that as a GSoC idea).
Thank you for having insisted with this port during so long!
Regards,
Phil.
Based-on: <[email protected]>
https://www.mail-archive.com/[email protected]/msg666384.html
Cc: Stefan Hajnoczi <[email protected]> (for GSoC idea)
Cc: Phillip Stevens <[email protected]> (for FreeRTOS example)
Philippe Mathieu-Daudé (8):
hw/char/avr: Reduce USART I/O size
hw/timer/avr_timer16: Rename memory region debugging name hw/avr: Add some ATmega microcontrollers
hw/avr: Add some Arduino boards
tests/boot-serial-test: Test some Arduino boards (AVR based) tests/acceptance: Do not set the machine type manually tests/acceptance: Keep multilines comment consistent with other tests tests/acceptance: Test the Arduino MEGA2560 board
hw/avr/atmega.h | 48 ++++ hw/avr/arduino.c | 177 ++++++++++++ hw/avr/atmega.c | 460 +++++++++++++++++++++++++++++++ hw/char/avr_usart.c | 2 +- hw/timer/avr_timer16.c | 6 +- tests/boot-serial-test.c | 2 + hw/avr/Makefile.objs | 2 + tests/acceptance/machine_avr6.py | 11 +- 8 files changed, 697 insertions(+), 11 deletions(-) create mode 100644 hw/avr/atmega.h create mode 100644 hw/avr/arduino.c create mode 100644 hw/avr/atmega.c
Signature · 1 line
2.21.0
PATCH v2 1/8 hw/char/avr: Reduce USART I/O size
Per the datasheet the USART uses 7 consecutive 8-bit registers.
--- hw/char/avr_usart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/char/avr_usart.c +1 -1
@@ -280,7 +280,7 @@ static void avr_usart_init(Object *obj)sysbus_init_irq (SYS_BUS_DEVICE (obj), &s->rxc_irq);sysbus_init_irq (SYS_BUS_DEVICE (obj), &s->dre_irq);sysbus_init_irq (SYS_BUS_DEVICE (obj), &s->txc_irq); -memory_region_init_io (&s->mmio, obj, &avr_usart_ops, s,TYPE_AVR_USART ,8 ); +memory_region_init_io (&s->mmio, obj, &avr_usart_ops, s,TYPE_AVR_USART ,7 );sysbus_init_mmio (SYS_BUS_DEVICE (obj), &s->mmio);qdev_init_gpio_in (DEVICE (s), avr_usart_pr,1 ); s->enabled = true;
Signature · 1 line
2.21.0
PATCH v2 2/8 hw/timer/avr_timer16: Rename memory region debugging name
In reply to Philippe Mathieu-DaudéThis device expose 3 different I/O regions. Name them differently to have a clearer 'info mtree' output.
--- hw/timer/avr_timer16.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
hw/timer/avr_timer16.c +3 -3
@@ -562,11 +562,11 @@ static void avr_timer16_init(Object *obj)sysbus_init_irq (SYS_BUS_DEVICE (obj), &s->ovf_irq);memory_region_init_io (&s->iomem, obj, &avr_timer16_ops, - s,TYPE_AVR_TIMER16 ,0xe ); + s,"avr-timer16" ,0xe );memory_region_init_io (&s->imsk_iomem, obj, &avr_timer16_imsk_ops, - s,TYPE_AVR_TIMER16 ,0x1 ); + s,"avr-timer16-intmask" ,0x1 );memory_region_init_io (&s->ifr_iomem, obj, &avr_timer16_ifr_ops, - s,TYPE_AVR_TIMER16 ,0x1 ); + s,"avr-timer16-intflag" ,0x1 );sysbus_init_mmio (SYS_BUS_DEVICE (obj), &s->iomem);sysbus_init_mmio (SYS_BUS_DEVICE (obj), &s->imsk_iomem);
Signature · 1 line
2.21.0
PATCH v2 3/8 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
---
v2:
- Reword description adding more information (Aleksandar)
- Use DEFINE_TYPES and memory_region_init_ram (Igor)
--- hw/avr/atmega.h | 48 +++++ hw/avr/atmega.c | 460 +++++++++++++++++++++++++++++++++++++++++++ hw/avr/Makefile.objs | 1 + 3 files changed, 509 insertions(+) create mode 100644 hw/avr/atmega.h create mode 100644 hw/avr/atmega.c
hw/avr/atmega.h new file +48 -0
@@ -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_ATMEGA_H +#define HW_AVR_ATMEGA_H + +#include "hw/char/avr_usart.h" +#include "hw/timer/avr_timer16.h" +#include "hw/misc/avr_mask.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_ATMEGA_H */
hw/avr/atmega.c new file +460 -0
460 lines not on this page · raw message
hw/avr/Makefile.objs +1 -0
@@ -1 +1,2 @@ obj-y += sample.o +obj-y += atmega.o
Signature · 1 line
2.21.0
Re: [PATCH v2 3/8] hw/avr: Add some ATmega microcontrollers
Quoted 28 lines · Philippe Mathieu-Daudé
Add some microcontrollers from the megaAVR family (ATmega series):
- middle range: ATmega168 and ATmega328
- high range: ATmega1280 and ATmega2560For product comparison:
https://www.microchip.com/wwwproducts/ProductCompare/ATmega168P/ATmega328P
https://www.microchip.com/wwwproducts/ProductCompare/ATmega1280/ATmega2560Datasheets:
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.pdfSigned-off-by Philippe Mathieu-Daudé <[email protected]>---
v2:
- Reword description adding more information (Aleksandar)
- UseDEFINE_TYPESandmemory_region_init_ram(Igor)Cc Igor Mammedov <[email protected]>--- hw/avr/atmega.h | 48 +++++ hw/avr/atmega.c | 460 +++++++++++++++++++++++++++++++++++++++++++ hw/avr/Makefile.objs | 1 + 3 files changed, 509 insertions(+)create mode 100644
hw/avr/atmega.h
create mode 100644hw/avr/atmega.c
[...]
hw/avr/Makefile.objs line 2
Quoted 7 lines
hw/avr/Makefile.objs +1
-0@@ -1 +1,2 @@ obj-y += sample.o +obj-y += atmega.o
Should this have a proper Kconfig entry, and in next patch Arduino selects it? Probably cleaner and in match with other archs.
PATCH v2 4/8 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
---
v2:
- Reword description adding more information (Aleksandar)
- Use DEFINE_TYPES (Igor)
--- hw/avr/arduino.c | 177 +++++++++++++++++++++++++++++++++++++++++++ hw/avr/Makefile.objs | 1 + 2 files changed, 178 insertions(+) create mode 100644 hw/avr/arduino.c
hw/avr/arduino.c new file +177 -0
@@ -0,0 +1,177 @@ +/* +* QEMU Arduino boards +* +* Copyright (c) 2019 Philippe Mathieu-Daudé +* +* This work is licensed under the terms of the GNU GPLv2 or later. +* See the COPYING file in the top-level directory. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +/* TODO: Implement the use of EXTRAM */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "hw/boards.h" +#include "hw/loader.h" +#include "elf.h" +#include "atmega.h" + +typedef struct ArduinoMachineState { +/*< private >*/ +MachineState parent_obj; +/*< public >*/ +AtmegaMcuState mcu; +MemoryRegion extram; +}ArduinoMachineState ; + +typedef struct ArduinoMachineClass { +/*< private >*/ +MachineClass parent_class; +/*< public >*/ +const char *mcu_type; +uint64_t xtal_hz; +size_t extram_size; +}ArduinoMachineClass ; + +#define TYPE_ARDUINO_MACHINE \ + MACHINE_TYPE_NAME("arduino") +#define ARDUINO_MACHINE (obj) \ + OBJECT_CHECK(ArduinoMachineState, (obj), TYPE_ARDUINO_MACHINE) +#define ARDUINO_MACHINE_CLASS (klass) \ + OBJECT_CLASS_CHECK(ArduinoMachineClass, (klass), TYPE_ARDUINO_MACHINE) +#define ARDUINO_MACHINE_GET_CLASS (obj) \ + OBJECT_GET_CLASS(ArduinoMachineClass, (obj), TYPE_ARDUINO_MACHINE) + +static void load_firmware (const char *firmware,uint64_t flash_size) +{ +const char *filename; +int bytes_loaded; + +/* Load firmware (contents of flash) trying to auto-detect format */ + filename =qemu_find_file (QEMU_FILE_TYPE_BIOS , firmware); +if (filename ==NULL ) { +error_report ("Unable to find %s" , firmware); +exit (1 ); + } + + bytes_loaded =load_elf (filename,NULL ,NULL ,NULL ,NULL ,NULL ,NULL , +0 ,EM_NONE ,0 ,0 ); +if (bytes_loaded <0 ) { + bytes_loaded =load_image_targphys (filename,OFFSET_CODE , flash_size); + } +if (bytes_loaded <0 ) { +error_report ("Unable to load firmware image %s as ELF or raw binary" , + firmware); +exit (1 ); + } +} + +static void arduino_machine_init (MachineState *machine) +{ +ArduinoMachineClass *amc =ARDUINO_MACHINE_GET_CLASS (machine); +ArduinoMachineState *ams =ARDUINO_MACHINE (machine); + +sysbus_init_child_obj (OBJECT (machine),"mcu" , &ams->mcu,sizeof (ams->mcu), + amc->mcu_type); +object_property_set_uint (OBJECT (&ams->mcu), amc->xtal_hz, +"xtal-frequency-hz" , &error_abort); +object_property_set_bool (OBJECT (&ams->mcu), true,"realized" , + &error_abort); + +if (machine->firmware) { +load_firmware (machine->firmware,memory_region_size (&ams->mcu.flash)); + } +} + +static void arduino_machine_class_init (ObjectClass *oc,void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); + + mc->init = arduino_machine_init; + mc->default_cpus =1 ; + mc->min_cpus = mc->default_cpus; + mc->max_cpus = mc->default_cpus; + mc->no_floppy =1 ; + mc->no_cdrom =1 ; + mc->no_parallel =1 ; +} + +static void arduino_duemilanove_class_init (ObjectClass *oc,void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); +ArduinoMachineClass *amc =ARDUINO_MACHINE_CLASS (oc); + +/* https://www.arduino.cc/en/Main/ArduinoBoardDuemilanove */ + mc->desc ="Arduino Duemilanove (ATmega168)" , + mc->alias ="2009" ; + amc->mcu_type =TYPE_ATMEGA168_MCU ; + amc->xtal_hz =16 *1000 *1000 ; +}; + +static void arduino_uno_class_init (ObjectClass *oc,void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); +ArduinoMachineClass *amc =ARDUINO_MACHINE_CLASS (oc); + +/* https://store.arduino.cc/arduino-uno-rev3 */ + mc->desc ="Arduino UNO (ATmega328P)" ; + mc->alias ="uno" ; + amc->mcu_type =TYPE_ATMEGA328_MCU ; + amc->xtal_hz =16 *1000 *1000 ; +}; + +static void arduino_mega_class_init (ObjectClass *oc,void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); +ArduinoMachineClass *amc =ARDUINO_MACHINE_CLASS (oc); + +/* https://www.arduino.cc/en/Main/ArduinoBoardMega */ + mc->desc ="Arduino Mega (ATmega1280)" ; + mc->alias ="mega" ; + amc->mcu_type =TYPE_ATMEGA1280_MCU ; + amc->xtal_hz =16 *1000 *1000 ; +}; + +static void arduino_mega2560_class_init (ObjectClass *oc,void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); +ArduinoMachineClass *amc =ARDUINO_MACHINE_CLASS (oc); + +/* https://store.arduino.cc/arduino-mega-2560-rev3 */ + mc->desc ="Arduino Mega 2560 (ATmega2560)" ; + mc->alias ="mega2560" ; + mc->is_default = true; + amc->mcu_type =TYPE_ATMEGA2560_MCU ; + amc->xtal_hz =16 *1000 *1000 ;/* CSTCE16M0V53-R0 */ +}; + +static const TypeInfo arduino_machine_types[] = { + { + .name =MACHINE_TYPE_NAME ("arduino-duemilanove" ), + .parent =TYPE_ARDUINO_MACHINE , + .class_init = arduino_duemilanove_class_init, + }, { + .name =MACHINE_TYPE_NAME ("arduino-uno" ), + .parent =TYPE_ARDUINO_MACHINE , + .class_init = arduino_uno_class_init, + }, { + .name =MACHINE_TYPE_NAME ("arduino-mega" ), + .parent =TYPE_ARDUINO_MACHINE , + .class_init = arduino_mega_class_init, + }, { + .name =MACHINE_TYPE_NAME ("arduino-mega-2560-v3" ), + .parent =TYPE_ARDUINO_MACHINE , + .class_init = arduino_mega2560_class_init, + }, { + .name =TYPE_ARDUINO_MACHINE , + .parent =TYPE_MACHINE , + .instance_size =sizeof (ArduinoMachineState), + .class_size =sizeof (ArduinoMachineClass), + .class_init = arduino_machine_class_init, + .abstract = true, + } +}; + +DEFINE_TYPES (arduino_machine_types)
hw/avr/Makefile.objs +1 -0
@@ -1,2 +1,3 @@ obj-y += sample.o obj-y += atmega.o +obj-y += arduino.o
Signature · 1 line
2.21.0
PATCH v2 5/8 tests/boot-serial-test: Test some Arduino boards (AVR based)
In reply to Philippe Mathieu-DaudéThe Arduino Duemilanove is based on a AVR5 CPU, while the Arduino MEGA2560 on a AVR6 CPU.
--- tests/boot-serial-test.c | 2 ++ 1 file changed, 2 insertions(+)
tests/boot-serial-test.c +2 -0
@@ -113,6 +113,8 @@ typedef struct testdef {static testdef_t tests[] = { {"alpha" ,"clipper" ,"" ,"PCI:" }, {"avr" ,"sample" ,"" ,"T" ,sizeof (bios_avr),NULL , bios_avr }, + {"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" },
Signature · 1 line
2.21.0
Quoted 11 lines · Philippe Mathieu-Daudé
Hi,
This series add the arduino boards, aiming at removing the 'sample' board that doesn't follow any specification.
Since v1:
- Addressed Igor comments
- Addressed Aleksandar comments
- Fixed UART issue (was due to IRQ shifted by 2 in CPU)
Quoted 8 lines
TODO after merge is:
- Extract Timer8 common parts from Timer16
- Add GPIOs
- Connect LED to GPIO on Arduino
- Plug to Scratch (See http://s4a.cat/).
(I plan to purpose that as a GSoC idea).Thank you for having insisted with this port during so long!
(This comment is for Michael)
I forgot to amend this series (including Michael patches) is available here:
https://gitlab.com/philmd/qemu/commits/arduino-v2
Quoted 6 lines
Regards,
Phil.
Based-on: <[email protected]>
https://www.mail-archive.com/[email protected]/msg666384.html
PATCH v2 6/8 tests/acceptance: Do not set the machine type manually
In reply to Philippe Mathieu-DaudéSince commit ba21bde93 we don't need to set the machine type manually, the one set by the ":avocado: tags=machine" will be used.
--- tests/acceptance/machine_avr6.py | 1 - 1 file changed, 1 deletion(-)
tests/acceptance/machine_avr6.py +0 -1
@@ -45,7 +45,6 @@ class AVR6Machine(Test): rom_hash = '7eb521f511ca8f2622e0a3c5e8dd686efbb911d4' rom_path = self.fetch_asset(rom_url, asset_hash=rom_hash) - self.vm.set_machine('sample') self.vm.add_args('-bios', rom_path) self.vm.add_args('-nographic') self.vm.launch()
Signature · 1 line
2.21.0
PATCH v2 7/8 tests/acceptance: Keep multilines comment consistent with other tests
In reply to Philippe Mathieu-Daudé---
Cc: Cleber Rosa <[email protected]>
tests/acceptance/machine_avr6.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
tests/acceptance/machine_avr6.py +3 -5
@@ -37,11 +37,9 @@ class AVR6Machine(Test): 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' - rom_sha1= '36c3e67b8755dcf37e06af6730ef5d477b8ed16d' - rom_url += '/raw/' - rom_url += rom_sha1 - rom_url += '/free-rtos/Demo/AVR_ATMega2560_GCC/demo.elf' + 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)
Signature · 1 line
2.21.0
PATCH v2 8/8 tests/acceptance: Test the Arduino MEGA2560 board
In reply to Philippe Mathieu-Daudé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')
---
Cc: Cleber Rosa <[email protected]>
Cc: Phillip Stevens <[email protected]> (for FreeRTOS example)
--- tests/acceptance/machine_avr6.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/acceptance/machine_avr6.py +1 -1
@@ -31,7 +31,7 @@ class AVR6Machine(Test): def test_freertos(self): """ :avocado: tags=arch:avr - :avocado: tags=machine:sample + :avocado: tags=machine:arduino-mega-2560-v3 """ """ https://github.com/seharris/qemu-avr-tests/raw/master/free-rtos/Demo/AVR_ATMega2560_GCC/demo.elf
Signature · 1 line
2.21.0