drv_uart.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "stdio.h"
  2. #include "xparameters.h"
  3. #include "xscugic.h"
  4. #include "xuartps.h"
  5. #include "board.h"
  6. #include <rtdevice.h>
  7. #include <rthw.h>
  8. struct hw_uart_device
  9. {
  10. XUartPs uart;
  11. rt_uint32_t irqno;
  12. uint16_t dev_id;
  13. u32 base_addr;
  14. rt_bool_t inited;
  15. };
  16. #ifdef BSP_USING_UART0
  17. static struct hw_uart_device _uart_device0 = {
  18. .dev_id = XPAR_PS7_UART_0_DEVICE_ID,
  19. .irqno = XPAR_XUARTPS_0_INTR,
  20. .base_addr = XPAR_PS7_UART_0_BASEADDR,
  21. .inited = RT_FALSE,
  22. };
  23. #endif
  24. #ifdef BSP_USING_UART1
  25. static struct hw_uart_device _uart_device1 = {
  26. .dev_id = XPAR_PS7_UART_1_DEVICE_ID,
  27. .irqno = XPAR_XUARTPS_1_INTR,
  28. .base_addr = XPAR_PS7_UART_1_BASEADDR,
  29. .inited = RT_FALSE,
  30. };
  31. #endif
  32. rt_inline rt_err_t _uart_init(struct hw_uart_device *device)
  33. {
  34. XUartPs_Config *uart_cfg = XUartPs_LookupConfig(device->dev_id);
  35. if (NULL == uart_cfg)
  36. return XST_FAILURE;
  37. int status = XUartPs_CfgInitialize(&device->uart, uart_cfg,
  38. uart_cfg->BaseAddress);
  39. if (status != XST_SUCCESS)
  40. return XST_FAILURE;
  41. status = XUartPs_SelfTest(&device->uart);
  42. if (status != XST_SUCCESS)
  43. return XST_FAILURE;
  44. XUartPs_SetOperMode(&device->uart, XUARTPS_OPER_MODE_NORMAL);
  45. XUartPs_SetFifoThreshold(&device->uart, 1);
  46. return RT_EOK;
  47. }
  48. rt_inline void _uart_set_fmt(struct hw_uart_device *device,
  49. struct serial_configure *cfg)
  50. {
  51. XUartPsFormat uart_format;
  52. uart_format.BaudRate = cfg->baud_rate;
  53. switch (cfg->data_bits)
  54. {
  55. case 7:
  56. uart_format.DataBits = XUARTPS_FORMAT_7_BITS;
  57. break;
  58. case 6:
  59. uart_format.DataBits = XUARTPS_FORMAT_6_BITS;
  60. break;
  61. default: /* 8 */
  62. uart_format.DataBits = XUARTPS_FORMAT_8_BITS;
  63. break;
  64. }
  65. // cfg->bit_order ?
  66. switch (cfg->parity)
  67. {
  68. case PARITY_ODD:
  69. uart_format.Parity = XUARTPS_FORMAT_ODD_PARITY;
  70. break;
  71. case PARITY_EVEN:
  72. uart_format.Parity = XUARTPS_FORMAT_EVEN_PARITY;
  73. break;
  74. default: /** None */
  75. uart_format.Parity = XUARTPS_FORMAT_NO_PARITY;
  76. break;
  77. }
  78. switch (cfg->stop_bits)
  79. {
  80. case STOP_BITS_2:
  81. uart_format.StopBits = XUARTPS_FORMAT_2_STOP_BIT;
  82. break;
  83. default:
  84. uart_format.StopBits = XUARTPS_FORMAT_1_STOP_BIT;
  85. break;
  86. }
  87. XUartPs_SetDataFormat(&device->uart, &uart_format);
  88. }
  89. static rt_err_t uart_configure(struct rt_serial_device *serial,
  90. struct serial_configure *cfg)
  91. {
  92. RT_ASSERT(serial != RT_NULL);
  93. RT_ASSERT(cfg != RT_NULL);
  94. struct hw_uart_device *device
  95. = (struct hw_uart_device *)serial->parent.user_data;
  96. RT_ASSERT(device != RT_NULL);
  97. if (!device->inited)
  98. {
  99. int err = _uart_init(device);
  100. if (err != RT_EOK)
  101. return err;
  102. device->inited = RT_TRUE;
  103. }
  104. _uart_set_fmt(device, cfg);
  105. return RT_EOK;
  106. }
  107. static void rt_hw_uart_isr(int irqno, void *param)
  108. {
  109. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  110. struct hw_uart_device *device
  111. = (struct hw_uart_device *)serial->parent.user_data;
  112. u32 isr_status;
  113. isr_status
  114. = XUartPs_ReadReg(device->uart.Config.BaseAddress, XUARTPS_IMR_OFFSET);
  115. isr_status &= XUartPs_ReadReg(device->uart.Config.BaseAddress,
  116. XUARTPS_ISR_OFFSET);
  117. if (isr_status & (u32)XUARTPS_IXR_RXOVR)
  118. {
  119. XUartPs_WriteReg(device->uart.Config.BaseAddress, XUARTPS_ISR_OFFSET,
  120. XUARTPS_IXR_RXOVR);
  121. }
  122. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  123. }
  124. static rt_err_t
  125. uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  126. {
  127. struct hw_uart_device *pdev;
  128. RT_ASSERT(serial != RT_NULL);
  129. pdev = serial->parent.user_data;
  130. switch (cmd)
  131. {
  132. case RT_DEVICE_CTRL_CLR_INT:
  133. break;
  134. case RT_DEVICE_CTRL_SET_INT:
  135. rt_hw_interrupt_install(pdev->irqno, rt_hw_uart_isr, (void *)serial,
  136. serial->parent.parent.name);
  137. rt_hw_interrupt_umask(pdev->irqno);
  138. XUartPs_SetInterruptMask(&pdev->uart, XUARTPS_IXR_RXOVR);
  139. break;
  140. }
  141. return RT_EOK;
  142. }
  143. static int uart_putc(struct rt_serial_device *serial, char c)
  144. {
  145. struct hw_uart_device *dev;
  146. RT_ASSERT(serial != RT_NULL);
  147. dev = (struct hw_uart_device *)serial->parent.user_data;
  148. RT_ASSERT(dev != RT_NULL);
  149. XUartPs_SendByte(dev->base_addr, c);
  150. return 1;
  151. }
  152. static int uart_getc(struct rt_serial_device *serial)
  153. {
  154. struct hw_uart_device *dev;
  155. RT_ASSERT(serial != RT_NULL);
  156. dev = (struct hw_uart_device *)serial->parent.user_data;
  157. u32 RecievedByte;
  158. if (!XUartPs_IsReceiveData(dev->base_addr))
  159. {
  160. return -1;
  161. }
  162. RecievedByte = XUartPs_ReadReg(dev->base_addr, XUARTPS_FIFO_OFFSET);
  163. /* Return the byte received */
  164. return RecievedByte;
  165. }
  166. #ifdef RT_SERIAL_USING_DMA
  167. static rt_ssize_t dma_transmit(struct rt_serial_device *serial,
  168. rt_uint8_t *buf,
  169. rt_size_t size,
  170. int direction)
  171. {
  172. RT_ASSERT(serial != RT_NULL);
  173. struct hw_uart_device *dev
  174. = (struct hw_uart_device *)serial->parent.user_data;
  175. RT_ASSERT(dev != RT_NULL);
  176. for (int i = 0; i < size; i++)
  177. XUartPs_SendByte(dev->base_addr, buf[i]);
  178. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DMADONE);
  179. return size;
  180. }
  181. #endif
  182. static const struct rt_uart_ops _uart_ops = {
  183. uart_configure,
  184. uart_control,
  185. uart_putc,
  186. uart_getc,
  187. #ifdef RT_SERIAL_USING_DMA
  188. dma_transmit,
  189. #endif
  190. };
  191. #include "rtthread.h"
  192. #ifdef BSP_USING_UART0
  193. static struct rt_serial_device _serial0;
  194. #endif
  195. #ifdef BSP_USING_UART1
  196. static struct rt_serial_device _serial1;
  197. #endif
  198. int rt_hw_uart_init(void)
  199. {
  200. struct serial_configure config;
  201. config.baud_rate = BAUD_RATE_115200;
  202. config.bit_order = BIT_ORDER_LSB;
  203. config.data_bits = DATA_BITS_8;
  204. config.parity = PARITY_NONE;
  205. config.stop_bits = STOP_BITS_1;
  206. config.invert = NRZ_NORMAL;
  207. config.bufsz = RT_SERIAL_RB_BUFSZ;
  208. #ifdef BSP_USING_UART0
  209. _serial0.ops = &_uart_ops;
  210. _serial0.config = config;
  211. /* register uart device */
  212. rt_hw_serial_register(&_serial0, "uart0",
  213. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX
  214. | RT_DEVICE_FLAG_DMA_TX,
  215. &_uart_device0);
  216. #endif
  217. #ifdef BSP_USING_UART1
  218. _serial1.ops = &_uart_ops;
  219. _serial1.config = config;
  220. rt_hw_serial_register(&_serial1, "uart1",
  221. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX
  222. | RT_DEVICE_FLAG_DMA_TX,
  223. &_uart_device1);
  224. #endif
  225. return 0;
  226. }
  227. INIT_BOARD_EXPORT(rt_hw_uart_init);