blob: f8e7e7e12aae78da3a796046c9078cc1f08acbad [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080011namespace frc971::teensy {
Brian Silverman55c62022018-09-03 19:13:44 -070012
13// Simple synchronous interface to a UART.
14class Uart {
15 public:
16 Uart(KINETISK_UART_t *module, int module_clock_frequency)
17 : module_(module), module_clock_frequency_(module_clock_frequency) {}
18 Uart(const Uart &) = delete;
19 ~Uart();
20 Uart &operator=(const Uart &) = delete;
21
22 void Initialize(int baud_rate);
23
24 // Blocks until all of the data is at least queued.
Austin Schuh7fe04492022-01-02 13:37:21 -080025 void Write(absl::Span<const char> data, const DisableInterrupts &) {
Brian Silverman4787a6e2018-10-06 16:00:54 -070026 DoWrite(data);
27 }
Brian Silverman55c62022018-09-03 19:13:44 -070028
Brian Silverman4f958792019-02-16 18:20:04 -080029 // Returns all the data which is currently available.
Philipp Schrader790cb542023-07-05 21:06:52 -070030 aos::SizedArray<char, 4> Read(const DisableInterrupts &) { return DoRead(); }
Brian Silverman4f958792019-02-16 18:20:04 -080031
Brian Silverman55c62022018-09-03 19:13:44 -070032 bool SpaceAvailable() const { return module_->S1 & M_UART_TDRE; }
33 // Only call this if SpaceAvailable() has just returned true.
34 void WriteCharacter(char c) { module_->D = c; }
35
Brian Silverman4f958792019-02-16 18:20:04 -080036 bool DataAvailable() const { return module_->S1 & M_UART_RDRF; }
37 // Only call this if DataAvailable() has just returned true.
38 char ReadCharacter() { return module_->D; }
39
40 // TODO(Brian): These APIs for enabling/disabling interrupts aren't quite
41 // right. Redo them some time. Some issues:
42 // * They get called during initialization/destruction time, which means
43 // interrupts don't really need to be disabled because everything is
44 // singlethreaded.
45 // * Often, several C2 modifications are made in a single
46 // interrupts-disabled section. These could be batched to reduce
47 // peripheral writes. Sometimes, no modifications are made at all, in
48 // which case there doesn't even need to be a single write.
49
50 void EnableTransmitInterrupt(const DisableInterrupts &) {
Brian Silverman55c62022018-09-03 19:13:44 -070051 c2_value_ |= M_UART_TIE;
52 module_->C2 = c2_value_;
53 }
54
Brian Silverman4f958792019-02-16 18:20:04 -080055 void DisableTransmitInterrupt(const DisableInterrupts &) {
56 DoDisableTransmitInterrupt();
57 }
58
59 void EnableReceiveInterrupt(const DisableInterrupts &) {
60 c2_value_ |= M_UART_RIE;
Brian Silverman55c62022018-09-03 19:13:44 -070061 module_->C2 = c2_value_;
62 }
63
Brian Silverman4f958792019-02-16 18:20:04 -080064 void DisableReceiveInterrupt(const DisableInterrupts &) {
65 DoDisableReceiveInterrupt();
66 }
67
Brian Silverman55c62022018-09-03 19:13:44 -070068 private:
Brian Silverman4f958792019-02-16 18:20:04 -080069 void DoDisableTransmitInterrupt() {
70 c2_value_ &= ~M_UART_TIE;
71 module_->C2 = c2_value_;
72 }
73 void DoDisableReceiveInterrupt() {
74 c2_value_ &= ~M_UART_RIE;
75 module_->C2 = c2_value_;
76 }
77
Austin Schuh7fe04492022-01-02 13:37:21 -080078 void DoWrite(absl::Span<const char> data);
Brian Silverman4f958792019-02-16 18:20:04 -080079 aos::SizedArray<char, 4> DoRead();
Brian Silverman55c62022018-09-03 19:13:44 -070080
81 KINETISK_UART_t *const module_;
82 const int module_clock_frequency_;
83 // What we put in C2 except TE.
84 uint8_t c2_value_;
85
86 int tx_fifo_size_, rx_fifo_size_;
87};
88
89// Interrupt-based buffered interface to a UART.
Brian Silverman4f958792019-02-16 18:20:04 -080090// TODO(Brian): Move DisableInterrupts calls up to the caller of this.
Brian Silverman55c62022018-09-03 19:13:44 -070091class InterruptBufferedUart {
92 public:
93 InterruptBufferedUart(KINETISK_UART_t *module, int module_clock_frequency)
94 : uart_(module, module_clock_frequency) {}
Brian Silverman4f958792019-02-16 18:20:04 -080095 ~InterruptBufferedUart();
Brian Silverman55c62022018-09-03 19:13:44 -070096
97 void Initialize(int baud_rate);
98
Brian Silverman4f958792019-02-16 18:20:04 -080099 // Queues up the given data for immediate writing. Blocks only if the queue
100 // fills up before all of data is enqueued.
Austin Schuh7fe04492022-01-02 13:37:21 -0800101 void Write(absl::Span<const char> data);
Brian Silverman55c62022018-09-03 19:13:44 -0700102
Brian Silverman4f958792019-02-16 18:20:04 -0800103 // Reads currently available data.
104 // Returns all the data which is currently available (possibly none);
105 // buffer is where to store the result. The return value will be a subspan of
106 // this.
Austin Schuh7fe04492022-01-02 13:37:21 -0800107 absl::Span<char> Read(absl::Span<char> buffer);
Brian Silverman4f958792019-02-16 18:20:04 -0800108
Brian Silverman55c62022018-09-03 19:13:44 -0700109 // Should be called as the body of the interrupt handler.
110 void HandleInterrupt(const DisableInterrupts &disable_interrupts) {
111 WriteCharacters(true, disable_interrupts);
Brian Silverman4f958792019-02-16 18:20:04 -0800112 ReadCharacters(disable_interrupts);
Brian Silverman55c62022018-09-03 19:13:44 -0700113 }
114
115 private:
116 void WriteCharacters(bool disable_empty, const DisableInterrupts &);
Brian Silverman4f958792019-02-16 18:20:04 -0800117 void ReadCharacters(const DisableInterrupts &);
Brian Silverman55c62022018-09-03 19:13:44 -0700118
119 Uart uart_;
Brian Silverman4f958792019-02-16 18:20:04 -0800120 UartBuffer<1024> transmit_buffer_, receive_buffer_;
Brian Silverman55c62022018-09-03 19:13:44 -0700121};
122
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800123} // namespace frc971::teensy
Brian Silverman55c62022018-09-03 19:13:44 -0700124
125#endif // MOTORS_PERIPHERAL_UART_H_