UART_DEMO.c 6.49 KB
Newer Older
李俭双's avatar
李俭双 committed
1 2 3 4 5 6 7 8 9 10 11 12
/*******************************************************************************
*                  COPYRIGHT (C) 2021 CMS Technologies Ltd.                    *
*                                                                              *
********************************************************************************
* FileName      : uart_demo.c                                                  *
* Author        :                                                              *
* Version       : 1.0                                                          *
* Date          : 2021.08.13                                                   *
* Description   :                                                              *
* Function List :                                                              *
********************************************************************************/
#include "UART_DEMO.h"
李俭双's avatar
李俭双 committed
13

李俭双's avatar
李俭双 committed
14 15
unsigned char UART0_RX_BUF[UART_MAX_RECV_LEN];
unsigned short UART0_RX_STA = 0;
李俭双's avatar
李俭双 committed
16
unsigned char UART0_TX_BUF[UART_MAX_RECV_LEN];
李俭双's avatar
李俭双 committed
17 18 19 20 21 22 23 24 25



/******************************************************************************
* Function Name: Uart0_Init
* @brief  UART0 init demo
* @param  bound
* @return init status
******************************************************************************/
李俭双's avatar
李俭双 committed
26
int8_t Uart0_Init(uint32_t bound)//主要查看寄存器
李俭双's avatar
李俭双 committed
27
{
李俭双's avatar
李俭双 committed
28
	int8_t ret;
李俭双's avatar
李俭双 committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    UART_InitTypeDef UART_InitStructure = {0};

    GPIO_PinAFConfig(GPIO_PORT1, GPIO_Pin_1, GPIO_P11, GROUP_AF_RXD0);
    GPIO_PinAFConfig(GPIO_PORT1, GPIO_Pin_2, GPIO_P12, GROUP_AF_TXD0);

    /*TX GPIO CONFIG*/
    GPIO_InitStruct.GPIO_Pin    = GPIO_Pin_2;
    GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_OType  = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Level   = GPIO_Level_HIGH;	
    GPIO_InitStruct.GPIO_Ctrl  = GPIO_Control_DIG;
    GPIO_Init(GPIO_PORT1, &GPIO_InitStruct);

    /*RX GPIO CONFIG*/
    GPIO_InitStruct.GPIO_Pin    = GPIO_Pin_1 ;
    GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_Ctrl   = GPIO_Control_DIG;
    GPIO_Init(GPIO_PORT1, &GPIO_InitStruct);

    /*USART CONFIG*/
    UART_InitStructure.UART_BaudRate = bound;
    UART_InitStructure.UART_WordLength = UART_WordLength_8b;
    UART_InitStructure.UART_StopBits = UART_StopBits_1;//Ò»¸öֹͣλ
    UART_InitStructure.UART_Parity = UART_Parity_No;//ÎÞÆæÅ¼Ð£Ñéλ
    UART_InitStructure.phase = UART_Phase_Normal;
    UART_InitStructure.bitorder = UART_Bit_LSB;
    UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx; //ÊÕ·¢Ä£Ê½

    ret = UART_Init(UART0, &UART_InitStructure); //³õʼ»¯´®¿Ú

    if (ret)
    {
        SCI_ERROR_LOG(ret);
        return ret;
    }

    ISR_Register(ST0_IRQn, uart0_interrupt_send);     //´®¿Ú0·¢ËÍÖжϷþÎñ·¾¶×¢²á
    ISR_Register(SR0_IRQn, uart0_interrupt_receive);  //´®¿Ú0½ÓÊÕÖжϷþÎñ·¾¶×¢²á


    return SCI_SUCCESS;
}



/******************************************************************************
* Function Name: Uart0_Send
* @brief  UART0 Send data
* @param  None
* @return None
*******************************************************************************/
void Uart0_Send(uint8_t ch)
{
    UART_SendByte(UART0, ch);
}
/*****************************************************************************
* Function Name: Uart0_Receive
* @brief  UART0 receive data
* @param  None
* @return rx data
*****************************************************************************/
char Uart0_Receive(void)
{
    return UART_ReceiveByte(UART0);
}

/*****************************************************************************
* Function Name: Uart0_IntSend
* @brief  UART0 Send data by interrupt
* @param  None
* @return None
*****************************************************************************/
void Uart0_IntSend(uint8_t *tx_buf, uint16_t tx_num)
{
    pData.data = tx_buf;
    pData.len = tx_num;

    INTC_SetPendingIRQ(ST0_IRQn);
}
/****************************************************************************
* Function Name: uart0_interrupt_send
* @brief  UART0 Send interrupt service routine
* @param  None
* @return None
*****************************************************************************/
void uart0_interrupt_send(void *msg)
{
    ATE_FRAME_t *pFrame = (ATE_FRAME_t *)msg;
    INTC_ClearPendingIRQ(ST0_IRQn);

    if ((pFrame->len > 0U) && pFrame->data)
    {
        UART0_TX = *pFrame->data;
        pFrame->data++;
        pFrame->len --;
    }
    else //send finished
    {
    }
}

/*****************************************************************************
* Function Name: uart_callback_error
* @brief
* @param  None
* @return None
******************************************************************************/
void uart_callback_error(uint8_t err_type)
{
    //user edit here when appear error
}

/*****************************************************************************
* Function Name: uart0_interrupt_receive
* @brief  UART0 Receive interrupt service routine
* @param  None
* @return None
*****************************************************************************/
李俭双's avatar
李俭双 committed
148
void uart0_interrupt_receive(void)
李俭双's avatar
李俭双 committed
149
{
李俭双's avatar
李俭双 committed
150 151
    volatile uint8_t rx_data;
    volatile uint8_t err_type;
李俭双's avatar
李俭双 committed
152 153 154

    INTC_ClearPendingIRQ(SR0_IRQn);
    err_type = UART_GetErrStaus(UART0, UART_FLAG_FEF | UART_FLAG_PEF | UART_FLAG_OVF);
李俭双's avatar
李俭双 committed
155
	
李俭双's avatar
李俭双 committed
156 157
    if (err_type)
    {
李俭双's avatar
李俭双 committed
158
		UART_ClearFlag(UART0,UART_FLAG_FEF | UART_FLAG_PEF | UART_FLAG_OVF);
李俭双's avatar
李俭双 committed
159 160
        uart_callback_error(err_type);
    }
161 162
    rx_data = UART0_RX;
    UART_Put((uint16_t)rx_data);
李俭双's avatar
李俭双 committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    


   // if ((UART0_RX_STA & 0x8000U) == 0) //½ÓÊÕδÍê³É
   // {
   //     if (UART0_RX_STA & 0x4000U) //½ÓÊÕµ½0x0d
   //     {
   //         if (rx_data != 0x0a)
	//		{
	//			UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
   //             UART0_RX_STA++;					
	//		}
   //         else
   //         {
   //             UART0_RX_STA |= 0x8000;
   //             UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
   //             UART0_RX_STA ++;
   //         }
   //     }
   //     else                     //»¹Î´½ÓÊÕµ½0x0d
   //     {
   //         if (rx_data == 0x0d)
   //         {
   //             UART0_RX_STA |= 0x4000;
   //             UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
   //             UART0_RX_STA ++;
   //         }
   //         else
   //         {
   //             UART0_RX_BUF[UART0_RX_STA & 0x3fff] = rx_data;
   //             UART0_RX_STA ++;
   //         }
   //     }
   // }
   // else if ((UART0_RX_STA & 0x8000U) == 1) // receive finished
   // {
   // }
李俭双's avatar
李俭双 committed
200 201 202

}