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@19 | 23 | /* Value output functions using UART1. */ |
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@19 | 62 | |
paul@19 | 63 | |
paul@19 | 64 | /* General input/output functions. */ |
paul@19 | 65 | |
paul@19 | 66 | int uart_can_read(int uart) |
paul@19 | 67 | { |
paul@19 | 68 | return REG(UART_REG(uart, UxSTA)) & 1; |
paul@19 | 69 | } |
paul@19 | 70 | |
paul@19 | 71 | char uart_read_char(int uart) |
paul@19 | 72 | { |
paul@19 | 73 | return (char) REG(UART_REG(uart, UxRXREG)); |
paul@19 | 74 | } |
paul@19 | 75 | |
paul@19 | 76 | int uart_can_write(int uart) |
paul@19 | 77 | { |
paul@19 | 78 | return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */ |
paul@19 | 79 | } |
paul@19 | 80 | |
paul@19 | 81 | void uart_write_char(int uart, char c) |
paul@19 | 82 | { |
paul@19 | 83 | while (!uart_can_write(uart)); /* busy loop */ |
paul@19 | 84 | |
paul@19 | 85 | REG(UART_REG(uart, UxTXREG)) = c; |
paul@19 | 86 | } |
paul@19 | 87 | |
paul@19 | 88 | |
paul@19 | 89 | |
paul@19 | 90 | /* Functions using UART1. */ |
paul@11 | 91 | |
paul@11 | 92 | void uart_write(char c) |
paul@11 | 93 | { |
paul@19 | 94 | uart_write_char(1, c); |
paul@11 | 95 | } |
paul@12 | 96 | |
paul@12 | 97 | void uart_write_string(const char *s) |
paul@12 | 98 | { |
paul@12 | 99 | while (*s) |
paul@12 | 100 | uart_write(*s++); |
paul@12 | 101 | } |