summaryrefslogtreecommitdiff
path: root/recipes/i2c/files/DumpMem.c
blob: e13e94b0f8bd43a78a2317f2949d82103babf32c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/****************************************************************************
*
*   Copyright (c) 2006 Dave Hylands     <dhylands@gmail.com>
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
*   See README and COPYING for more details.
*
****************************************************************************/
/**
*
*   @file   DumpMem.c
*
*   @brief  Memmory dump routine
*
****************************************************************************/

// ---- Include Files -------------------------------------------------------

#include <inttypes.h>
#include "DumpMem.h"
#include "Log.h"

// ---- Public Variables ----------------------------------------------------
// ---- Private Constants and Types -----------------------------------------
// ---- Private Variables ---------------------------------------------------
// ---- Private Function Prototypes -----------------------------------------
// ---- Functions -----------------------------------------------------------

/**************************************************************************/
/**
*   Dumps a page of output for debugging purposes.
*/

void DumpMem( const char *prefix, unsigned address, const void *inData, unsigned numBytes )
{
    const uint8_t  *data = (const uint8_t *)inData;
    unsigned        byteOffset;

    if ( numBytes == 0 )
    {
        Log( "%s: No data\n", prefix );
        return;
    }

#define LINE_WIDTH  16

    for ( byteOffset = 0; byteOffset < numBytes; byteOffset += LINE_WIDTH ) 
    {
        unsigned    i;

        Log( "%s: %04x: ", prefix, address + byteOffset );

        for ( i = 0; i < LINE_WIDTH; i++ ) 
        {
            if (( byteOffset + i ) < numBytes )
            {
                Log( "%02.2X ", data[ byteOffset + i ] );
            }
            else
            {
                Log( "   " );
            }
        }
        for ( i = 0; i < LINE_WIDTH; i++ ) 
        {
            if (( byteOffset + i ) < numBytes )
            {
                unsigned char ch = data[ byteOffset + i ];
                if (( ch < ' ' ) || ( ch > '~' ))
                {
                    Log( "." );
                }
                else
                {
                    Log( "%c", ch );
                }
            }
            else
            {
                break;
            }
        }
        Log( "\n" );
    }

} // DumpMem