CommonPIC32

Annotated debug.c

15:f145c8568fd6
2018-10-18 Paul Boddie Added copyright and licensing information. Adjusted the formatting in various places.
paul@15 1
/*
paul@15 2
 * Some simple debugging functions.
paul@15 3
 *
paul@15 4
 * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk>
paul@15 5
 *
paul@15 6
 * This program is free software: you can redistribute it and/or modify
paul@15 7
 * it under the terms of the GNU General Public License as published by
paul@15 8
 * the Free Software Foundation, either version 3 of the License, or
paul@15 9
 * (at your option) any later version.
paul@15 10
 *
paul@15 11
 * This program is distributed in the hope that it will be useful,
paul@15 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@15 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@15 14
 * GNU General Public License for more details.
paul@15 15
 *
paul@15 16
 * You should have received a copy of the GNU General Public License
paul@15 17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
paul@15 18
 */
paul@15 19
paul@11 20
#include "pic32_c.h"
paul@11 21
#include "debug.h"
paul@11 22
paul@11 23
/* Value output functions. */
paul@11 24
paul@11 25
void bits(uint32_t reg)
paul@11 26
{
paul@11 27
    vbits(REG(reg));
paul@11 28
}
paul@11 29
paul@11 30
void vbits(uint32_t val)
paul@11 31
{
paul@11 32
    uint32_t mask;
paul@11 33
paul@11 34
    for (mask = (1 << 31); mask; mask >>= 1)
paul@11 35
        if (val & mask)
paul@11 36
            uart_write('1');
paul@11 37
        else
paul@11 38
            uart_write('0');
paul@11 39
paul@11 40
    uart_write('\r');
paul@11 41
    uart_write('\n');
paul@11 42
}
paul@11 43
paul@11 44
void vhex(uint32_t val)
paul@11 45
{
paul@11 46
    uint32_t mask;
paul@11 47
    uint8_t digit, shift;
paul@11 48
paul@11 49
    for (mask = (0b1111 << 28), shift = 28; mask; mask >>= 4, shift -= 4)
paul@11 50
    {
paul@11 51
        digit = (val & mask) >> shift;
paul@11 52
        if (digit > 9)
paul@11 53
            uart_write('A' + digit - 10);
paul@11 54
        else
paul@11 55
            uart_write('0' + digit);
paul@11 56
    }
paul@11 57
paul@11 58
    uart_write('\r');
paul@11 59
    uart_write('\n');
paul@11 60
}
paul@11 61
paul@11 62
/* General output functions. */
paul@11 63
paul@11 64
void uart_write(char c)
paul@11 65
{
paul@11 66
    while (REG(UART_REG(1, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */
paul@11 67
paul@11 68
    REG(UART_REG(1, UxTXREG)) = c;
paul@11 69
}
paul@12 70
paul@12 71
void uart_write_string(const char *s)
paul@12 72
{
paul@12 73
    while (*s)
paul@12 74
        uart_write(*s++);
paul@12 75
}