From bc163529ee6f2aa8ba3ea60047a8471b6d81e1c9 Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sun, 14 Jul 2019 00:13:52 +0800 Subject: [PATCH] basic uart functionality (not tested) --- include/mmap.h | 28 +++++++++++----------------- main.c | 3 ++- systick.c | 3 ++- uart.c | 39 +++++++++++++++++++++++++++------------ 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/include/mmap.h b/include/mmap.h index 7caee1d..604579b 100644 --- a/include/mmap.h +++ b/include/mmap.h @@ -21,25 +21,19 @@ /* SYSTEM CONTROL REGISTER */ -#define SYSCTRL_RCC ((volatile unsigned long *)(0x400FE060)) +#define SYSCTRL_RCC ((volatile unsigned long *)(0x40021000)) +#define RCC_APB2ENR ((volatile unsigned long *)(0x40021018)) // register to enable USART1 + #define SYSCTRL_RIS ((volatile unsigned long *)(0x400FE050)) #define SYSCTRL_RCGC1 ((volatile unsigned long *)(0x400FE104)) #define SYSCTRL_RCGC2 ((volatile unsigned long *)(0x400FE108)) #define GPIOPA_AFSEL ((volatile unsigned long *)(0x40004420)) -/* USART REGISTERS */ -#define UART0_DATA ((volatile unsigned long *)(0x4000C000)) -#define UART0_FLAG ((volatile unsigned long *)(0x4000C018)) -#define UART0_IBRD ((volatile unsigned long *)(0x4000C024)) -#define UART0_FBRD ((volatile unsigned long *)(0x4000C028)) -#define UART0_LCRH ((volatile unsigned long *)(0x4000C02C)) -#define UART0_CTRL ((volatile unsigned long *)(0x4000C030)) -#define UART0_RIS ((volatile unsigned long *)(0x4000C03C)) - -#define UART1_DATA ((volatile unsigned long *)(0x4000D000)) -#define UART1_FLAG ((volatile unsigned long *)(0x4000D018)) -#define UART1_IBRD ((volatile unsigned long *)(0x4000D024)) -#define UART1_FBRD ((volatile unsigned long *)(0x4000D028)) -#define UART1_LCRH ((volatile unsigned long *)(0x4000D02C)) -#define UART1_CTRL ((volatile unsigned long *)(0x4000D030)) -#define UART1_RIS ((volatile unsigned long *)(0x4000D03C)) +#define GPIOA_CRH ((volatile unsigned long *)(0x40010804)) +#define AFIO_EVCR ((volatile unsigned long *)(0x40010000)) + +#define USART1_SR ((volatile unsigned long *)(0x40013800)) +#define USART1_DR ((volatile unsigned long *)(0x40013804)) +#define USART1_BRR ((volatile unsigned long *)(0x40013808)) +#define USART1_CR1 ((volatile unsigned long *)(0x4001380C)) +#define USART1_CR3 ((volatile unsigned long *)(0x40013814)) diff --git a/main.c b/main.c index 58e4771..5453cbd 100644 --- a/main.c +++ b/main.c @@ -41,11 +41,12 @@ int strlen(const char *str) void main() { - //uart_init(); //uart_puts("LOADING SYSTEM...\n"); ivt_init(); + uart_init(); systick_init(); + uart_puts("WOGSYS LOADING..."); // asm("cpsie i"); // enable irq , cpsied f (disable faukts( // loop diff --git a/systick.c b/systick.c index b9c8756..777d04d 100644 --- a/systick.c +++ b/systick.c @@ -7,7 +7,8 @@ void * systick_handler() { - *RANDOM_ADDR = (volatile uint32_t) 0x10101010 ; +// *RANDOM_ADDR = (volatile uint32_t) 0x10101010 ; +// uart_puts("TEST"); } void systick_init() { diff --git a/uart.c b/uart.c index bfef4b4..20a8d5e 100644 --- a/uart.c +++ b/uart.c @@ -6,10 +6,25 @@ void uart_init() { -/* To use the UARTs, the peripheral clock must be enabled by setting the UART0, UART1, or UART2 -bits in the RCGC1 register. (section 12.4: Initialization and Configuration */ + // enable clock to UART1 + *RCC_APB2ENR = 0x4005; + *GPIOA_CRH = 0x444444D4; + *AFIO_EVCR = 0x89; + + *USART1_CR1 = (0 << 13); // disable control register + *USART1_CR3 = 0xC0; + + /* baud rate 9600 + * 115200 = 8MHz / (16 * USARTDIV) + * USARTDIV = 4.34 + * FRACTION: 16 x 0.34 = 0d5.44 0d5 -> 0x5 + * MANTISSA: 0d4.34 0d4 -> 0x4 + * USART_BRR = 0x45*/ + + *USART1_BRR = 0x00000045; -*SYSCTRL_RCGC1 = *SYSCTRL_RCGC1 | 0x0003; + /* parity = 8 bit, UART1 enabled, TX and RX enabled, interrupts enabled */ + *USART1_CR1 = (volatile uint32_t) 0x000030AC; /* * Configure UART0: @@ -22,7 +37,7 @@ bits in the RCGC1 register. (section 12.4: Initialization and Configuration */ /* TODO: bitrate: How fast is CPU running?*/ -*UART0_CTRL = 0; +/* *UART0_CTRL = 0; *UART0_IBRD = 27; *UART0_FBRD = 9; *UART0_LCRH = 0x60; @@ -33,19 +48,19 @@ bits in the RCGC1 register. (section 12.4: Initialization and Configuration */ *UART1_FBRD = 9; *UART1_LCRH = 0x60; *UART1_CTRL = 0x301; - +*/ } extern void uart_putc(unsigned char ch) { - if (ch == '\n') { - while ((*UART0_FLAG & 0x8)); // busy bit - *UART0_DATA = 0x0D; // return line - } - - while ((*UART0_FLAG & 0x8)); // busy bit - *UART0_DATA = ch; +// if (ch == '\n') { +// while ((*UART1_DR & 0x8)); // busy bit +// *USART1_DR = 0x0D; // return line +// } +// +// while ((*UART0_FLAG & 0x8)); // busy bit + *USART1_DR = ch; } extern void uart_puts(unsigned char *str) -- 2.7.4