2016-03-23 02:27:53 +01:00
|
|
|
/*
|
2016-03-23 03:45:32 +01:00
|
|
|
* utils.c
|
2016-03-23 02:27:53 +01:00
|
|
|
*/
|
|
|
|
|
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-06-10 21:48:22 +02:00
|
|
|
#include "memory.h"
|
2016-06-13 21:14:53 +02:00
|
|
|
#include "cache.h"
|
2016-03-23 02:27:53 +01:00
|
|
|
|
2016-04-05 05:27:28 +02:00
|
|
|
u32 waitInput(void)
|
2016-04-02 17:58:06 +02:00
|
|
|
{
|
2016-04-13 01:08:13 +02:00
|
|
|
u32 pressedKey = 0,
|
|
|
|
key;
|
2016-03-23 02:27:53 +01:00
|
|
|
|
|
|
|
//Wait for no keys to be pressed
|
|
|
|
while(HID_PAD);
|
|
|
|
|
2016-04-14 17:10:55 +02:00
|
|
|
do
|
|
|
|
{
|
2016-03-23 02:27:53 +01:00
|
|
|
//Wait for a key to be pressed
|
|
|
|
while(!HID_PAD);
|
2016-04-04 18:19:00 +02:00
|
|
|
|
2016-03-23 02:27:53 +01:00
|
|
|
key = HID_PAD;
|
|
|
|
|
|
|
|
//Make sure it's pressed
|
2016-04-02 17:58:06 +02:00
|
|
|
for(u32 i = 0x13000; i; i--)
|
|
|
|
{
|
2016-03-27 16:26:09 +02:00
|
|
|
if(key != HID_PAD) break;
|
|
|
|
if(i == 1) pressedKey = 1;
|
2016-03-23 02:27:53 +01:00
|
|
|
}
|
2016-04-02 18:48:31 +02:00
|
|
|
}
|
|
|
|
while(!pressedKey);
|
2016-03-23 02:27:53 +01:00
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2016-04-15 16:10:32 +02:00
|
|
|
void mcuReboot(void)
|
|
|
|
{
|
2016-06-13 21:14:53 +02:00
|
|
|
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(1);
|
2016-05-10 01:27:58 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 00:00:53 +02:00
|
|
|
void mcuPowerOff(void)
|
|
|
|
{
|
2016-06-13 23:16:33 +02:00
|
|
|
flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
|
2016-06-11 00:00:53 +02:00
|
|
|
|
|
|
|
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:27:58 +02:00
|
|
|
//TODO: add support for TIMER IRQ
|
2016-05-13 05:01:32 +02:00
|
|
|
static inline void startChrono(u64 initialTicks)
|
2016-05-10 01:27:58 +02:00
|
|
|
{
|
|
|
|
//Based on a NATIVE_FIRM disassembly
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-06-05 20:43:49 +02:00
|
|
|
REG_TIMER_CNT(0) = 0; //67MHz
|
|
|
|
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-06-05 20:43:49 +02:00
|
|
|
for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = (u16)(initialTicks >> (16 * i));
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-06-05 20:43:49 +02:00
|
|
|
REG_TIMER_CNT(0) = 0x80; //67MHz; enabled
|
|
|
|
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled
|
2016-05-10 01:27:58 +02:00
|
|
|
}
|
|
|
|
|
2016-05-13 05:01:32 +02:00
|
|
|
void chrono(u32 seconds)
|
2016-05-10 01:27:58 +02:00
|
|
|
{
|
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-11 19:28:28 +02:00
|
|
|
|
2016-05-13 05:01:32 +02:00
|
|
|
u64 res;
|
2016-05-11 19:28:28 +02:00
|
|
|
|
2016-05-13 05:01:32 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
res = 0;
|
2016-06-05 20:43:49 +02:00
|
|
|
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-11 19:28:28 +02:00
|
|
|
|
2016-05-13 05:01:32 +02:00
|
|
|
if(!seconds) startingTicks = res;
|
2016-05-10 01:27:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void stopChrono(void)
|
|
|
|
{
|
2016-06-05 20:43:49 +02:00
|
|
|
for(u32 i = 0; i < 4; i++) REG_TIMER_CNT(i) &= ~0x80;
|
2016-03-23 02:27:53 +01:00
|
|
|
}
|