Low power sleep with WDT wakeup on PIC32MM
It appears the key is clearing the the WDTS flag in RNMICON. According to the datasheet, this should be zero at reset, so the function should work once (but doesn't appear to).

The below code works (ie sleep(1); sleeps for 1 WDT period), although it may still have some unnecessary lines in it.

void sleep(uint32_t t){
    SYSKEY = 0x0;
    SYSKEY = 0xAA996655;                    //unlock
    SYSKEY = 0x556699AA;
    //OSCCONbits.SLPEN=0;                     //enable idle
    OSCCONbits.SLPEN=1;                     //enable sleep
    SYSKEY = 0x0;                           //lock
    while(t){
        RCONCLR=0xDF;                       //clear all reset reason flags
        __builtin_disable_interrupts();
        SYSKEY = 0x0;
        SYSKEY = 0xAA996655;
        SYSKEY = 0x556699AA;
        RNMICONbits.WDTS=0;                 //clear sleep WDT flag
        SYSKEY = 0x0;
        WDTCONbits.WDTCLRKEY=0x5743;        //magic sequence to reset WDT
        WDTCONbits.ON=1;                    //enable
        asm volatile( "wait" );             //sleep/idle as per SLPEN
        Nop();
        WDTCONbits.ON=0;                    //disable
        t--;
    }
}