/* 3. Map UART0 to GPIO pins * * Goal: Set GPIO14, 15 to ALT0 * GPIO 0-9 => GPFSEL0 * GPIO 10-19 => GPFSEL1 * GPIO 20-29 => GPFSEL2 * ... * GPIO14-15 use FSEL4 and FSEL5 field in GPFSEL1. */ registerunsignedint r; r = *GPFSEL1; r &= ~((7 << 12) | (7 << 15)); r |= (4 << 12) | (4 << 15); // ALT0 = 100 *GPFSEL1 = r;
/* 4. Disable GPIO pull-up/down */ *GPPUD = 0; // Wait 150 cycles (required set-up time for the control signal) r = 150; while (r--) { asmvolatile("nop"); } *GPPUDCLK0 = (1 << 14) | (1 << 15); // Wait 150 cycles (required hold time for the control signal) r = 150; while (r--) { asmvolatile("nop"); } *GPPUDCLK0 = 0; // Flush GPIO setup
charuart_getc() { /* wait until something is in the buffer */ do { asmvolatile("nop"); } while (*UART0_FR & 0x10); char c = (char)(*UART0_DR); return c == '\r' ? '\n' : c; }
Write
Check FR
Write to DR
1 2 3 4 5 6 7
voiduart_putc(unsignedint c) { /* Wait until we can send */ do { asmvolatile("nop"); } while (*UART0_FR & 0x20); *UART0_DR = c; }