CommonPIC32

Change of lib/debug.c

38:a8a72e893562
lib/debug.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/debug.c	Mon Oct 22 21:44:02 2018 +0200
     1.3 @@ -0,0 +1,108 @@
     1.4 +/*
     1.5 + * Some simple debugging functions.
     1.6 + *
     1.7 + * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk>
     1.8 + *
     1.9 + * This program is free software: you can redistribute it and/or modify
    1.10 + * it under the terms of the GNU General Public License as published by
    1.11 + * the Free Software Foundation, either version 3 of the License, or
    1.12 + * (at your option) any later version.
    1.13 + *
    1.14 + * This program is distributed in the hope that it will be useful,
    1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.21 + */
    1.22 +
    1.23 +#include "pic32_c.h"
    1.24 +#include "debug.h"
    1.25 +
    1.26 +/* Register output functions using UART1. */
    1.27 +
    1.28 +void rbits(uint32_t reg)
    1.29 +{
    1.30 +    bits(REG(reg), 4);
    1.31 +}
    1.32 +
    1.33 +void rhex(uint32_t reg)
    1.34 +{
    1.35 +    hex(REG(reg), 4);
    1.36 +}
    1.37 +
    1.38 +/* Value output functions using UART1. */
    1.39 +
    1.40 +void bits(uint32_t val, int bytes)
    1.41 +{
    1.42 +    uint32_t mask;
    1.43 +
    1.44 +    for (mask = (1 << 31); mask; mask >>= 1)
    1.45 +        if (val & mask)
    1.46 +            uart_write('1');
    1.47 +        else
    1.48 +            uart_write('0');
    1.49 +}
    1.50 +
    1.51 +void hex(uint32_t val, int bytes)
    1.52 +{
    1.53 +    uint32_t mask;
    1.54 +    uint8_t digit, shift, start = bytes * 8 - 4;
    1.55 +
    1.56 +    for (mask = (0b1111 << start), shift = start; mask; mask >>= 4, shift -= 4)
    1.57 +    {
    1.58 +        digit = (val & mask) >> shift;
    1.59 +        if (digit > 9)
    1.60 +            uart_write('A' + digit - 10);
    1.61 +        else
    1.62 +            uart_write('0' + digit);
    1.63 +    }
    1.64 +}
    1.65 +
    1.66 +
    1.67 +
    1.68 +/* General input/output functions. */
    1.69 +
    1.70 +int uart_can_read(int uart)
    1.71 +{
    1.72 +    return REG(UART_REG(uart, UxSTA)) & 1;
    1.73 +}
    1.74 +
    1.75 +char uart_read_char(int uart)
    1.76 +{
    1.77 +    return (char) REG(UART_REG(uart, UxRXREG));
    1.78 +}
    1.79 +
    1.80 +int uart_can_write(int uart)
    1.81 +{
    1.82 +    return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */
    1.83 +}
    1.84 +
    1.85 +void uart_write_char(int uart, char c)
    1.86 +{
    1.87 +    while (!uart_can_write(uart)); /* busy loop */
    1.88 +
    1.89 +    REG(UART_REG(uart, UxTXREG)) = c;
    1.90 +}
    1.91 +
    1.92 +
    1.93 +
    1.94 +/* Functions using UART1. */
    1.95 +
    1.96 +void uart_write(char c)
    1.97 +{
    1.98 +    uart_write_char(1, c);
    1.99 +}
   1.100 +
   1.101 +void uart_write_nl(void)
   1.102 +{
   1.103 +    uart_write('\r');
   1.104 +    uart_write('\n');
   1.105 +}
   1.106 +
   1.107 +void uart_write_string(const char *s)
   1.108 +{
   1.109 +    while (*s)
   1.110 +        uart_write(*s++);
   1.111 +}