From 123cc4c756cc8a22f66351ab65595c5a20e53e27 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 31 Jul 2021 10:47:00 -0400 Subject: reorganized source, wip --- source/periph/usbserial.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 source/periph/usbserial.cpp (limited to 'source/periph/usbserial.cpp') diff --git a/source/periph/usbserial.cpp b/source/periph/usbserial.cpp new file mode 100644 index 0000000..775a911 --- /dev/null +++ b/source/periph/usbserial.cpp @@ -0,0 +1,52 @@ +/** + * @file usbserial.cpp + * @brief Wrapper for ChibiOS's SerialUSBDriver. + * + * Copyright (C) 2021 Clyne Sullivan + * + * Distributed under the GNU GPL v3 or later. You should have received a copy of + * the GNU General Public License along with this program. + * If not, see . + */ + +#include "usbserial.hpp" + +SerialUSBDriver *USBSerial::m_driver = &SDU1; + +void USBSerial::begin() +{ + palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10)); + palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10)); + + sduObjectInit(m_driver); + sduStart(m_driver, &serusbcfg); + + // Reconnect bus so device can re-enumerate on reset + usbDisconnectBus(serusbcfg.usbp); + chThdSleepMilliseconds(1500); + usbStart(serusbcfg.usbp, &usbcfg); + usbConnectBus(serusbcfg.usbp); +} + +bool USBSerial::isActive() +{ + if (auto config = m_driver->config; config != nullptr) { + if (auto usbp = config->usbp; usbp != nullptr) + return usbp->state == USB_ACTIVE && !ibqIsEmptyI(&m_driver->ibqueue); + } + + return false; +} + +size_t USBSerial::read(unsigned char *buffer, size_t count) +{ + auto bss = reinterpret_cast(m_driver); + return streamRead(bss, buffer, count); +} + +size_t USBSerial::write(const unsigned char *buffer, size_t count) +{ + auto bss = reinterpret_cast(m_driver); + return streamWrite(bss, buffer, count); +} + -- cgit v1.2.3