0
\$\begingroup\$

When i'm doing code using MCC for example i will put my code the following way:

#include "stdio.h"
#include <xc.h>
#include "mcc_generated_files/mcc.h"

void main(void)
{
    SYSTEM_Initialize();
    printf("Booting loop debugger\r\n");
    LED_GREEN_Toggle();
    loop_debug_initialize();// start debug variables
    loop_initialize();
    while (1)
    {
        loop_debug_manage();
    }
}

My question now is using the Harmony framework i have something like this:

int main ( void )
{
    __XC_UART = 1;  // Code is configured to use UART1 
    /* Initialize all MPLAB Harmony modules, including application(s). */
    SYS_Initialize ( NULL );


    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Where shall i put the same code? can i put everything on main? like this:

int main ( void )
{
    __XC_UART = 1;  // Code is configured to use UART1 
    /* Initialize all MPLAB Harmony modules, including application(s). */
    SYS_Initialize ( NULL );
    loop_debug_initialize();// start debug variables
    loop_initialize();

    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
        loop_debug_manage();
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}

Or this is not ok? Does it need to go inside void SYS_Tasks ( void )?

It's my first time working with Harmony, so any help is appreciated.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I understand that Harmony is supposed to make things simpler. It does this well for configuration -- but it does makes a lot more things confusing especially for newcomers. Your main code can go absolutely anywhere you want and you can just dump the APP_Tasks() function.

HOWEVER --- if you really want to follow the correct Harmony procedure and not kill any Harmony peripherals or "system services", your main code MUST be non blocking finite state machine and must be in APP_Tasks.

You should create the states you require for your application state in APP_STATES enum.

Harmony gives you two states right off the bat that you can use if you want.

The first state is APP_STATE_INIT, you can put things in this switch case for starting your application code.

The last state and only second state it gives you is APP_STATE_SERVICE_TASKS which you put your main running code.

However you should really make your own new states, as your main program probably won't be that simple. For example, this code will go into three states, INIT, AWAIT_ON, AWAIT_OFF. Basically just flipping states based on button presses.

app.h

typedef enum
{
    /* Application's state machine's initial state. */
    APP_STATE_INIT=0,
    APP_STATE_AWAIT_PRESS_ON, // Wait for the on button
    APP_STATE_AWAIT_PRESS_OFF, // Switch off! (back to await press on)

    /* TODO: Define states used by the application state machine. */

} APP_STATES;

App_Tasks()

/* Check the application's current state. */
switch ( appData.state )
{
    /* Application's initial state. */
    case APP_STATE_INIT:
    {
        bool appInitialized = true;

        if (appInitialized)
        {
            // initial LATG state
            LATGbits.LATG0 = 0;

            appData.state = APP_STATE_AWAIT_PRESS_ON;
        }
        break;
    }

    case APP_STATE_AWAIT_PRESS_ON:
    {
        if ( PORTEbits.PORTE0 )
        {
            LATGbits.LATG0 = 1;
            appData.state = APP_STATE_AWAIT_PRESS_OFF;
        }
        break;
    }
    case APP_STATE_AWAIT_PRESS_OFF:
    {
        if ( !PORTEbits.PORTE0 )
        {
            LATGbits.LATG0 = 0;
            appData.state = APP_STATE_AWAIT_PRESS_ON;
        }
        break;
    }

    /* The default state should never be executed. */
    default:
    {
        /* TODO: Handle error in application's state machine. */
        break;
    }
}

There is "heartbeat indicator" example code in MPLAB Harmony Tutorial (Step 7: Develop the Application). I would suggest reading these tutorials before jumping straight in.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.