6 #include <sys/robsys.h>
8 #include <lib/regfunc.h>
9 #include <lib/string.h>
11 #include <drivers/uart.h>
24 void * uart_handler() {
26 while (rchkbit(USART1_SR, 5)) {
27 char echochar = *USART1_DR;
28 linefeed.buf[linefeed.wpos++] = echochar;
29 if (linefeed.wpos == UARTBUF)
40 //memset(&linefeed, 0, (sizeof(struct linefeed) ));
41 //rsetbitsfrom(RCC_APB2ENR, 0, 0x4005);
42 rsetbit(RCC_APB2ENR, 0);
43 rsetbit(RCC_APB2ENR, 2);
44 rsetbit(RCC_APB2ENR, 14);
46 /* (after enable GPIOA), on PA9&PA10 and set mode
47 * to alternative output */
48 rwrite(GPIOA_CRH, 0x444444D4);
49 // set event control register, output on PA, Pin 9 TODO: check
50 rsetbitsfrom(AFIO_EVCR, 0, 0x89);
52 //disable temporarily to set values
53 rclrbit(USART1_CR1, 13);
57 //set stop bit, default is 1 stop bit 0x00
58 rwrite(USART1_CR2, 0x0000);
60 /* parity = 8 bit, UART1 enabled,
61 * TX and RX enabled, interrupts enabled */
62 rwrite(USART1_CR1, 0x0000302C);
64 ivt_set_gate(53, uart_handler, 0);
66 rsetbit(NVIC_ISER1, 5);
69 void uart_putc(unsigned char ch) {
72 while(!rchkbit(USART1_SR, 6));
73 regw_u8(USART1_DR, 0x0D, 0, OWRITE); // return line
75 while(!rchkbit(USART1_SR, 6));
76 regw_u8(USART1_DR, ch, 0, OWRITE);
79 char uart_getc(void) {
82 if (linefeed.rpos != linefeed.wpos) {
83 c = linefeed.buf[linefeed.rpos++];
84 if (linefeed.rpos == UARTBUF)
91 /* Calculate baud rate. Example how this is done
92 * to set this register on a stm32
93 * Desired baudrate: 115200, CLK: 8 MHz
94 * Desired Baudrate = CLK / (16 * USARTDIV)
96 * FRACTION: 16 x 0.34 = 0d5.44 0d5 -> 0x5
97 * MANTISSA: 0d4.34 0d4 -> 0x4
100 void set_baudrate() {
102 // rwrite(USART1_BRR, 0x000001A1); 48 MHZ
103 // rwrite(USART1_BRR, 0x0000022B); 64 MHz
104 // rwrite(USART1_BRR, 0x00000138); 36 MHz
105 // rwrite(USART1_BRR, 0x00000271); 72 MHz
107 rwrite(USART1_BRR, 0x00000138);
109 rwrite(USART1_BRR, 0x00000045);