|
| 1 | +#include "odroid_system.h" |
| 2 | +#include "freertos/FreeRTOS.h" |
| 3 | +#include "esp_heap_caps.h" |
| 4 | +#include "esp_partition.h" |
| 5 | +#include "esp_ota_ops.h" |
| 6 | +#include "esp_system.h" |
| 7 | +#include "esp_event.h" |
| 8 | +#include "driver/rtc_io.h" |
| 9 | +#include "string.h" |
| 10 | +#include "stdio.h" |
| 11 | + |
| 12 | +static bool system_initialized = false; |
| 13 | + |
| 14 | +ODROID_START_ACTION startAction = 0; |
| 15 | +ODROID_SCALING scalingMode = ODROID_SCALING_FILL; |
| 16 | +int8_t speedupEnabled = 0; |
| 17 | +int8_t forceRedraw = 0; |
| 18 | + |
| 19 | +void odroid_system_init(int app_id, int sampleRate, char **romPath) |
| 20 | +{ |
| 21 | + if (system_initialized) abort(); |
| 22 | + |
| 23 | + printf("odroid_system_init: %d KB free\n", esp_get_free_heap_size() / 1024); |
| 24 | + |
| 25 | + odroid_settings_init(app_id); |
| 26 | + odroid_overlay_init(); |
| 27 | + odroid_system_gpio_init(); |
| 28 | + odroid_input_gamepad_init(); |
| 29 | + odroid_input_battery_level_init(); |
| 30 | + |
| 31 | + odroid_gamepad_state bootState = odroid_input_read_raw(); |
| 32 | + if (bootState.values[ODROID_INPUT_MENU]) |
| 33 | + { |
| 34 | + // Force return to menu to recover from ROM loading crashes |
| 35 | + odroid_system_application_set(0); |
| 36 | + esp_restart(); |
| 37 | + } |
| 38 | + |
| 39 | + startAction = odroid_settings_StartAction_get(); |
| 40 | + if (startAction == ODROID_START_ACTION_RESTART) |
| 41 | + { |
| 42 | + odroid_settings_StartAction_set(ODROID_START_ACTION_RESUME); |
| 43 | + } |
| 44 | + |
| 45 | + //sdcard init must be before LCD init |
| 46 | + esp_err_t sd_init = odroid_sdcard_open(); |
| 47 | + |
| 48 | + ili9341_init(); |
| 49 | + |
| 50 | + if (esp_reset_reason() == ESP_RST_PANIC) |
| 51 | + { |
| 52 | + odroid_overlay_alert("The emulator crashed"); |
| 53 | + odroid_system_application_set(0); |
| 54 | + esp_restart(); |
| 55 | + } |
| 56 | + |
| 57 | + if (esp_reset_reason() != ESP_RST_SW) |
| 58 | + { |
| 59 | + ili9341_blank_screen(); |
| 60 | + //odroid_display_show_hourglass(); |
| 61 | + } |
| 62 | + |
| 63 | + if (sd_init != ESP_OK) |
| 64 | + { |
| 65 | + odroid_display_show_error(ODROID_SD_ERR_NOCARD); |
| 66 | + odroid_system_halt(); |
| 67 | + } |
| 68 | + |
| 69 | + *romPath = odroid_settings_RomFilePath_get(); |
| 70 | + if (!*romPath || strlen(*romPath) < 4) |
| 71 | + { |
| 72 | + odroid_overlay_alert("ROM File not found!"); |
| 73 | + odroid_system_halt(); |
| 74 | + } |
| 75 | + |
| 76 | + odroid_audio_init(odroid_settings_AudioSink_get(), sampleRate); |
| 77 | + |
| 78 | + scalingMode = odroid_settings_Scaling_get(); |
| 79 | + |
| 80 | + if (startAction == ODROID_START_ACTION_NETPLAY) |
| 81 | + { |
| 82 | + // odroid_netplay_init(); |
| 83 | + } |
| 84 | + |
| 85 | + system_initialized = true; |
| 86 | + |
| 87 | + printf("odroid_system_init: System ready!\n"); |
| 88 | +} |
| 89 | + |
| 90 | +void odroid_system_gpio_init() |
| 91 | +{ |
| 92 | + rtc_gpio_deinit(ODROID_GAMEPAD_IO_MENU); |
| 93 | + //rtc_gpio_deinit(GPIO_NUM_14); |
| 94 | + |
| 95 | + // blue led |
| 96 | + gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT); |
| 97 | + gpio_set_level(GPIO_NUM_2, 0); |
| 98 | + |
| 99 | + // Disable LCD CD to prevent garbage |
| 100 | + gpio_set_direction(GPIO_NUM_5, GPIO_MODE_OUTPUT); |
| 101 | + gpio_set_level(GPIO_NUM_5, 1); |
| 102 | +} |
| 103 | + |
| 104 | +void odroid_system_application_set(int slot) |
| 105 | +{ |
| 106 | + const esp_partition_t* partition = esp_partition_find_first( |
| 107 | + ESP_PARTITION_TYPE_APP, |
| 108 | + ESP_PARTITION_SUBTYPE_APP_OTA_MIN + slot, |
| 109 | + NULL); |
| 110 | + if (partition != NULL) |
| 111 | + { |
| 112 | + esp_err_t err = esp_ota_set_boot_partition(partition); |
| 113 | + if (err != ESP_OK) |
| 114 | + { |
| 115 | + printf("odroid_system_application_set: esp_ota_set_boot_partition failed.\n"); |
| 116 | + abort(); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +void odroid_system_sleep() |
| 122 | +{ |
| 123 | + printf("odroid_system_sleep: Entered.\n"); |
| 124 | + |
| 125 | + // Wait for button release |
| 126 | + odroid_gamepad_state joystick; |
| 127 | + odroid_input_gamepad_read(&joystick); |
| 128 | + |
| 129 | + while (joystick.values[ODROID_INPUT_MENU]) |
| 130 | + { |
| 131 | + vTaskDelay(1); |
| 132 | + odroid_input_gamepad_read(&joystick); |
| 133 | + } |
| 134 | + |
| 135 | + vTaskDelay(100); |
| 136 | + esp_deep_sleep_start(); |
| 137 | +} |
| 138 | + |
| 139 | +void odroid_system_halt() |
| 140 | +{ |
| 141 | + vTaskSuspendAll(); |
| 142 | + while (1); |
| 143 | +} |
| 144 | + |
| 145 | +void odroid_system_led_set(int value) |
| 146 | +{ |
| 147 | + if (!system_initialized) |
| 148 | + { |
| 149 | + printf("odroid_system_init not called before use.\n"); |
| 150 | + abort(); |
| 151 | + } |
| 152 | + |
| 153 | + gpio_set_level(GPIO_NUM_2, value); |
| 154 | +} |
0 commit comments