# PM_GoSleepMode

## Syntax

      #include <nitro/spi.h>

      void PM_GoSleepMode( PMWakeUpTrigger trigger, PMLogic logic, u16 keyPattern );

## Arguments

trigger | Trigger to recover from sleep state.
logic | Key combination logic when recovering with a key interrupt.
keyPattern | Key when recovering with a key interrupt.

## Return Values

None.

## Description

Transitions both ARM9 and ARM7 to sleep state.

> Note:
> - The Programming Guidelines prohibit the system from entering sleep mode while wireless communication is still turned ON.
> Be sure to turn it OFF.
> - Entering sleep mode while the DSP is loaded is prohibited.
> Always shut down operations.( Be sure to call the `DSP_UnloadG711` function.)
> - Do not call from the interrupt handler.

This function sends a sleep command to the ARM7, and the ARM9 itself enters a halted state as well through the `OS_Halt` function. The trigger argument is a `PMWakeUpTrigger` enum type that specifies a trigger to recover from the sleep state. The trigger can specify multiple items by getting a logical OR. The following items can be specified.

Platform | Value | Cause of Recovery
--- | --- | ---
| `PM_TRIGGER_KEY` | A key interrupt
| `PM_TRIGGER_RTC_ALARM` | An RTC alarm interrupt
| `PM_TRIGGER_COVER_OPEN` | Opening the cover
| `PM_TRIGGER_CARD` | A card interrupt or Game Card removal
| `PM_TRIGGER_CARTRIDGE` (Note 1) | Generate a cartridge interrupt or remove the cartridge
| `PM_TRIGGER_SDIO` | SDIO interrupt

Note 1: Waking from sleep upon removal of a Game Pak is prohibited by the programming guidelines.

> Example: To enter sleep mode and recover by opening the cover or removing the Game Card, use `PM_GoSleepMode( PM_TRIGGER_COVER_OPEN | PM_TRIGGER_CARD ,0 ,0 );`

In terms of the hardware specification, these recovery triggers are level triggers, not edge triggers. For example, strictly speaking, `PM_TRIGGER_CARTRIDGE` does not detect when a Game Pak is removed, but rather that no Game Pak is inserted, and then returns. (More specifically, it detects the status of the `OS_IE_CARTRIDGE_IREQ` interrupt in operation.) Therefore, if you try to enter sleep mode with `PM_TRIGGER_CARTRIDGE` as the recovery trigger while no Game Pak is inserted (the system determines this as already removed), it recovers immediately.

However, Game Card and Game Pak interrupts check (inside the function) whether a Game Card or Game Pak is actually inserted. As a result, when you try to insert a recovery trigger while no Game Card or Game Pak is inserted, that trigger is discarded.

Recovery by a Key Interrupt

When you include key interrupt in recovery trigger, specify the keys to trigger recovery and their combination logic with keyPattern and logic. The combination logic is either `PM_PAD_LOGIC_AND` (when input is from all keys), or `PM_PAD_LOGIC_OR` (when input is from any key). For example, to make recovery possible only by pressing the A Button and START at the same time:

`PM_GoSleepMode( PM_TRIGGER_KEY, PM_PAD_LOGIC_AND, PAD_BUTTON_A | PAD_BUTTON_START );`

To make recovery possible by pushing either the A button or START:

`PM_GoSleepMode( PM_TRIGGER_KEY, PM_PAD_LOGIC_OR, PAD_BUTTON_A | PAD_BUTTON_START );`

When you do not include a key interrupt in the recovery trigger, specifications for keyPattern and logic are meaningless. Also, when there is only one key assignment, the combination logic logic may be either `PM_PAD_LOGIC_AND` or `PM_PAD_LOGIC_OR`.

Recovery by an SDIO Interrupt

SDIO interrupts can be used for recovery on the TWL platform. This interrupt is provided for future extensions and assumes that a supported device has been plugged into the SD card slot. This can also be used with wireless communications. This should not usually be specified.

Callback Functions

It is possible to set callback functions to be called before entering sleep mode and after awakening from sleep mode. The callback function that is called when entering sleep mode is called at the beginning of the `PM_GoSleepMode` function, and the function that is called after awakening from sleep mode is called at the end of `PM_GoSleepMode`. This is equivalent to calling these functions before and after `PM_GoSleepMode`.

The callback function is a `PMSleepCallback` type (a function type that retains one `void*` argument and no return values), and multiple functions can be registered. The registered functions are called in order, so pay special attention when registering functions that must be performed in a specific order.

When registering a callback, specify with the callback function and the callback information including the arguments. For more information, see the reference for the `PM_AppendPreSleepCallback` function.

Calls from an Interrupt Handler

The `PM_GoSleepMode` function does not expect to be called from an interrupt handler. Do not call it from an interrupt handler.

Possible Delays

The `PM_GoSleepMode` function internally calls several PM functions. When the SPI is occupied by another device and therefore BUSY, this function continues calling the PM functions until they succeed. As a result, the time at which the system actually enters sleep mode may be delayed from the time this function is called.

PM Library Initialization

Before using the `PM_GoSleepMode` function, initialize the PM library with the `PM_Init` function. (The `PM_Init` function has to be called only once. Also, when you call the `OS_Init` function, there is no need to call the `PM_Init` function separately because it is called from inside `OS_Init`.)

## See Also

`PM_Init`
`PM_AppendPreSleepCallback`
`PM_PrependPreSleepCallback`
`PM_AppendPostSleepCallback`
`PM_PrependPostSleepCallback`
`PM_DeletePreSleepCallback`
`PM_DeletePostSleepCallback`
`PM_SetSleepCallbackInfo`

## Revision History

2008/09/11 Indicated that sleep was prohibited when using the DSP.
2008/08/23 Indicated the possibility for delays.
2008/03/13 Added the SDIO interrupt and denoted that calls from an interrupt handler are not possible.
2006/05/16 Modified the description of cartridge removal as a cause for restoring.
2005/09/14 Changed the `PM_SetSleepCallback` function to the `PM_SetSleepCallbackInfo` function.
2005/03/08 Standardized Japanese terminology for "interrupt."
2004/10/06 Revised to allow registration of multiple callbacks.
2004/10/05 Added more information on callbacks.
2004/08/07 Separated combination logic by arguments.
2004/08/02 Initial version.

CONFIDENTIAL