| 1 |
577 |
jeremybenn |
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
|
| 2 |
|
|
* File Name : uart.c
|
| 3 |
|
|
* Author : MCD Application Team
|
| 4 |
|
|
* Date First Issued : 06/08/2003
|
| 5 |
|
|
* Description : This file provides all the UART software functions
|
| 6 |
|
|
********************************************************************************
|
| 7 |
|
|
* History:
|
| 8 |
|
|
* 30/11/2004 : V2.0
|
| 9 |
|
|
* 14/07/2004 : V1.3
|
| 10 |
|
|
* 01/01/2004 : V1.2
|
| 11 |
|
|
*******************************************************************************
|
| 12 |
|
|
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
|
| 13 |
|
|
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
|
| 14 |
|
|
AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
|
| 15 |
|
|
OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
|
| 16 |
|
|
OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
|
| 17 |
|
|
CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
| 18 |
|
|
*******************************************************************************/
|
| 19 |
|
|
|
| 20 |
|
|
#include "uart.h"
|
| 21 |
|
|
|
| 22 |
|
|
extern u16 UART_FlagStatus(UART_TypeDef *UARTx);
|
| 23 |
|
|
|
| 24 |
|
|
/*******************************************************************************
|
| 25 |
|
|
* Function Name : UART_Init
|
| 26 |
|
|
* Description : This function initializes the selected UART.
|
| 27 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 28 |
|
|
* Output : None
|
| 29 |
|
|
* Return : None
|
| 30 |
|
|
*******************************************************************************/
|
| 31 |
|
|
void UART_Init(UART_TypeDef *UARTx)
|
| 32 |
|
|
{
|
| 33 |
|
|
UARTx->IER = 0x00;
|
| 34 |
|
|
UARTx->CR = 0x00;
|
| 35 |
|
|
(void)UARTx->RxBUFR;
|
| 36 |
|
|
UARTx->RxRSTR = 0xFFFF;
|
| 37 |
|
|
UARTx->TxRSTR = 0xFFFF;
|
| 38 |
|
|
}
|
| 39 |
|
|
|
| 40 |
|
|
/*******************************************************************************
|
| 41 |
|
|
* Function Name : UART_BaudRateConfig
|
| 42 |
|
|
* Description : This function configures the baud rate of the selected UART.
|
| 43 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 44 |
|
|
* Input 2 : The baudrate value
|
| 45 |
|
|
* Output : None
|
| 46 |
|
|
* Return : None
|
| 47 |
|
|
*******************************************************************************/
|
| 48 |
|
|
void UART_BaudRateConfig(UART_TypeDef *UARTx, u32 BaudRate)
|
| 49 |
|
|
{
|
| 50 |
|
|
UARTx->BR = (u16)(RCCU_FrequencyValue(RCCU_FCLK)/(16*BaudRate));
|
| 51 |
|
|
}
|
| 52 |
|
|
|
| 53 |
|
|
/*******************************************************************************
|
| 54 |
|
|
* Function Name : UART_Config
|
| 55 |
|
|
* Description : This function configures the baudrate, the mode, the data
|
| 56 |
|
|
* parity and the number of stop bits of the selected UART.
|
| 57 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 58 |
|
|
* Input 2 : The baudrate value
|
| 59 |
|
|
* Input 3 : The parity type
|
| 60 |
|
|
* Input 4 : The number of stop bits
|
| 61 |
|
|
* Input 5 : The UART mode
|
| 62 |
|
|
* Output : None
|
| 63 |
|
|
* Return : None
|
| 64 |
|
|
*******************************************************************************/
|
| 65 |
|
|
void UART_Config(UART_TypeDef *UARTx, u32 BaudRate, UARTParity_TypeDef Parity,
|
| 66 |
|
|
UARTStopBits_TypeDef StopBits, UARTMode_TypeDef Mode)
|
| 67 |
|
|
{
|
| 68 |
|
|
UART_ModeConfig(UARTx, Mode);
|
| 69 |
|
|
UART_BaudRateConfig(UARTx, BaudRate);
|
| 70 |
|
|
UART_ParityConfig(UARTx, Parity);
|
| 71 |
|
|
UART_StopBitsConfig(UARTx, StopBits);
|
| 72 |
|
|
}
|
| 73 |
|
|
|
| 74 |
|
|
/*******************************************************************************
|
| 75 |
|
|
* Function Name : UART_ItConfig
|
| 76 |
|
|
* Description : This function enables or disables the interrupts of the
|
| 77 |
|
|
* selected UART.
|
| 78 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 79 |
|
|
* Input 2 : The new interrupt flag
|
| 80 |
|
|
* Input 3 : ENABLE or DISABLE
|
| 81 |
|
|
* Output : None
|
| 82 |
|
|
* Return : None
|
| 83 |
|
|
*******************************************************************************/
|
| 84 |
|
|
void UART_ItConfig(UART_TypeDef *UARTx, u16 UART_Flag, FunctionalState NewState)
|
| 85 |
|
|
{
|
| 86 |
|
|
if (NewState==ENABLE) UARTx->IER|=UART_Flag; else UARTx->IER&=~UART_Flag;
|
| 87 |
|
|
}
|
| 88 |
|
|
|
| 89 |
|
|
/*******************************************************************************
|
| 90 |
|
|
* Function Name : UART_FifoConfig
|
| 91 |
|
|
* Description : This function enables or disables the Rx and Tx FIFOs of
|
| 92 |
|
|
* the selected UART.
|
| 93 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 94 |
|
|
* Input 2 : ENABLE or DISABLE
|
| 95 |
|
|
* Output : None
|
| 96 |
|
|
* Return : None
|
| 97 |
|
|
*******************************************************************************/
|
| 98 |
|
|
void UART_FifoConfig(UART_TypeDef *UARTx, FunctionalState NewState)
|
| 99 |
|
|
{
|
| 100 |
|
|
if (NewState==ENABLE) UARTx->CR|=0x0400; else UARTx->CR&=~0x0400;
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
/*******************************************************************************
|
| 104 |
|
|
* Function Name : UART_FifoReset
|
| 105 |
|
|
* Description : This function resets the Rx and the Tx FIFOs of the
|
| 106 |
|
|
* selected UART.
|
| 107 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 108 |
|
|
* Input 2 : UART_RxFIFO or UART_TxFIFO
|
| 109 |
|
|
* Output : None
|
| 110 |
|
|
* Return : None
|
| 111 |
|
|
*******************************************************************************/
|
| 112 |
|
|
void UART_FifoReset(UART_TypeDef *UARTx, UARTFIFO_TypeDef FIFO)
|
| 113 |
|
|
{
|
| 114 |
|
|
if (FIFO==UART_RxFIFO) UARTx->RxRSTR=0xFFFF; else UARTx->TxRSTR=0xFFFF;
|
| 115 |
|
|
}
|
| 116 |
|
|
|
| 117 |
|
|
/*******************************************************************************
|
| 118 |
|
|
* Function Name : UART_LoopBackConfig
|
| 119 |
|
|
* Description : This function enables or disables the loop back mode of
|
| 120 |
|
|
* the selected UART.
|
| 121 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 122 |
|
|
* Input 2 : ENABLE or DISABLE
|
| 123 |
|
|
* Output : None
|
| 124 |
|
|
* Return : None
|
| 125 |
|
|
*******************************************************************************/
|
| 126 |
|
|
void UART_LoopBackConfig(UART_TypeDef *UARTx, FunctionalState NewState)
|
| 127 |
|
|
{
|
| 128 |
|
|
if (NewState==ENABLE) UARTx->CR|=0x0040; else UARTx->CR&=~0x0040;
|
| 129 |
|
|
}
|
| 130 |
|
|
|
| 131 |
|
|
/*******************************************************************************
|
| 132 |
|
|
* Function Name : UART_RxConfig
|
| 133 |
|
|
* Description : This function enables or disables the UART data reception.
|
| 134 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 135 |
|
|
* Input 2 : ENABLE or DISABLE
|
| 136 |
|
|
* Output : None
|
| 137 |
|
|
* Return : None
|
| 138 |
|
|
*******************************************************************************/
|
| 139 |
|
|
void UART_RxConfig(UART_TypeDef *UARTx, FunctionalState NewState)
|
| 140 |
|
|
{
|
| 141 |
|
|
if (NewState==ENABLE) UARTx->CR|=0x0100; else UARTx->CR&=~0x0100;
|
| 142 |
|
|
}
|
| 143 |
|
|
|
| 144 |
|
|
/*******************************************************************************
|
| 145 |
|
|
* Function Name : UART_OnOffConfig
|
| 146 |
|
|
* Description : This function sets On/Off the selected UART.
|
| 147 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 148 |
|
|
* Input 2 : ENABLE or DISABLE
|
| 149 |
|
|
* Output : None
|
| 150 |
|
|
* Return : None
|
| 151 |
|
|
*******************************************************************************/
|
| 152 |
|
|
void UART_OnOffConfig(UART_TypeDef *UARTx, FunctionalState NewState)
|
| 153 |
|
|
{
|
| 154 |
|
|
if (NewState==ENABLE) UARTx->CR|=0x0080; else UARTx->CR&=~0x0080;
|
| 155 |
|
|
}
|
| 156 |
|
|
|
| 157 |
|
|
/*******************************************************************************
|
| 158 |
|
|
* Function Name : UART_ByteSend
|
| 159 |
|
|
* Description : This function sends a data byte to the selected UART.
|
| 160 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 161 |
|
|
* Input 2 : A pointer to the data byte to send
|
| 162 |
|
|
* Output : None
|
| 163 |
|
|
* Return : None
|
| 164 |
|
|
*******************************************************************************/
|
| 165 |
|
|
void UART_ByteSend(UART_TypeDef *UARTx, u8 *Data)
|
| 166 |
|
|
{
|
| 167 |
|
|
if (UARTx->CR & (0x0001<<UART_FIFOEnableBit))// if FIFO ENABLED
|
| 168 |
|
|
while((UARTx->SR & UART_TxFull)); // while the UART_TxFIFO contain 16 characters.
|
| 169 |
|
|
else // if FIFO DISABLED
|
| 170 |
|
|
while (!(UARTx->SR & UART_TxEmpty)); // while the transmit shift register not empty
|
| 171 |
|
|
UARTx->TxBUFR = *Data;
|
| 172 |
|
|
}
|
| 173 |
|
|
|
| 174 |
|
|
/*******************************************************************************
|
| 175 |
|
|
* Function Name : UART_9BitByteSend
|
| 176 |
|
|
* Description : This function sends a 9 bits data byte to the selected UART.
|
| 177 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 178 |
|
|
* Input 2 : A pointer to the data to send
|
| 179 |
|
|
* Output : None
|
| 180 |
|
|
* Return : None
|
| 181 |
|
|
*******************************************************************************/
|
| 182 |
|
|
void UART_9BitByteSend(UART_TypeDef *UARTx, u16 *Data)
|
| 183 |
|
|
{
|
| 184 |
|
|
if(UARTx->CR & (0x0001<<UART_FIFOEnableBit))// if FIFO ENABLED
|
| 185 |
|
|
while((UARTx->SR & UART_TxFull)); // while the UART_TxFIFO contain 16 characters.
|
| 186 |
|
|
else // if FIFO DISABLED
|
| 187 |
|
|
while (!(UARTx->SR & UART_TxEmpty)); // while the transmit shift register not empty
|
| 188 |
|
|
UARTx->TxBUFR = *Data;
|
| 189 |
|
|
}
|
| 190 |
|
|
|
| 191 |
|
|
/*******************************************************************************
|
| 192 |
|
|
* Function Name : UART_DataSend
|
| 193 |
|
|
* Description : This function sends several data bytes to the selected UART.
|
| 194 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 195 |
|
|
* Input 2 : A pointer to the data to send
|
| 196 |
|
|
* Input 3 : The data length in bytes
|
| 197 |
|
|
* Output : None
|
| 198 |
|
|
* Return : None
|
| 199 |
|
|
*******************************************************************************/
|
| 200 |
|
|
void UART_DataSend(UART_TypeDef *UARTx, u8 *Data, u8 DataLength)
|
| 201 |
|
|
{
|
| 202 |
|
|
while(DataLength--)
|
| 203 |
|
|
{
|
| 204 |
|
|
UART_ByteSend(UARTx,Data);
|
| 205 |
|
|
Data++;
|
| 206 |
|
|
}
|
| 207 |
|
|
}
|
| 208 |
|
|
|
| 209 |
|
|
/*******************************************************************************
|
| 210 |
|
|
* Function Name : UART_9BitDataSend
|
| 211 |
|
|
* Description : This function sends several 9 bits data bytes to the selected UART.
|
| 212 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 213 |
|
|
* Input 2 : A pointer to the data to send
|
| 214 |
|
|
* Input 3 : The data length
|
| 215 |
|
|
* Output : None
|
| 216 |
|
|
* Return : None
|
| 217 |
|
|
*******************************************************************************/
|
| 218 |
|
|
void UART_9BitDataSend(UART_TypeDef *UARTx, u16 *Data, u8 DataLength)
|
| 219 |
|
|
{
|
| 220 |
|
|
while(DataLength--)
|
| 221 |
|
|
{
|
| 222 |
|
|
UART_9BitByteSend(UARTx,Data);
|
| 223 |
|
|
Data++;
|
| 224 |
|
|
}
|
| 225 |
|
|
}
|
| 226 |
|
|
|
| 227 |
|
|
/*******************************************************************************
|
| 228 |
|
|
* Function Name : UART_StringSend
|
| 229 |
|
|
* Description : This function sends a string to the selected UART.
|
| 230 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 231 |
|
|
* Input 2 : A pointer to the string to send
|
| 232 |
|
|
* Output : None
|
| 233 |
|
|
* Return : None
|
| 234 |
|
|
*******************************************************************************/
|
| 235 |
|
|
void UART_StringSend(UART_TypeDef *UARTx, u8 *String)
|
| 236 |
|
|
{
|
| 237 |
|
|
u8 *Data=String;
|
| 238 |
|
|
while(*Data != '\0')
|
| 239 |
|
|
UART_ByteSend(UARTx, Data++);
|
| 240 |
|
|
*Data='\0';
|
| 241 |
|
|
UART_ByteSend(UARTx, Data);
|
| 242 |
|
|
}
|
| 243 |
|
|
|
| 244 |
|
|
/*******************************************************************************
|
| 245 |
|
|
* Function Name : UART_ByteReceive
|
| 246 |
|
|
* Description : This function gets a data byte from the selected UART.
|
| 247 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 248 |
|
|
* Input 2 : A pointer to the buffer where the data will be stored
|
| 249 |
|
|
* Input 3 : The time-out period
|
| 250 |
|
|
* Output : The received data
|
| 251 |
|
|
* Return : The UARTx.SR register contents
|
| 252 |
|
|
*******************************************************************************/
|
| 253 |
|
|
u16 UART_ByteReceive(UART_TypeDef *UARTx, u8 *Data, u8 TimeOut)
|
| 254 |
|
|
{
|
| 255 |
|
|
u16 wStatus;
|
| 256 |
|
|
UARTx->TOR=TimeOut;// reload the Timeout counter
|
| 257 |
|
|
while (!((wStatus=UARTx->SR) & (UART_TimeOutIdle|UART_RxHalfFull|UART_RxBufFull)));// while the UART_RxFIFO is empty and no Timeoutidle
|
| 258 |
|
|
*Data = (u8)UARTx->RxBUFR; // then read the Receive Buffer Register
|
| 259 |
|
|
return wStatus;
|
| 260 |
|
|
}
|
| 261 |
|
|
|
| 262 |
|
|
/*******************************************************************************
|
| 263 |
|
|
* Function Name : UART_9BitByteReceive
|
| 264 |
|
|
* Description : This function gets a 9 bits data byte from the selected UART.
|
| 265 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 266 |
|
|
* Input 2 : A pointer to the buffer where the data will be stored
|
| 267 |
|
|
* Input 3 : The time-out period value
|
| 268 |
|
|
* Output : The received data
|
| 269 |
|
|
* Return : The UARTx.SR register contents
|
| 270 |
|
|
*******************************************************************************/
|
| 271 |
|
|
u16 UART_9BitByteReceive(UART_TypeDef *UARTx, u16 *Data, u8 TimeOut)
|
| 272 |
|
|
{
|
| 273 |
|
|
u16 wStatus;
|
| 274 |
|
|
UARTx->TOR=TimeOut;// reload the Timeout counter
|
| 275 |
|
|
while (!((wStatus=UARTx->SR) & (UART_TimeOutIdle|UART_RxHalfFull|UART_RxBufFull)));// while the UART_RxFIFO is empty and no Timeoutidle
|
| 276 |
|
|
*Data = (u16)UARTx->RxBUFR; // then read the RxBUFR
|
| 277 |
|
|
return wStatus;
|
| 278 |
|
|
}
|
| 279 |
|
|
|
| 280 |
|
|
/*******************************************************************************
|
| 281 |
|
|
* Function Name : UART_DataReceive
|
| 282 |
|
|
* Description : This function gets 8 bits data bytes from the selected UART.
|
| 283 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 284 |
|
|
* Input 2 : A pointer to the buffer where the data will be stored
|
| 285 |
|
|
* Input 3 : The data length
|
| 286 |
|
|
* Input 4 : The time-out period value
|
| 287 |
|
|
* Output : The received data
|
| 288 |
|
|
* Return : The UARTx.SR register contents
|
| 289 |
|
|
*******************************************************************************/
|
| 290 |
|
|
u16 UART_DataReceive(UART_TypeDef *UARTx, u8 *Data, u8 DataLength, u8 TimeOut)
|
| 291 |
|
|
{
|
| 292 |
|
|
u16 wStatus;
|
| 293 |
|
|
while(DataLength--)
|
| 294 |
|
|
wStatus=UART_ByteReceive(UARTx,Data++,TimeOut);
|
| 295 |
|
|
return wStatus;
|
| 296 |
|
|
}
|
| 297 |
|
|
|
| 298 |
|
|
/*******************************************************************************
|
| 299 |
|
|
* Function Name : UART_9BitDataReceive
|
| 300 |
|
|
* Description : This function gets 9 bits data bytes from the selected UART.
|
| 301 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 302 |
|
|
* Input 2 : A pointer to the buffer where the data will be stored
|
| 303 |
|
|
* Input 3 : The data length
|
| 304 |
|
|
* Input 4 : The time-out value
|
| 305 |
|
|
* Output : The received data
|
| 306 |
|
|
* Return : The UARTx.SR register contents
|
| 307 |
|
|
*******************************************************************************/
|
| 308 |
|
|
u16 UART_9BitDataReceive(UART_TypeDef *UARTx, u16 *Data, u8 DataLength, u8 TimeOut)
|
| 309 |
|
|
{
|
| 310 |
|
|
u16 wStatus;
|
| 311 |
|
|
while(DataLength--)
|
| 312 |
|
|
wStatus=UART_9BitByteReceive(UARTx,Data++,TimeOut);
|
| 313 |
|
|
return wStatus;
|
| 314 |
|
|
}
|
| 315 |
|
|
|
| 316 |
|
|
/*******************************************************************************
|
| 317 |
|
|
* Function Name : UART_StringReceive
|
| 318 |
|
|
* Description : This function gets 8 bits data bytes from the selected UART.
|
| 319 |
|
|
* Input 1 : UARTx (x can be 0,1, 2 or 3) the desired UART
|
| 320 |
|
|
* Input 2 : A pointer to the buffer where the string will be stored
|
| 321 |
|
|
* Output : The received string
|
| 322 |
|
|
* Return : The UARTx.SR register contents
|
| 323 |
|
|
*******************************************************************************/
|
| 324 |
|
|
u16 UART_StringReceive(UART_TypeDef *UARTx, u8 *Data)
|
| 325 |
|
|
{
|
| 326 |
|
|
u8 *pSTRING=Data;
|
| 327 |
|
|
u16 wStatus;
|
| 328 |
|
|
do
|
| 329 |
|
|
{
|
| 330 |
|
|
while (!((wStatus=UARTx->SR) & (UART_RxHalfFull|UART_RxBufFull)));// while the UART_RxFIFO is empty and no Timeoutidle
|
| 331 |
|
|
*(pSTRING++) = (u8)UARTx->RxBUFR; // then read the RxBUFR
|
| 332 |
|
|
} while((*(pSTRING - 1)!=0x0D)&(*(pSTRING - 1)!='\0'));
|
| 333 |
|
|
*(pSTRING - 1)='\0';
|
| 334 |
|
|
return wStatus;
|
| 335 |
|
|
}
|
| 336 |
|
|
|
| 337 |
|
|
#ifdef USE_SERIAL_PORT
|
| 338 |
|
|
/*******************************************************************************
|
| 339 |
|
|
* Function Name : sendchar
|
| 340 |
|
|
* Description : This function sends a character to the selected UART.
|
| 341 |
|
|
* Input 1 : A pointer to the character to send.
|
| 342 |
|
|
* Output : None
|
| 343 |
|
|
* Return : None
|
| 344 |
|
|
*******************************************************************************/
|
| 345 |
|
|
void sendchar( char *ch )
|
| 346 |
|
|
{
|
| 347 |
|
|
#ifdef USE_UART0
|
| 348 |
|
|
#define UARTx UART0
|
| 349 |
|
|
#endif /* Use_UART0 */
|
| 350 |
|
|
|
| 351 |
|
|
#ifdef USE_UART1
|
| 352 |
|
|
#define UARTx UART1
|
| 353 |
|
|
#endif /* Use_UART1 */
|
| 354 |
|
|
|
| 355 |
|
|
#ifdef USE_UART2
|
| 356 |
|
|
#define UARTx UART2
|
| 357 |
|
|
#endif /* Use_UART2 */
|
| 358 |
|
|
|
| 359 |
|
|
#ifdef USE_UART3
|
| 360 |
|
|
#define UARTx UART3
|
| 361 |
|
|
#endif /* Use_UART3 */
|
| 362 |
|
|
|
| 363 |
|
|
UART_ByteSend(UARTx,(u8 *)ch);
|
| 364 |
|
|
}
|
| 365 |
|
|
#endif /* USE_SERIAL_PORT */
|
| 366 |
|
|
|
| 367 |
|
|
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
|