aboutsummaryrefslogtreecommitdiffstats
path: root/source/usbserial.cpp
blob: ec2fc5d575a9fa12c46de62b40d15885f4d3d9f1 (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) 2020 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"

#include "usbcfg.h"

constexpr static const auto sdud = &SDU1;

namespace usbserial
{
    void init()
    {
        palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10));
        palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10));
    
        sduObjectInit(sdud);
        sduStart(sdud, &serusbcfg);
    
        // Reconnect bus so device can re-enumerate on reset
        usbDisconnectBus(serusbcfg.usbp);
        chThdSleepMilliseconds(1500);
        usbStart(serusbcfg.usbp, &usbcfg);
        usbConnectBus(serusbcfg.usbp);
    }
    
    bool is_active()
    {
        return sdud->config->usbp->state == USB_ACTIVE;
    }
    
    size_t read(void *buffer, size_t count)
    {
        auto bss = reinterpret_cast<BaseSequentialStream *>(sdud);
        return streamRead(bss, static_cast<uint8_t *>(buffer), count);
    }
    
    size_t write(const void *buffer, size_t count)
    {
        auto bss = reinterpret_cast<BaseSequentialStream *>(sdud);
        return streamWrite(bss, static_cast<const uint8_t *>(buffer), count);
    }
}