aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/demos/STM32/RT-STM32F407-DISCOVERY-TinyUSB-CDC-MSC/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/demos/STM32/RT-STM32F407-DISCOVERY-TinyUSB-CDC-MSC/main.c')
-rw-r--r--lib/chibios-contrib/demos/STM32/RT-STM32F407-DISCOVERY-TinyUSB-CDC-MSC/main.c167
1 files changed, 167 insertions, 0 deletions
diff --git a/lib/chibios-contrib/demos/STM32/RT-STM32F407-DISCOVERY-TinyUSB-CDC-MSC/main.c b/lib/chibios-contrib/demos/STM32/RT-STM32F407-DISCOVERY-TinyUSB-CDC-MSC/main.c
new file mode 100644
index 000000000..6e3aa1e21
--- /dev/null
+++ b/lib/chibios-contrib/demos/STM32/RT-STM32F407-DISCOVERY-TinyUSB-CDC-MSC/main.c
@@ -0,0 +1,167 @@
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
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 <stdio.h>
27#include <string.h>
28
29#include "ch.h"
30#include "hal.h"
31#include "tusb.h"
32
33/* Blink pattern
34 * - 250 ms : device not mounted
35 * - 1000 ms : device mounted
36 * - 2500 ms : device is suspended
37 */
38enum {
39 BLINK_NOT_MOUNTED = 250,
40 BLINK_MOUNTED = 1000,
41 BLINK_SUSPENDED = 2500,
42};
43
44static uint16_t blink_timer = TIME_MS2I(BLINK_NOT_MOUNTED);
45
46//--------------------------------------------------------------------+
47// Device callbacks
48//--------------------------------------------------------------------+
49
50// Invoked when device is mounted
51void tud_mount_cb(void)
52{
53 blink_timer = TIME_MS2I(BLINK_MOUNTED);
54}
55
56// Invoked when device is unmounted
57void tud_umount_cb(void)
58{
59 blink_timer = TIME_MS2I(BLINK_NOT_MOUNTED);
60}
61
62// Invoked when usb bus is suspended
63// remote_wakeup_en : if host allow us to perform remote wakeup
64// Within 7ms, device must draw an average of current less than 2.5 mA from bus
65void tud_suspend_cb(bool remote_wakeup_en)
66{
67 (void) remote_wakeup_en;
68 blink_timer = TIME_MS2I(BLINK_SUSPENDED);
69}
70
71// Invoked when usb bus is resumed
72void tud_resume_cb(void)
73{
74 blink_timer = TIME_MS2I(BLINK_MOUNTED);
75}
76
77static THD_WORKING_AREA(waUsbThread, 512);
78static THD_FUNCTION(UsbThread, arg) {
79 (void) arg;
80
81 tusb_init();
82
83 // RTOS forever loop
84 while (1)
85 {
86 // tinyusb device task
87 tud_task();
88 }
89}
90
91static THD_WORKING_AREA(waCdcThread, 256);
92static THD_FUNCTION(CdcThread, arg) {
93 (void) arg;
94
95 while (true) {
96 // connected() check for DTR bit
97 // Most but not all terminal client set this when making connection
98 // if ( tud_cdc_connected() )
99 {
100 // There are data available
101 if ( tud_cdc_available() )
102 {
103 uint8_t buf[64];
104
105 // read and echo back
106 uint32_t count = tud_cdc_read(buf, sizeof(buf));
107
108 for(uint32_t i=0; i<count; i++)
109 {
110 tud_cdc_write_char(buf[i]);
111
112 if ( buf[i] == '\r' ) tud_cdc_write_char('\n');
113 }
114
115 tud_cdc_write_flush();
116 }
117 }
118 chThdSleepMilliseconds(10);
119 }
120}
121
122// Invoked when cdc when line state changed e.g connected/disconnected
123void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
124{
125 (void) itf;
126 (void) rts;
127
128 // connected
129 if ( dtr )
130 {
131 // print initial message when connected
132 tud_cdc_write_str("\r\nTinyUSB CDC MSC device with FreeRTOS example\r\n");
133 tud_cdc_write_flush();
134 }
135}
136
137// Invoked when CDC interface received data from host
138void tud_cdc_rx_cb(uint8_t itf)
139{
140 (void) itf;
141}
142
143
144/*
145 * Application entry point.
146 */
147int main(void) {
148
149 /*
150 * System initializations.
151 * - HAL initialization, this also initializes the configured device drivers
152 * and performs the board-specific initializations.
153 * - Kernel initialization, the main() function becomes a thread and the
154 * RTOS is active.
155 */
156 halInit();
157 chSysInit();
158
159 chThdCreateStatic(waUsbThread, sizeof(waUsbThread), HIGHPRIO-1, UsbThread, NULL);
160 chThdCreateStatic(waCdcThread, sizeof(waCdcThread), HIGHPRIO-2, CdcThread, NULL);
161
162 while (1)
163 {
164 chThdSleepMilliseconds(blink_timer);
165 //palToggleLine(LINE_LED);
166 }
167}