CommonPIC32

Annotated lib/debug.c

108:ac6df9915cae
2018-11-08 Paul Boddie Moved various common functions to a utilities module.
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@108 22
#include "utils.h"
paul@108 23
paul@108 24
paul@11 25
paul@24 26
/* Register output functions using UART1. */
paul@24 27
paul@24 28
void rbits(uint32_t reg)
paul@24 29
{
paul@24 30
    bits(REG(reg), 4);
paul@24 31
}
paul@24 32
paul@24 33
void rhex(uint32_t reg)
paul@24 34
{
paul@24 35
    hex(REG(reg), 4);
paul@24 36
}
paul@24 37
paul@19 38
/* Value output functions using UART1. */
paul@11 39
paul@24 40
void bits(uint32_t val, int bytes)
paul@11 41
{
paul@11 42
    uint32_t mask;
paul@11 43
paul@11 44
    for (mask = (1 << 31); mask; mask >>= 1)
paul@11 45
        if (val & mask)
paul@11 46
            uart_write('1');
paul@11 47
        else
paul@11 48
            uart_write('0');
paul@11 49
}
paul@11 50
paul@24 51
void hex(uint32_t val, int bytes)
paul@11 52
{
paul@11 53
    uint32_t mask;
paul@24 54
    uint8_t digit, shift, start = bytes * 8 - 4;
paul@11 55
paul@24 56
    for (mask = (0b1111 << start), shift = start; mask; mask >>= 4, shift -= 4)
paul@11 57
    {
paul@11 58
        digit = (val & mask) >> shift;
paul@11 59
        if (digit > 9)
paul@11 60
            uart_write('A' + digit - 10);
paul@11 61
        else
paul@11 62
            uart_write('0' + digit);
paul@11 63
    }
paul@11 64
}
paul@11 65
paul@19 66
paul@19 67
paul@19 68
/* General input/output functions. */
paul@19 69
paul@19 70
int uart_can_read(int uart)
paul@19 71
{
paul@19 72
    return REG(UART_REG(uart, UxSTA)) & 1;
paul@19 73
}
paul@19 74
paul@19 75
char uart_read_char(int uart)
paul@19 76
{
paul@19 77
    return (char) REG(UART_REG(uart, UxRXREG));
paul@19 78
}
paul@19 79
paul@19 80
int uart_can_write(int uart)
paul@19 81
{
paul@19 82
    return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */
paul@19 83
}
paul@19 84
paul@19 85
void uart_write_char(int uart, char c)
paul@19 86
{
paul@19 87
    while (!uart_can_write(uart)); /* busy loop */
paul@19 88
paul@19 89
    REG(UART_REG(uart, UxTXREG)) = c;
paul@19 90
}
paul@19 91
paul@19 92
paul@19 93
paul@19 94
/* Functions using UART1. */
paul@11 95
paul@11 96
void uart_write(char c)
paul@11 97
{
paul@19 98
    uart_write_char(1, c);
paul@11 99
}
paul@12 100
paul@24 101
void uart_write_nl(void)
paul@24 102
{
paul@24 103
    uart_write('\r');
paul@24 104
    uart_write('\n');
paul@24 105
}
paul@24 106
paul@12 107
void uart_write_string(const char *s)
paul@12 108
{
paul@12 109
    while (*s)
paul@12 110
        uart_write(*s++);
paul@12 111
}
paul@108 112
paul@108 113
paul@108 114
paul@108 115
/* Blink an attached LED with delays implemented using a loop. */
paul@108 116
paul@108 117
void blink(uint32_t delay, uint32_t port, uint32_t pins)
paul@108 118
{
paul@108 119
    /* Clear outputs (LED). */
paul@108 120
paul@108 121
    CLR_REG(port, pins);
paul@108 122
paul@108 123
    while (1)
paul@108 124
    {
paul@108 125
        wait(delay);
paul@108 126
paul@108 127
        /* Invert outputs (LED). */
paul@108 128
paul@108 129
        INV_REG(port, pins);
paul@108 130
    }
paul@108 131
}