1
0

uart.c 3.1 KB

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