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