aboutsummaryrefslogtreecommitdiffstats
path: root/source/usbserial.cpp
blob: cead28a94e88281a1cbee43ec29812a6ce42304d (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
#include "usbserial.hpp"

#include "hal.h"

void USBSeriald::start()
{
    initPins();

    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 USBSeriald::active() const
{
    return m_driver->config->usbp->state == USB_ACTIVE;
}

std::size_t USBSeriald::read(void *buffer, std::size_t count)
{
    auto *bss = reinterpret_cast<BaseSequentialStream *>(m_driver);
    return streamRead(bss, static_cast<uint8_t *>(buffer), count);
}

std::size_t USBSeriald::write(const void *buffer, std::size_t count)
{
    auto *bss = reinterpret_cast<BaseSequentialStream *>(m_driver);
    return streamWrite(bss, static_cast<const uint8_t *>(buffer), count);
}

void USBSeriald::initPins()
{
    palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10));
    palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10));
}