当前位置:   article > 正文

学习ALINX_ZYNQ(7Z020开发板SDK)第二天——RS485串口_alinx sdk开发

alinx sdk开发
第一路 RS485 用的是 PS 端的 UART0

主程序流程:
初始化DS485_0 DE->设置MIO 9为输出->使能MIO 9->UART 初始化->设置 UART 模式->设置数据格式->串口中断设置
中断程序流程:
中断初始化->设置接收器FIFO中断触发级别,这里设置为32, 即收到32个数据就中断->设置超时时间->打开RX FIFO触发中断和超时中断->RS485为发送
中断服务程序:
读取中断ID寄存器,判断触发的是哪种中断->清除相应中断->判断数据是否发送完->数据全部发送完,RS485为接收,否则继续发送
代码如下:
  1. /* Definitions for peripheral PS7_GPIO_0 */
  2. //串口器件ID
  3. #define RS485_DEVICE_ID XPAR_PS7_UART_0_DEVICE_ID
  4. #define RS485_INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID//中断的设备ID
  5. #define RS485_UART_INT_IRQ_ID XPAR_XUARTPS_0_INTR//串口的中断号
  6. #define XPAR_PS7_GPIO_0_DEVICE_ID 0
  7. XUartPsFormat UartFormat =
  8. {
  9. 9600,
  10. XUARTPS_FORMAT_8_BITS,
  11. XUARTPS_FORMAT_NO_PARITY,
  12. XUARTPS_FORMAT_1_STOP_BIT
  13. };
  14. //串口
  15. int uart_init(void)
  16. {
  17. static int status;
  18. //设置RS485
  19. PsGpioSetup();
  20. //串口初始化
  21. status = uart_config();
  22. //串口中断初始化
  23. uart_intr();
  24. return status;
  25. }
  26. int PsGpioSetup(void)
  27. {
  28. int Status;
  29. /* Initial RS485_0 DE */
  30. XGpioPs_Config *GPIO_CONFIG ;
  31. GPIO_CONFIG = XGpioPs_LookupConfig(XPAR_PS7_GPIO_0_DEVICE_ID) ;
  32. Status = XGpioPs_CfgInitialize(&rs485_0_de, GPIO_CONFIG, GPIO_CONFIG->BaseAddr) ;
  33. if (Status != XST_SUCCESS)
  34. {
  35. return XST_FAILURE ;
  36. }
  37. /* set MIO 9 as output */
  38. XGpioPs_SetDirectionPin(&rs485_0_de, 9, 1) ;
  39. /* enable MIO 9 output */
  40. XGpioPs_SetOutputEnablePin(&rs485_0_de, 9, 1) ;
  41. return XST_SUCCESS ;
  42. }
  43. //串口初始化
  44. int uart_config(void)
  45. {
  46. int Status;
  47. XUartPs_Config *Config;
  48. //根据器件ID查找配置信息
  49. Config = XUartPs_LookupConfig(RS485_DEVICE_ID);
  50. if (NULL == Config) {
  51. return XST_FAILURE;
  52. }
  53. //根据配置信息对PS UART进行初始化
  54. Status = XUartPs_CfgInitialize(&Uart_PS, Config, Config->BaseAddress);
  55. if (Status != XST_SUCCESS) {
  56. return XST_FAILURE;
  57. }
  58. /* Check hardware build./检查硬件搭配是否正确 */
  59. Status = XUartPs_SelfTest(&Uart_PS);
  60. if (Status != XST_SUCCESS) {
  61. return XST_FAILURE;
  62. }
  63. /* Use Normal mode. */
  64. XUartPs_SetOperMode(&Uart_PS, XUARTPS_OPER_MODE_NORMAL);
  65. /* Set uart mode Baud Rate 115200, 8bits, no parity, 1 stop bit */
  66. XUartPs_SetDataFormat(&Uart_PS, &UartFormat) ;
  67. return 1;
  68. }
  69. //串口中断初始化
  70. int uart_intr(void)
  71. {
  72. /*Set receiver FIFO interrupt trigger level, here set to 32*/
  73. XUartPs_SetFifoThreshold(&Uart_PS,32);
  74. //超时时间
  75. XUartPs_SetRecvTimeout(&Uart_PS,10);
  76. //RX FIFO trigger interrupt Timeout error interrupt
  77. XUartPs_SetInterruptMask(&Uart_PS,XUARTPS_IXR_RXOVR|XUARTPS_IXR_TOUT);
  78. SetupInterruptSystem(&IntcInstPtr, &Uart_PS, RS485_UART_INT_IRQ_ID) ;
  79. XGpioPs_WritePin(&rs485_0_de, 9, 0) ;
  80. return 2;
  81. }
  82. int SetupInterruptSystem(XScuGic *IntcInstancePtr, XUartPs *UartInstancePtr, u16 UartIntrId)
  83. {
  84. int Status;
  85. /* Configuration for interrupt controller */
  86. XScuGic_Config *IntcConfig;
  87. /* Initialize the interrupt controller driver */
  88. IntcConfig = XScuGic_LookupConfig(RS485_INTC_DEVICE_ID);
  89. if (NULL == IntcConfig) {
  90. return XST_FAILURE;
  91. }
  92. Status = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
  93. IntcConfig->CpuBaseAddress);
  94. if (Status != XST_SUCCESS) {
  95. return XST_FAILURE;
  96. }
  97. //设置并打开中断异常处理功能
  98. Xil_ExceptionInit();
  99. /*
  100. * Connect the interrupt controller interrupt handler to the
  101. * hardware interrupt handling logic in the processor.
  102. */
  103. Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
  104. (Xil_ExceptionHandler) XScuGic_InterruptHandler,
  105. IntcInstancePtr);
  106. Status = XScuGic_Connect(IntcInstancePtr, UartIntrId,
  107. (Xil_ExceptionHandler) Handler,
  108. (void *) UartInstancePtr);
  109. if (Status != XST_SUCCESS) {
  110. return XST_FAILURE;
  111. }
  112. XScuGic_Enable(IntcInstancePtr, UartIntrId);
  113. Xil_ExceptionEnable();
  114. return Status ;
  115. }
