|
|
| #include "mfp_memory_mapped_registers.h" |
| #include <stdint.h> |
| #include <mips/cpu.h> |
| #include "uart16550.h" |
|
|
| #define SIMULATION 0 |
| #define HARDWARE 1 |
|
|
| |
|
|
| #define RUNTYPE SIMULATION |
|
|
| |
|
|
| |
| |
| #define DIVISOR_50M (50*1000*1000 / (16*115200)) |
| #define DIVISOR_SIM 1 |
|
|
| #if RUNTYPE == SIMULATION |
| #define UART_DIVISOR DIVISOR_SIM |
| #elif RUNTYPE == HARDWARE |
| #define UART_DIVISOR DIVISOR_50M |
| #endif |
|
|
| void uartInit(uint16_t divisor) |
| { |
| MFP_UART_LCR = MFP_UART_LCR_8N1; |
| MFP_UART_LCR |= MFP_UART_LCR_LATCH; |
| MFP_UART_DLL = divisor & 0xFF; |
| MFP_UART_DLH = (divisor >> 8) & 0xff; |
| MFP_UART_LCR &= ~MFP_UART_LCR_LATCH; |
|
|
| MFP_UART_IER = MFP_UART_IER_RDA; |
| MFP_UART_FCR = MFP_UART_FCR_ITL4; |
| } |
|
|
| void uartTransmit(uint8_t data) |
| { |
| while (!(MFP_UART_LSR & MFP_UART_LSR_TFE)); |
| MFP_UART_TXR = data; |
| } |
|
|
| void receivedDataOutput(uint8_t data) |
| { |
| MFP_RED_LEDS = data; |
| MFP_GREEN_LEDS = data; |
| MFP_7_SEGMENT_HEX = data; |
| } |
|
|
| void uartReceive(void) |
| { |
| while (MFP_UART_LSR & MFP_UART_LSR_DR) |
| { |
| uint8_t data = MFP_UART_RXR; |
| receivedDataOutput(data); |
|
|
| #if RUNTYPE == HARDWARE |
| uartTransmit(data); |
| #endif |
| } |
| } |
|
|
| void mipsInterruptInit(void) |
| { |
| |
| mips32_bicsr (SR_BEV); |
| mips32_biscr (CR_IV); |
|
|
| uint32_t intCtl = mips32_getintctl(); |
| mips32_setintctl(intCtl | INTCTL_VS_32); |
| |
|
|
| mips32_bissr (SR_IE | SR_HINT3); |
| } |
|
|
| |
| void __attribute__ ((interrupt, keep_interrupts_masked)) __mips_isr_hw3 () |
| { |
| |
| if(MFP_UART_IIR & MFP_UART_IIR_RDA) |
| uartReceive(); |
| } |
|
|
| void uartWrite(const char str[]) |
| { |
| while(*str) |
| uartTransmit(*str++); |
| } |
|
|
| int main () |
| { |
| const uint16_t uartDivisor = UART_DIVISOR; |
|
|
| uartInit(uartDivisor); |
| mipsInterruptInit(); |
|
|
| |
| uartWrite("Hello!"); |
|
|
| |
| while(1); |
| } |
|
|