Skip to content

Commit 4f07e93

Browse files
committed
Moved some UART stuff into rp-hal-common.
1 parent 5cb6f9e commit 4f07e93

File tree

9 files changed

+153
-259
lines changed

9 files changed

+153
-259
lines changed

rp-hal-common/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ repository = "https://github.com/rp-rs/rp-hal"
99
rust-version = "1.77"
1010
version = "0.1.0"
1111

12-
[dependencies]
1312
# DO NOT LIST ANY PAC CRATES OR ARCHITECTURE CRATES HERE
13+
[dependencies]
14+
fugit = "0.3.7"

rp-hal-common/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
99
#![no_std]
1010

11-
/// Not useful - just a placeholder.
12-
pub struct Placeholder;
11+
pub mod uart;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Common UART configurations
2+
3+
use fugit::HertzU32;
4+
5+
use super::{DataBits, StopBits, UartConfig};
6+
7+
/// 9600 baud, 8 data bits, no parity, 1 stop bit
8+
pub const _9600_8_N_1: UartConfig = UartConfig {
9+
baudrate: HertzU32::from_raw(9600),
10+
data_bits: DataBits::Eight,
11+
stop_bits: StopBits::One,
12+
parity: None,
13+
};
14+
15+
/// 19200 baud, 8 data bits, no parity, 1 stop bit
16+
pub const _19200_8_N_1: UartConfig = UartConfig {
17+
baudrate: HertzU32::from_raw(19200),
18+
data_bits: DataBits::Eight,
19+
stop_bits: StopBits::One,
20+
parity: None,
21+
};
22+
23+
/// 38400 baud, 8 data bits, no parity, 1 stop bit
24+
pub const _38400_8_N_1: UartConfig = UartConfig {
25+
baudrate: HertzU32::from_raw(38400),
26+
data_bits: DataBits::Eight,
27+
stop_bits: StopBits::One,
28+
parity: None,
29+
};
30+
31+
/// 57600 baud, 8 data bits, no parity, 1 stop bit
32+
pub const _57600_8_N_1: UartConfig = UartConfig {
33+
baudrate: HertzU32::from_raw(57600),
34+
data_bits: DataBits::Eight,
35+
stop_bits: StopBits::One,
36+
parity: None,
37+
};
38+
39+
/// 115200 baud, 8 data bits, no parity, 1 stop bit
40+
pub const _115200_8_N_1: UartConfig = UartConfig {
41+
baudrate: HertzU32::from_raw(115200),
42+
data_bits: DataBits::Eight,
43+
stop_bits: StopBits::One,
44+
parity: None,
45+
};

rp-hal-common/src/uart/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Shared code and types for Raspberry Pi Silicon UARTS
2+
3+
pub mod common_configs;
4+
mod utils;
5+
6+
pub use utils::*;

rp-hal-common/src/uart/utils.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//! Useful UART types
2+
3+
use fugit::HertzU32;
4+
5+
/// Data bits
6+
pub enum DataBits {
7+
/// 5 bits
8+
Five,
9+
/// 6 bits
10+
Six,
11+
/// 7 bits
12+
Seven,
13+
/// 8 bits
14+
Eight,
15+
}
16+
17+
/// Stop bits
18+
pub enum StopBits {
19+
/// 1 bit
20+
One,
21+
/// 2 bits
22+
Two,
23+
}
24+
25+
/// Parity
26+
///
27+
/// The "none" state of parity is represented with the Option type (None).
28+
pub enum Parity {
29+
/// Odd parity
30+
Odd,
31+
/// Even parity
32+
Even,
33+
}
34+
35+
/// A struct holding the configuration for an UART device.
36+
///
37+
/// The `Default` implementation implements the following values:
38+
/// ```ignore
39+
/// # // can't actually create this with the non_exhaustive attribute
40+
/// UartConfig {
41+
/// baudrate: Baud(115_200),
42+
/// data_bits: DataBits::Eight,
43+
/// stop_bits: StopBits::One,
44+
/// parity: None,
45+
///}
46+
/// ```
47+
#[non_exhaustive]
48+
pub struct UartConfig {
49+
/// The baudrate the uart will run at.
50+
pub baudrate: HertzU32,
51+
52+
/// The amount of data bits the uart should be configured to.
53+
pub data_bits: DataBits,
54+
55+
/// The amount of stop bits the uart should be configured to.
56+
pub stop_bits: StopBits,
57+
58+
/// The parity that this uart should have
59+
pub parity: Option<Parity>,
60+
}
61+
62+
impl UartConfig {
63+
/// Create a new instance of UartConfig
64+
pub const fn new(
65+
baudrate: HertzU32,
66+
data_bits: DataBits,
67+
parity: Option<Parity>,
68+
stop_bits: StopBits,
69+
) -> UartConfig {
70+
UartConfig {
71+
baudrate,
72+
data_bits,
73+
stop_bits,
74+
parity,
75+
}
76+
}
77+
}
78+
79+
impl Default for UartConfig {
80+
fn default() -> Self {
81+
Self {
82+
baudrate: HertzU32::from_raw(115_200),
83+
data_bits: DataBits::Eight,
84+
stop_bits: StopBits::One,
85+
parity: None,
86+
}
87+
}
88+
}

