blob: bfd630bf7904af9ebfede53647dd860fba81b15c [file] [log] [blame]
Brian Silverman55c62022018-09-03 19:13:44 -07001#ifndef MOTORS_PERIPHERAL_UART_H_
2#define MOTORS_PERIPHERAL_UART_H_
3
Austin Schuh7fe04492022-01-02 13:37:21 -08004#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07005
Brian Silverman4f958792019-02-16 18:20:04 -08006#include "aos/containers/sized_array.h"
Brian Silverman55c62022018-09-03 19:13:44 -07007#include "motors/core/kinetis.h"
8#include "motors/peripheral/uart_buffer.h"
9#include "motors/util.h"
Brian Silverman55c62022018-09-03 19:13:44 -070010
11namespace frc971 {
12namespace teensy {
13
14// Simple synchronous interface to a UART.
15class 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 Schuh7fe04492022-01-02 13:37:21 -080026 void Write(absl::Span<const char> data, const DisableInterrupts &) {
Brian Silverman4787a6e2018-10-06 16:00:54 -070027 DoWrite(data);
28 }
Brian Silverman55c62022018-09-03 19:13:44 -070029
Brian Silverman4f958792019-02-16 18:20:04 -080030 // Returns all the data which is currently available.
Philipp Schrader790cb542023-07-05 21:06:52 -070031 aos::SizedArray<char, 4> Read(const DisableInterrupts &) { return DoRead(); }
Brian Silverman4f958792019-02-16 18:20:04 -080032
Brian Silverman55c62022018-09-03 19:13:44 -070033 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 Silverman4f958792019-02-16 18:20:04 -080037 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 Silverman55c62022018-09-03 19:13:44 -070052 c2_value_ |= M_UART_TIE;
53 module_->C2 = c2_value_;
54 }
55
Brian Silverman4f958792019-02-16 18:20:04 -080056 void DisableTransmitInterrupt(const DisableInterrupts &) {
57 DoDisableTransmitInterrupt();
58 }
59
60 void EnableReceiveInterrupt(const DisableInterrupts &) {
61 c2_value_ |= M_UART_RIE;
Brian Silverman55c62022018-09-03 19:13:44 -070062 module_->C2 = c2_value_;
63 }
64
Brian Silverman4f958792019-02-16 18:20:04 -080065 void DisableReceiveInterrupt(const DisableInterrupts &) {
66 DoDisableReceiveInterrupt();
67 }
68
Brian Silverman55c62022018-09-03 19:13:44 -070069 private:
Brian Silverman4f958792019-02-16 18:20:04 -080070 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 Schuh7fe04492022-01-02 13:37:21 -080079 void DoWrite(absl::Span<const char> data);
Brian Silverman4f958792019-02-16 18:20:04 -080080 aos::SizedArray<char, 4> DoRead();
Brian Silverman55c62022018-09-03 19:13:44 -070081
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 Silverman4f958792019-02-16 18:20:04 -080091// TODO(Brian): Move DisableInterrupts calls up to the caller of this.
Brian Silverman55c62022018-09-03 19:13:44 -070092class InterruptBufferedUart {
93 public:
94 InterruptBufferedUart(KINETISK_UART_t *module, int module_clock_frequency)
95 : uart_(module, module_clock_frequency) {}
Brian Silverman4f958792019-02-16 18:20:04 -080096 ~InterruptBufferedUart();
Brian Silverman55c62022018-09-03 19:13:44 -070097
98 void Initialize(int baud_rate);
99
Brian Silverman4f958792019-02-16 18:20:04 -0800100 // Queues up the given data for immediate writing. Blocks only if the queue
101 // fills up before all of data is enqueued.
Austin Schuh7fe04492022-01-02 13:37:21 -0800102 void Write(absl::Span<const char> data);
Brian Silverman55c62022018-09-03 19:13:44 -0700103
Brian Silverman4f958792019-02-16 18:20:04 -0800104 // 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 Schuh7fe04492022-01-02 13:37:21 -0800108 absl::Span<char> Read(absl::Span<char> buffer);
Brian Silverman4f958792019-02-16 18:20:04 -0800109
Brian Silverman55c62022018-09-03 19:13:44 -0700110 // Should be called as the body of the interrupt handler.
111 void HandleInterrupt(const DisableInterrupts &disable_interrupts) {
112 WriteCharacters(true, disable_interrupts);
Brian Silverman4f958792019-02-16 18:20:04 -0800113 ReadCharacters(disable_interrupts);
Brian Silverman55c62022018-09-03 19:13:44 -0700114 }
115
116 private:
117 void WriteCharacters(bool disable_empty, const DisableInterrupts &);
Brian Silverman4f958792019-02-16 18:20:04 -0800118 void ReadCharacters(const DisableInterrupts &);
Brian Silverman55c62022018-09-03 19:13:44 -0700119
120 Uart uart_;
Brian Silverman4f958792019-02-16 18:20:04 -0800121 UartBuffer<1024> transmit_buffer_, receive_buffer_;
Brian Silverman55c62022018-09-03 19:13:44 -0700122};
123
124} // namespace teensy
125} // namespace frc971
126
127#endif // MOTORS_PERIPHERAL_UART_H_