2016-03-23 02:27:53 +01:00
|
|
|
/*
|
2016-07-05 16:05:53 +02:00
|
|
|
* This file is part of Luma3DS
|
2020-04-25 14:26:21 +02:00
|
|
|
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
|
2016-07-05 16:05:53 +02:00
|
|
|
*
|
|
|
|
* 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 3 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/>.
|
|
|
|
*
|
2017-06-05 02:02:04 +02:00
|
|
|
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
|
|
|
|
* * Requiring preservation of specified reasonable legal notices or
|
|
|
|
* author attributions in that material or in the Appropriate Legal
|
|
|
|
* Notices displayed by works containing it.
|
|
|
|
* * Prohibiting misrepresentation of the origin of that material,
|
|
|
|
* or requiring that modified versions of such material be marked in
|
|
|
|
* reasonable ways as different from the original version.
|
2016-03-23 02:27:53 +01:00
|
|
|
*/
|
|
|
|
|
2016-11-13 22:13:24 +01:00
|
|
|
/*
|
|
|
|
* waitInput function based on code by d0k3 https://github.com/d0k3/Decrypt9WIP/blob/master/source/hid.c
|
|
|
|
*/
|
|
|
|
|
2016-03-23 03:45:32 +01:00
|
|
|
#include "utils.h"
|
2016-03-23 02:27:53 +01:00
|
|
|
#include "i2c.h"
|
|
|
|
#include "buttons.h"
|
2016-07-18 22:42:55 +02:00
|
|
|
#include "screen.h"
|
|
|
|
#include "draw.h"
|
2016-06-13 21:14:53 +02:00
|
|
|
#include "cache.h"
|
2017-05-23 17:13:43 +02:00
|
|
|
#include "fmt.h"
|
2018-05-24 00:55:38 +02:00
|
|
|
#include "memory.h"
|
2017-06-19 18:13:59 +02:00
|
|
|
#include "fs.h"
|
2016-03-23 02:27:53 +01:00
|
|
|
|
2016-11-14 15:39:26 +01:00
|
|
|
static void startChrono(void)
|
|
|
|
{
|
2017-06-05 02:02:04 +02:00
|
|
|
static bool isChronoStarted = false;
|
2017-06-04 18:52:52 +02:00
|
|
|
|
|
|
|
if(isChronoStarted) return;
|
|
|
|
|
2016-11-14 15:39:26 +01:00
|
|
|
REG_TIMER_CNT(0) = 0; //67MHz
|
|
|
|
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up
|
|
|
|
|
|
|
|
for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = 0;
|
|
|
|
|
|
|
|
REG_TIMER_CNT(0) = 0x80; //67MHz; enabled
|
|
|
|
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled
|
2017-06-04 18:52:52 +02:00
|
|
|
|
|
|
|
isChronoStarted = true;
|
2016-11-14 15:39:26 +01:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:11:30 +01:00
|
|
|
static u64 chrono(void)
|
2016-11-14 15:39:26 +01:00
|
|
|
{
|
2016-11-29 20:11:30 +01:00
|
|
|
u64 res = 0;
|
2016-11-14 15:39:26 +01:00
|
|
|
for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i);
|
|
|
|
|
2016-11-29 20:11:30 +01:00
|
|
|
res /= (TICKS_PER_SEC / 1000);
|
2016-11-14 15:39:26 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-11-13 22:13:24 +01:00
|
|
|
u32 waitInput(bool isMenu)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-11-14 15:39:26 +01:00
|
|
|
static u64 dPadDelay = 0ULL;
|
2017-06-04 18:52:52 +02:00
|
|
|
u64 initialValue = 0ULL;
|
2016-11-13 22:13:24 +01:00
|
|
|
u32 key,
|
|
|
|
oldKey = HID_PAD;
|
2017-12-18 21:02:41 +01:00
|
|
|
bool shouldShellShutdown = bootType != B9SNTR && bootType != NTR;
|
2016-03-23 02:27:53 +01:00
|
|
|
|
2016-11-13 22:13:24 +01:00
|
|
|
if(isMenu)
|
2016-04-14 17:10:55 +02:00
|
|
|
{
|
2016-11-14 15:39:26 +01:00
|
|
|
dPadDelay = dPadDelay > 0ULL ? 87ULL : 143ULL;
|
2016-11-13 22:13:24 +01:00
|
|
|
startChrono();
|
2017-06-04 18:52:52 +02:00
|
|
|
initialValue = chrono();
|
2016-11-13 22:13:24 +01:00
|
|
|
}
|
2016-04-04 18:19:00 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
while(true)
|
2016-11-13 22:13:24 +01:00
|
|
|
{
|
2016-03-23 02:27:53 +01:00
|
|
|
key = HID_PAD;
|
|
|
|
|
2016-11-13 22:13:24 +01:00
|
|
|
if(!key)
|
|
|
|
{
|
2019-04-06 16:27:17 +02:00
|
|
|
if(shouldShellShutdown)
|
|
|
|
{
|
2019-04-05 00:55:13 +02:00
|
|
|
u8 shellState = I2C_readReg(I2C_DEV_MCU, 0xF);
|
2019-04-07 16:13:14 +02:00
|
|
|
wait(5);
|
2019-04-06 16:27:17 +02:00
|
|
|
if(!(shellState & 2)) mcuPowerOff();
|
2019-04-05 00:55:13 +02:00
|
|
|
}
|
|
|
|
|
2019-04-06 16:27:17 +02:00
|
|
|
u8 intStatus = I2C_readReg(I2C_DEV_MCU, 0x10);
|
2019-04-07 16:13:14 +02:00
|
|
|
wait(5);
|
2019-04-06 16:27:17 +02:00
|
|
|
if(intStatus & 1) mcuPowerOff(); //Power button pressed
|
2019-04-05 00:55:13 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
oldKey = 0;
|
2016-11-13 22:13:24 +01:00
|
|
|
dPadDelay = 0;
|
2016-11-15 19:29:48 +01:00
|
|
|
continue;
|
2016-11-13 22:13:24 +01:00
|
|
|
}
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2017-06-04 18:52:52 +02:00
|
|
|
if(key == oldKey && (!isMenu || (!(key & DPAD_BUTTONS) || chrono() - initialValue < dPadDelay))) continue;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
//Make sure the key is pressed
|
|
|
|
u32 i;
|
|
|
|
for(i = 0; i < 0x13000 && key == HID_PAD; i++);
|
|
|
|
if(i == 0x13000) break;
|
2016-04-02 18:48:31 +02:00
|
|
|
}
|
2016-03-23 02:27:53 +01:00
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2016-08-03 14:13:26 +02:00
|
|
|
void mcuPowerOff(void)
|
|
|
|
{
|
2019-02-26 20:19:52 +01:00
|
|
|
if(!needToSetupScreens) clearScreens(false);
|
2016-08-15 14:46:33 +02:00
|
|
|
|
2017-08-28 02:40:05 +02:00
|
|
|
//Shutdown LCD
|
2019-03-13 16:34:11 +01:00
|
|
|
if(ARESCREENSINITIALIZED) I2C_writeReg(I2C_DEV_MCU, 0x22, 1 << 0);
|
2017-08-28 02:40:05 +02:00
|
|
|
|
2016-09-15 19:53:51 +02:00
|
|
|
//Ensure that all memory transfers have completed and that the data cache has been flushed
|
|
|
|
flushEntireDCache();
|
2016-10-11 16:52:51 +02:00
|
|
|
|
2019-03-13 16:34:11 +01:00
|
|
|
I2C_writeReg(I2C_DEV_MCU, 0x20, 1 << 0);
|
2016-08-03 14:13:26 +02:00
|
|
|
while(true);
|
2016-05-10 01:27:58 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:11:30 +01:00
|
|
|
void wait(u64 amount)
|
2016-08-15 03:51:48 +02:00
|
|
|
{
|
2016-11-14 15:39:26 +01:00
|
|
|
startChrono();
|
2017-06-04 18:52:52 +02:00
|
|
|
|
|
|
|
u64 initialValue = chrono();
|
|
|
|
|
|
|
|
while(chrono() - initialValue < amount);
|
2016-07-18 22:42:55 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 17:13:43 +02:00
|
|
|
void error(const char *fmt, ...)
|
2016-07-18 22:42:55 +02:00
|
|
|
{
|
2017-06-19 18:13:59 +02:00
|
|
|
char buf[DRAW_MAX_FORMATTED_STRING_SIZE + 1];
|
2017-05-23 17:13:43 +02:00
|
|
|
|
2017-06-19 18:13:59 +02:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsprintf(buf, fmt, args);
|
|
|
|
va_end(args);
|
2017-05-23 17:13:43 +02:00
|
|
|
|
2019-02-26 20:19:52 +01:00
|
|
|
initScreens();
|
|
|
|
drawString(true, 10, 10, COLOR_RED, "An error has occurred:");
|
|
|
|
u32 posY = drawString(true, 10, 30, COLOR_WHITE, buf);
|
|
|
|
drawString(true, 10, posY + 2 * SPACING_Y, COLOR_WHITE, "Press any button to shutdown");
|
2016-07-18 22:42:55 +02:00
|
|
|
|
2019-02-26 20:19:52 +01:00
|
|
|
waitInput(false);
|
2016-07-18 22:42:55 +02:00
|
|
|
|
2016-08-03 14:13:26 +02:00
|
|
|
mcuPowerOff();
|
2017-01-24 21:59:02 +01:00
|
|
|
}
|