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@24 | 23 | /* Register output functions using UART1. */ |
paul@24 | 24 | |
paul@24 | 25 | void rbits(uint32_t reg) |
paul@24 | 26 | { |
paul@24 | 27 | bits(REG(reg), 4); |
paul@24 | 28 | } |
paul@24 | 29 | |
paul@24 | 30 | void rhex(uint32_t reg) |
paul@24 | 31 | { |
paul@24 | 32 | hex(REG(reg), 4); |
paul@24 | 33 | } |
paul@24 | 34 | |
paul@19 | 35 | /* Value output functions using UART1. */ |
paul@11 | 36 | |
paul@24 | 37 | void bits(uint32_t val, int bytes) |
paul@11 | 38 | { |
paul@11 | 39 | uint32_t mask; |
paul@11 | 40 | |
paul@11 | 41 | for (mask = (1 << 31); mask; mask >>= 1) |
paul@11 | 42 | if (val & mask) |
paul@11 | 43 | uart_write('1'); |
paul@11 | 44 | else |
paul@11 | 45 | uart_write('0'); |
paul@11 | 46 | } |
paul@11 | 47 | |
paul@24 | 48 | void hex(uint32_t val, int bytes) |
paul@11 | 49 | { |
paul@11 | 50 | uint32_t mask; |
paul@24 | 51 | uint8_t digit, shift, start = bytes * 8 - 4; |
paul@11 | 52 | |
paul@24 | 53 | for (mask = (0b1111 << start), shift = start; mask; mask >>= 4, shift -= 4) |
paul@11 | 54 | { |
paul@11 | 55 | digit = (val & mask) >> shift; |
paul@11 | 56 | if (digit > 9) |
paul@11 | 57 | uart_write('A' + digit - 10); |
paul@11 | 58 | else |
paul@11 | 59 | uart_write('0' + digit); |
paul@11 | 60 | } |
paul@11 | 61 | } |
paul@11 | 62 | |
paul@19 | 63 | |
paul@19 | 64 | |
paul@19 | 65 | /* General input/output functions. */ |
paul@19 | 66 | |
paul@19 | 67 | int uart_can_read(int uart) |
paul@19 | 68 | { |
paul@19 | 69 | return REG(UART_REG(uart, UxSTA)) & 1; |
paul@19 | 70 | } |
paul@19 | 71 | |
paul@19 | 72 | char uart_read_char(int uart) |
paul@19 | 73 | { |
paul@19 | 74 | return (char) REG(UART_REG(uart, UxRXREG)); |
paul@19 | 75 | } |
paul@19 | 76 | |
paul@19 | 77 | int uart_can_write(int uart) |
paul@19 | 78 | { |
paul@19 | 79 | return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */ |
paul@19 | 80 | } |
paul@19 | 81 | |
paul@19 | 82 | void uart_write_char(int uart, char c) |
paul@19 | 83 | { |
paul@19 | 84 | while (!uart_can_write(uart)); /* busy loop */ |
paul@19 | 85 | |
paul@19 | 86 | REG(UART_REG(uart, UxTXREG)) = c; |
paul@19 | 87 | } |
paul@19 | 88 | |
paul@19 | 89 | |
paul@19 | 90 | |
paul@19 | 91 | /* Functions using UART1. */ |
paul@11 | 92 | |
paul@11 | 93 | void uart_write(char c) |
paul@11 | 94 | { |
paul@19 | 95 | uart_write_char(1, c); |
paul@11 | 96 | } |
paul@12 | 97 | |
paul@24 | 98 | void uart_write_nl(void) |
paul@24 | 99 | { |
paul@24 | 100 | uart_write('\r'); |
paul@24 | 101 | uart_write('\n'); |
paul@24 | 102 | } |
paul@24 | 103 | |
paul@12 | 104 | void uart_write_string(const char *s) |
paul@12 | 105 | { |
paul@12 | 106 | while (*s) |
paul@12 | 107 | uart_write(*s++); |
paul@12 | 108 | } |