rp2040-hal/src/uart/common_configs.rs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,2 @@
1-
use fugit::HertzU32;
2-
3-
use super::{DataBits, StopBits, UartConfig};
4-
5-
/// 9600 baud, 8 data bits, no parity, 1 stop bit
6-
pub const _9600_8_N_1: UartConfig = UartConfig {
7-
baudrate: HertzU32::from_raw(9600),
8-
data_bits: DataBits::Eight,
9-
stop_bits: StopBits::One,
10-
parity: None,
11-
};
12-
13-
/// 19200 baud, 8 data bits, no parity, 1 stop bit
14-
pub const _19200_8_N_1: UartConfig = UartConfig {
15-
baudrate: HertzU32::from_raw(19200),
16-
data_bits: DataBits::Eight,
17-
stop_bits: StopBits::One,
18-
parity: None,
19-
};
20-
21-
/// 38400 baud, 8 data bits, no parity, 1 stop bit
22-
pub const _38400_8_N_1: UartConfig = UartConfig {
23-
baudrate: HertzU32::from_raw(38400),
24-
data_bits: DataBits::Eight,
25-
stop_bits: StopBits::One,
26-
parity: None,
27-
};
28-
29-
/// 57600 baud, 8 data bits, no parity, 1 stop bit
30-
pub const _57600_8_N_1: UartConfig = UartConfig {
31-
baudrate: HertzU32::from_raw(57600),
32-
data_bits: DataBits::Eight,
33-
stop_bits: StopBits::One,
34-
parity: None,
35-
};
36-
37-
/// 115200 baud, 8 data bits, no parity, 1 stop bit
38-
pub const _115200_8_N_1: UartConfig = UartConfig {
39-
baudrate: HertzU32::from_raw(115200),
40-
data_bits: DataBits::Eight,
41-
stop_bits: StopBits::One,
42-
parity: None,
43-
};
1+
#[doc(inline)]
2+
pub use rp_hal_common::uart::common_configs::*;

rp2040-hal/src/uart/utils.rs

Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::pac::{uart0::RegisterBlock, UART0, UART1};
33
use crate::resets::SubsystemReset;
44
use crate::typelevel::Sealed;
55
use core::ops::Deref;
6-
use fugit::HertzU32;
6+
7+
#[doc(inline)]
8+
pub use rp_hal_common::uart::{DataBits, Parity, StopBits, UartConfig};
79

810
/// Error type for UART operations.
911
#[derive(Debug)]
@@ -68,79 +70,6 @@ impl Sealed for Enabled {}
6870
impl State for Disabled {}
6971
impl Sealed for Disabled {}
7072

