1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qapi/error.h"
15#include "atmega.h"
16#include "boot.h"
17#include "qom/object.h"
18
19struct ArduinoMachineState {
20
21 MachineState parent_obj;
22
23 AtmegaMcuState mcu;
24};
25typedef struct ArduinoMachineState ArduinoMachineState;
26
27struct ArduinoMachineClass {
28
29 MachineClass parent_class;
30
31 const char *mcu_type;
32 uint64_t xtal_hz;
33};
34typedef struct ArduinoMachineClass ArduinoMachineClass;
35
36#define TYPE_ARDUINO_MACHINE \
37 MACHINE_TYPE_NAME("arduino")
38DECLARE_OBJ_CHECKERS(ArduinoMachineState, ArduinoMachineClass,
39 ARDUINO_MACHINE, TYPE_ARDUINO_MACHINE)
40
41static void arduino_machine_init(MachineState *machine)
42{
43 ArduinoMachineClass *amc = ARDUINO_MACHINE_GET_CLASS(machine);
44 ArduinoMachineState *ams = ARDUINO_MACHINE(machine);
45
46 object_initialize_child(OBJECT(machine), "mcu", &ams->mcu, amc->mcu_type);
47 object_property_set_uint(OBJECT(&ams->mcu), "xtal-frequency-hz",
48 amc->xtal_hz, &error_abort);
49 sysbus_realize(SYS_BUS_DEVICE(&ams->mcu), &error_abort);
50
51 if (machine->firmware) {
52 if (!avr_load_firmware(&ams->mcu.cpu, machine,
53 &ams->mcu.flash, machine->firmware)) {
54 exit(1);
55 }
56 }
57}
58
59static void arduino_machine_class_init(ObjectClass *oc, void *data)
60{
61 MachineClass *mc = MACHINE_CLASS(oc);
62
63 mc->init = arduino_machine_init;
64 mc->default_cpus = 1;
65 mc->min_cpus = mc->default_cpus;
66 mc->max_cpus = mc->default_cpus;
67 mc->no_floppy = 1;
68 mc->no_cdrom = 1;
69 mc->no_parallel = 1;
70}
71
72static void arduino_duemilanove_class_init(ObjectClass *oc, void *data)
73{
74 MachineClass *mc = MACHINE_CLASS(oc);
75 ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
76
77
78
79
80
81 mc->desc = "Arduino Duemilanove (ATmega168)",
82 mc->alias = "2009";
83 amc->mcu_type = TYPE_ATMEGA168_MCU;
84 amc->xtal_hz = 16 * 1000 * 1000;
85};
86
87static void arduino_uno_class_init(ObjectClass *oc, void *data)
88{
89 MachineClass *mc = MACHINE_CLASS(oc);
90 ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
91
92
93
94
95
96 mc->desc = "Arduino UNO (ATmega328P)";
97 mc->alias = "uno";
98 amc->mcu_type = TYPE_ATMEGA328_MCU;
99 amc->xtal_hz = 16 * 1000 * 1000;
100};
101
102static void arduino_mega_class_init(ObjectClass *oc, void *data)
103{
104 MachineClass *mc = MACHINE_CLASS(oc);
105 ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
106
107
108
109
110
111 mc->desc = "Arduino Mega (ATmega1280)";
112 mc->alias = "mega";
113 amc->mcu_type = TYPE_ATMEGA1280_MCU;
114 amc->xtal_hz = 16 * 1000 * 1000;
115};
116
117static void arduino_mega2560_class_init(ObjectClass *oc, void *data)
118{
119 MachineClass *mc = MACHINE_CLASS(oc);
120 ArduinoMachineClass *amc = ARDUINO_MACHINE_CLASS(oc);
121
122
123
124
125
126 mc->desc = "Arduino Mega 2560 (ATmega2560)";
127 mc->alias = "mega2560";
128 amc->mcu_type = TYPE_ATMEGA2560_MCU;
129 amc->xtal_hz = 16 * 1000 * 1000;
130};
131
132static const TypeInfo arduino_machine_types[] = {
133 {
134 .name = MACHINE_TYPE_NAME("arduino-duemilanove"),
135 .parent = TYPE_ARDUINO_MACHINE,
136 .class_init = arduino_duemilanove_class_init,
137 }, {
138 .name = MACHINE_TYPE_NAME("arduino-uno"),
139 .parent = TYPE_ARDUINO_MACHINE,
140 .class_init = arduino_uno_class_init,
141 }, {
142 .name = MACHINE_TYPE_NAME("arduino-mega"),
143 .parent = TYPE_ARDUINO_MACHINE,
144 .class_init = arduino_mega_class_init,
145 }, {
146 .name = MACHINE_TYPE_NAME("arduino-mega-2560-v3"),
147 .parent = TYPE_ARDUINO_MACHINE,
148 .class_init = arduino_mega2560_class_init,
149 }, {
150 .name = TYPE_ARDUINO_MACHINE,
151 .parent = TYPE_MACHINE,
152 .instance_size = sizeof(ArduinoMachineState),
153 .class_size = sizeof(ArduinoMachineClass),
154 .class_init = arduino_machine_class_init,
155 .abstract = true,
156 }
157};
158
159DEFINE_TYPES(arduino_machine_types)
160