WXK
2024-09-18 05e2e954bd127de378a9d1dfbb0ed95d725aad63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
/**************************************************************************//**
* @file     uart.c
* @version  V1.10
* $Revision: 2 $
* $Date:    19/10/08 09:25 $
* @brief    Panchip series UART driver source file
*
* @note
* Copyright (C) 2016 Panchip Technology Corp. All rights reserved.
*****************************************************************************/
#include "PanSeries.h"
#include "pan_uart.h"
#include "pan_clk.h"
 
/**
  * @brief  Initializes the UARTx peripheral according to the specified
  *         parameters in the UART_InitStruct .
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  UART_InitStruct: pointer to a UART_InitTypeDef structure that contains
  *         the configuration information for the specified UART peripheral.
  * @retval UART init success (true) or fail (false)
  */
 
bool UART_Init(UART_T* UARTx, UART_InitTypeDef* UART_InitStruct)
{
    uint32_t tmpreg = 0x00;
    uint32_t integerdivider = 0x00;
    uint32_t fractionaldivider = 0x00;
    uint64_t apbclock = 0x00;
    /*---------------------------- UART BRR Configuration -----------------------*/
    /* Configure the UART Baud Rate */
    apbclock = CLK_GetPeripheralFreq((void*)UARTx);
 
    /*unlock to enable write & read divisor register*/
    UARTx->LCR |= UART_LCR_DLAB_Msk;
    /* Determine the integer part baud_rate_divisor =  PCLK*100 / (16*required_baud_rate)*/
    integerdivider = ((25 * apbclock) / (4 * (UART_InitStruct->UART_BaudRate)));
 
    //Too high baudrate (too small divider) would cause DLL/DLH be all 0 which means UART disabled,
    //thus return false if this happens.
    if (integerdivider < 100)
        return false;
 
    tmpreg = (integerdivider / 100);
    UARTx->RBR_THR_DLL = tmpreg & 0xFF;
    UARTx->IER_DLH = (tmpreg & 0xFF00 ) >> 8;
 
    /* Determine the fractional part */
    fractionaldivider = integerdivider - (100 * tmpreg);
 
    /* Implement the fractional part in the register */
    UARTx->DLF = ((((fractionaldivider * 16) + 50) / 100)) ;
    UARTx->LCR &=  ~UART_LCR_DLAB_Msk;
 
    /*---------------------------- UART Line Configuration -----------------------*/
    tmpreg = UARTx->LCR;
    tmpreg &= ~(UART_LCR_SP_Msk | UART_LCR_EPS_Msk | UART_LCR_PEN_Msk | UART_LCR_STOP_Msk | UART_LCR_DLS_Msk);
    tmpreg |= (UART_InitStruct->UART_LineCtrl);
    UARTx->LCR = tmpreg;
 
    return true;
}
 
void UART_DeInit(UART_T* UARTx)
{
 
}
 
bool UART_SendMultipleData(UART_T* UARTx, uint8_t* Buf, size_t BufSize, size_t SendSize)
{
    if (BufSize < SendSize)
        return false;
 
    while (SendSize--)
    {
        UART_SendData(UARTx, *Buf++);
        while(!(UART_GetLineStatus(UARTx) & UART_LSR_TEMT_Msk));    // Wait until THR is empty to avoid data lost
    }
 
    return true;
}
 
bool UART_ReceiveMultipleData(UART_T* UARTx, uint8_t* Buf, size_t BufSize, size_t ExpectSize)
{
    if (BufSize < ExpectSize)
        return false;
 
    while ((UART_GetLineStatus(UARTx) & UART_LSR_DR_Msk) && ExpectSize)
    {
        *Buf++ = UART_ReceiveData(UARTx);
        ExpectSize--;
    }
 
    return true;
}
 
 
/*** (C) COPYRIGHT 2016 Panchip Technology Corp. ***/