1 #include <rthw.h>
2 #include <rtthread.h>
3 
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 
9 #include "system.h"
10 #include "sys/alt_irq.h"
11 #include "altera_avalon_uart_regs.h"
12 
13 extern int alt_irq_register (alt_u32 id,
14                              void*   context,
15                              void (*alt_isr_func)(void* isr_context, alt_u32 id) );
16 
set_baudrate(unsigned int baudrate)17 static void set_baudrate(unsigned int baudrate)
18 {
19     IOWR_ALTERA_AVALON_UART_DIVISOR(RS232_BASE,
20                                     (unsigned int)(ALT_CPU_FREQ/baudrate+0.5) );
21 }
22 
23 /********* rt-thread *********/
24 #include <rtthread.h>
25 
26 struct rt_device   uart_device;
27 uint8_t     rx_buf[100];
28 uint32_t    rx_put_index;
29 uint32_t    rx_get_index;
30 
rt_uart_init(rt_device_t dev)31 static rt_err_t rt_uart_init (rt_device_t dev)
32 {
33     set_baudrate(115200);
34 
35     IOWR_ALTERA_AVALON_UART_CONTROL(RS232_BASE, 0x80);//接收中断使能
36     IOWR_ALTERA_AVALON_UART_STATUS(RS232_BASE, 0x0); // clean status
37 
38     rx_put_index = 0;
39     rx_get_index = 0;
40 
41     return RT_EOK;
42 }
43 
rt_uart_open(rt_device_t dev,rt_uint16_t oflag)44 static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag)
45 {
46     return RT_EOK;
47 }
48 
rt_uart_close(rt_device_t dev)49 static rt_err_t rt_uart_close(rt_device_t dev)
50 {
51     return RT_EOK;
52 }
53 
rt_uart_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)54 static rt_ssize_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
55 {
56     if( rx_get_index )
57     {
58         *(uint8_t *)buffer = rx_buf[0];
59         rx_get_index--;
60         return size;
61     }
62     return 0;
63 }
64 
rt_uart_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)65 static rt_ssize_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
66 {
67     const char * write_point = buffer;
68     while(size--)
69     {
70         if(*write_point == '\n')
71         {
72             IOWR_ALTERA_AVALON_UART_TXDATA(RS232_BASE,'\r');
73             while( !(IORD_ALTERA_AVALON_UART_STATUS(RS232_BASE)&(1<<6)) ); // status bit6 : TRDY
74         }
75         IOWR_ALTERA_AVALON_UART_TXDATA(RS232_BASE,*write_point);
76         write_point++;
77         while( !(IORD_ALTERA_AVALON_UART_STATUS(RS232_BASE)&(1<<6)) ); // status bit6 : TRDY
78     }
79 
80     return size;
81 }
82 
rt_uart_control(rt_device_t dev,int cmd,void * args)83 static rt_err_t rt_uart_control(rt_device_t dev, int cmd, void *args)
84 {
85     return RT_EOK;
86 }
87 
uart_isr(void * context,alt_u32 id)88 static void uart_isr(void * context,alt_u32 id)
89 {
90     rx_buf[rx_get_index] = IORD_ALTERA_AVALON_UART_RXDATA(RS232_BASE);
91     rx_get_index++;
92     if (uart_device.rx_indicate != RT_NULL)
93     {
94         uart_device.rx_indicate(&uart_device, 1);
95     }
96 }
97 
rt_hw_uart_init(void)98 void rt_hw_uart_init(void)
99 {
100     // init uart
101     set_baudrate(115200);
102     IOWR_ALTERA_AVALON_UART_CONTROL(RS232_BASE, 0x80);//接收中断使能
103     IOWR_ALTERA_AVALON_UART_STATUS(RS232_BASE, 0x0); // clean status
104     alt_irq_register(RS232_IRQ, NULL, uart_isr);
105 
106     // register device
107     uart_device.type = RT_Device_Class_Char;
108     /* device interface */
109     uart_device.init        = rt_uart_init;
110     uart_device.open        = rt_uart_open;
111     uart_device.close       = rt_uart_close;
112     uart_device.read        = rt_uart_read;
113     uart_device.write       = rt_uart_write;
114     uart_device.control     = rt_uart_control;
115 
116     uart_device.user_data   = RT_NULL;
117     uart_device.rx_indicate = RT_NULL;
118     uart_device.tx_complete = RT_NULL;
119 
120     rt_device_register(&uart_device,
121                        "uart", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX);
122 }
123