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 #include "utils.h" 23 24 25 26 /* Register output functions using UART1. */ 27 28 void rbits(uint32_t reg) 29 { 30 bits(REG(reg), 4); 31 } 32 33 void rhex(uint32_t reg) 34 { 35 hex(REG(reg), 4); 36 } 37 38 /* Value output functions using UART1. */ 39 40 void bits(uint32_t val, int bytes) 41 { 42 uint32_t mask; 43 44 for (mask = (1 << 31); mask; mask >>= 1) 45 if (val & mask) 46 uart_write('1'); 47 else 48 uart_write('0'); 49 } 50 51 void hex(uint32_t val, int bytes) 52 { 53 uint32_t mask; 54 uint8_t digit, shift, start = bytes * 8 - 4; 55 56 for (mask = (0b1111 << start), shift = start; mask; mask >>= 4, shift -= 4) 57 { 58 digit = (val & mask) >> shift; 59 if (digit > 9) 60 uart_write('A' + digit - 10); 61 else 62 uart_write('0' + digit); 63 } 64 } 65 66 67 68 /* General input/output functions. */ 69 70 int uart_can_read(int uart) 71 { 72 return REG(UART_REG(uart, UxSTA)) & 1; 73 } 74 75 char uart_read_char(int uart) 76 { 77 return (char) REG(UART_REG(uart, UxRXREG)); 78 } 79 80 int uart_can_write(int uart) 81 { 82 return !(REG(UART_REG(uart, UxSTA)) & (1 << 9)); /* UTXBF (buffer full) */ 83 } 84 85 void uart_write_char(int uart, char c) 86 { 87 while (!uart_can_write(uart)); /* busy loop */ 88 89 REG(UART_REG(uart, UxTXREG)) = c; 90 } 91 92 93 94 /* Functions using UART1. */ 95 96 void uart_write(char c) 97 { 98 uart_write_char(1, c); 99 } 100 101 void uart_write_nl(void) 102 { 103 uart_write('\r'); 104 uart_write('\n'); 105 } 106 107 void uart_write_string(const char *s) 108 { 109 while (*s) 110 uart_write(*s++); 111 } 112 113 114 115 /* Blink an attached LED with delays implemented using a loop. */ 116 117 void blink(uint32_t delay, uint32_t port, uint32_t pins) 118 { 119 /* Clear outputs (LED). */ 120 121 CLR_REG(port, pins); 122 123 while (1) 124 { 125 wait(delay); 126 127 /* Invert outputs (LED). */ 128 129 INV_REG(port, pins); 130 } 131 }