diff options
| author | Mathias <[email protected]> | 2024-07-17 12:20:31 +0200 |
|---|---|---|
| committer | Mathias <[email protected]> | 2024-07-17 12:23:59 +0200 |
| commit | 4c34132337f12af079f4c81f5f6e0ff03cf9c728 (patch) | |
| tree | 7c7b00c5290be7c7c25b05403386991b88d6ae8d | |
| parent | d1207706c0dcf684f9350f956c155d8874fc2550 (diff) | |
Correctly handle modifying LCR register after uart enable
| -rw-r--r-- | embassy-rp/src/uart/mod.rs | 73 |
1 files changed, 51 insertions, 22 deletions
diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index f7c90e97b..f546abe71 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs | |||
| @@ -860,6 +860,56 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { | |||
| 860 | }); | 860 | }); |
| 861 | } | 861 | } |
| 862 | 862 | ||
| 863 | fn lcr_modify<R>(f: impl FnOnce(&mut rp_pac::uart::regs::UartlcrH) -> R) -> R { | ||
| 864 | let r = T::regs(); | ||
| 865 | |||
| 866 | // Notes from PL011 reference manual: | ||
| 867 | // | ||
| 868 | // - Before writing the LCR, if the UART is enabled it needs to be | ||
| 869 | // disabled and any current TX + RX activity has to be completed | ||
| 870 | // | ||
| 871 | // - There is a BUSY flag which waits for the current TX char, but this is | ||
| 872 | // OR'd with TX FIFO !FULL, so not usable when FIFOs are enabled and | ||
| 873 | // potentially nonempty | ||
| 874 | // | ||
| 875 | // - FIFOs can't be set to disabled whilst a character is in progress | ||
| 876 | // (else "FIFO integrity is not guaranteed") | ||
| 877 | // | ||
| 878 | // Combination of these means there is no general way to halt and poll for | ||
| 879 | // end of TX character, if FIFOs may be enabled. Either way, there is no | ||
| 880 | // way to poll for end of RX character. | ||
| 881 | // | ||
| 882 | // So, insert a 15 Baud period delay before changing the settings. | ||
| 883 | // 15 Baud is comfortably higher than start + max data + parity + stop. | ||
| 884 | // Anything else would require API changes to permit a non-enabled UART | ||
| 885 | // state after init() where settings can be changed safely. | ||
| 886 | let clk_base = crate::clocks::clk_peri_freq(); | ||
| 887 | |||
| 888 | let cr = r.uartcr().read(); | ||
| 889 | if cr.uarten() { | ||
| 890 | r.uartcr().modify(|w| { | ||
| 891 | w.set_uarten(false); | ||
| 892 | w.set_txe(false); | ||
| 893 | w.set_rxe(false); | ||
| 894 | }); | ||
| 895 | |||
| 896 | // Note: Maximise precision here. Show working, the compiler will mop this up. | ||
| 897 | // Create a 16.6 fixed-point fractional division ratio; then scale to 32-bits. | ||
| 898 | let mut brdiv_ratio = 64 * r.uartibrd().read().0 + r.uartfbrd().read().0; | ||
| 899 | brdiv_ratio <<= 10; | ||
| 900 | // 3662 is ~(15 * 244.14) where 244.14 is 16e6 / 2^16 | ||
| 901 | let scaled_freq = clk_base / 3662; | ||
| 902 | let wait_time_us = brdiv_ratio / scaled_freq; | ||
| 903 | embedded_hal_1::delay::DelayNs::delay_us(&mut Delay, wait_time_us); | ||
| 904 | } | ||
| 905 | |||
| 906 | let res = r.uartlcr_h().modify(f); | ||
| 907 | |||
| 908 | r.uartcr().write_value(cr); | ||
| 909 | |||
| 910 | res | ||
| 911 | } | ||
| 912 | |||
| 863 | /// sets baudrate on runtime | 913 | /// sets baudrate on runtime |
| 864 | pub fn set_baudrate(&mut self, baudrate: u32) { | 914 | pub fn set_baudrate(&mut self, baudrate: u32) { |
| 865 | Self::set_baudrate_inner(baudrate); | 915 | Self::set_baudrate_inner(baudrate); |
| @@ -886,28 +936,7 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { | |||
| 886 | r.uartibrd().write_value(pac::uart::regs::Uartibrd(baud_ibrd)); | 936 | r.uartibrd().write_value(pac::uart::regs::Uartibrd(baud_ibrd)); |
| 887 | r.uartfbrd().write_value(pac::uart::regs::Uartfbrd(baud_fbrd)); | 937 | r.uartfbrd().write_value(pac::uart::regs::Uartfbrd(baud_fbrd)); |
| 888 | 938 | ||
| 889 | let cr = r.uartcr().read(); | 939 | Self::lcr_modify(|_| {}); |
| 890 | if cr.uarten() { | ||
| 891 | r.uartcr().modify(|w| { | ||
| 892 | w.set_uarten(false); | ||
| 893 | w.set_txe(false); | ||
| 894 | w.set_rxe(false); | ||
| 895 | }); | ||
| 896 | |||
| 897 | // Note: Maximise precision here. Show working, the compiler will mop this up. | ||
| 898 | // Create a 16.6 fixed-point fractional division ratio; then scale to 32-bits. | ||
| 899 | let mut brdiv_ratio = 64 * r.uartibrd().read().0 + r.uartfbrd().read().0; | ||
| 900 | brdiv_ratio <<= 10; | ||
| 901 | // 3662 is ~(15 * 244.14) where 244.14 is 16e6 / 2^16 | ||
| 902 | let scaled_freq = clk_base / 3662; | ||
| 903 | let wait_time_us = brdiv_ratio / scaled_freq; | ||
| 904 | embedded_hal_1::delay::DelayNs::delay_us(&mut Delay, wait_time_us); | ||
| 905 | } | ||
| 906 | // PL011 needs a (dummy) line control register write to latch in the | ||
| 907 | // divisors. We don't want to actually change LCR contents here. | ||
| 908 | r.uartlcr_h().modify(|_| {}); | ||
| 909 | |||
| 910 | r.uartcr().write_value(cr); | ||
| 911 | } | 940 | } |
| 912 | } | 941 | } |
| 913 | 942 | ||
