Skip to content

Commit 02b14b3

Browse files
committed
Add MTP class device
1 parent f01c4be commit 02b14b3

24 files changed

+2461
-0
lines changed

examples/device/mtp/CMakeLists.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
4+
5+
# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
6+
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
7+
8+
project(${PROJECT} C CXX ASM)
9+
10+
# Checks this example is valid for the family and initializes the project
11+
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
12+
13+
# Espressif has its own cmake build system
14+
if(FAMILY STREQUAL "espressif")
15+
return()
16+
endif()
17+
18+
if (RTOS STREQUAL zephyr)
19+
set(EXE_NAME app)
20+
else()
21+
set(EXE_NAME ${PROJECT})
22+
add_executable(${EXE_NAME})
23+
endif()
24+
25+
# Example source
26+
target_sources(${EXE_NAME} PRIVATE
27+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
28+
${CMAKE_CURRENT_SOURCE_DIR}/src/mtp_fs_example.c
29+
${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
30+
)
31+
32+
# Example include
33+
target_include_directories(${EXE_NAME} PUBLIC
34+
${CMAKE_CURRENT_SOURCE_DIR}/src
35+
)
36+
37+
# Configure compilation flags and libraries for the example without RTOS.
38+
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
39+
family_configure_device_example(${EXE_NAME} ${RTOS})

examples/device/mtp/CMakePresets.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 6,
3+
"include": [
4+
"../../../hw/bsp/BoardPresets.json"
5+
]
6+
}

examples/device/mtp/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
include ../../build_system/make/make.mk
2+
3+
INC += \
4+
src \
5+
$(TOP)/hw \
6+
7+
# Example source
8+
EXAMPLE_SOURCE += $(wildcard src/*.c)
9+
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
10+
11+
include ../../build_system/make/rules.mk

examples/device/mtp/prj.conf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CONFIG_GPIO=y
2+
CONFIG_FPU=y
3+
CONFIG_NO_OPTIMIZATIONS=y
4+
CONFIG_UART_INTERRUPT_DRIVEN=y
5+
CONFIG_NRFX_POWER=y
6+
CONFIG_NRFX_UARTE0=y

examples/device/mtp/skip.txt

Whitespace-only changes.

examples/device/mtp/src/main.c

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2025 Ennebi Elettronica (https://ennebielettronica.com)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <stdlib.h>
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
#include "bsp/board_api.h"
31+
#include "tusb.h"
32+
33+
//--------------------------------------------------------------------+
34+
// MACRO CONSTANT TYPEDEF PROTYPES
35+
//--------------------------------------------------------------------+
36+
37+
/* Blink pattern
38+
* - 250 ms : device not mounted
39+
* - 1000 ms : device mounted
40+
* - 2500 ms : device is suspended
41+
*/
42+
enum {
43+
BLINK_NOT_MOUNTED = 250,
44+
BLINK_MOUNTED = 1000,
45+
BLINK_SUSPENDED = 2500,
46+
};
47+
48+
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
49+
50+
void led_blinking_task(void);
51+
52+
/*------------- MAIN -------------*/
53+
int main(void) {
54+
board_init();
55+
56+
// init device stack on configured roothub port
57+
tusb_rhport_init_t dev_init = {
58+
.role = TUSB_ROLE_DEVICE,
59+
.speed = TUSB_SPEED_AUTO
60+
};
61+
tusb_init(BOARD_TUD_RHPORT, &dev_init);
62+
63+
if (board_init_after_tusb) {
64+
board_init_after_tusb();
65+
}
66+
67+
while (1) {
68+
tud_task(); // tinyusb device task
69+
led_blinking_task();
70+
}
71+
}
72+
73+
//--------------------------------------------------------------------+
74+
// Device callbacks
75+
//--------------------------------------------------------------------+
76+
77+
// Invoked when device is mounted
78+
void tud_mount_cb(void) {
79+
blink_interval_ms = BLINK_MOUNTED;
80+
}
81+
82+
// Invoked when device is unmounted
83+
void tud_umount_cb(void) {
84+
blink_interval_ms = BLINK_NOT_MOUNTED;
85+
}
86+
87+
// Invoked when usb bus is suspended
88+
// remote_wakeup_en : if host allow us to perform remote wakeup
89+
// Within 7ms, device must draw an average of current less than 2.5 mA from bus
90+
void tud_suspend_cb(bool remote_wakeup_en) {
91+
(void) remote_wakeup_en;
92+
blink_interval_ms = BLINK_SUSPENDED;
93+
}
94+
95+
// Invoked when usb bus is resumed
96+
void tud_resume_cb(void) {
97+
blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
98+
}
99+
100+
//--------------------------------------------------------------------+
101+
// BLINKING TASK
102+
//--------------------------------------------------------------------+
103+
void led_blinking_task(void) {
104+
static uint32_t start_ms = 0;
105+
static bool led_state = false;
106+
107+
// Blink every interval ms
108+
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time
109+
start_ms += blink_interval_ms;
110+
111+
board_led_write(led_state);
112+
led_state = 1 - led_state; // toggle
113+
}

0 commit comments

Comments
 (0)