drv_uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * File : drv_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2017, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-09-19 Quintin.Z the first version
  13. */
  14. #include <rtdevice.h>
  15. #include <board.h>
  16. #include "drv_uart.h"
  17. #include "nv32.h"
  18. #include "uart.h"
  19. #include "sim.h"
  20. /* NV32 uart driver */
  21. struct nv32_uart
  22. {
  23. UART_Type* uart_device;
  24. IRQn_Type irq;
  25. };
  26. static rt_err_t nv32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  27. {
  28. struct nv32_uart* uart;
  29. UART_ConfigBaudrateType uart_config;
  30. RT_ASSERT(serial != RT_NULL);
  31. RT_ASSERT(cfg != RT_NULL);
  32. uart = (struct nv32_uart *)serial->parent.user_data;
  33. uart_config.u32SysClkHz = BUS_CLK_HZ;
  34. uart_config.u32Baudrate = cfg->baud_rate;
  35. UART_SetBaudrate(uart->uart_device, &uart_config);
  36. if (cfg->data_bits == DATA_BITS_8)
  37. {
  38. UART_Set8BitMode(uart->uart_device);
  39. }
  40. else if(cfg->data_bits == DATA_BITS_9)
  41. {
  42. UART_Set9BitMode(uart->uart_device);
  43. }
  44. if (cfg->stop_bits == STOP_BITS_1)
  45. {
  46. uart->uart_device->BDH &= (~UART_BDH_SBNS_MASK);
  47. }
  48. else if (cfg->stop_bits == STOP_BITS_2)
  49. {
  50. uart->uart_device->BDH |= UART_BDH_SBNS_MASK;
  51. }
  52. /* Enable receiver and transmitter */
  53. uart->uart_device->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
  54. UART_EnableInterrupt(UART0, UART_RxBuffFullInt);
  55. NVIC_EnableIRQ(UART0_IRQn);
  56. return RT_EOK;
  57. }
  58. static rt_err_t nv32_control(struct rt_serial_device *serial, int cmd, void *arg)
  59. {
  60. struct nv32_uart* uart;
  61. RT_ASSERT(serial != RT_NULL);
  62. uart = (struct nv32_uart *)serial->parent.user_data;
  63. switch (cmd)
  64. {
  65. case RT_DEVICE_CTRL_CLR_INT:
  66. /* disable rx irq */
  67. NVIC_DisableIRQ(uart->irq);
  68. break;
  69. case RT_DEVICE_CTRL_SET_INT:
  70. /* enable rx irq */
  71. NVIC_EnableIRQ(uart->irq);
  72. break;
  73. }
  74. return RT_EOK;
  75. }
  76. static int nv32_putc(struct rt_serial_device *serial, char c)
  77. {
  78. struct nv32_uart* uart;
  79. RT_ASSERT(serial != RT_NULL);
  80. uart = (struct nv32_uart *)serial->parent.user_data;
  81. while (!(uart->uart_device->S1 & UART_S1_TDRE_MASK));
  82. uart->uart_device->D = (uint8_t)c;
  83. return 1;
  84. }
  85. static int nv32_getc(struct rt_serial_device *serial)
  86. {
  87. int ch;
  88. struct nv32_uart* uart;
  89. RT_ASSERT(serial != RT_NULL);
  90. uart = (struct nv32_uart *)serial->parent.user_data;
  91. ch = -1;
  92. if (uart->uart_device->S1 & UART_S1_RDRF_MASK)
  93. {
  94. ch = uart->uart_device->D;
  95. }
  96. return ch;
  97. }
  98. static const struct rt_uart_ops nv32_uart_ops =
  99. {
  100. nv32_configure,
  101. nv32_control,
  102. nv32_putc,
  103. nv32_getc,
  104. };
  105. #ifdef RT_USING_UART0
  106. struct nv32_uart uart0 =
  107. {
  108. UART0,
  109. UART0_IRQn,
  110. };
  111. struct rt_serial_device serial0;
  112. void UART0_IRQHandler(void)
  113. {
  114. /* enter interrupt */
  115. rt_interrupt_enter();
  116. if(UART0->S1 & UART_S1_RDRF_MASK)
  117. {
  118. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  119. }
  120. /* leave interrupt */
  121. rt_interrupt_leave();
  122. }
  123. #endif
  124. void rt_hw_uart_init(void)
  125. {
  126. struct nv32_uart* uart;
  127. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  128. #ifdef RT_USING_UART0
  129. uart = &uart0;
  130. serial0.ops = &nv32_uart_ops;
  131. serial0.config = config;
  132. SIM->PINSEL |= SIM_PINSEL_UART0PS_MASK;
  133. SIM->SCGC |= SIM_SCGC_UART0_MASK;
  134. uart->uart_device->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
  135. /* Configure the UART for 8-bit mode, no parity */
  136. uart->uart_device->C1 = 0;
  137. rt_hw_serial_register(&serial0, "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  138. #endif
  139. }