mirror of
https://github.com/d0k3/GodMode9.git
synced 2025-06-26 13:42:47 +00:00
- completely moved MCU interrupt handling outside of the critical section - refactored a bit of the PXI code and command names - merge the I2C read and write cmds to be one - remove SET_VMODE cmd, now it's always initialized to BGR565 on boot and to RGB565 on firmlaunch - atomic-ize more stuff
234 lines
5.5 KiB
C
234 lines
5.5 KiB
C
/*
|
|
* This file is part of GodMode9
|
|
* Copyright (C) 2019 Wolfvak
|
|
*
|
|
* 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 2 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/>.
|
|
*/
|
|
|
|
#include <common.h>
|
|
#include <types.h>
|
|
#include <shmem.h>
|
|
#include <arm.h>
|
|
#include <pxi.h>
|
|
|
|
#include <stdatomic.h>
|
|
|
|
#include "arm/gic.h"
|
|
|
|
#include "hw/hid.h"
|
|
#include "hw/gpulcd.h"
|
|
#include "hw/i2c.h"
|
|
#include "hw/mcu.h"
|
|
#include "hw/nvram.h"
|
|
|
|
#include "system/sys.h"
|
|
|
|
static const u8 brightness_lvls[] = {
|
|
0x10, 0x17, 0x1E, 0x25,
|
|
0x2C, 0x34, 0x3C, 0x44,
|
|
0x4D, 0x56, 0x60, 0x6B,
|
|
0x79, 0x8C, 0xA7, 0xD2
|
|
};
|
|
|
|
#ifndef FIXED_BRIGHTNESS
|
|
static int prev_bright_lvl;
|
|
static bool auto_brightness;
|
|
#endif
|
|
|
|
static SystemSHMEM __attribute__((section(".shared"))) sharedMem;
|
|
|
|
static _Atomic(u32) pendingVblank, pendingPxiRx;
|
|
|
|
static void vblankHandler(u32 __attribute__((unused)) irqn)
|
|
{
|
|
atomic_store(&pendingVblank, 1);
|
|
}
|
|
|
|
static void vblankUpdate(void)
|
|
{
|
|
if (!atomic_exchange(&pendingVblank, 0))
|
|
return;
|
|
|
|
#ifndef FIXED_BRIGHTNESS
|
|
int cur_bright_lvl = (mcuGetVolumeSlider() >> 2) % countof(brightness_lvls);
|
|
if ((cur_bright_lvl != prev_bright_lvl) && auto_brightness) {
|
|
prev_bright_lvl = cur_bright_lvl;
|
|
u8 br = brightness_lvls[cur_bright_lvl];
|
|
GFX_setBrightness(br, br);
|
|
}
|
|
#endif
|
|
|
|
// handle shell events
|
|
u32 shell = mcuEventClear(MCUEV_HID_SHELL_OPEN | MCUEV_HID_SHELL_CLOSE);
|
|
if (shell & MCUEV_HID_SHELL_CLOSE) {
|
|
GFX_powerOffBacklights(GFX_BLIGHT_BOTH);
|
|
} else if (shell & MCUEV_HID_SHELL_OPEN) {
|
|
GFX_powerOnBacklights(GFX_BLIGHT_BOTH);
|
|
}
|
|
|
|
sharedMem.hidState.full = HID_GetState();
|
|
}
|
|
|
|
static void pxiRxHandler(u32 __attribute__((unused)) irqn)
|
|
{
|
|
atomic_store(&pendingPxiRx, 1);
|
|
}
|
|
|
|
static void pxiRxUpdate(u32 *cmd, u32 *args)
|
|
{
|
|
u32 msg, lo, hi;
|
|
|
|
*cmd = PXICMD_NONE;
|
|
|
|
if (!atomic_exchange(&pendingPxiRx, 0))
|
|
return;
|
|
|
|
msg = PXI_Recv();
|
|
lo = msg & 0xFFFF;
|
|
hi = msg >> 16;
|
|
|
|
*cmd = lo;
|
|
PXI_RecvArray(args, hi);
|
|
}
|
|
|
|
void __attribute__((noreturn)) MainLoop(void)
|
|
{
|
|
bool runCmdProcessor = true;
|
|
|
|
#ifdef FIXED_BRIGHTNESS
|
|
u8 fixed_bright_lvl = brightness_lvls[clamp(FIXED_BRIGHTNESS, 0, countof(brightness_lvls)-1)];
|
|
GFX_setBrightness(fixed_bright_lvl, fixed_bright_lvl);
|
|
#else
|
|
prev_bright_lvl = -1;
|
|
auto_brightness = true;
|
|
#endif
|
|
|
|
// initialize state stuff
|
|
atomic_init(&pendingVblank, 0);
|
|
atomic_init(&pendingPxiRx, 0);
|
|
memset(&sharedMem, 0, sizeof(sharedMem));
|
|
|
|
// configure interrupts
|
|
gicSetInterruptConfig(PXI_RX_INTERRUPT, BIT(0), GIC_PRIO0, pxiRxHandler);
|
|
gicSetInterruptConfig(MCU_INTERRUPT, BIT(0), GIC_PRIO0, mcuInterruptHandler);
|
|
gicSetInterruptConfig(VBLANK_INTERRUPT, BIT(0), GIC_PRIO0, vblankHandler);
|
|
|
|
// enable interrupts
|
|
gicEnableInterrupt(MCU_INTERRUPT);
|
|
|
|
// perform gpu init after initializing mcu but before
|
|
// enabling the pxi system and the vblank handler
|
|
GFX_init(GFX_RGB565);
|
|
|
|
gicEnableInterrupt(PXI_RX_INTERRUPT);
|
|
gicEnableInterrupt(VBLANK_INTERRUPT);
|
|
|
|
// ARM9 won't try anything funny until this point
|
|
PXI_Barrier(PXI_BOOT_BARRIER);
|
|
|
|
// Process commands until the ARM9 tells
|
|
// us it's time to boot something else
|
|
// also handles Vblank events as needed
|
|
do {
|
|
u32 cmd, resp, args[PXI_MAX_ARGS];
|
|
|
|
vblankUpdate();
|
|
pxiRxUpdate(&cmd, args);
|
|
|
|
switch(cmd) {
|
|
case PXICMD_NONE:
|
|
ARM_WFI();
|
|
break;
|
|
|
|
case PXICMD_LEGACY_BOOT:
|
|
runCmdProcessor = false;
|
|
resp = 0;
|
|
break;
|
|
|
|
case PXICMD_GET_SHMEM_ADDRESS:
|
|
resp = (u32)&sharedMem;
|
|
break;
|
|
|
|
case PXICMD_I2C_OP:
|
|
{
|
|
u32 devId, regAddr, size;
|
|
|
|
devId = (args[0] & 0xff);
|
|
regAddr = (args[0] >> 8) & 0xff;
|
|
size = (args[0] >> 16) % I2C_SHARED_BUFSZ;
|
|
|
|
if (args[0] & BIT(31)) {
|
|
resp = I2C_writeRegBuf(devId, regAddr, sharedMem.i2cBuffer, size);
|
|
} else {
|
|
resp = I2C_readRegBuf(devId, regAddr, sharedMem.i2cBuffer, size);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case PXICMD_NVRAM_ONLINE:
|
|
resp = (NVRAM_Status() & NVRAM_SR_WIP) == 0;
|
|
break;
|
|
|
|
case PXICMD_NVRAM_READ:
|
|
NVRAM_Read(args[0], (u32*)sharedMem.spiBuffer, args[1]);
|
|
resp = 0;
|
|
break;
|
|
|
|
case PXICMD_SET_NOTIFY_LED:
|
|
mcuSetStatusLED(args[0], args[1]);
|
|
resp = 0;
|
|
break;
|
|
|
|
case PXICMD_SET_BRIGHTNESS:
|
|
{
|
|
s32 newbrightness = (s32)args[0];
|
|
resp = GFX_getBrightness();
|
|
#ifndef FIXED_BRIGHTNESS
|
|
if ((newbrightness > 0) && (newbrightness < 0x100)) {
|
|
GFX_setBrightness(newbrightness, newbrightness);
|
|
auto_brightness = false;
|
|
} else {
|
|
prev_bright_lvl = -1;
|
|
auto_brightness = true;
|
|
}
|
|
#endif
|
|
break;
|
|
}
|
|
|
|
default:
|
|
resp = 0xFFFFFFFF;
|
|
break;
|
|
}
|
|
|
|
if (cmd != PXICMD_NONE)
|
|
PXI_Send(resp); // was a command sent from the ARM9, send a response
|
|
} while(runCmdProcessor);
|
|
|
|
// perform deinit in reverse order
|
|
gicDisableInterrupt(VBLANK_INTERRUPT);
|
|
gicDisableInterrupt(PXI_RX_INTERRUPT);
|
|
|
|
// unconditionally reinitialize the screens
|
|
// in RGB24 framebuffer mode
|
|
GFX_init(GFX_BGR8);
|
|
|
|
gicDisableInterrupt(MCU_INTERRUPT);
|
|
|
|
// Wait for the ARM9 to do its firmlaunch setup
|
|
PXI_Barrier(PXI_FIRMLAUNCH_BARRIER);
|
|
|
|
SYS_CoreZeroShutdown();
|
|
SYS_CoreShutdown();
|
|
}
|