Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 1 | #ifndef MOTORS_PERIPHERAL_UART_H_ |
| 2 | #define MOTORS_PERIPHERAL_UART_H_ |
| 3 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 4 | #include "absl/types/span.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 5 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 6 | #include "aos/containers/sized_array.h" |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 7 | #include "motors/core/kinetis.h" |
| 8 | #include "motors/peripheral/uart_buffer.h" |
| 9 | #include "motors/util.h" |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 10 | |
| 11 | namespace frc971 { |
| 12 | namespace teensy { |
| 13 | |
| 14 | // Simple synchronous interface to a UART. |
| 15 | class Uart { |
| 16 | public: |
| 17 | Uart(KINETISK_UART_t *module, int module_clock_frequency) |
| 18 | : module_(module), module_clock_frequency_(module_clock_frequency) {} |
| 19 | Uart(const Uart &) = delete; |
| 20 | ~Uart(); |
| 21 | Uart &operator=(const Uart &) = delete; |
| 22 | |
| 23 | void Initialize(int baud_rate); |
| 24 | |
| 25 | // Blocks until all of the data is at least queued. |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 26 | void Write(absl::Span<const char> data, const DisableInterrupts &) { |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 27 | DoWrite(data); |
| 28 | } |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 29 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 30 | // Returns all the data which is currently available. |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 31 | aos::SizedArray<char, 4> Read(const DisableInterrupts &) { return DoRead(); } |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 32 | |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 33 | bool SpaceAvailable() const { return module_->S1 & M_UART_TDRE; } |
| 34 | // Only call this if SpaceAvailable() has just returned true. |
| 35 | void WriteCharacter(char c) { module_->D = c; } |
| 36 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 37 | bool DataAvailable() const { return module_->S1 & M_UART_RDRF; } |
| 38 | // Only call this if DataAvailable() has just returned true. |
| 39 | char ReadCharacter() { return module_->D; } |
| 40 | |
| 41 | // TODO(Brian): These APIs for enabling/disabling interrupts aren't quite |
| 42 | // right. Redo them some time. Some issues: |
| 43 | // * They get called during initialization/destruction time, which means |
| 44 | // interrupts don't really need to be disabled because everything is |
| 45 | // singlethreaded. |
| 46 | // * Often, several C2 modifications are made in a single |
| 47 | // interrupts-disabled section. These could be batched to reduce |
| 48 | // peripheral writes. Sometimes, no modifications are made at all, in |
| 49 | // which case there doesn't even need to be a single write. |
| 50 | |
| 51 | void EnableTransmitInterrupt(const DisableInterrupts &) { |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 52 | c2_value_ |= M_UART_TIE; |
| 53 | module_->C2 = c2_value_; |
| 54 | } |
| 55 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 56 | void DisableTransmitInterrupt(const DisableInterrupts &) { |
| 57 | DoDisableTransmitInterrupt(); |
| 58 | } |
| 59 | |
| 60 | void EnableReceiveInterrupt(const DisableInterrupts &) { |
| 61 | c2_value_ |= M_UART_RIE; |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 62 | module_->C2 = c2_value_; |
| 63 | } |
| 64 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 65 | void DisableReceiveInterrupt(const DisableInterrupts &) { |
| 66 | DoDisableReceiveInterrupt(); |
| 67 | } |
| 68 | |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 69 | private: |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 70 | void DoDisableTransmitInterrupt() { |
| 71 | c2_value_ &= ~M_UART_TIE; |
| 72 | module_->C2 = c2_value_; |
| 73 | } |
| 74 | void DoDisableReceiveInterrupt() { |
| 75 | c2_value_ &= ~M_UART_RIE; |
| 76 | module_->C2 = c2_value_; |
| 77 | } |
| 78 | |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 79 | void DoWrite(absl::Span<const char> data); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 80 | aos::SizedArray<char, 4> DoRead(); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 81 | |
| 82 | KINETISK_UART_t *const module_; |
| 83 | const int module_clock_frequency_; |
| 84 | // What we put in C2 except TE. |
| 85 | uint8_t c2_value_; |
| 86 | |
| 87 | int tx_fifo_size_, rx_fifo_size_; |
| 88 | }; |
| 89 | |
| 90 | // Interrupt-based buffered interface to a UART. |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 91 | // TODO(Brian): Move DisableInterrupts calls up to the caller of this. |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 92 | class InterruptBufferedUart { |
| 93 | public: |
| 94 | InterruptBufferedUart(KINETISK_UART_t *module, int module_clock_frequency) |
| 95 | : uart_(module, module_clock_frequency) {} |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 96 | ~InterruptBufferedUart(); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 97 | |
| 98 | void Initialize(int baud_rate); |
| 99 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 100 | // Queues up the given data for immediate writing. Blocks only if the queue |
| 101 | // fills up before all of data is enqueued. |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 102 | void Write(absl::Span<const char> data); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 103 | |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 104 | // Reads currently available data. |
| 105 | // Returns all the data which is currently available (possibly none); |
| 106 | // buffer is where to store the result. The return value will be a subspan of |
| 107 | // this. |
Austin Schuh | 7fe0449 | 2022-01-02 13:37:21 -0800 | [diff] [blame] | 108 | absl::Span<char> Read(absl::Span<char> buffer); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 109 | |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 110 | // Should be called as the body of the interrupt handler. |
| 111 | void HandleInterrupt(const DisableInterrupts &disable_interrupts) { |
| 112 | WriteCharacters(true, disable_interrupts); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 113 | ReadCharacters(disable_interrupts); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | private: |
| 117 | void WriteCharacters(bool disable_empty, const DisableInterrupts &); |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 118 | void ReadCharacters(const DisableInterrupts &); |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 119 | |
| 120 | Uart uart_; |
Brian Silverman | 4f95879 | 2019-02-16 18:20:04 -0800 | [diff] [blame] | 121 | UartBuffer<1024> transmit_buffer_, receive_buffer_; |
Brian Silverman | 55c6202 | 2018-09-03 19:13:44 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace teensy |
| 125 | } // namespace frc971 |
| 126 | |
| 127 | #endif // MOTORS_PERIPHERAL_UART_H_ |