Low-level APIs for Embedded Systems
by Tom Cunningham and Chad Peckham


Listing One
while (!((data = uart->URX) & URX_CHARRDY) &&   /* no data */
       !(data & URX_ERR) &&
       timeout != 0)
{
    if (timeout > 0)
    {
        for (i = 0; i < delay; i++)
            ;       /* ~1 us. busy-wait */
        --timeout;
    }
}


Listing Two
#define UART_A_Receive(UARTPtr,Datap)                                   \
(                                                                       \
    (UART_A_PARAM_CHECKING) ?                                           \
    (                                                                   \
        ((UARTPtr) == NULL) ? DD_ERR_INVALID_HANDLE :                   \
        ((Datap) == NULL) ? DD_ERR_INVALID_ADDRESS :                    \
        UART_A_Receive_f(UARTPtr,Datap)                                 \
    )                                                                   \
    :                                                                   \
        UART_A_Receive_f(UARTPtr,Datap)                                 \
)


Listing Three
#define UART_A_Transmit_m(UARTPtr,Data)                                 \
(                                                                       \
    (UART_A_INLINE_CODE) ?                                              \
    (                                                                   \
        !(((pUART_A_t)(UARTPtr))->USR & USR_TRDY) ?                     \
            UART_A_ERR_DATA_PENDING :                                   \
        (((pUART_A_t)(UARTPtr))->UTX =                                  \
            (Data) & (!(((pUART_A_t)(UARTPtr))->UCR2 & UCR2_WS) ?       \
            SEVEN_BIT_MASK : EIGHT_BIT_MASK),                           \
            DD_ERR_NONE)                                                \
    )                                                                   \
    :                                                                   \
       UART_A_Transmit_f(UARTPtr,Data)                                  \
)
#define UART_A_Transmit(UARTPtr,Data)                                   \
(                                                                       \
    (UART_A_PARAM_CHECKING) ?                                           \
    (                                                                   \
        ((UARTPtr) == NULL) ? DD_ERR_INVALID_HANDLE :                   \
        ((!(((pUART_A_t)(UARTPtr))->UCR2 & UCR2_WS)) && Data > 127) ?   \
            UART_A_ERR_INVALID_DATA_VALUE :                             \
        UART_A_Transmit_m(UARTPtr,Data)                                 \
    )                                                                   \
    :                                                                   \
        UART_A_Transmit_m(UARTPtr,Data)                                 \
)


Listing Four
#include "emusrvr.h"

HINSTANCE hLibrary=NULL;
PSERVERCONNECT ESLConnect=NULL;
PSERVERDISCONNECT ESLDisconnect=NULL;
PSETMCUINFORMATION ESLSetMCU=NULL;
PTARGETRESET ESLTargetReset=NULL;
SERVER_RETVAL ret;
BYTE bESLClientID;

MCUINFO MCUInfo={0x40, 0x00};       // Set CPUType = 0x40

BOOL fConnected = FALSE;

// Load API Library
hLibrary = LoadLibrary("Esrv32.dll");
if (hLibrary)
{
    // Get pointers to APIs
    ESLConnect = (PSERVERCONNECT)GetProcAddress( hLibrary, 
                                                  cszSERVERCONNECT );
    ESLSetMCU = (PSETMCUINFORMATION)GetProcAddress( hLibrary, 
                                                  cszSETMCUINFORMATION );
    ESLDisconnect = (PSERVERDISCONNECT)GetProcAddress( hLibrary, 
                                                   cszSERVERDISCONNECT );
    ESLTargetReset = (PTARGETRESET)GetProcAddress( hLibrary, 
                                                   cszTARGETRESET );
    if (ESLConnect && ESLSetMCU && ESLDisconnect && ESLTargetReset)
    {
        // Connect to EBDI
        ret = ESLConnect( NULL, "COM1", EBDI, &bESLClientID );
        if (ret == SERVER_READY)
        {
            // Tell EBDI we want to do M*CORE
            fConnected = TRUE;
            ret = ESLSetMCU( &MCUInfo, 0, 0, bESLClientID );
        }
    }
}


Listing Five
RESETSTRUCT Reset = {0};        // Default= reset into debug mode
ASYNCSTRUCT Async = {0};        // Storage for event structure

ret = ESLTargetReset( &Reset, bESLClientID );
if ( ret == SERVER_COMPLETE )
{
    ret = SERVER_ASYNC;
    while (ret == SERVER_ASYNC) // process until No events
    {
        ret = ESLGetAsync( &Async, bESLClientID );
        if ( ret == SERVER_ASYNC )  // got one
        {
            // Process event types
        }
    }
}


Listing Six
while (wStatus == CSrecord::srecordOK)
{
    wStatus = pSrecord->GetNextSrecord( &dwSrecAddress, 
                                             srecBytes, 256, &nSrecLen );
    if (wStatus == CSrecord::srecordOK)
    {
        if (fFirst)
        {
            // Initialize current address for first s-record
            dwCurAddress = dwSrecAddress;
            fFirst = FALSE;
        }
        if (nSrecLen)
        {
            // s-record file not at end
            // check for address discontinuity or full buffer
            dwNextAddress = dwCurAddress + nTotal;
            nDatalen = nTotal + nSrecLen;       // buffer plus this s-record

            if (( dwNextAddress != dwSrecAddress) || (nDatalen > 508))
            {
                // Address discontinuity or buffer full - download buffer
                // before appending this s-record to buffer
                if (nTotal)
                {
                    // buffer has data
                    dwGrandTotal += nTotal;     // running total of all bytes
                    wESLStatus = 
                      ESLSetTargetMemory( 0x00,  // bModifier = Target
                                          0x00,  // bAdderSpace = 0 (ignored)
                                          0x02,  // bSize = 32-bit writes
                                          dwCurAddress, // Address
                                          nTotal - 1,   // bytes to write
                                            buffer,    // bytes
                                            nTotal,    // bytes in buffer
                                            &dwErrorAddress, // Error address
                                            ESLId);    // ESL ID
                    if (wESLStatus != SERVER_COMPLETE)
                    {
                       // Error writing target memory - notify user
                       printf("\nError writing address %08.8lX, nBytes = 
                         %d, status = %d\n",dwCurAddress,nTotal,wESLStatus);
                        DoExit();
                        return (-1);
                    }
                    // buffer now empty
                    nTotal = 0;
                    dwCurAddress = dwSrecAddress;
                }
            }
            // Add this s-record to buffer and adjust length of buffer
            memcpy( buffer+nTotal, srecBytes, nSrecLen );
            nTotal += nSrecLen;
        }
    }
}






4


