6 #include <sys/robsys.h>
8 #include <lib/regfunc.h>
9 #include <lib/string.h>
11 #include <drivers/uart.h>
13 #define RXNE ((*USART1_SR >> 5) & 0x1)
24 void * uart_handler() {
26 //uart_puts("echo: ");
28 char echochar = *USART1_DR;
29 // uart_putc(echochar);
30 linefeed.buf[linefeed.wpos++] = echochar;
31 if (linefeed.wpos == UARTBUF)
33 //regw_u32(USART1_DR, echochar, 0, O_WRITE);
44 //memset(&linefeed, 0, (sizeof(struct linefeed) ));
45 regw_u32(RCC_APB2ENR, 0x4005, 0, SETBIT);// enable clock to UART1, AFIO and GPIOA
47 /* (after enable GPIOA), on PA9&PA10 and set mode
48 * to alternative output */
49 regw_u32(GPIOA_CRH, 0x444444D4, 0, OWRITE);
50 regw_u8(AFIO_EVCR, 0x89, 0, OWRITE);// set event control register, output on PA, Pin 9
52 //disable temporarily to set values
53 regw_u8(USART1_CR1, 0x0, 13, SETBIT);
55 /* baud rate 115200, 8MHz / (16 * USARTDIV)
57 * FRACTION: 16 x 0.34 = 0d5.44 0d5 -> 0x5
58 * MANTISSA: 0d4.34 0d4 -> 0x4
61 regw_u32(USART1_BRR, 0x00000045, 0, OWRITE);
62 regw_u32(USART1_CR2, 0x0000, 0, OWRITE); //set stop bit, default is 1 stop bit 0x00
64 /* parity = 8 bit, UART1 enabled,
65 * TX and RX enabled, interrupts enabled */
66 //regw_u32(USART1_CR1, 0x000030AC, 0, O_WRITE);
67 regw_u32(USART1_CR1, 0x0000302C, 0, OWRITE);
69 ivt_set_gate(53, uart_handler, 0);
71 *NVIC_ISER1 = (1 << 5); // Enable UART interrupt at NVIC
75 for (int i = 0; i < 100; i++);
78 extern void uart_putc(unsigned char ch) {
81 while (*USART1_SR & 0x0C) { } // transmit data register empty and complete
82 regw_u8(USART1_DR, 0x0D, 0, OWRITE); // return line
85 while (*USART1_SR & 0x0C) {}
86 regw_u8(USART1_DR, ch, 0, OWRITE);
91 char uart_getc(void) {
94 if (linefeed.rpos != linefeed.wpos) {
95 c = linefeed.buf[linefeed.rpos++];
96 if (linefeed.rpos == UARTBUF)
105 extern void uart_puts(unsigned char *str) {
107 for (i = 0; i < strlen(str); i++) {