forked from qemu/qemu
/
stm32_p103.c
144 lines (118 loc) · 3.72 KB
/
stm32_p103.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Olimex STM32 P103 Development Board
*
* Copyright (C) 2010 Andre Beckus
*
* Implementation based on
* Olimex "STM-P103 Development Board Users Manual Rev. A, April 2008"
*
* 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/>.
*/
#include "hw/arm/stm32.h"
#include "hw/sysbus.h"
#include "hw/arm/arm.h"
#include "hw/devices.h"
#include "ui/console.h"
#include "sysemu/sysemu.h"
#include "hw/boards.h"
typedef struct {
Stm32 *stm32;
bool last_button_pressed;
qemu_irq button_irq;
} Stm32P103;
static void led_irq_handler(void *opaque, int n, int level)
{
/* There should only be one IRQ for the LED */
assert(n == 0);
/* Assume that the IRQ is only triggered if the LED has changed state.
* If this is not correct, we may get multiple LED Offs or Ons in a row.
*/
switch (level) {
case 0:
printf("LED Off\n");
break;
case 1:
printf("LED On\n");
break;
}
}
static void stm32_p103_key_event(void *opaque, int keycode)
{
Stm32P103 *s = (Stm32P103 *)opaque;
bool make;
int core_keycode;
if((keycode & 0x80) == 0) {
make = true;
core_keycode = keycode;
} else {
make = false;
core_keycode = keycode & 0x7f;
}
/* Responds when a "B" key press is received.
* Inside the monitor, you can type "sendkey b"
*/
if(core_keycode == 0x30) {
if(make) {
if(!s->last_button_pressed) {
qemu_irq_raise(s->button_irq);
s->last_button_pressed = true;
}
} else {
if(s->last_button_pressed) {
qemu_irq_lower(s->button_irq);
s->last_button_pressed = false;
}
}
}
return;
}
static void stm32_p103_init(MachineState *machine)
{
const char* kernel_filename = machine->kernel_filename;
qemu_irq *led_irq;
Stm32P103 *s;
s = (Stm32P103 *)g_malloc0(sizeof(Stm32P103));
stm32_init(/*flash_size*/0x0001ffff,
/*ram_size*/0x00004fff,
kernel_filename,
8000000,
32768);
DeviceState *gpio_a = DEVICE(object_resolve_path("/machine/stm32/gpio[a]", NULL));
DeviceState *gpio_c = DEVICE(object_resolve_path("/machine/stm32/gpio[c]", NULL));
DeviceState *uart2 = DEVICE(object_resolve_path("/machine/stm32/uart[2]", NULL));
assert(gpio_a);
assert(gpio_c);
assert(uart2);
/* Connect LED to GPIO C pin 12 */
led_irq = qemu_allocate_irqs(led_irq_handler, NULL, 1);
qdev_connect_gpio_out(gpio_c, 12, led_irq[0]);
/* Connect button to GPIO A pin 0 */
s->button_irq = qdev_get_gpio_in(gpio_a, 0);
qemu_add_kbd_event_handler(stm32_p103_key_event, s);
/* Connect RS232 to UART */
stm32_uart_connect(
(Stm32Uart *)uart2,
serial_hds[0],
STM32_USART2_NO_REMAP);
}
static QEMUMachine stm32_p103_machine = {
.name = "stm32-p103",
.desc = "Olimex STM32 p103 Dev Board",
.init = stm32_p103_init,
};
static void stm32_p103_machine_init(void)
{
qemu_register_machine(&stm32_p103_machine);
}
machine_init(stm32_p103_machine_init);