Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 1 | #include "motors/peripheral/uart.h" |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 5 | namespace frc971::teensy { |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 6 | |
James Kuszmaul | 0844eb2 | 2018-09-08 15:54:04 -0700 | [diff] [blame] | 7 | // Currently hard-coded for 8-bit + no parity + start bit + stop bit. |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 8 | void Uart::Initialize(int baud_rate) { |
| 9 | { |
| 10 | // UART baud rate = UART module clock / (16 * (SBR[12:0] + BRFD)) |
| 11 | // BRFD = BRFA (bitfield) / 32 |
| 12 | const int desired_receiver_clock = baud_rate * 16; |
| 13 | const int sbr_and_brfd32 = |
| 14 | ((static_cast<int64_t>(module_clock_frequency_) * UINT64_C(64) / |
| 15 | static_cast<int64_t>(desired_receiver_clock)) + |
| 16 | 1) / |
| 17 | 2; |
| 18 | const int sbr = sbr_and_brfd32 / 32; |
| 19 | const int brfa = sbr_and_brfd32 % 32; |
| 20 | |
| 21 | module_->BDH = (sbr >> 8) & 0x1F; |
| 22 | module_->BDL = sbr & 0xFF; |
James Kuszmaul | 0844eb2 | 2018-09-08 15:54:04 -0700 | [diff] [blame] | 23 | module_->C1 = M_UART_ILT /* only detect idle after stop bit */ | |
| 24 | M_UART_PT /* odd parity */; |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 25 | module_->C4 = V_UART_BRFA(brfa); |
| 26 | } |
| 27 | { |
| 28 | const uint8_t pfifo = module_->PFIFO; |
| 29 | tx_fifo_size_ = G_UART_TXFIFOSIZE(pfifo); |
| 30 | rx_fifo_size_ = G_UART_RXFIFOSIZE(pfifo); |
| 31 | } |
| 32 | |
| 33 | // When C1[M] is set and C4[M10] is cleared, the UART is configured for 9-bit |
| 34 | // data characters. If C1[PE] is enabled, the ninth bit is either C3[T8/R8] or |
| 35 | // the internally generated parity bit |
| 36 | |
| 37 | // TODO(Brian): M_UART_TIE /* Enable TX interrupt or DMA */ | |
| 38 | // M_UART_RIE /* Enable RX interrupt or DMA */ |
| 39 | // Also set in C5: M_UART_TDMAS /* Do DMA for TX */ | |
| 40 | // M_UART_RDMAS /* Do DMA for RX */ |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 41 | c2_value_ = 0; |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 42 | module_->C2 = c2_value_; |
| 43 | module_->PFIFO = |
| 44 | M_UART_TXFE /* Enable TX FIFO */ | M_UART_RXFE /* Enable RX FIFO */; |
| 45 | module_->CFIFO = |
| 46 | M_UART_TXFLUSH /* Flush TX FIFO */ | M_UART_RXFLUSH /* Flush RX FIFO */; |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 47 | c2_value_ = M_UART_TE | M_UART_RE; |
| 48 | module_->C2 = c2_value_; |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 49 | // TODO(Brian): Adjust for DMA? |
| 50 | module_->TWFIFO = tx_fifo_size_ - 1; |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 51 | module_->RWFIFO = 1; |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 54 | void Uart::DoWrite(absl::Span<const char> data) { |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 55 | // In theory, we could be more efficient about this by writing the number of |
| 56 | // bytes we know there's space for and only calling SpaceAvailable() (or |
| 57 | // otherwise reading S1) before the final one. In practice, the FIFOs are so |
| 58 | // short on this part it probably won't help anything. |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 59 | for (size_t i = 0; i < data.size(); ++i) { |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 60 | while (!SpaceAvailable()) { |
| 61 | } |
| 62 | WriteCharacter(data[i]); |
| 63 | } |
| 64 | } |
| 65 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 66 | aos::SizedArray<char, 4> Uart::DoRead() { |
| 67 | // In theory, we could be more efficient about this by reading the number of |
| 68 | // bytes we know to be accessible and only calling DataAvailable() (or |
| 69 | // otherwise reading S1) before the final one. In practice, the FIFOs are so |
| 70 | // short on this part it probably won't help anything. |
| 71 | aos::SizedArray<char, 4> result; |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame] | 72 | while (DataAvailable() && result.size() != result.capacity()) { |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 73 | result.push_back(ReadCharacter()); |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | Uart::~Uart() { |
| 79 | DoDisableTransmitInterrupt(); |
| 80 | DoDisableReceiveInterrupt(); |
| 81 | } |
| 82 | |
| 83 | InterruptBufferedUart::~InterruptBufferedUart() { |
| 84 | uart_.DisableReceiveInterrupt(DisableInterrupts()); |
| 85 | } |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 86 | |
| 87 | void InterruptBufferedUart::Initialize(int baud_rate) { |
| 88 | uart_.Initialize(baud_rate); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 89 | { |
| 90 | DisableInterrupts disable_interrupts; |
| 91 | uart_.EnableReceiveInterrupt(disable_interrupts); |
| 92 | } |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 95 | void InterruptBufferedUart::Write(absl::Span<const char> data) { |
Brian Silverman | 12fec3f | 2018-09-09 16:09:50 -0700 | [diff] [blame] | 96 | DisableInterrupts disable_interrupts; |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 97 | uart_.EnableTransmitInterrupt(disable_interrupts); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 98 | while (!data.empty()) { |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 99 | const int bytes_written = transmit_buffer_.PushSpan(data); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 100 | data = data.subspan(bytes_written); |
| 101 | WriteCharacters(data.empty(), disable_interrupts); |
Brian Silverman | 12fec3f | 2018-09-09 16:09:50 -0700 | [diff] [blame] | 102 | ReenableInterrupts{&disable_interrupts}; |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 106 | absl::Span<char> InterruptBufferedUart::Read(absl::Span<char> buffer) { |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 107 | size_t bytes_read = 0; |
| 108 | { |
| 109 | DisableInterrupts disable_interrupts; |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 110 | const absl::Span<const char> read_data = |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 111 | receive_buffer_.PopSpan(buffer.size()); |
| 112 | std::copy(read_data.begin(), read_data.end(), buffer.begin()); |
| 113 | bytes_read += read_data.size(); |
| 114 | } |
| 115 | { |
| 116 | DisableInterrupts disable_interrupts; |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 117 | const absl::Span<const char> read_data = |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 118 | receive_buffer_.PopSpan(buffer.size() - bytes_read); |
| 119 | std::copy(read_data.begin(), read_data.end(), |
| 120 | buffer.subspan(bytes_read).begin()); |
| 121 | bytes_read += read_data.size(); |
| 122 | } |
| 123 | return buffer.subspan(0, bytes_read); |
| 124 | } |
| 125 | |
| 126 | void InterruptBufferedUart::WriteCharacters( |
| 127 | bool disable_empty, const DisableInterrupts &disable_interrupts) { |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 128 | while (true) { |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 129 | if (transmit_buffer_.empty()) { |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 130 | if (disable_empty) { |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 131 | uart_.DisableTransmitInterrupt(disable_interrupts); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 132 | } |
| 133 | return; |
| 134 | } |
| 135 | if (!uart_.SpaceAvailable()) { |
| 136 | return; |
| 137 | } |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 138 | uart_.WriteCharacter(transmit_buffer_.PopSingle()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void InterruptBufferedUart::ReadCharacters(const DisableInterrupts &) { |
| 143 | while (true) { |
| 144 | if (receive_buffer_.full()) { |
| 145 | return; |
| 146 | } |
| 147 | if (!uart_.DataAvailable()) { |
| 148 | return; |
| 149 | } |
| 150 | receive_buffer_.PushSingle(uart_.ReadCharacter()); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 154 | } // namespace frc971::teensy |