b-l475e-iot01a.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * B-L475E-IOT01A Discovery Kit machine
  3. * (B-L475E-IOT01A IoT Node)
  4. *
  5. * Copyright (c) 2023-2024 Arnaud Minier <arnaud.minier@telecom-paris.fr>
  6. * Copyright (c) 2023-2024 Inès Varhol <ines.varhol@telecom-paris.fr>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0-or-later
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. *
  13. * This work is heavily inspired by the netduinoplus2 by Alistair Francis.
  14. * Original code is licensed under the MIT License:
  15. *
  16. * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
  17. */
  18. /*
  19. * The reference used is the STMicroElectronics UM2153 User manual
  20. * Discovery kit for IoT node, multi-channel communication with STM32L4.
  21. * https://www.st.com/en/evaluation-tools/b-l475e-iot01a.html#documentation
  22. */
  23. #include "qemu/osdep.h"
  24. #include "qapi/error.h"
  25. #include "hw/boards.h"
  26. #include "hw/qdev-properties.h"
  27. #include "qemu/error-report.h"
  28. #include "hw/arm/boot.h"
  29. #include "hw/core/split-irq.h"
  30. #include "hw/arm/stm32l4x5_soc.h"
  31. #include "hw/gpio/stm32l4x5_gpio.h"
  32. #include "hw/display/dm163.h"
  33. /* B-L475E-IOT01A implementation is inspired from netduinoplus2 and arduino */
  34. /*
  35. * There are actually 14 input pins in the DM163 device.
  36. * Here the DM163 input pin EN isn't connected to the STM32L4x5
  37. * GPIOs as the IM120417002 colors shield doesn't actually use
  38. * this pin to drive the RGB matrix.
  39. */
  40. #define NUM_DM163_INPUTS 13
  41. static const unsigned dm163_input[NUM_DM163_INPUTS] = {
  42. 1 * GPIO_NUM_PINS + 2, /* ROW0 PB2 */
  43. 0 * GPIO_NUM_PINS + 15, /* ROW1 PA15 */
  44. 0 * GPIO_NUM_PINS + 2, /* ROW2 PA2 */
  45. 0 * GPIO_NUM_PINS + 7, /* ROW3 PA7 */
  46. 0 * GPIO_NUM_PINS + 6, /* ROW4 PA6 */
  47. 0 * GPIO_NUM_PINS + 5, /* ROW5 PA5 */
  48. 1 * GPIO_NUM_PINS + 0, /* ROW6 PB0 */
  49. 0 * GPIO_NUM_PINS + 3, /* ROW7 PA3 */
  50. 0 * GPIO_NUM_PINS + 4, /* SIN (SDA) PA4 */
  51. 1 * GPIO_NUM_PINS + 1, /* DCK (SCK) PB1 */
  52. 2 * GPIO_NUM_PINS + 3, /* RST_B (RST) PC3 */
  53. 2 * GPIO_NUM_PINS + 4, /* LAT_B (LAT) PC4 */
  54. 2 * GPIO_NUM_PINS + 5, /* SELBK (SB) PC5 */
  55. };
  56. #define TYPE_B_L475E_IOT01A MACHINE_TYPE_NAME("b-l475e-iot01a")
  57. OBJECT_DECLARE_SIMPLE_TYPE(Bl475eMachineState, B_L475E_IOT01A)
  58. typedef struct Bl475eMachineState {
  59. MachineState parent_obj;
  60. Stm32l4x5SocState soc;
  61. SplitIRQ gpio_splitters[NUM_DM163_INPUTS];
  62. DM163State dm163;
  63. } Bl475eMachineState;
  64. static void bl475e_init(MachineState *machine)
  65. {
  66. Bl475eMachineState *s = B_L475E_IOT01A(machine);
  67. const Stm32l4x5SocClass *sc;
  68. DeviceState *dev, *gpio_out_splitter;
  69. unsigned gpio, pin;
  70. object_initialize_child(OBJECT(machine), "soc", &s->soc,
  71. TYPE_STM32L4X5XG_SOC);
  72. sysbus_realize(SYS_BUS_DEVICE(&s->soc), &error_fatal);
  73. sc = STM32L4X5_SOC_GET_CLASS(&s->soc);
  74. armv7m_load_kernel(s->soc.armv7m.cpu, machine->kernel_filename, 0,
  75. sc->flash_size);
  76. if (object_class_by_name(TYPE_DM163)) {
  77. object_initialize_child(OBJECT(machine), "dm163",
  78. &s->dm163, TYPE_DM163);
  79. dev = DEVICE(&s->dm163);
  80. qdev_realize(dev, NULL, &error_abort);
  81. for (unsigned i = 0; i < NUM_DM163_INPUTS; i++) {
  82. object_initialize_child(OBJECT(machine), "gpio-out-splitters[*]",
  83. &s->gpio_splitters[i], TYPE_SPLIT_IRQ);
  84. gpio_out_splitter = DEVICE(&s->gpio_splitters[i]);
  85. qdev_prop_set_uint32(gpio_out_splitter, "num-lines", 2);
  86. qdev_realize(gpio_out_splitter, NULL, &error_fatal);
  87. qdev_connect_gpio_out(gpio_out_splitter, 0,
  88. qdev_get_gpio_in(DEVICE(&s->soc), dm163_input[i]));
  89. qdev_connect_gpio_out(gpio_out_splitter, 1,
  90. qdev_get_gpio_in(dev, i));
  91. gpio = dm163_input[i] / GPIO_NUM_PINS;
  92. pin = dm163_input[i] % GPIO_NUM_PINS;
  93. qdev_connect_gpio_out(DEVICE(&s->soc.gpio[gpio]), pin,
  94. qdev_get_gpio_in(DEVICE(gpio_out_splitter), 0));
  95. }
  96. }
  97. }
  98. static void bl475e_machine_init(ObjectClass *oc, void *data)
  99. {
  100. MachineClass *mc = MACHINE_CLASS(oc);
  101. static const char *machine_valid_cpu_types[] = {
  102. ARM_CPU_TYPE_NAME("cortex-m4"),
  103. NULL
  104. };
  105. mc->desc = "B-L475E-IOT01A Discovery Kit (Cortex-M4)";
  106. mc->init = bl475e_init;
  107. mc->valid_cpu_types = machine_valid_cpu_types;
  108. /* SRAM pre-allocated as part of the SoC instantiation */
  109. mc->default_ram_size = 0;
  110. }
  111. static const TypeInfo bl475e_machine_type[] = {
  112. {
  113. .name = TYPE_B_L475E_IOT01A,
  114. .parent = TYPE_MACHINE,
  115. .instance_size = sizeof(Bl475eMachineState),
  116. .class_init = bl475e_machine_init,
  117. }
  118. };
  119. DEFINE_TYPES(bl475e_machine_type)