Test function for CThunk class

Dependencies:   CThunk mbed

main.cpp

Committer:
meriac
Date:
2014-08-15
Revision:
3:c929fd5a4d3d
Parent:
1:2e8f85f1ed3e
Child:
4:97c886c2d89b

File content as of revision 3:c929fd5a4d3d:

#include <mbed.h>
#include <CThunk.h>

DigitalOut g_led1(LED1);
DigitalOut g_led2(LED2);
DigitalOut g_led3(LED3);
DigitalOut g_led4(LED4);
Serial pc(USBTX, USBRX);

class CTest
{
public:
    CTest(void);
    void callback1(void);
    void callback2(void* context);
    void callback3(void* context);
    static void callback4(uint32_t r0, uint32_t r1);

    uint32_t counter;

    CThunk<CTest> thunk;
};

CTest::CTest(void)
    :thunk(this)
{
    counter = 0;
}

void CTest::callback1(void)
{
    pc.printf("callback1 called (this=0x%0X\n", this);

    /* increment member variable */
//    counter++;
}

void CTest::callback2(void* context)
{
    pc.printf("Called with context value 0x%08X\n", context);

    /* increment member variable */
//    counter = (uint32_t)context;
}

void CTest::callback3(void* context)
{
    pc.printf("Called by ticker object 0x%08X: \n", context);
}

void CTest::callback4(uint32_t r0, uint32_t r1)
{
    pc.printf("callback4: r0=0x%08X r1=0x%08X\n",r0, r1);
}

void hexdump(const void* data, int length)
{
    int i;

    pc.printf("Dump %u bytes from 0x%08Xn", length, data);

    for(i=0; i<length; i++)
    {
        if((i%16) == 0)
            pc.printf("\n");
        else
            if((i%8) == 0)
                pc.printf(" - ");
        pc.printf("0x%02X ", ((uint8_t*)data)[i]);
    }
    pc.printf("\n");
}

static void test(void)
{
    CThunkEntry entry;
    CTest test;

    /* get 32 bit entry point pointer from thunk */
    entry = test.thunk;
    /* TEST1: */

    /* assign callback1 to thunk - no context needed */
    test.thunk = (void*)0xDEADBEEF;
    test.thunk = &CTest::callback1;
    hexdump((const void*)entry, 16);
    /* call entry point */

    pc.printf("before entry 1\n");
    g_led2 = 1;
    entry();
    pc.printf("after entry 1\n");

    /* TEST2: */

    /* assign a context ... */
    test.thunk = 0xDEADBEEF;
    /* and switch callback to callback2 */
    test.thunk = &CTest::callback2;
    /* call entry point */
    pc.printf("before entry 2\n");
    g_led3 = 1;
    entry();
    pc.printf("after entry 2\n");
}

int main(void)
{
    g_led1 = 1;

    /* run tests */
    test();

    /* turn both LED's on */
    g_led4 = 1;
    while(1)
        __WFI();
    return 0;
}