7 #define RXNE ((*USART1_SR >> 5) & 0x1)
13 void * uart_handler() {
15 uart_puts("\n echo: ");
17 char echochar = *USART1_DR;
18 // regw_u8(USART1_DR, echochar, 0, O_WRITE);
26 // global interrupt setup
27 // regw_u32(EXTI_IMR, 0x000FFFFF, 0, O_WRITE);
28 // regw_u32(EXTI_RTSR, 0x000FFFFF, 0, O_WRITE);
31 regw_u32(RCC_APB2ENR, 0x4005, 0, SET);// enable clock to UART1, AFIO and GPIOA
33 /* (after enable GPIOA), on PA9&PA10 and set mode
34 * to alternative output */
35 regw_u32(GPIOA_CRH, 0x444444D4, 0, O_WRITE);
36 regw_u8(AFIO_EVCR, 0x89, 0, O_WRITE);// set event control register, output on PA, Pin 9
38 //disable temporarily to set values
39 regw_u8(USART1_CR1, 0x0, 13, SET);
41 /* baud rate 115200, 8MHz / (16 * USARTDIV)
43 * FRACTION: 16 x 0.34 = 0d5.44 0d5 -> 0x5
44 * MANTISSA: 0d4.34 0d4 -> 0x4
47 regw_u32(USART1_BRR, 0x00000045, 0, O_WRITE);
48 regw_u32(USART1_CR2, 0x0000, 0, O_WRITE); //set stop bit, default is 1 stop bit 0x00
50 /* parity = 8 bit, UART1 enabled,
51 * TX and RX enabled, interrupts enabled */
52 //regw_u32(USART1_CR1, 0x000030AC, 0, O_WRITE);
53 regw_u32(USART1_CR1, 0x0000302C, 0, O_WRITE);
55 ivt_set_gate(53, uart_handler, 0);
57 *NVIC_ISER1 = (1 << 5); // Enable UART interrupt at NVIC
61 for (int i = 0; i < 100; i++);
64 extern void uart_putc(unsigned char ch) {
67 while (*USART1_SR & 0x0C) { } // transmit data register empty and complete
68 regw_u8(USART1_DR, 0x0D, 0, O_WRITE); // return line
71 while (*USART1_SR & 0x0C) {}
72 regw_u8(USART1_DR, ch, 0, O_WRITE);
77 extern void uart_puts(unsigned char *str) {
79 for (i = 0; i < strlen(str); i++) {
88 /* while (buffer not empty)
90 * uart_putc(ch) // echo