diff --git a/include/arm11/hardware/i2c.h b/include/arm11/hardware/i2c.h index ecd96a1..dd7472a 100644 --- a/include/arm11/hardware/i2c.h +++ b/include/arm11/hardware/i2c.h @@ -115,20 +115,11 @@ return data[18]; } -static inline bool i2cmcu_lcd_poweron(void) -{ - return I2C_writeReg(I2C_DEV_MCU, 0x22, 1<<5 | 1<<3 | 1<<1); // bit1 = lcd power enable for both screens -} - static inline bool i2cmcu_lcd_backlight_poweron(void) { return I2C_writeReg(I2C_DEV_MCU, 0x22, 1<<5 | 1<<3); // bit3 = lower screen, bit5 = upper } -static bool i2cmcu_lcd_poweroff(void) -{ - return I2C_writeReg(I2C_DEV_MCU, 0x22, 1); // bit0 = lcd power disable for both screens (also disabled backlight) -} #define MCU_HID_POWER_BUTTON_PRESSED (1u) #define MCU_HID_POWER_BUTTON_LONG_PRESSED (1u<<1) diff --git a/include/arm11/hardware/mcu.h b/include/arm11/hardware/mcu.h index 370068c..ad51dc2 100644 --- a/include/arm11/hardware/mcu.h +++ b/include/arm11/hardware/mcu.h @@ -21,4 +21,9 @@ #include "types.h" void MCU_disableLEDs(void); +void MCU_powerOnLCDs(void); +void MCU_powerOffLCDs(void); +void MCU_triggerPowerOff(void); +void MCU_triggerReboot(void); u8 MCU_readBatteryLevel(void); +bool MCU_readBatteryChargeState(void); diff --git a/include/arm11/power.h b/include/arm11/power.h index c95c0fc..c01b56b 100644 --- a/include/arm11/power.h +++ b/include/arm11/power.h @@ -20,6 +20,5 @@ -bool battery_ok(void); noreturn void power_off(void); noreturn void power_reboot(void); diff --git a/source/arm11/debug.c b/source/arm11/debug.c index 4be61a9..858c20b 100644 --- a/source/arm11/debug.c +++ b/source/arm11/debug.c @@ -27,7 +27,7 @@ #include "ipc_handler.h" #include "hardware/gfx.h" #include "arm11/hardware/interrupt.h" -#include "arm11/hardware/i2c.h" +#include "arm11/hardware/mcu.h" #include "arm11/hardware/hid.h" @@ -73,8 +73,8 @@ hidScanInput(); } while(!(hidKeysDown() & HID_KEY_MASK_ALL)); - i2cmcu_lcd_poweroff(); - I2C_writeReg(I2C_DEV_MCU, 0x20, 1u); + MCU_powerOffLCDs(); + MCU_triggerPowerOff(); while(1) __wfi(); } @@ -99,8 +99,8 @@ hidScanInput(); } while(!(hidKeysDown() & HID_KEY_MASK_ALL)); - i2cmcu_lcd_poweroff(); - I2C_writeReg(I2C_DEV_MCU, 0x20, 1u); + MCU_powerOffLCDs(); + MCU_triggerPowerOff(); while(1) __wfi(); } @@ -169,8 +169,8 @@ hidScanInput(); } while(!(hidKeysDown() & HID_KEY_MASK_ALL)); - i2cmcu_lcd_poweroff(); - I2C_writeReg(I2C_DEV_MCU, 0x20, 1u); + MCU_powerOffLCDs(); + MCU_triggerPowerOff(); while(1) __wfi(); } diff --git a/source/arm11/hardware/gfx.c b/source/arm11/hardware/gfx.c index 00f359d..6f4c617 100644 --- a/source/arm11/hardware/gfx.c +++ b/source/arm11/hardware/gfx.c @@ -26,7 +26,7 @@ #include "types.h" #include "mem_map.h" #include "hardware/gfx.h" -#include "arm11/hardware/i2c.h" +#include "arm11/hardware/mcu.h" #include "arm11/hardware/interrupt.h" #include "arm11/event.h" @@ -252,7 +252,7 @@ gfxSetupLcdTop(); gfxSetupLcdSub(); - I2C_writeReg(I2C_DEV_MCU, 0x22, 1<<5 | 1<<3 | 1<<1); // Power on LCDs and backlight + MCU_powerOnLCDs(); // Power on LCDs and backlight } else { @@ -288,13 +288,13 @@ void GFX_enterLowPowerState(void) { IRQ_disable(IRQ_PDC0); - i2cmcu_lcd_poweroff(); + MCU_powerOffLCDs(); } void GFX_returnFromLowPowerState(void) { IRQ_enable(IRQ_PDC0); - i2cmcu_lcd_poweron(); + MCU_powerOnLCDs(); } void GFX_deinit(void) diff --git a/source/arm11/hardware/mcu.c b/source/arm11/hardware/mcu.c index e52eb55..442181d 100644 --- a/source/arm11/hardware/mcu.c +++ b/source/arm11/hardware/mcu.c @@ -26,6 +26,9 @@ enum McuRegisters { RegBattery = 0x0B, + RegExHW = 0x0F, + RegPower = 0x20, + RegLCDs = 0x22, RegWifiLED = 0x2A, Reg3DLED = 0x2C, }; @@ -39,6 +42,28 @@ I2C_writeReg(I2C_DEV_MCU, Reg3DLED, 0); } +void MCU_powerOnLCDs(void) +{ + // bit1 = lcd power enable for both screens + I2C_writeReg(I2C_DEV_MCU, RegLCDs, 1<<5 | 1<<3 | 1<<1); +} + +void MCU_powerOffLCDs(void) +{ + // bit0 = lcd power disable for both screens (also disables backlight) + I2C_writeReg(I2C_DEV_MCU, RegLCDs, 1); +} + +void MCU_triggerPowerOff(void) +{ + I2C_writeReg(I2C_DEV_MCU, RegPower, 1); +} + +void MCU_triggerReboot(void) +{ + I2C_writeReg(I2C_DEV_MCU, RegPower, 1u << 2); +} + u8 MCU_readBatteryLevel(void) { u8 state; @@ -48,3 +73,12 @@ return state; } + +bool MCU_readBatteryChargeState(void) +{ + u8 state; + + state = I2C_readReg(I2C_DEV_MCU, RegExHW); + + return (state & (1u << 4)) == 1; +} diff --git a/source/arm11/power.c b/source/arm11/power.c index 18bd354..6e55343 100644 --- a/source/arm11/power.c +++ b/source/arm11/power.c @@ -18,7 +18,7 @@ #include "types.h" #include "util.h" -#include "arm11/hardware/i2c.h" +#include "arm11/hardware/mcu.h" #include "hardware/cache.h" #include "arm11/hardware/interrupt.h" #include "ipc_handler.h" @@ -27,12 +27,12 @@ noreturn void power_off(void) { PXI_sendCmd(IPC_CMD9_PREPARE_POWER, NULL, 0); - i2cmcu_lcd_poweroff(); + MCU_powerOffLCDs(); flushDCache(); __asm__ __volatile__("cpsid aif" : :); - I2C_writeReg(I2C_DEV_MCU, 0x20, 1u); + MCU_triggerPowerOff(); while(1) __wfi(); } @@ -40,12 +40,12 @@ noreturn void power_reboot(void) { PXI_sendCmd(IPC_CMD9_PREPARE_POWER, NULL, 0); - i2cmcu_lcd_poweroff(); + MCU_powerOffLCDs(); flushDCache(); __asm__ __volatile__("cpsid aif" : :); - I2C_writeReg(I2C_DEV_MCU, 0x20, 1u << 2); + MCU_triggerReboot(); while(1) __wfi(); }