This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathf103rb_usart.cpp
79 lines (67 loc) · 2.17 KB
/
f103rb_usart.cpp
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
/**
* @file f103rb_usart.cpp
* @author ZiTe <[email protected]>
*/
#include "f103rb_usart.hpp"
namespace F103RB
{
/**
* @brief Only for USART2
*/
USART::USART(uint32_t baud_Rate)
{
this->_BaudRate = baud_Rate;
}
void USART::Init()
{
GPIO tx(PA2, GPIO_Mode_AF_PP, GPIO_Speed_50MHz);
GPIO rx(PA3, GPIO_Mode_IN_FLOATING);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = this->_BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
// Enable "Receive data register not empty" interrupt
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
// Enable USART
USART_Cmd(USART2, ENABLE);
// Clear "Transmission Complete" flag, else the first bit of data will lose.
USART_ClearFlag(USART2, USART_FLAG_TC);
}
void USART::Send(uint8_t *data)
{
for (int i = 0; data[i] != '\0'; i++)
{
// Transmits single data through the USARTx peripheral
USART_SendData(USART2, (uint16_t)data[i]);
// Wait until transmission complete, use TC or TXE flag
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{
/* Null */
}
}
}
void USART::Send(std::string data)
{
for (int i = 0; data[i] != '\0'; i++)
{
// Transmits single data through the USARTx peripheral
USART_SendData(USART2, (uint16_t)data[i]);
// Wait until transmission complete, use TC or TXE flag
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{
/* Null */
}
}
}
}