Test function for CThunk class

Dependencies:   CThunk mbed

main.cpp

Committer:
meriac
Date:
2014-08-13
Revision:
1:2e8f85f1ed3e
Parent:
0:a6725b4373c6
Child:
3:c929fd5a4d3d

File content as of revision 1:2e8f85f1ed3e:

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

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;

private:
    DigitalOut m_led;
};

CTest::CTest(void)
    :m_led(LED1)
{
    counter = 0;
}

void CTest::callback1(void)
{
    pc.printf("callback1 called\n");

    /* 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)
{
    Ticker* sender = static_cast<Ticker*>(context);

    pc.printf("Called by ticker object 0x%08X: \n", sender);

  //  m_led = !m_led;
}

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");
}

int main(void)
{
    Ticker tick;
    CThunkEntry entry;
    CTest test;

    /* set meaningful baud rate for CMSIS DAP */
    pc.baud(115200);
    pc.printf("Starting CTHunk tests...\n");

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

    pc.printf(" - thunk entry at 0x%08X\n", entry);

    /* TEST1: */

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

    /* TEST2: */

    /* assign a context ... */
    test.thunk = 0xDEADBEEF;
    /* and switch callback to callback2 */
    test.thunk = &CTest::callback2;
    /* call entry point */
    entry();


    /* TEST3: */

    /* try ticker */
    tick.attach(test.thunk, 1.0);
    /* assign a context ... */
    test.thunk = (void*)&tick;
    /* change the callback function */
    test.thunk = &CTest::callback3;
    /* call entry point */
    entry();
}