This repository has been archived on 2022-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Luma3DS-3GX/source/utils.c

129 lines
3.2 KiB
C
Raw Normal View History

/*
2016-07-05 16:05:53 +02:00
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* 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/>.
*
* Additional Terms 7.b of GPLv3 applies 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.
*/
#include "utils.h"
#include "i2c.h"
#include "buttons.h"
#include "screen.h"
#include "draw.h"
#include "cache.h"
2016-04-05 05:27:28 +02:00
u32 waitInput(void)
{
u32 pressedKey = 0,
key;
//Wait for no keys to be pressed
while(HID_PAD);
2016-04-14 17:10:55 +02:00
do
{
//Wait for a key to be pressed
while(!HID_PAD);
2016-04-04 18:19:00 +02:00
key = HID_PAD;
//Make sure it's pressed
for(u32 i = 0x13000; i; i--)
{
if(key != HID_PAD) break;
if(i == 1) pressedKey = 1;
}
2016-04-02 18:48:31 +02:00
}
while(!pressedKey);
return key;
}
2016-04-15 16:10:32 +02:00
void mcuReboot(void)
{
flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
2016-07-01 20:27:28 +02:00
2016-04-15 16:10:32 +02:00
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2);
while(true);
}
void mcuPowerOff(void)
{
flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
while(true);
}
void mcuPowerOff(void)
{
flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
while(1);
}
//TODO: add support for TIMER IRQ
2016-05-13 05:01:32 +02:00
static inline void startChrono(u64 initialTicks)
{
//Based on a NATIVE_FIRM disassembly
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) = (u16)(initialTicks >> (16 * i));
REG_TIMER_CNT(0) = 0x80; //67MHz; enabled
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled
}
2016-05-13 05:01:32 +02:00
void chrono(u32 seconds)
{
2016-05-13 05:01:32 +02:00
static u64 startingTicks = 0;
2016-05-12 02:59:21 +02:00
2016-05-13 05:01:32 +02:00
if(!startingTicks) startChrono(0);
2016-05-13 05:01:32 +02:00
u64 res;
2016-05-13 05:01:32 +02:00
do
{
res = 0;
for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i);
2016-05-13 05:01:32 +02:00
}
while(res - startingTicks < seconds * TICKS_PER_SEC);
2016-05-13 05:01:32 +02:00
if(!seconds) startingTicks = res;
}
void stopChrono(void)
{
for(u32 i = 0; i < 4; i++) REG_TIMER_CNT(i) &= ~0x80;
}
void error(const char *message)
{
initScreens();
clearScreens();
drawString("An error has occurred:", 10, 10, COLOR_RED);
int posY = drawString(message, 10, 30, COLOR_WHITE);
drawString("Press any button to shutdown", 10, posY + 2 * SPACING_Y, COLOR_WHITE);
waitInput();
mcuPowerOff();
}