Clean up printing in MCU code and add new options
We now have four different ways of getting debug prints off of boards,
with varying tradeoffs. Split out the printing into dedicated libraries
that are easy to switch between to avoid duplicating even more code, and
also make using the new options easy.
USB is handy for testing the code on a Teensy.
Semihosting is nice in theory, but in practice it's super slow and
messes up the code's timing.
ITM works well, as long as you have a debugger attached.
Serial also works pretty well, but it means having another cable.
Change-Id: I7af5099d421c33f0324aeca92b46732e341848d4
diff --git a/motors/peripheral/uart.cc b/motors/peripheral/uart.cc
index 70eadf1..3f7ddaf 100644
--- a/motors/peripheral/uart.cc
+++ b/motors/peripheral/uart.cc
@@ -50,7 +50,7 @@
module_->RWFIFO = rx_fifo_size_ - 1;
}
-void Uart::DoWrite(gsl::span<char> data) {
+void Uart::DoWrite(gsl::span<const char> data) {
for (int i = 0; i < data.size(); ++i) {
while (!SpaceAvailable()) {
}
@@ -64,7 +64,7 @@
uart_.Initialize(baud_rate);
}
-void InterruptBufferedUart::Write(gsl::span<char> data) {
+void InterruptBufferedUart::Write(gsl::span<const char> data) {
DisableInterrupts disable_interrupts;
uart_.EnableTransmitInterrupt();
static_assert(buffer_.size() >= 8,
diff --git a/motors/peripheral/uart.h b/motors/peripheral/uart.h
index 12b8523..ca40ab7 100644
--- a/motors/peripheral/uart.h
+++ b/motors/peripheral/uart.h
@@ -21,7 +21,9 @@
void Initialize(int baud_rate);
// Blocks until all of the data is at least queued.
- void Write(gsl::span<char> data, const DisableInterrupts &) { DoWrite(data); }
+ void Write(gsl::span<const char> data, const DisableInterrupts &) {
+ DoWrite(data);
+ }
bool SpaceAvailable() const { return module_->S1 & M_UART_TDRE; }
// Only call this if SpaceAvailable() has just returned true.
@@ -38,7 +40,7 @@
}
private:
- void DoWrite(gsl::span<char> data);
+ void DoWrite(gsl::span<const char> data);
KINETISK_UART_t *const module_;
const int module_clock_frequency_;
@@ -56,7 +58,7 @@
void Initialize(int baud_rate);
- void Write(gsl::span<char> data);
+ void Write(gsl::span<const char> data);
// Should be called as the body of the interrupt handler.
void HandleInterrupt(const DisableInterrupts &disable_interrupts) {
diff --git a/motors/peripheral/uart_buffer.h b/motors/peripheral/uart_buffer.h
index fb7d126..63dd70d 100644
--- a/motors/peripheral/uart_buffer.h
+++ b/motors/peripheral/uart_buffer.h
@@ -13,7 +13,7 @@
class UartBuffer {
public:
// Returns the number of characters added.
- __attribute__((warn_unused_result)) int PushSpan(gsl::span<char> data);
+ __attribute__((warn_unused_result)) int PushSpan(gsl::span<const char> data);
bool empty() const { return size_ == 0; }
@@ -32,7 +32,7 @@
};
template<int kSize>
-int UartBuffer<kSize>::PushSpan(gsl::span<char> data) {
+int UartBuffer<kSize>::PushSpan(gsl::span<const char> data) {
const int end_location = (start_ + size_) % kSize;
const int remaining_end = ::std::min(kSize - size_, kSize - end_location);
const int on_end = ::std::min<int>(data.size(), remaining_end);