Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver/nuc472_uart.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /**************************************************************************//** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file uart.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | * @version V1.00 |
sahilmgandhi | 18:6a4db94011d3 | 4 | * $Revision: 14 $ |
sahilmgandhi | 18:6a4db94011d3 | 5 | * $Date: 15/11/26 10:47a $ |
sahilmgandhi | 18:6a4db94011d3 | 6 | * @brief NUC472/NUC442 UART driver source file |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * @note |
sahilmgandhi | 18:6a4db94011d3 | 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 10 | *****************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 11 | |
sahilmgandhi | 18:6a4db94011d3 | 12 | #include <stdio.h> |
sahilmgandhi | 18:6a4db94011d3 | 13 | #include "NUC472_442.h" |
sahilmgandhi | 18:6a4db94011d3 | 14 | |
sahilmgandhi | 18:6a4db94011d3 | 15 | /*---------------------------------------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 16 | /* Includes of local headers */ |
sahilmgandhi | 18:6a4db94011d3 | 17 | /*---------------------------------------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 18 | |
sahilmgandhi | 18:6a4db94011d3 | 19 | |
sahilmgandhi | 18:6a4db94011d3 | 20 | /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver |
sahilmgandhi | 18:6a4db94011d3 | 21 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 22 | */ |
sahilmgandhi | 18:6a4db94011d3 | 23 | |
sahilmgandhi | 18:6a4db94011d3 | 24 | /** @addtogroup NUC472_442_UART_Driver UART Driver |
sahilmgandhi | 18:6a4db94011d3 | 25 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 26 | */ |
sahilmgandhi | 18:6a4db94011d3 | 27 | |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | /** @addtogroup NUC472_442_UART_EXPORTED_FUNCTIONS UART Exported Functions |
sahilmgandhi | 18:6a4db94011d3 | 30 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 31 | */ |
sahilmgandhi | 18:6a4db94011d3 | 32 | |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | /** |
sahilmgandhi | 18:6a4db94011d3 | 35 | * @brief The function is used to clear UART specified interrupt flag. |
sahilmgandhi | 18:6a4db94011d3 | 36 | * |
sahilmgandhi | 18:6a4db94011d3 | 37 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 38 | * @param[in] u32InterruptFlag The specified interrupt of UART module.. |
sahilmgandhi | 18:6a4db94011d3 | 39 | * |
sahilmgandhi | 18:6a4db94011d3 | 40 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 41 | */ |
sahilmgandhi | 18:6a4db94011d3 | 42 | void UART_ClearIntFlag(UART_T* uart , uint32_t u32InterruptFlag) |
sahilmgandhi | 18:6a4db94011d3 | 43 | { |
sahilmgandhi | 18:6a4db94011d3 | 44 | if(u32InterruptFlag & UART_INTSTS_RLSINT_Msk) { /* clear Receive Line Status Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 45 | uart->FIFOSTS |= UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 46 | uart->FIFOSTS |= UART_FIFOSTS_ADDRDETF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 47 | } |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | if(u32InterruptFlag & UART_INTSTS_MODEMINT_Msk) /* clear Modem Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 50 | uart->MODEMSTS |= UART_MODEMSTS_CTSDETF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 51 | |
sahilmgandhi | 18:6a4db94011d3 | 52 | if(u32InterruptFlag & UART_INTSTS_BUFERRINT_Msk) { /* clear Buffer Error Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 53 | uart->FIFOSTS |= UART_FIFOSTS_RXOVIF_Msk | UART_FIFOSTS_TXOVIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 54 | } |
sahilmgandhi | 18:6a4db94011d3 | 55 | |
sahilmgandhi | 18:6a4db94011d3 | 56 | if(u32InterruptFlag & UART_INTSTS_RXTOINT_Msk) /* clear Modem Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 57 | uart->INTSTS |= UART_INTSTS_RXTOIF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 58 | |
sahilmgandhi | 18:6a4db94011d3 | 59 | } |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | |
sahilmgandhi | 18:6a4db94011d3 | 62 | /** |
sahilmgandhi | 18:6a4db94011d3 | 63 | * @brief The function is used to disable UART. |
sahilmgandhi | 18:6a4db94011d3 | 64 | * |
sahilmgandhi | 18:6a4db94011d3 | 65 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 66 | * |
sahilmgandhi | 18:6a4db94011d3 | 67 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 68 | */ |
sahilmgandhi | 18:6a4db94011d3 | 69 | void UART_Close(UART_T* uart) |
sahilmgandhi | 18:6a4db94011d3 | 70 | { |
sahilmgandhi | 18:6a4db94011d3 | 71 | uart->INTEN = 0; |
sahilmgandhi | 18:6a4db94011d3 | 72 | } |
sahilmgandhi | 18:6a4db94011d3 | 73 | |
sahilmgandhi | 18:6a4db94011d3 | 74 | |
sahilmgandhi | 18:6a4db94011d3 | 75 | /** |
sahilmgandhi | 18:6a4db94011d3 | 76 | * @brief The function is used to disable UART auto flow control. |
sahilmgandhi | 18:6a4db94011d3 | 77 | * |
sahilmgandhi | 18:6a4db94011d3 | 78 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 79 | * |
sahilmgandhi | 18:6a4db94011d3 | 80 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 81 | */ |
sahilmgandhi | 18:6a4db94011d3 | 82 | void UART_DisableFlowCtrl(UART_T* uart) |
sahilmgandhi | 18:6a4db94011d3 | 83 | { |
sahilmgandhi | 18:6a4db94011d3 | 84 | uart->INTEN &= ~(UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 85 | } |
sahilmgandhi | 18:6a4db94011d3 | 86 | |
sahilmgandhi | 18:6a4db94011d3 | 87 | |
sahilmgandhi | 18:6a4db94011d3 | 88 | /** |
sahilmgandhi | 18:6a4db94011d3 | 89 | * @brief The function is used to disable UART specified interrupt and disable NVIC UART IRQ. |
sahilmgandhi | 18:6a4db94011d3 | 90 | * |
sahilmgandhi | 18:6a4db94011d3 | 91 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 92 | * @param[in] u32InterruptFlag The specified interrupt of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 93 | * - \ref UART_INTEN_TOCNTEN_Msk : Rx Time Out interrupt |
sahilmgandhi | 18:6a4db94011d3 | 94 | * - \ref UART_INTEN_WKCTSIEN_Msk : Wakeup interrupt |
sahilmgandhi | 18:6a4db94011d3 | 95 | * - \ref UART_INTEN_BUFERRIEN_Msk : Buffer Error interrupt |
sahilmgandhi | 18:6a4db94011d3 | 96 | * - \ref UART_INTEN_RXTOIEN_Msk : Rx time-out interrupt |
sahilmgandhi | 18:6a4db94011d3 | 97 | * - \ref UART_INTEN_MODEMIEN_Msk : Modem interrupt |
sahilmgandhi | 18:6a4db94011d3 | 98 | * - \ref UART_INTEN_RLSIEN_Msk : Rx Line status interrupt |
sahilmgandhi | 18:6a4db94011d3 | 99 | * - \ref UART_INTEN_THREIEN_Msk : Tx empty interrupt |
sahilmgandhi | 18:6a4db94011d3 | 100 | * - \ref UART_INTEN_RDAIEN_Msk : Rx ready interrupt |
sahilmgandhi | 18:6a4db94011d3 | 101 | * |
sahilmgandhi | 18:6a4db94011d3 | 102 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 103 | */ |
sahilmgandhi | 18:6a4db94011d3 | 104 | void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag ) |
sahilmgandhi | 18:6a4db94011d3 | 105 | { |
sahilmgandhi | 18:6a4db94011d3 | 106 | uart->INTEN &= ~ u32InterruptFlag; |
sahilmgandhi | 18:6a4db94011d3 | 107 | } |
sahilmgandhi | 18:6a4db94011d3 | 108 | |
sahilmgandhi | 18:6a4db94011d3 | 109 | |
sahilmgandhi | 18:6a4db94011d3 | 110 | |
sahilmgandhi | 18:6a4db94011d3 | 111 | /** |
sahilmgandhi | 18:6a4db94011d3 | 112 | * @brief The function is used to Enable UART auto flow control. |
sahilmgandhi | 18:6a4db94011d3 | 113 | * |
sahilmgandhi | 18:6a4db94011d3 | 114 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 115 | * |
sahilmgandhi | 18:6a4db94011d3 | 116 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 117 | */ |
sahilmgandhi | 18:6a4db94011d3 | 118 | void UART_EnableFlowCtrl(UART_T* uart ) |
sahilmgandhi | 18:6a4db94011d3 | 119 | { |
sahilmgandhi | 18:6a4db94011d3 | 120 | uart->MODEM |= UART_MODEM_RTSACTLV_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 121 | uart->MODEM &= ~UART_MODEM_RTS_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 122 | uart->MODEMSTS |= UART_MODEMSTS_CTSACTLV_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 123 | uart->INTEN |= UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 124 | } |
sahilmgandhi | 18:6a4db94011d3 | 125 | |
sahilmgandhi | 18:6a4db94011d3 | 126 | |
sahilmgandhi | 18:6a4db94011d3 | 127 | /** |
sahilmgandhi | 18:6a4db94011d3 | 128 | * @brief The function is used to enable UART specified interrupt and disable NVIC UART IRQ. |
sahilmgandhi | 18:6a4db94011d3 | 129 | * |
sahilmgandhi | 18:6a4db94011d3 | 130 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 131 | * @param[in] u32InterruptFlag The specified interrupt of UART module: |
sahilmgandhi | 18:6a4db94011d3 | 132 | * - \ref UART_INTEN_TOCNTEN_Msk : Rx Time Out interrupt |
sahilmgandhi | 18:6a4db94011d3 | 133 | * - \ref UART_INTEN_WKCTSIEN_Msk : Wakeup interrupt |
sahilmgandhi | 18:6a4db94011d3 | 134 | * - \ref UART_INTEN_BUFERRIEN_Msk : Buffer Error interrupt |
sahilmgandhi | 18:6a4db94011d3 | 135 | * - \ref UART_INTEN_RXTOIEN_Msk : Rx time-out interrupt |
sahilmgandhi | 18:6a4db94011d3 | 136 | * - \ref UART_INTEN_MODEMIEN_Msk : Modem interrupt |
sahilmgandhi | 18:6a4db94011d3 | 137 | * - \ref UART_INTEN_RLSIEN_Msk : Rx Line status interrupt |
sahilmgandhi | 18:6a4db94011d3 | 138 | * - \ref UART_INTEN_THREIEN_Msk : Tx empty interrupt |
sahilmgandhi | 18:6a4db94011d3 | 139 | * - \ref UART_INTEN_RDAIEN_Msk : Rx ready interrupt |
sahilmgandhi | 18:6a4db94011d3 | 140 | * |
sahilmgandhi | 18:6a4db94011d3 | 141 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 142 | */ |
sahilmgandhi | 18:6a4db94011d3 | 143 | void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag ) |
sahilmgandhi | 18:6a4db94011d3 | 144 | { |
sahilmgandhi | 18:6a4db94011d3 | 145 | uart->INTEN |= u32InterruptFlag; |
sahilmgandhi | 18:6a4db94011d3 | 146 | } |
sahilmgandhi | 18:6a4db94011d3 | 147 | |
sahilmgandhi | 18:6a4db94011d3 | 148 | |
sahilmgandhi | 18:6a4db94011d3 | 149 | /** |
sahilmgandhi | 18:6a4db94011d3 | 150 | * @brief This function use to enable UART function and set baud-rate. |
sahilmgandhi | 18:6a4db94011d3 | 151 | * |
sahilmgandhi | 18:6a4db94011d3 | 152 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 153 | * @param[in] u32baudrate The baudrate of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 154 | * |
sahilmgandhi | 18:6a4db94011d3 | 155 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 156 | */ |
sahilmgandhi | 18:6a4db94011d3 | 157 | void UART_Open(UART_T* uart, uint32_t u32baudrate) |
sahilmgandhi | 18:6a4db94011d3 | 158 | { |
sahilmgandhi | 18:6a4db94011d3 | 159 | uint8_t u8UartClkSrcSel; |
sahilmgandhi | 18:6a4db94011d3 | 160 | uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC}; |
sahilmgandhi | 18:6a4db94011d3 | 161 | uint32_t u32Clk; |
sahilmgandhi | 18:6a4db94011d3 | 162 | uint32_t u32Baud_Div; |
sahilmgandhi | 18:6a4db94011d3 | 163 | |
sahilmgandhi | 18:6a4db94011d3 | 164 | u32ClkTbl[1] = CLK_GetPLLClockFreq(); |
sahilmgandhi | 18:6a4db94011d3 | 165 | |
sahilmgandhi | 18:6a4db94011d3 | 166 | u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 167 | |
sahilmgandhi | 18:6a4db94011d3 | 168 | uart->FUNCSEL = UART_FUNCSEL_UART; |
sahilmgandhi | 18:6a4db94011d3 | 169 | uart->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1; |
sahilmgandhi | 18:6a4db94011d3 | 170 | uart->FIFO = UART_FIFO_RFITL_1BYTE | UART_FIFO_RTSTRGLV_1BYTE; |
sahilmgandhi | 18:6a4db94011d3 | 171 | |
sahilmgandhi | 18:6a4db94011d3 | 172 | u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1); |
sahilmgandhi | 18:6a4db94011d3 | 173 | |
sahilmgandhi | 18:6a4db94011d3 | 174 | if(u32baudrate != 0) { |
sahilmgandhi | 18:6a4db94011d3 | 175 | u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate); |
sahilmgandhi | 18:6a4db94011d3 | 176 | |
sahilmgandhi | 18:6a4db94011d3 | 177 | if(u32Baud_Div > 0xFFFF) |
sahilmgandhi | 18:6a4db94011d3 | 178 | uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate)); |
sahilmgandhi | 18:6a4db94011d3 | 179 | else |
sahilmgandhi | 18:6a4db94011d3 | 180 | uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); |
sahilmgandhi | 18:6a4db94011d3 | 181 | } |
sahilmgandhi | 18:6a4db94011d3 | 182 | } |
sahilmgandhi | 18:6a4db94011d3 | 183 | |
sahilmgandhi | 18:6a4db94011d3 | 184 | |
sahilmgandhi | 18:6a4db94011d3 | 185 | /** |
sahilmgandhi | 18:6a4db94011d3 | 186 | * @brief The function is used to read Rx data from RX FIFO and the data will be stored in pu8RxBuf. |
sahilmgandhi | 18:6a4db94011d3 | 187 | * |
sahilmgandhi | 18:6a4db94011d3 | 188 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 189 | * @param[out] pu8RxBuf The buffer to receive the data of receive FIFO. |
sahilmgandhi | 18:6a4db94011d3 | 190 | * @param[in] u32ReadBytes The the read bytes number of data. |
sahilmgandhi | 18:6a4db94011d3 | 191 | * |
sahilmgandhi | 18:6a4db94011d3 | 192 | * @return u32Count: Receive byte count |
sahilmgandhi | 18:6a4db94011d3 | 193 | * |
sahilmgandhi | 18:6a4db94011d3 | 194 | */ |
sahilmgandhi | 18:6a4db94011d3 | 195 | uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes) |
sahilmgandhi | 18:6a4db94011d3 | 196 | { |
sahilmgandhi | 18:6a4db94011d3 | 197 | uint32_t u32Count; |
sahilmgandhi | 18:6a4db94011d3 | 198 | |
sahilmgandhi | 18:6a4db94011d3 | 199 | for(u32Count=0; u32Count < u32ReadBytes; u32Count++) { |
sahilmgandhi | 18:6a4db94011d3 | 200 | if(uart->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) { /* Check RX empty => failed */ |
sahilmgandhi | 18:6a4db94011d3 | 201 | return u32Count; |
sahilmgandhi | 18:6a4db94011d3 | 202 | } |
sahilmgandhi | 18:6a4db94011d3 | 203 | pu8RxBuf[u32Count] = uart->DAT; /* Get Data from UART RX */ |
sahilmgandhi | 18:6a4db94011d3 | 204 | } |
sahilmgandhi | 18:6a4db94011d3 | 205 | |
sahilmgandhi | 18:6a4db94011d3 | 206 | return u32Count; |
sahilmgandhi | 18:6a4db94011d3 | 207 | } |
sahilmgandhi | 18:6a4db94011d3 | 208 | |
sahilmgandhi | 18:6a4db94011d3 | 209 | |
sahilmgandhi | 18:6a4db94011d3 | 210 | /** |
sahilmgandhi | 18:6a4db94011d3 | 211 | * @brief This function use to config UART line setting. |
sahilmgandhi | 18:6a4db94011d3 | 212 | * |
sahilmgandhi | 18:6a4db94011d3 | 213 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 214 | * @param[in] u32baudrate The register value of baudrate of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 215 | * if u32baudrate = 0, UART baudrate will not change. |
sahilmgandhi | 18:6a4db94011d3 | 216 | * @param[in] u32data_width The data length of UART module. [ \ref UART_WORD_LEN_5 / \ref UART_WORD_LEN_6 / \ref UART_WORD_LEN_7 / \ref UART_WORD_LEN_8] |
sahilmgandhi | 18:6a4db94011d3 | 217 | * @param[in] u32parity The parity setting (odd/even/none) of UART module. [ \ref UART_PARITY_NONE / \ref UART_PARITY_ODD / |
sahilmgandhi | 18:6a4db94011d3 | 218 | * \ref UART_PARITY_EVEN / \ref UART_PARITY_MARK / \ref UART_PARITY_SPACE] |
sahilmgandhi | 18:6a4db94011d3 | 219 | * @param[in] u32stop_bits The stop bit length (1/1.5/2 bit) of UART module. [ \ref UART_STOP_BIT_1 / \ref UART_STOP_BIT_1_5 / \ref UART_STOP_BIT_2] |
sahilmgandhi | 18:6a4db94011d3 | 220 | * |
sahilmgandhi | 18:6a4db94011d3 | 221 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 222 | */ |
sahilmgandhi | 18:6a4db94011d3 | 223 | void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits) |
sahilmgandhi | 18:6a4db94011d3 | 224 | { |
sahilmgandhi | 18:6a4db94011d3 | 225 | uint8_t u8UartClkSrcSel; |
sahilmgandhi | 18:6a4db94011d3 | 226 | uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC}; |
sahilmgandhi | 18:6a4db94011d3 | 227 | uint32_t u32Clk; |
sahilmgandhi | 18:6a4db94011d3 | 228 | uint32_t u32Baud_Div = 0; |
sahilmgandhi | 18:6a4db94011d3 | 229 | |
sahilmgandhi | 18:6a4db94011d3 | 230 | u32ClkTbl[1] = CLK_GetPLLClockFreq(); |
sahilmgandhi | 18:6a4db94011d3 | 231 | |
sahilmgandhi | 18:6a4db94011d3 | 232 | u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 233 | |
sahilmgandhi | 18:6a4db94011d3 | 234 | u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1); |
sahilmgandhi | 18:6a4db94011d3 | 235 | |
sahilmgandhi | 18:6a4db94011d3 | 236 | if(u32baudrate != 0) { |
sahilmgandhi | 18:6a4db94011d3 | 237 | u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate); |
sahilmgandhi | 18:6a4db94011d3 | 238 | |
sahilmgandhi | 18:6a4db94011d3 | 239 | if(u32Baud_Div > 0xFFFF) |
sahilmgandhi | 18:6a4db94011d3 | 240 | uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate)); |
sahilmgandhi | 18:6a4db94011d3 | 241 | else |
sahilmgandhi | 18:6a4db94011d3 | 242 | uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); |
sahilmgandhi | 18:6a4db94011d3 | 243 | } |
sahilmgandhi | 18:6a4db94011d3 | 244 | |
sahilmgandhi | 18:6a4db94011d3 | 245 | uart->LINE = u32data_width | u32parity | u32stop_bits; |
sahilmgandhi | 18:6a4db94011d3 | 246 | } |
sahilmgandhi | 18:6a4db94011d3 | 247 | |
sahilmgandhi | 18:6a4db94011d3 | 248 | |
sahilmgandhi | 18:6a4db94011d3 | 249 | /** |
sahilmgandhi | 18:6a4db94011d3 | 250 | * @brief This function use to set Rx timeout count. |
sahilmgandhi | 18:6a4db94011d3 | 251 | * |
sahilmgandhi | 18:6a4db94011d3 | 252 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 253 | * @param[in] u32TOC Rx timeout counter. |
sahilmgandhi | 18:6a4db94011d3 | 254 | * |
sahilmgandhi | 18:6a4db94011d3 | 255 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 256 | */ |
sahilmgandhi | 18:6a4db94011d3 | 257 | void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC) |
sahilmgandhi | 18:6a4db94011d3 | 258 | { |
sahilmgandhi | 18:6a4db94011d3 | 259 | uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC); |
sahilmgandhi | 18:6a4db94011d3 | 260 | uart->INTEN |= UART_INTEN_TOCNTEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 261 | } |
sahilmgandhi | 18:6a4db94011d3 | 262 | |
sahilmgandhi | 18:6a4db94011d3 | 263 | |
sahilmgandhi | 18:6a4db94011d3 | 264 | /** |
sahilmgandhi | 18:6a4db94011d3 | 265 | * @brief The function is used to configure IrDA relative settings. It consists of TX or RX mode and baudrate. |
sahilmgandhi | 18:6a4db94011d3 | 266 | * |
sahilmgandhi | 18:6a4db94011d3 | 267 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 268 | * @param[in] u32Buadrate The baudrate of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 269 | * @param[in] u32Direction The direction(transmit:1/receive:0) of UART module in IrDA mode. |
sahilmgandhi | 18:6a4db94011d3 | 270 | * |
sahilmgandhi | 18:6a4db94011d3 | 271 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 272 | */ |
sahilmgandhi | 18:6a4db94011d3 | 273 | void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction) |
sahilmgandhi | 18:6a4db94011d3 | 274 | { |
sahilmgandhi | 18:6a4db94011d3 | 275 | uint8_t u8UartClkSrcSel; |
sahilmgandhi | 18:6a4db94011d3 | 276 | uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC}; |
sahilmgandhi | 18:6a4db94011d3 | 277 | uint32_t u32Clk; |
sahilmgandhi | 18:6a4db94011d3 | 278 | |
sahilmgandhi | 18:6a4db94011d3 | 279 | u32ClkTbl[1] = CLK_GetPLLClockFreq(); |
sahilmgandhi | 18:6a4db94011d3 | 280 | |
sahilmgandhi | 18:6a4db94011d3 | 281 | u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos; |
sahilmgandhi | 18:6a4db94011d3 | 282 | |
sahilmgandhi | 18:6a4db94011d3 | 283 | u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1); |
sahilmgandhi | 18:6a4db94011d3 | 284 | |
sahilmgandhi | 18:6a4db94011d3 | 285 | uart->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32Buadrate); |
sahilmgandhi | 18:6a4db94011d3 | 286 | |
sahilmgandhi | 18:6a4db94011d3 | 287 | uart->IRDA &= ~UART_IRDA_TXINV_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 288 | uart->IRDA |= UART_IRDA_RXINV_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 289 | uart->IRDA = u32Direction ? uart->IRDA | UART_IRDA_TXEN_Msk : uart->IRDA &~ UART_IRDA_TXEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 290 | uart->FUNCSEL = (0x2 << UART_FUNCSEL_FUNCSEL_Pos); |
sahilmgandhi | 18:6a4db94011d3 | 291 | } |
sahilmgandhi | 18:6a4db94011d3 | 292 | |
sahilmgandhi | 18:6a4db94011d3 | 293 | |
sahilmgandhi | 18:6a4db94011d3 | 294 | /** |
sahilmgandhi | 18:6a4db94011d3 | 295 | * @brief The function is used to set RS485 relative setting. |
sahilmgandhi | 18:6a4db94011d3 | 296 | * |
sahilmgandhi | 18:6a4db94011d3 | 297 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 298 | * @param[in] u32Mode The operation mode( \ref UART_ALTCTL_RS485NMM_Msk / \ref UART_ALTCTL_RS485AUD_Msk / \ref UART_ALTCTL_RS485AAD_Msk). |
sahilmgandhi | 18:6a4db94011d3 | 299 | * @param[in] u32Addr The RS485 address. |
sahilmgandhi | 18:6a4db94011d3 | 300 | * |
sahilmgandhi | 18:6a4db94011d3 | 301 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 302 | */ |
sahilmgandhi | 18:6a4db94011d3 | 303 | void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr) |
sahilmgandhi | 18:6a4db94011d3 | 304 | { |
sahilmgandhi | 18:6a4db94011d3 | 305 | uart->FUNCSEL = UART_FUNCSEL_RS485; |
sahilmgandhi | 18:6a4db94011d3 | 306 | uart->ALTCTL = 0; |
sahilmgandhi | 18:6a4db94011d3 | 307 | uart->ALTCTL |= u32Mode | (u32Addr << UART_ALTCTL_ADDRMV_Pos); |
sahilmgandhi | 18:6a4db94011d3 | 308 | } |
sahilmgandhi | 18:6a4db94011d3 | 309 | |
sahilmgandhi | 18:6a4db94011d3 | 310 | |
sahilmgandhi | 18:6a4db94011d3 | 311 | /** |
sahilmgandhi | 18:6a4db94011d3 | 312 | * @brief The function is to write data into TX buffer to transmit data by UART. |
sahilmgandhi | 18:6a4db94011d3 | 313 | * |
sahilmgandhi | 18:6a4db94011d3 | 314 | * @param[in] uart The base address of UART module. |
sahilmgandhi | 18:6a4db94011d3 | 315 | * @param[in] pu8TxBuf The buffer to send the data to UART transmission FIFO. |
sahilmgandhi | 18:6a4db94011d3 | 316 | * @param[in] u32WriteBytes The byte number of data. |
sahilmgandhi | 18:6a4db94011d3 | 317 | * |
sahilmgandhi | 18:6a4db94011d3 | 318 | * @return u32Count: transfer byte count |
sahilmgandhi | 18:6a4db94011d3 | 319 | */ |
sahilmgandhi | 18:6a4db94011d3 | 320 | uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes) |
sahilmgandhi | 18:6a4db94011d3 | 321 | { |
sahilmgandhi | 18:6a4db94011d3 | 322 | uint32_t u32Count; |
sahilmgandhi | 18:6a4db94011d3 | 323 | |
sahilmgandhi | 18:6a4db94011d3 | 324 | for(u32Count=0; u32Count != u32WriteBytes; u32Count++) { |
sahilmgandhi | 18:6a4db94011d3 | 325 | if(uart->FIFOSTS & UART_FIFOSTS_TXFULL_Msk) { /* Wait Tx empty and Time-out manner */ |
sahilmgandhi | 18:6a4db94011d3 | 326 | return u32Count; |
sahilmgandhi | 18:6a4db94011d3 | 327 | } |
sahilmgandhi | 18:6a4db94011d3 | 328 | uart->DAT = pu8TxBuf[u32Count]; /* Send UART Data from buffer */ |
sahilmgandhi | 18:6a4db94011d3 | 329 | } |
sahilmgandhi | 18:6a4db94011d3 | 330 | |
sahilmgandhi | 18:6a4db94011d3 | 331 | return u32Count; |
sahilmgandhi | 18:6a4db94011d3 | 332 | } |
sahilmgandhi | 18:6a4db94011d3 | 333 | |
sahilmgandhi | 18:6a4db94011d3 | 334 | |
sahilmgandhi | 18:6a4db94011d3 | 335 | /*@}*/ /* end of group NUC472_442_UART_EXPORTED_FUNCTIONS */ |
sahilmgandhi | 18:6a4db94011d3 | 336 | |
sahilmgandhi | 18:6a4db94011d3 | 337 | /*@}*/ /* end of group NUC472_442_UART_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 338 | |
sahilmgandhi | 18:6a4db94011d3 | 339 | /*@}*/ /* end of group NUC472_442_Device_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 340 | |
sahilmgandhi | 18:6a4db94011d3 | 341 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
sahilmgandhi | 18:6a4db94011d3 | 342 | |
sahilmgandhi | 18:6a4db94011d3 | 343 |