Skip to main content

ch32_hal/
patches.rs

1//! Patches for some peripherals
2//!
3//! This should be removed in the future.
4//!
5//! - Patches for remap implementation
6//! - Patches for peripherals without remap(fake remap)
7
8#[cfg(peri_i2c2)]
9mod i2c2 {
10    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::I2C2 {
11        fn set_remap(_remap: u8) {}
12    }
13    impl crate::peripheral::RemapPeripheral for crate::peripherals::I2C2 {}
14}
15
16/// CH32V2, CH32V3
17#[cfg(all(any(ch32v2, ch32v3), peri_usart1))]
18mod usart1 {
19    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::USART1 {
20        fn set_remap(remap: u8) {
21            let afio = &crate::pac::AFIO;
22            afio.pcfr1().modify(|w| w.set_usart1_rm(remap & 0b1 != 0));
23            afio.pcfr2().modify(|w| w.set_usart1_rm2(remap & 0b10 != 0));
24        }
25    }
26    impl crate::peripheral::RemapPeripheral for crate::peripherals::USART1 {}
27}
28
29#[cfg(all(ch32v4, peri_usart1))]
30mod usart1 {
31    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::USART1 {
32        fn set_remap(remap: u8) {
33            let afio = crate::pac::AFIO;
34            afio.pcfr1().modify(|w| w.set_usart1_rm(remap & 1 != 0));
35            afio.pcfr2().modify(|w| w.set_usart1_rm1(remap & 2 != 0));
36        }
37    }
38    impl crate::peripheral::RemapPeripheral for crate::peripherals::USART1 {}
39}
40
41#[cfg(all(any(ch32v0, ch32m0), peri_usart1))]
42mod usart1 {
43    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::USART1 {
44        fn set_remap(remap: u8) {
45            let afio = &crate::pac::AFIO;
46            #[cfg(ch32v003)]
47            {
48                afio.pcfr1().modify(|w| w.set_usart1_rm(remap & 0b1 != 0));
49                afio.pcfr1().modify(|w| w.set_usart1_rm1(remap & 0b10 != 0));
50            }
51            #[cfg(not(ch32v003))]
52            {
53                afio.pcfr1().modify(|w| w.set_usart1_rm(remap));
54            }
55        }
56    }
57    impl crate::peripheral::RemapPeripheral for crate::peripherals::USART1 {}
58}
59
60#[cfg(all(any(ch32v0, ch32m0), not(ch32v003), peri_usart2))]
61mod usart2 {
62    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::USART2 {
63        fn set_remap(remap: u8) {
64            let _ = &crate::pac::AFIO.pcfr1().modify(|w| w.set_usart2_rm(remap));
65        }
66    }
67    impl crate::peripheral::RemapPeripheral for crate::peripherals::USART2 {}
68}
69
70#[cfg(all(peri_i2c1, any(ch32v0, ch32m0)))]
71mod i2c1 {
72    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::I2C1 {
73        fn set_remap(remap: u8) {
74            let afio = &crate::pac::AFIO;
75            #[cfg(ch32v003)]
76            {
77                afio.pcfr1().modify(|w| w.set_i2c1_rm(remap & 0b1 != 0));
78                afio.pcfr1().modify(|w| w.set_i2c1_rm1(remap & 0b10 != 0));
79            }
80            #[cfg(not(ch32v003))]
81            {
82                afio.pcfr1().modify(|w| w.set_i2c1_rm(remap));
83            }
84        }
85    }
86    impl crate::peripheral::RemapPeripheral for crate::peripherals::I2C1 {}
87}
88
89#[cfg(peri_spi2)]
90mod spi2 {
91    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::SPI2 {
92        fn set_remap(_remap: u8) {}
93    }
94    impl crate::peripheral::RemapPeripheral for crate::peripherals::SPI2 {}
95}
96
97#[cfg(all(peri_tim5))]
98mod tim5 {
99    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::TIM5 {
100        fn set_remap(_remap: u8) {}
101    }
102    impl crate::peripheral::RemapPeripheral for crate::peripherals::TIM5 {}
103}
104
105#[cfg(all(peri_tim6))]
106mod tim6 {
107    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::TIM6 {
108        fn set_remap(_remap: u8) {}
109    }
110    impl crate::peripheral::RemapPeripheral for crate::peripherals::TIM6 {}
111}
112
113#[cfg(all(peri_tim7))]
114mod tim7 {
115    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::TIM7 {
116        fn set_remap(_remap: u8) {}
117    }
118    impl crate::peripheral::RemapPeripheral for crate::peripherals::TIM7 {}
119}
120
121#[cfg(all(peri_sdio, not(ch32v4)))]
122mod sdio {
123    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::SDIO {
124        fn set_remap(_remap: u8) {}
125    }
126    impl crate::peripheral::RemapPeripheral for crate::peripherals::SDIO {}
127}
128
129#[cfg(ch32l1)]
130mod ch32l1 {
131    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::USART1 {
132        fn set_remap(remap: u8) {
133            let afio = &crate::pac::AFIO;
134            afio.pcfr2().modify(|w| w.set_usart1_rm_h((remap & 0b110) >> 1));
135            afio.pcfr1().modify(|w| w.set_usart1_rm(remap & 0b1 != 0));
136        }
137    }
138    impl crate::peripheral::RemapPeripheral for crate::peripherals::USART1 {}
139
140    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::USART2 {
141        fn set_remap(remap: u8) {
142            let afio = &crate::pac::AFIO;
143            afio.pcfr2().modify(|w| w.set_usart2_rm_h(remap & 0b10 != 0));
144            afio.pcfr1().modify(|w| w.set_usart2_rm(remap & 0b1 != 0));
145        }
146    }
147    impl crate::peripheral::RemapPeripheral for crate::peripherals::USART2 {}
148
149    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::SPI1 {
150        fn set_remap(remap: u8) {
151            let afio = &crate::pac::AFIO;
152            afio.pcfr2().modify(|w| w.set_spi1_rm_h(remap & 0b10 != 0));
153            afio.pcfr1().modify(|w| w.set_spi1_rm(remap & 0b1 != 0));
154        }
155    }
156    impl crate::peripheral::RemapPeripheral for crate::peripherals::SPI1 {}
157
158    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::TIM1 {
159        fn set_remap(remap: u8) {
160            let afio = &crate::pac::AFIO;
161            afio.pcfr2().modify(|w| w.set_tim1_rm_h(remap & 0b100 != 0));
162            afio.pcfr1().modify(|w| w.set_tim1_rm(remap & 0b11));
163        }
164    }
165    impl crate::peripheral::RemapPeripheral for crate::peripherals::TIM1 {}
166
167    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::TIM2 {
168        fn set_remap(remap: u8) {
169            let afio = &crate::pac::AFIO;
170            afio.pcfr2().modify(|w| w.set_tim2_rm_h(remap & 0b100 != 0));
171            afio.pcfr1().modify(|w| w.set_tim2_rm(remap & 0b11));
172        }
173    }
174    impl crate::peripheral::RemapPeripheral for crate::peripherals::TIM2 {}
175
176    impl crate::peripheral::SealedRemapPeripheral for crate::peripherals::I2C1 {
177        fn set_remap(remap: u8) {
178            let afio = &crate::pac::AFIO;
179            afio.pcfr2().modify(|w| w.set_i2c1_rm_h(remap & 0b10 != 0));
180            afio.pcfr1().modify(|w| w.set_i2c1_rm(remap & 0b1 != 0));
181        }
182    }
183    impl crate::peripheral::RemapPeripheral for crate::peripherals::I2C1 {}
184}