aboutsummaryrefslogtreecommitdiffstats
path: root/source/usbserial.cpp
blob: 775a9117c24acc2679402e14f057902313e765c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 <https://www.gnu.org/licenses/>.
 */

#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<BaseSequentialStream *>(m_driver);
    return streamRead(bss, buffer, count);
}

size_t USBSerial::write(const unsigned char *buffer, size_t count)
{
    auto bss = reinterpret_cast<BaseSequentialStream *>(m_driver);
    return streamWrite(bss, buffer, count);
}