Jump to content
ferdster

MSP432: Switching between LPM0 and LPM3?

Recommended Posts

Hi, I am just starting out with the MSP432P401R LaunchPad and wanted to try out a simple program that would toggle between 'sleep' and 'deepsleep'. I am using Code Composer Studio v7 and started with the powerdeepsleep example.  I can initially get it into deepsleep and then out again, but then it will never go into deepsleep again (confirmed by looking at the current draw from external psu and by using EnergyTrace).

 

main_tirtos.c (added second thread)

/*
 *  ======== main_tirtos.c ========
 */
#include <stdint.h>

/* POSIX Header files */
#include <pthread.h>

/* RTOS header files */
#include <ti/sysbios/BIOS.h>

/* Example/Board Header files */
#include "Board.h"

extern void *deepSleepThread(void *arg0);
extern void *sleepThread(void *arg0);

/* Stack size in bytes */
#define THREADSTACKSIZE    1024

/*
 *  ======== main ========
 */
int main(void)
{
    pthread_t           thread;
    pthread_attr_t      pAttrs;
    struct sched_param  priParam;
    int                 retc;
    int                 detachState;

    /* Call driver init functions */
    Board_initGeneral();
    Board_initGPIO();

    /* Set priority and stack size attributes */
    pthread_attr_init(&pAttrs);
    priParam.sched_priority = 1;

    detachState = PTHREAD_CREATE_DETACHED;
    retc = pthread_attr_setdetachstate(&pAttrs, detachState);
    if (retc != 0) {
        /* pthread_attr_setdetachstate() failed */
        while (1);
    }

    pthread_attr_setschedparam(&pAttrs, &priParam);

    retc |= pthread_attr_setstacksize(&pAttrs, THREADSTACKSIZE);
    if (retc != 0) {
        /* pthread_attr_setstacksize() failed */
        while (1);
    }

    retc = pthread_create(&thread, &pAttrs, sleepThread, NULL);
    if (retc != 0) {
        /* pthread_create() failed */
        while (1);
    }

    retc = pthread_create(&thread, &pAttrs, deepSleepThread, NULL);
    if (retc != 0) {
        /* pthread_create() failed */
        while (1);
    }

    BIOS_start();

    return (0);
}

powersleep.c

/*
 *  ======== powersleep.c ========
 */

/* Driver Header files */
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerMSP432.h>
#include <ti/drivers/GPIO.h>

/* Driverlib Header files */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>

/* Board Header file */
#include "Board.h"

/*
 *  ======== gpioButtonFxn0 ========
 *  Callback function for the GPIO interrupt on Board_GPIO_BUTTON0.
 */

void gpioButtonFxn1(uint_least8_t x)
{
    GPIO_clearInt(Board_GPIO_BUTTON1);

    GPIO_toggle(Board_GPIO_LED1);

    Power_setPolicy((Power_PolicyFxn)PowerMSP432_sleepPolicy);
}

/*
 *  ======== sleepThread ========
 */
void *sleepThread(void *arg0)
{
    GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_ON);

    GPIO_setCallback(Board_GPIO_BUTTON1, gpioButtonFxn1);
    GPIO_enableInt(Board_GPIO_BUTTON1);

    return NULL;
}

powerdeepsleep.c

/*
 *  ======== powerdeepsleep.c ========
 */

/* Driver Header files */
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerMSP432.h>
#include <ti/drivers/GPIO.h>

/* Driverlib Header files */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>

/* Board Header file */
#include "Board.h"

/*
 *  ======== gpioButtonFxn0 ========
 *  Callback function for the GPIO interrupt on Board_GPIO_BUTTON0.
 */

void gpioButtonFxn0(uint_least8_t x)
{
    GPIO_clearInt(Board_GPIO_BUTTON0);

    //GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);
    //GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_OFF);
    GPIO_toggle(Board_GPIO_LED0);

    /* Select PowerMSP432_deepSleepPolicy to enable transition
     * into the PowerMSP432_DEEPSLEEP_1 (LPM4)state
     */
    Power_setPolicy((Power_PolicyFxn)PowerMSP432_deepSleepPolicy);

    /* Refer to Device datasheet for Wake-up Sources From Low Power Modes to wake up from
     * PowerMSP432_DEEPSLEEP_1
     */
}

/*
 *  ======== deepSleepThread ========
 */
void *deepSleepThread(void *arg0)
{
    GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);

    GPIO_setCallback(Board_GPIO_BUTTON0, gpioButtonFxn0);
    GPIO_enableInt(Board_GPIO_BUTTON0);

    return NULL;
}
In 'free run', it seems to only go into gpioButtonFxn0() on the very first button0 press. Subsequent button0 presses don't toggle the led and so it never enters deepsleep again. But, if I insert a breakpoint in that function, it does go there and toggles the led. button1 works as expected and toggles the led on every button1 press.
 
Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×