71-
/// Data bits
72-
pub enum DataBits {
73-
/// 5 bits
74-
Five,
75-
/// 6 bits
76-
Six,
77-
/// 7 bits
78-
Seven,
79-
/// 8 bits
80-
Eight,
81-
}
82-
83-
/// Stop bits
84-
pub enum StopBits {
85-
/// 1 bit
86-
One,
87-
/// 2 bits
88-
Two,
89-
}
90-
91-
/// Parity
92-
/// The "none" state of parity is represented with the Option type (None).
93-
pub enum Parity {
94-
/// Odd parity
95-
Odd,
96-
/// Even parity
97-
Even,
98-
}
99-
100-
/// A struct holding the configuration for an UART device.
101-
///
102-
/// The `Default` implementation implements the following values:
103-
/// ```ignore
104-
/// # // can't actually create this with the non_exhaustive attribute
105-
/// UartConfig {
106-
/// baudrate: Baud(115_200),
107-
/// data_bits: DataBits::Eight,
108-
/// stop_bits: StopBits::One,
109-
/// parity: None,
110-
///}
111-
/// ```
112-
#[non_exhaustive]
113-
pub struct UartConfig {
114-
/// The baudrate the uart will run at.
115-
pub baudrate: HertzU32,
116-
117-
/// The amount of data bits the uart should be configured to.
118-
pub data_bits: DataBits,
119-
120-
/// The amount of stop bits the uart should be configured to.
121-
pub stop_bits: StopBits,
122-
123-
/// The parity that this uart should have
124-
pub parity: Option<Parity>,
125-
}
126-
127-
impl UartConfig {
128-
/// Create a new instance of UartConfig
129-
pub const fn new(
130-
baudrate: HertzU32,
131-
data_bits: DataBits,
132-
parity: Option<Parity>,
133-
stop_bits: StopBits,
134-
) -> UartConfig {
135-
UartConfig {
136-
baudrate,
137-
data_bits,
138-
stop_bits,
139-
parity,
140-
}
141-
}
142-
}
143-
14473
/// Rx/Tx FIFO Watermark
14574
///
14675
/// Determine the FIFO level that trigger DMA/Interrupt
@@ -162,14 +91,3 @@ pub enum FifoWatermark {
16291
/// Trigger when 28 bytes are (Rx: filled / Tx: available)
16392
Bytes28,
16493
}
165-
166-
impl Default for UartConfig {
167-
fn default() -> Self {
168-
Self {
169-
baudrate: HertzU32::from_raw(115_200),
170-
data_bits: DataBits::Eight,
171-
stop_bits: StopBits::One,
172-
parity: None,
173-
}
174-
}
175-
}

rp235x-hal/src/uart/common_configs.rs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,2 @@
1-
use fugit::HertzU32;
2-
3-
use super::{DataBits, StopBits, UartConfig};
4-
5-
/// 9600 baud, 8 data bits, no parity, 1 stop bit
6-
pub const _9600_8_N_1: UartConfig = UartConfig {
7-
baudrate: HertzU32::from_raw(9600),
8-
data_bits: DataBits::Eight,
9-
stop_bits: StopBits::One,
10-
parity: None,
11-
};
12-
13-
/// 19200 baud, 8 data bits, no parity, 1 stop bit
14-
pub const _19200_8_N_1: UartConfig = UartConfig {
15-
baudrate: HertzU32::from_raw(19200),
16-
data_bits: DataBits::Eight,
17-
stop_bits: StopBits::One,
18-
parity: None,
19-
};
20-
21-
/// 38400 baud, 8 data bits, no parity, 1 stop bit
22-
pub const _38400_8_N_1: UartConfig = UartConfig {
23-
baudrate: HertzU32::from_raw(38400),
24-
data_bits: DataBits::Eight,
25-
stop_bits: StopBits::One,
26-
parity: None,
27-
};
28-
29-
/// 57600 baud, 8 data bits, no parity, 1 stop bit
30-
pub const _57600_8_N_1: UartConfig = UartConfig {
31-
baudrate: HertzU32::from_raw(57600),
32-
data_bits: DataBits::Eight,
33-
stop_bits: StopBits::One,
34-
parity: None,
35-
};
36-
37-
/// 115200 baud, 8 data bits, no parity, 1 stop bit
38-
pub const _115200_8_N_1: UartConfig = UartConfig {
39-
baudrate: HertzU32::from_raw(115200),
40-
data_bits: DataBits::Eight,
41-
stop_bits: StopBits::One,
42-
parity: None,
43-
};
1+
#[doc(inline)]
2+
pub use rp_hal_common::uart::common_configs::*;

0 commit comments

Comments
 (0)