1 /* 2 * Some simple debugging functions. 3 * 4 * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "pic32_c.h" 21 #include "debug.h" 22 23 /* Register output functions using UART1. */ 24 25 void rbits(uint32_t reg) 26 { 27 bits(REG(reg), 4); 28 } 29 30 void rhex(uint32_t reg) 31 { 32 hex(REG(reg), 4); 33 } 34 35 /* Value output functions using UART1. */ 36 37 void bits(uint32_t val, int bytes) 38 { 39 uint32_t mask; 40 41 for (mask = (1 << 31); mask; mask >>= 1) 42 if (val & mask) 43 uart_write('1'); 44 else 45 uart_write('0'); 46 } 47 48 void hex(uint32_t val, int bytes) 49 { 50 uint32_t mask; 51 uint8_t digit, shift, start = bytes * 8 - 4; 52 53 for (mask = (0b1111 << start), shift = start; mask; mask >>= 4, shift -= 4) 54 { 55 digit = (val & mask) >> shift; 56 if (digit > 9) 57 uart_write('A' + digit - 10); 58 else 59 uart_write('0' + digit); 60 } 61 } 62 63 64 65 /* General input/output functions. */ 66 67 int uart_can_read(int uart) 68 { 69 return REG(UART_REG(uart, UxSTA)) & 1; 70 } 71 72 char uart_read_char(int uart) 73 { 74 return (char) REG(UART_REG(uart, UxRXREG)); 75 } 76 77 int uart_can_write(int uart) 78 { 79 return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */ 80 } 81 82 void uart_write_char(int uart, char c) 83 { 84 while (!uart_can_write(uart)); /* busy loop */ 85 86 REG(UART_REG(uart, UxTXREG)) = c; 87 } 88 89 90 91 /* Functions using UART1. */ 92 93 void uart_write(char c) 94 { 95 uart_write_char(1, c); 96 } 97 98 void uart_write_nl(void) 99 { 100 uart_write('\r'); 101 uart_write('\n'); 102 } 103 104 void uart_write_string(const char *s) 105 { 106 while (*s) 107 uart_write(*s++); 108 }