drv_uart.c 3.6 KB

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