FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
FASTUSBasp/lib/serial.cpp at master · feer9/FASTUSBasp · GitHub
feer9
/
FASTUSBasp
Public
forked from
amitesh-singh/FASTUSBasp
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
FASTUSBasp
/
lib
/
serial.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
150 lines (131 loc) · 4.39 KB
Breadcrumbs
FASTUSBasp
/
lib
/
serial.cpp
Copy path
File metadata and controls
150 lines (131 loc) · 4.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* FASTUSBasp v2 - Fastest programmer for AVR
* Copyright (C) 2018 Amitesh Singh <singh.amitesh@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#
include
"
serial.h
"
#
include
"
pin.h
"
#
include
"
rcc.h
"
//
Note:
//
In Stm32f1, USART use abp2 frequency for USART1
//
and it uses abp1 frequency for other USARTs (2 and 3).
//
//
Only USART1 can run at the maximum speed of 4.5Mbits/s. Other USART are limited to 2.25Mbits/s.
void
serial::begin
(SerialType _st,
uint32_t
usart)
{
//
RCC::enable(RCC_AFIO);
//
Tested and works
if
(_st == SerialType::Serial1)
{
_nativeUSART =
USART1
;
RCC::enable
(
RCC_GPIOA
);
RCC::enable
(
RCC_USART1
);
if
(_serialMode == SerialMode::
RX
|| _serialMode == SerialMode::
TX_RX
)
{
_rx.
assign
(
GPIO10
,
GPIOA
);
_rx.
setMode
(
GPIO_MODE_AF
,
GPIO_PUPD_PULLUP
);
_rx.
setAF
(
GPIO_AF7
);
}
if
(_serialMode == SerialMode::
TX
|| _serialMode == SerialMode::
TX_RX
)
{
_tx.
assign
(
GPIO9
,
GPIOA
);
_tx.
setMode
(
GPIO_MODE_AF
,
GPIO_PUPD_PULLUP
);
_tx.
setAF
(
GPIO_AF7
);
_tx.
setOutputOptions
(
GPIO_OTYPE_PP
,
GPIO_OSPEED_2MHZ
);
//
PushPull, originally 2MHz
}
}
else
if
(_st == SerialType::Serial2)
{
//
Tested and it works
_nativeUSART =
USART2
;
RCC::enable
(
RCC_GPIOA
);
RCC::enable
(
RCC_USART2
);
if
(_serialMode == SerialMode::
RX
|| _serialMode == SerialMode::
TX_RX
)
{
_rx.
assign
(
GPIO3
,
GPIOA
);
_rx.
setMode
(
GPIO_MODE_AF
,
GPIO_PUPD_PULLUP
);
_rx.
setAF
(
GPIO_AF7
);
}
if
(_serialMode == SerialMode::
TX
|| _serialMode == SerialMode::
TX_RX
)
{
_tx.
assign
(
GPIO2
,
GPIOA
);
_tx.
setMode
(
GPIO_MODE_AF
,
GPIO_PUPD_PULLUP
);
_tx.
setAF
(
GPIO_AF7
);
_tx.
setOutputOptions
(
GPIO_OTYPE_PP
,
GPIO_OSPEED_2MHZ
);
}
}
else
if
(_st == SerialType::Serial3)
{
_nativeUSART =
USART3
;
//
there is no USART6 ?¿?¿
RCC::enable
(
RCC_GPIOC
);
RCC::enable
(
RCC_USART3
);
if
(_serialMode == SerialMode::
RX
|| _serialMode == SerialMode::
TX_RX
)
{
//
GPIO_USART3_RX
_rx.
assign
(
GPIO7
,
GPIOC
);
_rx.
setMode
(
GPIO_MODE_AF
,
GPIO_PUPD_PULLUP
);
_rx.
setAF
(
GPIO_AF7
);
}
if
(_serialMode == SerialMode::
TX
|| _serialMode == SerialMode::
TX_RX
)
{
//
GPIO_USART3_TX
_tx.
assign
(
GPIO6
,
GPIOC
);
_tx.
setMode
(
GPIO_MODE_AF
,
GPIO_PUPD_PULLUP
);
_tx.
setAF
(
GPIO_AF7
);
_tx.
setOutputOptions
(
GPIO_OTYPE_PP
,
GPIO_OSPEED_2MHZ
);
}
}
//
disable the USAST before setting it.
usart_disable
(_nativeUSART);
usart_set_baudrate
(_nativeUSART, usart);
usart_set_databits
(_nativeUSART,
8
);
usart_set_stopbits
(_nativeUSART,
USART_STOPBITS_1
);
if
(_serialMode == SerialMode::
TX
)
usart_set_mode
(_nativeUSART,
USART_MODE_TX
);
else
if
(_serialMode == SerialMode::
TX_RX
)
usart_set_mode
(_nativeUSART,
USART_MODE_TX_RX
);
else
usart_set_mode
(_nativeUSART,
USART_MODE_RX
);
usart_set_parity
(_nativeUSART,
USART_PARITY_NONE
);
usart_set_flow_control
(_nativeUSART,
USART_FLOWCONTROL_NONE
);
/*
Finally enable the USART.
*/
usart_enable
(_nativeUSART);
}
void
serial::print
(
const
char
*s)
{
while
(*s !=
0
)
{
usart_send_blocking
(_nativeUSART, *s);
s++;
}
}
void
serial::println
(
const
char
*s)
{
print
(s);
usart_send_blocking
(_nativeUSART,
'
\r
'
);
usart_send_blocking
(_nativeUSART,
'
\n
'
);
}
uint16_t
serial::recv
()
{
return
usart_recv_blocking
(_nativeUSART);
}
#
include
<
cstdio
>
void
serial::printint
(
int
x)
{
char
buf[
10
];
sprintf
(buf,
"
%d
"
, x);
print
(buf);
}
Back
|
FazBrowse Home
|
New Git URL