[c1aed7]: stm8s_uart2.c Maximize Restore History

Download this file

stm8s_uart2.c    179 lines (149 with data), 7.3 kB

  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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
********************************************************************************
* @file stm8s_uart2.c
* @brief This file contains all the functions for the UART2 peripheral.
* @author STMicroelectronics - MCD Application Team
* @version V1.1.1
* @date 06/05/2009
******************************************************************************
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
* @image html logo.bmp
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "stm8s_uart2.h"
#include "stm8s_clk.h"
#include "includes.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
/** @}
* @addtogroup UART2_Public_Functions
* @{
*/
/**
* @brief Initializes the UART2 according to the specified parameters.
* @param[in] BaudRate: The baudrate.
* @param[in] WordLength : This parameter can be any of the @ref UART2_WordLength_TypeDef enumeration.
* @param[in] StopBits: This parameter can be any of the @ref UART2_StopBits_TypeDef enumeration.
* @param[in] Parity: This parameter can be any of the @ref UART2_Parity_TypeDef enumeration.
* @param[in] SyncMode: This parameter can be any of the @ref UART2_SyncMode_TypeDef values.
* @param[in] Mode: This parameter can be any of the @ref UART2_Mode_TypeDef values
* @retval None
*/
void UART2_Init(u32 BaudRate, UART2_WordLength_TypeDef WordLength, UART2_StopBits_TypeDef StopBits, UART2_Parity_TypeDef Parity, UART2_SyncMode_TypeDef SyncMode, UART2_Mode_TypeDef Mode)
{
u8 BRR2_1, BRR2_2 = 0;
u32 BaudRate_Mantissa, BaudRate_Mantissa100 = 0;
/* assert_param: BaudRate value should be <= 625000 bps */
assert_param(IS_UART2_BAUDRATE_OK(BaudRate));
assert_param(IS_UART2_WORDLENGTH_OK(WordLength));
assert_param(IS_UART2_STOPBITS_OK(StopBits));
assert_param(IS_UART2_PARITY_OK(Parity));
/* assert_param: UART2_Mode value should exclude values such as UART2_ModeTx_Enable|UART2_ModeTx_Disable */
assert_param(IS_UART2_MODE_OK((u8)Mode));
/* assert_param: UART2_SyncMode value should exclude values such as
UART2_CLOCK_ENABLE|UART2_CLOCK_DISABLE */
assert_param(IS_UART2_SYNCMODE_OK((u8)SyncMode));
UART2->CR1 &= (u8)(~UART2_CR1_M); /**< Clear the word length bit */
UART2->CR1 |= (u8)WordLength; /**< Set the word length bit according to UART2_WordLength value */
UART2->CR3 &= (u8)(~UART2_CR3_STOP); /**< Clear the STOP bits */
UART2->CR3 |= (u8)StopBits; /**< Set the STOP bits number according to UART2_StopBits value */
UART2->CR1 &= (u8)(~(UART2_CR1_PCEN | UART2_CR1_PS )); /**< Clear the Parity Control bit */
UART2->CR1 |= (u8)Parity; /**< Set the Parity Control bit to UART2_Parity value */
UART2->BRR1 &= (u8)(~UART2_BRR1_DIVM); /**< Clear the LSB mantissa of UARTDIV */
UART2->BRR2 &= (u8)(~UART2_BRR2_DIVM); /**< Clear the MSB mantissa of UARTDIV */
UART2->BRR2 &= (u8)(~UART2_BRR2_DIVF); /**< Clear the Fraction bits of UARTDIV */
/**< Set the UART2 BaudRates in BRR1 and BRR2 registers according to UART2_BaudRate value */
BaudRate_Mantissa = ((u32)CLK_GetClockFreq() / (BaudRate << 4));
BaudRate_Mantissa100 = (((u32)CLK_GetClockFreq() * 100) / (BaudRate << 4));
/**< The fraction and MSB mantissa should be loaded in one step in the BRR2 register*/
BRR2_1 = (u8)((u8)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100))
<< 4) / 100) & (u8)0x0F); /**< Set the fraction of UARTDIV */
BRR2_2 = (u8)((BaudRate_Mantissa >> 4) & (u8)0xF0);
UART2->BRR2 = (u8)(BRR2_1 | BRR2_2);
UART2->BRR1 = (u8)BaudRate_Mantissa; /**< Set the LSB mantissa of UARTDIV */
UART2->CR2 &= (u8)~(UART2_CR2_TEN | UART2_CR2_REN); /**< Disable the Transmitter and Receiver before seting the LBCL, CPOL and CPHA bits */
UART2->CR3 &= (u8)~(UART2_CR3_CPOL | UART2_CR3_CPHA | UART2_CR3_LBCL); /**< Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */
UART2->CR3 |= (u8)((u8)SyncMode & (u8)(UART2_CR3_CPOL | UART2_CR3_CPHA | UART2_CR3_LBCL)); /**< Set the Clock Polarity, lock Phase, Last Bit Clock pulse */
if ((u8)Mode & (u8)UART2_MODE_TX_ENABLE)
{
UART2->CR2 |= (u8)UART2_CR2_TEN; /**< Set the Transmitter Enable bit */
}
else
{
UART2->CR2 &= (u8)(~UART2_CR2_TEN); /**< Clear the Transmitter Disable bit */
}
if ((u8)Mode & (u8)UART2_MODE_RX_ENABLE)
{
UART2->CR2 |= (u8)UART2_CR2_REN; /**< Set the Receiver Enable bit */
}
else
{
UART2->CR2 &= (u8)(~UART2_CR2_REN); /**< Clear the Receiver Disable bit */
}
/**< Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit Clock pulse bits according to UART2_Mode value */
if ((u8)SyncMode&(u8)UART2_SYNCMODE_CLOCK_DISABLE)
{
UART2->CR3 &= (u8)(~UART2_CR3_CKEN); /**< Clear the Clock Enable bit */
/**< configure in Push Pull or Open Drain mode the Tx I/O line by setting the correct I/O Port register according the product package and line configuration*/
}
else
{
UART2->CR3 |= (u8)((u8)SyncMode & UART2_CR3_CKEN);
}
UART2->CR1 &= (u8)(~UART2_CR1_UARTD); /**< UART2 Enable */
}
/**
* @brief Returns the most recent received data by the UART2 peripheral.
* @par Full description:
* Returns the most recent received data by the UART2 peripheral.
* @retval u16 Received Data
* @par Required preconditions:
* UART2_Cmd(ENABLE);
*/
u8 UART2_ReceiveData8(void)
{
return ((u8)UART2->DR);
}
/**
* @brief Transmits 8 bit data through the UART2 peripheral.
* @par Full description:
* Transmits 8 bit data through the UART2 peripheral.
* @param[in] Data: the data to transmit.
* @retval None
* @par Required preconditions:
* UART2_Cmd(ENABLE);
*/
void UART2_SendData8(u8 Data)
{
/* Transmit Data */
UART2->DR = Data;
}
void UART2_WaitTX()
{
// while(!VS1053_CHECK_XPORTRTS);
while(!(UART2->SR & UART2_SR_TXE));
}
void UART2_WaitRX()
{
while(!(UART2->SR & UART2_SR_RXNE));
}
unsigned char UART2_IsRX()
{
return (UART2->SR & UART2_SR_RXNE);
}
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/