串口中断服务函数根据自己的功能编写
本代码是根据收到的数据回复对应的数据

 

  1. void Handler(void *CallBackRef)
  2. {
  3. XUartPs *UartInstancePtr = (XUartPs *) CallBackRef ;
  4. u32 UartSrValue ;
  5. //读取中断ID寄存器,判断触发的是哪种中断
  6. UartSrValue = XUartPs_ReadReg(UartInstancePtr->Config.BaseAddress,XUARTPS_IMR_OFFSET);
  7. UartSrValue &= XUartPs_ReadReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET);
  8. if (UartSrValue & XUARTPS_IXR_RXOVR) /* check if receiver FIFO trigger */
  9. {
  10. if(uart.Received_OK == TRUE)
  11. {
  12. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET, XUARTPS_IXR_RXOVR);
  13. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET, XUARTPS_IXR_TOUT) ;
  14. return;
  15. }
  16. /* clear trigger interrupt */
  17. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress, XUARTPS_ISR_OFFSET, XUARTPS_IXR_RXOVR) ;
  18. uart.rx_realnumber = XUartPs_Recv(&Uart_PS, uart.Buffer_R_temp, 1000) ;
  19. if(uart.rx_totalnumber+uart.rx_realnumber>1000)
  20. {
  21. uart.rx_totalnumber = 0;
  22. return;
  23. }
  24. else
  25. {
  26. memcpy(&uart.RX_buff[uart.rx_totalnumber],uart.Buffer_R_temp,uart.rx_realnumber);
  27. }
  28. uart.rx_totalnumber += uart.rx_realnumber;
  29. }
  30. else if (UartSrValue & (u32)XUARTPS_IXR_TOUT)
  31. {
  32. if(uart.Received_OK == TRUE)
  33. {
  34. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET, XUARTPS_IXR_RXOVR);
  35. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET, XUARTPS_IXR_TOUT) ;
  36. return;
  37. }
  38. //清除中断标志
  39. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET, XUARTPS_IXR_TOUT) ;
  40. uart.rx_realnumber = XUartPs_Recv(&Uart_PS,uart.Buffer_R_temp,1000);
  41. if(uart.rx_totalnumber+uart.rx_realnumber>1000)
  42. {
  43. uart.rx_totalnumber = 0;
  44. return;
  45. }
  46. else
  47. {
  48. memcpy(&uart.RX_buff[uart.rx_totalnumber],uart.Buffer_R_temp,uart.rx_realnumber);
  49. }
  50. uart.rx_totalnumber += uart.rx_realnumber;
  51. uart.whitch_Num_R=uart.rx_totalnumber;
  52. if(uart.whitch_Num_R>1)
  53. {
  54. uart.Received_OK=TRUE;
  55. XGpioPs_WritePin(&rs485_0_de, 9, 1) ;
  56. //接收数据处理
  57. PROTOCOL_Analysis((u8 *)uart.RX_buff,uart.whitch_Num_R);
  58. uart.whitch_Num_R = 0;
  59. uart.Received_OK = FALSE;
  60. }
  61. else
  62. {
  63. XUartPs_EnableUart(&Uart_PS);
  64. }
  65. uart.rx_totalnumber = 0;
  66. }
  67. else if (UartSrValue & (u32)XUARTPS_IXR_TXEMPTY)
  68. {
  69. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET, XUARTPS_IXR_TXEMPTY) ;
  70. u32 rx_cnt=0;
  71. rx_cnt=XUartPs_SendBuffer(&Uart_PS);
  72. // printf("%ld\r\n",rx_cnt);
  73. if(rx_cnt==0)
  74. {
  75. //数据全部发送,485为接收
  76. XGpioPs_WritePin(&rs485_0_de, 9, 0);
  77. }
  78. else
  79. {
  80. }
  81. }
  82. else
  83. {
  84. XUartPs_WriteReg(UartInstancePtr->Config.BaseAddress,XUARTPS_ISR_OFFSET, XUARTPS_IXR_MASK);
  85. }
  86. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号