Set AUX_MU_CNTL_REG to 3. Enable the transmitter and receiver.
Read
Check AUX_MU_LSR_REG’s data ready field.
If set, read from AUX_MU_IO_REG
Write
Check AUX_MU_LSR_REG’s Transmitter empty field.
If set, write to AUX_MU_IO_REG
Map UART to GPIO pins
These registers must be used in conjunction with the GPPUD register to effect GPIO Pull-up/down changes. The following sequence of events is required:
Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither to remove the current Pull-up/down)
Wait 150 cycles – this provides the required set-up time for the control signal
Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to modify – NOTE only the pads which receive a clock will be modified, all others will retain their previous state.
Wait 150 cycles – this provides the required hold time for the control signal
#include"aux.h"#include"gpio.h"voiduart_init() {
/* Initialize UART */*AUX_ENABLES |=1; // Enable mini UART
*AUX_MU_CNTL =0; // Disable TX, RX during configuration
*AUX_MU_IER =0; // Disable interrupt
*AUX_MU_LCR =3; // Set the data size to 8 bit
*AUX_MU_MCR =0; // Don't need auto flow control
*AUX_MU_BAUD =270; // Set baud rate to 115200
*AUX_MU_IIR =6; // No FIFO
/* Map UART to GPIO Pins */// 1. Change GPIO 14, 15 to alternate function
registerunsignedint r =*GPFSEL1;
r &=~((7<<12) | (7<<15)); // Reset GPIO 14, 15
r |= (2<<12) | (2<<15); // Set ALT5
*GPFSEL1 = r;
// 2. Disable GPIO pull up/down (Because these GPIO pins use alternate functions, not basic input-output)
// Set control signal to disable
*GPPUD =0;
// Wait 150 cycles
r =150;
while (r--) {
asmvolatile("nop");
}
// Clock the control signal into the GPIO pads
*GPPUDCLK0 = (1<<14) | (1<<15);
// Wait 150 cycles
r =150;
while (r--) {
asmvolatile("nop");
}
// Remove the clock
*GPPUDCLK0 =0;
// 3. Enable TX, RX
*AUX_MU_CNTL =3;
}
charuart_read() {
// Check data ready field
do {
asmvolatile("nop");
} while (!(*AUX_MU_LSR &0x01));
// Read
char r = (char)(*AUX_MU_IO);
// Convert carrige return to newline
return r =='\r'?'\n': r;
}
voiduart_write(unsignedint c) {
// Check transmitter idle field
do {
asmvolatile("nop");
} while (!(*AUX_MU_LSR &0x20));
// Write
*AUX_MU_IO = c;
}
main.c
1
2
3
4
5
6
7
8
9
#include"uart.h"intmain() {
uart_init();
while (1) {
uart_write(uart_read());
}
}
Registers
GPFSELn
GPIO Function Select Registers
The function select registers are used to define the operation of the general-purpose I/O pins.
The GPIO Pull-up/down Register controls the actuation of the internal pull-up/down control line to ALL the GPIO pins. This register must be used in conjunction with the 2 GPPUDCLKn registers.
The AUX_MU_IER_REG register is primary used to enable interrupts
If the DLAB bit in the line control register is set this register gives access to the MS 8 bits of the baud rate