diff --git a/Firmware/MotorControl/nvm_config.hpp b/Firmware/MotorControl/nvm_config.hpp index a5d3623d..706a8d43 100644 --- a/Firmware/MotorControl/nvm_config.hpp +++ b/Firmware/MotorControl/nvm_config.hpp @@ -11,7 +11,7 @@ #include #include -#include +#include /* Private defines -----------------------------------------------------------*/ diff --git a/Firmware/MotorControl/odrive_main.h b/Firmware/MotorControl/odrive_main.h index a05c8a59..5c565b33 100644 --- a/Firmware/MotorControl/odrive_main.h +++ b/Firmware/MotorControl/odrive_main.h @@ -5,7 +5,6 @@ #include #ifdef __cplusplus -#include #include #include #include diff --git a/Firmware/Tupfile.lua b/Firmware/Tupfile.lua index 7d190005..e9657ffa 100644 --- a/Firmware/Tupfile.lua +++ b/Firmware/Tupfile.lua @@ -51,6 +51,14 @@ end -- Packages -------------------------------------------------------------------- +tup.include('fibre-cpp/package.lua') +fibre_pkg = get_fibre_package({ + enable_server=true, + enable_client=false, + allow_heap=false, + max_log_verbosity=0, +}) + odrive_firmware_pkg = { root = '.', include_dirs = { @@ -90,7 +98,6 @@ odrive_firmware_pkg = { 'communication/interface_usb.cpp', 'communication/interface_can.cpp', 'communication/interface_i2c.cpp', - 'fibre-cpp/legacy_protocol.cpp', 'FreeRTOS-openocd.c', 'autogen/version.c' } @@ -413,6 +420,7 @@ add_pkg(freertos_pkg) add_pkg(cmsis_pkg) add_pkg(stm32_usb_device_library_pkg) add_pkg(board) +add_pkg(fibre_pkg) add_pkg(odrive_firmware_pkg) diff --git a/Firmware/communication/ascii_protocol.cpp b/Firmware/communication/ascii_protocol.cpp index 282422f7..18733081 100644 --- a/Firmware/communication/ascii_protocol.cpp +++ b/Firmware/communication/ascii_protocol.cpp @@ -60,7 +60,7 @@ void AsciiProtocol::respond(bool include_checksum, const char * fmt, TArgs&& ... len = std::min(len, sizeof(tx_buf_)); tx_end_ = (const uint8_t*)tx_buf_ + len; - tx_channel_->start_write({(const uint8_t*)tx_buf_, tx_end_}, &tx_handle_, *static_cast(this)); + tx_channel_->start_write({(const uint8_t*)tx_buf_, tx_end_}, &tx_handle_, MEMBER_CB(this, on_write_finished)); } @@ -413,7 +413,7 @@ void AsciiProtocol::on_write_finished(WriteResult result) { if (result.status == kStreamOk && result.end < tx_end_) { // Not everything was written. Try again. - tx_channel_->start_write({result.end, tx_end_}, &tx_handle_, *static_cast(this)); + tx_channel_->start_write({result.end, tx_end_}, &tx_handle_, MEMBER_CB(this, on_write_finished)); return; } @@ -467,10 +467,10 @@ void AsciiProtocol::on_read_finished(ReadResult result) { } TransferHandle dummy; - rx_channel_->start_read({result.end, rx_buf_ + sizeof(rx_buf_)}, &dummy, *static_cast(this)); + rx_channel_->start_read({result.end, rx_buf_ + sizeof(rx_buf_)}, &dummy, MEMBER_CB(this, on_read_finished)); } void AsciiProtocol::start() { TransferHandle dummy; - rx_channel_->start_read(rx_buf_, &dummy, *static_cast(this)); + rx_channel_->start_read(rx_buf_, &dummy, MEMBER_CB(this, on_read_finished)); } diff --git a/Firmware/communication/ascii_protocol.hpp b/Firmware/communication/ascii_protocol.hpp index 7c29588d..fb15a4ae 100644 --- a/Firmware/communication/ascii_protocol.hpp +++ b/Firmware/communication/ascii_protocol.hpp @@ -1,11 +1,11 @@ #ifndef __ASCII_PROTOCOL_HPP #define __ASCII_PROTOCOL_HPP -#include +#include #define MAX_LINE_LENGTH ((size_t)256) -class AsciiProtocol : fibre::ReadCompleter, fibre::WriteCompleter { +class AsciiProtocol { public: AsciiProtocol(fibre::AsyncStreamSource* rx_channel, fibre::AsyncStreamSink* tx_channel) : rx_channel_(rx_channel), tx_channel_(tx_channel) {} diff --git a/Firmware/communication/communication.cpp b/Firmware/communication/communication.cpp index 3089c076..36e0fb93 100644 --- a/Firmware/communication/communication.cpp +++ b/Firmware/communication/communication.cpp @@ -60,7 +60,7 @@ void init_communication(void) { } } -#include +#include extern "C" { diff --git a/Firmware/communication/interface_can.cpp b/Firmware/communication/interface_can.cpp index 8a557878..3a788483 100644 --- a/Firmware/communication/interface_can.cpp +++ b/Firmware/communication/interface_can.cpp @@ -1,6 +1,6 @@ #include "interface_can.hpp" -#include "fibre/crc.hpp" +#include #include "freertos_vars.h" #include "utils.hpp" diff --git a/Firmware/communication/interface_can.hpp b/Firmware/communication/interface_can.hpp index 89223f31..edd1836b 100644 --- a/Firmware/communication/interface_can.hpp +++ b/Firmware/communication/interface_can.hpp @@ -2,7 +2,6 @@ #define __INTERFACE_CAN_HPP #include -#include "fibre/protocol.hpp" #include "odrive_main.h" #include "can_helpers.hpp" diff --git a/Firmware/communication/interface_i2c.cpp b/Firmware/communication/interface_i2c.cpp index c4df0373..b72806bc 100644 --- a/Firmware/communication/interface_i2c.cpp +++ b/Firmware/communication/interface_i2c.cpp @@ -1,6 +1,5 @@ #include "interface_i2c.h" -#include "fibre/protocol.hpp" #include diff --git a/Firmware/communication/interface_uart.cpp b/Firmware/communication/interface_uart.cpp index 0bcea5ed..9cfc6eda 100644 --- a/Firmware/communication/interface_uart.cpp +++ b/Firmware/communication/interface_uart.cpp @@ -5,8 +5,7 @@ #include -#include -#include +#include #include #include #include @@ -31,22 +30,22 @@ class Stm32UartTxStream : public AsyncStreamSink { public: Stm32UartTxStream(UART_HandleTypeDef* huart) : huart_(huart) {} - void start_write(cbufptr_t buffer, TransferHandle* handle, Completer& completer) final; + void start_write(cbufptr_t buffer, TransferHandle* handle, Callback completer) final; void cancel_write(TransferHandle transfer_handle) final; void did_finish(); UART_HandleTypeDef *huart_; - Completer* completer_ = nullptr; + Callback completer_; const uint8_t* tx_end_ = nullptr; }; class Stm32UartRxStream : public AsyncStreamSource { public: - void start_read(bufptr_t buffer, TransferHandle* handle, Completer& completer) final; + void start_read(bufptr_t buffer, TransferHandle* handle, Callback completer) final; void cancel_read(TransferHandle transfer_handle) final; void did_receive(uint8_t* buffer, size_t length); - Completer* completer_ = nullptr; + Callback completer_; bufptr_t rx_buf_ = {nullptr, nullptr}; }; @@ -54,10 +53,10 @@ public: using namespace fibre; -void Stm32UartTxStream::start_write(cbufptr_t buffer, TransferHandle* handle, Completer& completer) { +void Stm32UartTxStream::start_write(cbufptr_t buffer, TransferHandle* handle, Callback completer) { size_t chunk = std::min(buffer.size(), (size_t)UART_TX_BUFFER_SIZE); - completer_ = &completer; + completer_ = completer; tx_end_ = buffer.begin() + chunk; if (handle) { @@ -67,7 +66,7 @@ void Stm32UartTxStream::start_write(cbufptr_t buffer, TransferHandle* handle, Co if (HAL_UART_Transmit_DMA(huart_, const_cast(buffer.begin()), chunk) != HAL_OK) { completer_ = nullptr; tx_end_ = nullptr; - completer.complete({kStreamError, buffer.begin()}); + completer.invoke({kStreamError, buffer.begin()}); } } @@ -78,11 +77,11 @@ void Stm32UartTxStream::cancel_write(TransferHandle transfer_handle) { void Stm32UartTxStream::did_finish() { const uint8_t* tx_end = tx_end_; tx_end_ = nullptr; - safe_complete(completer_, {kStreamOk, tx_end}); + completer_.invoke_and_clear({kStreamOk, tx_end}); } -void Stm32UartRxStream::start_read(bufptr_t buffer, TransferHandle* handle, Completer& completer) { - completer_ = &completer; +void Stm32UartRxStream::start_read(bufptr_t buffer, TransferHandle* handle, Callback completer) { + completer_ = completer; rx_buf_ = buffer; if (handle) { *handle = reinterpret_cast(this); @@ -102,7 +101,7 @@ void Stm32UartRxStream::did_receive(uint8_t* buffer, size_t length) { rx_buf_ = {nullptr, nullptr}; size_t chunk = std::min(length, rx_buf.size()); memcpy(rx_buf.begin(), buffer, chunk); - safe_complete(completer_, {kStreamOk, rx_buf.begin() + chunk}); + completer_.invoke_and_clear({kStreamOk, rx_buf.begin() + chunk}); } } @@ -121,7 +120,7 @@ static void uart_server_thread(void * ctx) { (void) ctx; if (odrv.config_.uart0_protocol == ODrive::STREAM_PROTOCOL_TYPE_FIBRE) { - fibre_over_uart.start(Completer::get_dummy()); + fibre_over_uart.start({}); } else if (odrv.config_.uart0_protocol == ODrive::STREAM_PROTOCOL_TYPE_ASCII || odrv.config_.uart0_protocol == ODrive::STREAM_PROTOCOL_TYPE_ASCII_AND_STDOUT) { ascii_over_uart.start(); diff --git a/Firmware/communication/interface_usb.cpp b/Firmware/communication/interface_usb.cpp index 55d08351..68625ef5 100644 --- a/Firmware/communication/interface_usb.cpp +++ b/Firmware/communication/interface_usb.cpp @@ -4,8 +4,7 @@ #include -#include -#include +#include #include #include #include @@ -25,13 +24,13 @@ class Stm32UsbTxStream : public AsyncStreamSink { public: Stm32UsbTxStream(uint8_t endpoint_num) : endpoint_num_(endpoint_num) {} - void start_write(cbufptr_t buffer, TransferHandle* handle, Completer& completer) final; + void start_write(cbufptr_t buffer, TransferHandle* handle, Callback completer) final; void cancel_write(TransferHandle transfer_handle) final; void did_finish(); const uint8_t endpoint_num_; bool connected_ = false; - Completer* completer_ = nullptr; + Callback completer_; const uint8_t* tx_end_ = nullptr; }; @@ -39,13 +38,13 @@ class Stm32UsbRxStream : public AsyncStreamSource { public: Stm32UsbRxStream(uint8_t endpoint_num) : endpoint_num_(endpoint_num) {} - void start_read(bufptr_t buffer, TransferHandle* handle, Completer& completer) final; + void start_read(bufptr_t buffer, TransferHandle* handle, Callback completer) final; void cancel_read(TransferHandle transfer_handle) final; void did_finish(); const uint8_t endpoint_num_; bool connected_ = false; - Completer* completer_ = nullptr; + Callback completer_; uint8_t* rx_end_ = nullptr; }; @@ -53,13 +52,13 @@ public: using namespace fibre; -void Stm32UsbTxStream::start_write(cbufptr_t buffer, TransferHandle* handle, Completer& completer) { +void Stm32UsbTxStream::start_write(cbufptr_t buffer, TransferHandle* handle, Callback completer) { if (handle) { *handle = reinterpret_cast(this); } if (!connected_) { - completer.complete({kStreamClosed, buffer.begin()}); + completer.invoke({kStreamClosed, buffer.begin()}); return; } @@ -70,16 +69,16 @@ void Stm32UsbTxStream::start_write(cbufptr_t buffer, TransferHandle* handle, Com // must ensure that all packets are < 64 bytes, otherwise the host will wait // for more. if (buffer.size() >= USB_TX_DATA_SIZE) { - completer.complete({kStreamError, buffer.begin()}); + completer.invoke({kStreamError, buffer.begin()}); return; } if (completer_ || tx_end_) { - completer.complete({kStreamError, buffer.begin()}); + completer.invoke({kStreamError, buffer.begin()}); return; } - completer_ = &completer; + completer_ = completer; tx_end_ = buffer.end(); if ( @@ -92,7 +91,7 @@ void Stm32UsbTxStream::start_write(cbufptr_t buffer, TransferHandle* handle, Com #endif (const_cast(buffer.begin()), buffer.size(), endpoint_num_) != USBD_OK) { tx_end_ = nullptr; - safe_complete(completer_, {kStreamError, buffer.begin()}); + completer_.invoke_and_clear({kStreamError, buffer.begin()}); } } @@ -103,30 +102,30 @@ void Stm32UsbTxStream::cancel_write(TransferHandle transfer_handle) { void Stm32UsbTxStream::did_finish() { const uint8_t* tx_end = tx_end_; tx_end_ = nullptr; - safe_complete(completer_, {connected_ ? kStreamOk : kStreamClosed, tx_end}); + completer_.invoke_and_clear({connected_ ? kStreamOk : kStreamClosed, tx_end}); } -void Stm32UsbRxStream::start_read(bufptr_t buffer, TransferHandle* handle, Completer& completer) { +void Stm32UsbRxStream::start_read(bufptr_t buffer, TransferHandle* handle, Callback completer) { if (handle) { *handle = reinterpret_cast(this); } if (!connected_) { - completer.complete({kStreamClosed, buffer.begin()}); + completer.invoke({kStreamClosed, buffer.begin()}); return; } if (completer_ || rx_end_) { - completer.complete({kStreamError, buffer.begin()}); + completer.invoke({kStreamError, buffer.begin()}); return; } - completer_ = &completer; + completer_ = completer; rx_end_ = buffer.begin(); // the pointer is updated at the end of the transfer if (USBD_CDC_ReceivePacket(&usb_dev_handle, buffer.begin(), buffer.size(), endpoint_num_) != USBD_OK) { rx_end_ = nullptr; - safe_complete(completer_, {kStreamError, buffer.begin()}); + completer_.invoke_and_clear({kStreamError, buffer.begin()}); return; } } @@ -138,7 +137,7 @@ void Stm32UsbRxStream::cancel_read(TransferHandle transfer_handle) { void Stm32UsbRxStream::did_finish() { uint8_t* rx_end = rx_end_; rx_end_ = nullptr; - safe_complete(completer_, {connected_ ? kStreamOk : kStreamClosed, rx_end}); + completer_.invoke_and_clear({connected_ ? kStreamOk : kStreamClosed, rx_end}); } Stm32UsbTxStream usb_cdc_tx_stream(CDC_IN_EP); @@ -174,10 +173,10 @@ static void usb_server_thread(void * ctx) { usb_cdc_rx_stream.connected_ = true; usb_native_rx_stream.connected_ = true; - fibre_over_usb.start(Completer::get_dummy()); + fibre_over_usb.start({}); if (odrv.config_.usb_cdc_protocol == ODrive::STREAM_PROTOCOL_TYPE_FIBRE) { - fibre_over_cdc.start(Completer::get_dummy()); + fibre_over_cdc.start({}); } else if (odrv.config_.usb_cdc_protocol == ODrive::STREAM_PROTOCOL_TYPE_ASCII || odrv.config_.usb_cdc_protocol == ODrive::STREAM_PROTOCOL_TYPE_ASCII_AND_STDOUT) { ascii_over_cdc.start();