[X]关闭

miz720串口接收中断出现进不去的问题,下面是我的代码

文档创建者:Cherish_
浏览次数:6119
最后更新:2016-08-13
悬赏1积分未解决
串口接收中断进不去,


    uart_init(uart1,115200);
    uart_irq_enable(uart1);
-------------------------------------------------------------------->

下面是我的驱动代码



UART.c



#include "uart.h"
#include "stdio.h"

XUartPs UART_InitStruct[2];
/*
* UART_InitStruct[0] --> UART0
* UART_InitStruct[1] --> UART1
* If have more uart,define like this.
* */

XScuGic UART_InterruptStruct[2];
/*
* The interrupt Struct
* */

extern XUartPs_Config XUartPs_ConfigTable[2];


int uart_init(enum UARTx uart, u32 baud)
{
    /* 2016.07.11 */
    /* FIXME:This code is better, but have not test */
    int Status;
    XUartPs_Config *Config;
    Config = XUartPs_LookupConfig(uart);
    if (NULL == Config) {
        return XST_FAILURE;
    }

    Status = XUartPs_CfgInitialize(&UART_InitStruct[uart], Config, Config->BaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    XUartPs_SetBaudRate(&UART_InitStruct[uart], baud);

    /* FIXME: not test! but I think it is better */

    Status = XUartPs_SelfTest(&UART_InitStruct[uart]);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    return XST_SUCCESS;
}


u32 temp_IsrStatus;


int uart_irq_enable(enum UARTx uart)
{
    /* 2016.07.11 */
    /* We need know the lib of scugic -->  global interrupt contrl*/

    int UART_INT_IRQ_ID;
    XUartPs_Handler Handle;
    int Status;
    u32 IntrMask;

    switch(uart) {
    case uart0:
        UART_INT_IRQ_ID = XPS_UART0_INT_ID;
        Handle = USART0_IRQHandler;
        break;
    case uart1:
        UART_INT_IRQ_ID = XPS_UART1_INT_ID;
        Handle = USART1_IRQHandler;
        break;
    }

    Status = UartSetupInterruptSystem(&UART_InterruptStruct[uart], &UART_InitStruct[uart], UART_INT_IRQ_ID);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
    XUartPs_SetHandler(&UART_InitStruct[uart], Handle, &UART_InitStruct[uart]);

    IntrMask = XUARTPS_IXR_OVER | XUARTPS_IXR_RXFULL |XUARTPS_IXR_RXOVR;

    XUartPs_SetInterruptMask(&UART_InitStruct[uart], IntrMask);

    XUartPs_WriteReg(UART_InitStruct[uart].Config.BaseAddress, XUARTPS_ISR_OFFSET,
            0xffffffff);
/*
    temp_IsrStatus = XUartPs_ReadReg(UART_InitStruct[uart].Config.BaseAddress,
               XUARTPS_ISR_OFFSET);
*/
    XUartPs_SetOperMode(&UART_InitStruct[uart], XUARTPS_OPER_MODE_NORMAL); /*  */
    //XUartPs_SetRecvTimeout(&UART_InitStruct[uart], 10);
    XUartPs_EnableUart(&UART_InitStruct[uart]);
    return XST_SUCCESS;
}


/* This is a tao lu */
int UartSetupInterruptSystem(XScuGic *IntcInstancePtr,
        XUartPs *UartInstancePtr,
        u16 UartIntrId)
{
    int Status;
    XScuGic_Config *IntcConfig;

    IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
    if (NULL == IntcConfig) {
        return XST_FAILURE;
    }

    Status = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
                        IntcConfig->CpuBaseAddress);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }

    Xil_ExceptionInit(); /* No use */
    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
                (Xil_ExceptionHandler) XScuGic_InterruptHandler,
                IntcInstancePtr);
    /*************************************************/
    //Xil_ExceptionEnable();
    XScuGic_Connect(IntcInstancePtr,UartIntrId,
            (Xil_InterruptHandler)XUartPs_InterruptHandler,
            (void *)UartInstancePtr);
    if (Status != XST_SUCCESS) {
        return XST_FAILURE;
    }
    XScuGic_Enable(IntcInstancePtr, UartIntrId);  /* jump to interrupt */
    Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
    return XST_SUCCESS;
}



void uart_send_char(enum UARTx uart, u8 Data)
{
    u32 BaseAddress;
    switch(uart){
    case uart0: {BaseAddress = XPAR_PS7_UART_0_BASEADDR;}; break;
    case uart1: {BaseAddress = XPAR_PS7_UART_1_BASEADDR;}; break;
    }
    XUartPs_SendByte(BaseAddress,Data);
}


void uart_print(enum UARTx uart,uint8_t *ptr)
{
      while (*ptr) {
          uart_send_char(uart,*ptr++);
      }
}

u32 show_status(enum UARTx uart)
{
    u32 temp;
    temp = XUartPs_ReadReg(UART_InitStruct[uart].Config.BaseAddress,
               XUARTPS_ISR_OFFSET);
    return temp;
}



void uart_send_string(enum UARTx uart, u8 *Pst, u16 length)
{
    XUartPs_Send(&UART_InitStruct[uart],Pst,length);
}

void uart_receive_string(enum UARTx uart, u8 *Pst, u16 length)
{
    XUartPs_Recv(&UART_InitStruct[uart],Pst,length);
}

u8 uart_receive_char(enum UARTx uart)
{
    u32 BaseAddress;
    switch(uart){
    case uart0: {BaseAddress = XPAR_PS7_UART_0_BASEADDR;}; break;
    case uart1: {BaseAddress = XPAR_PS7_UART_1_BASEADDR;}; break;
    }
    return XUartPs_RecvByte(BaseAddress);
}

void USART0_IRQHandler(void *CallBackRef, u32 Event, unsigned int EventData)
{
    uart_print(uart1,"Hello World\n\r");
}


void USART1_IRQHandler(void *CallBackRef, u32 Event, unsigned int EventData)
{
    uart_print(uart1,"Hello World\n\r");
}




UART.h


#pragma once

#include "xuartps_hw.h"
#include "xparameters.h"
#include "xuartps.h"
#include "xstatus.h"
#include "xscugic.h"


#define INTC_DEVICE_ID        XPAR_SCUGIC_SINGLE_DEVICE_ID

enum UARTx{
    uart0 = 0,uart1 = 1,
};


int uart_init(enum UARTx uart, u32 baud);
int UartSetupInterruptSystem(XScuGic *IntcInstancePtr,
        XUartPs *UartInstancePtr,
        u16 UartIntrId);
void uart_print(enum UARTx uart,uint8_t *ptr);
void uart_send_char(enum UARTx uart, u8 Data);
void uart_send_string(enum UARTx uart, u8 *Pst, u16 length);
u8 uart_receive_char(enum UARTx uart);
int uart_irq_enable(enum UARTx uart);
u32 show_status(enum UARTx uart);
void USART0_IRQHandler(void *CallBackRef, u32 Event, unsigned int EventData);
void USART1_IRQHandler(void *CallBackRef, u32 Event, unsigned int EventData);


发表评论已发布 2

Cherish_

发表于 2016-8-13 17:57:13 | 显示全部楼层

大神们,帮忙看一下那里出了问题?实在是百思不得其解。小弟在此谢谢了。
回复

使用道具 举报

Cherish_

发表于 2016-8-13 18:07:47 | 显示全部楼层

目前的现状是:

串口初始化没有问题--->串口发送给电脑没有问题---->串口接收来自电脑的数据-->X不能进入串口接收中断
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则