Skip to content

Commit 8297cf3

Browse files
andryblacksalkinium
authored andcommitted
[rp] Add USB module and TinyUSB port
1 parent 91233ba commit 8297cf3

File tree

9 files changed

+225
-0
lines changed

9 files changed

+225
-0
lines changed

ext/hathach/module.lb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ def build(env):
104104
tusb_config["CFG_TUSB_MCU"] = "OPT_MCU_SAM{}{}".format(target.family.upper(), target.series.upper())
105105
env.copy("tinyusb/src/portable/microchip/samd/", "portable/microchip/samd/")
106106

107+
elif target.platform == "rp":
108+
if target.family == "20":
109+
tusb_config["CFG_TUSB_MCU"] = "OPT_MCU_RP2040"
110+
env.copy("tinyusb/src/portable/raspberrypi/rp2040/", "portable/raspberrypi/rp2040/")
111+
107112
if env.has_module(":freertos"):
108113
tusb_config["CFG_TUSB_OS"] = "OPT_OS_FREERTOS"
109114
env.copy("tinyusb/src/osal/osal_freertos.h", "osal/osal_freertos.h")
@@ -165,6 +170,8 @@ def build(env):
165170
irqs = irq_data["port_irqs"][speed]
166171
elif target.platform == "sam" and target.family in ["g"]:
167172
irqs = ["UDP"]
173+
elif target.platform == "rp" and target.family in ["20"]:
174+
irqs = ["USBCTRL_IRQ"]
168175
else:
169176
irqs = ["USB"]
170177

ext/hathach/tusb_port.cpp.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ tusb_get_device_serial(uint16_t *serial_str)
4040
(uint32_t *)0x0080A00C, (uint32_t *)0x0080A040,
4141
(uint32_t *)0x0080A044, (uint32_t *)0x0080A048
4242
%% endif
43+
%% elif target.platform in ["rp"]
44+
/* sysinfo CHIP_ID and GIT_REV */
45+
((uint32_t *)0x40000000),((uint32_t *)0x40000040),
46+
((uint32_t *)0x40000000),((uint32_t *)0x40000040),
4347
%% endif
4448
};
4549

ext/rp/irq.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022, Andrey Kunitsyn
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
14+
/**
15+
* Pico SDK compatibility for tinyusb
16+
*/
17+
#include "hardware/regs/intctrl.h"
18+
19+
static inline void
20+
irq_set_enabled(int irqn, bool enable)
21+
{
22+
if (enable) NVIC_EnableIRQ(irqn);
23+
else NVIC_DisableIRQ(irqn);
24+
}
25+
26+
static inline void
27+
irq_set_exclusive_handler(int irqn, void (*handler)())
28+
{
29+
(void)irqn;
30+
(void)handler;
31+
// do nothing, irq implemented at modm
32+
}

ext/rp/module.lb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ def build(env):
3838
env.copy("pico-sdk/include", "")
3939
env.copy("address_mapped.h", "hardware/address_mapped.h")
4040

41+
if env.has_module(":tinyusb"):
42+
env.substitutions = {
43+
"with_debug": env.has_module(":debug")
44+
}
45+
env.copy("pico.h")
46+
env.template("pico.cpp.in", "pico.cpp")
47+
env.copy("irq.h", "hardware/irq.h")
48+
env.copy("resets.h", "hardware/resets.h")
49+
4150
env.substitutions = {
4251
"headers": headers,
4352
"defines": defines,

ext/rp/pico.cpp.in

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "pico.h"
2+
#include "hardware/resets.h"
3+
4+
#include <modm/platform/core/resets.hpp>
5+
%% if with_debug
6+
#include <modm/debug.hpp>
7+
%% endif
8+
9+
/**
10+
* Pico SDK compatibility for tinyusb
11+
*/
12+
13+
extern "C" void reset_block(uint32_t blocks)
14+
{
15+
modm::platform::Resets::reset(blocks);
16+
}
17+
extern "C" void unreset_block_wait(uint32_t blocks)
18+
{
19+
modm::platform::Resets::unresetWait(blocks);
20+
}
21+
22+
extern "C" void panic(const char *fmt, ...)
23+
{
24+
%% if with_debug
25+
va_list va;
26+
va_start(va, fmt);
27+
modm::log::info.vprintf(fmt, va);
28+
va_end(va);
29+
%% endif
30+
modm_assert(0, "pico", "Pico-SDK panic!");
31+
}

ext/rp/pico.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2022, Andrey Kunitsyn
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
14+
/**
15+
* Pico SDK compatibility for tinyusb
16+
*/
17+
18+
#include <modm/architecture/interface/assert.h>
19+
#include <stdint.h>
20+
/* need for static_assert at C compilation */
21+
#include <assert.h>
22+
#define TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX 0
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
/* override expensive assert implementation */
28+
#undef assert
29+
#define assert(X) modm_assert((X), "pico", __FILE__ ":" MODM_STRINGIFY(__LINE__) " -> \"" #X "\"")
30+
void
31+
panic(const char *fmt, ...);
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif

ext/rp/resets.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2022, Andrey Kunitsyn
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
14+
/**
15+
* Pico SDK compatibility for tinyusb
16+
*/
17+
#include "hardware/structs/resets.h"
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
void
24+
reset_block(uint32_t blocks);
25+
void
26+
unreset_block_wait(uint32_t blocks);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif

src/modm/platform/usb/rp/module.lb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2022, Andrey Kunitsyn
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
# -----------------------------------------------------------------------------
14+
def init(module):
15+
module.name = ":platform:usb"
16+
module.description = "Universal Serial Bus (USB)"
17+
18+
def prepare(module, options):
19+
device = options[":target"]
20+
if not device.has_driver("usb:rp20*"):
21+
return False
22+
23+
24+
module.depends(
25+
":architecture:interrupt",
26+
":cmsis:device",
27+
":platform:gpio")
28+
29+
return True
30+
31+
def build(env):
32+
env.outbasepath = "modm/src/modm/platform/usb"
33+
env.copy('usb.hpp')
34+
if env.has_module(":tinyusb:device:cdc"):
35+
env.copy(repopath("ext/hathach/uart.hpp"), "uart.hpp")

src/modm/platform/usb/rp/usb.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2022, Andrey Kunitsyn
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
14+
#include <modm/platform/device.hpp>
15+
#include <modm/platform/gpio/connector.hpp>
16+
17+
namespace modm::platform
18+
{
19+
20+
/// @ingroup modm_platform_usb
21+
class Usb
22+
{
23+
public:
24+
template<class SystemClock>
25+
static void
26+
initialize(uint8_t priority = 3)
27+
{
28+
static_assert(SystemClock::UsbFrequency == 48_MHz, "USB must have a 48MHz clock!");
29+
NVIC_SetPriority(USBCTRL_IRQ_IRQn, priority);
30+
}
31+
32+
template<class... Signals>
33+
static void
34+
connect()
35+
{
36+
using Connector = GpioConnector<Peripheral::Usb, Signals...>;
37+
// Dp, Dm use dedicated pins, but Vbus and Overcurrent can be connected
38+
Connector::connect();
39+
}
40+
};
41+
42+
} // namespace modm::platform

0 commit comments

Comments
 (0)