-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpico-usb-host-msc-demo.c
209 lines (179 loc) · 5.98 KB
/
pico-usb-host-msc-demo.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* @file Pico-USB-Host-MIDI-Adapter.c
* @brief A USB Host to Serial Port MIDI adapter that runs on a Raspberry Pi
* Pico board
*
* MIT License
* Copyright (c) 2022 rppicomidi
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#if CFG_TUH_RPI_PIO_USB
#include "pico/multicore.h"
#include "pico/bootrom.h"
#include "pio_usb.h"
#endif
#include "bsp/board.h"
#include "tusb.h"
#include "class/msc/msc_host.h"
#include "ff.h"
#include "diskio.h"
#include "msc-demo-cli.h"
#ifdef RPPICOMIDI_PICO_W
#include "pico/cyw43_arch.h"
#endif
// On-board LED mapping. If no LED, set to NO_LED_GPIO
const uint NO_LED_GPIO = 255;
const uint LED_GPIO = 25;
static scsi_inquiry_resp_t inquiry_resp;
static FATFS fatfs[CFG_TUH_DEVICE_MAX];
static_assert(FF_VOLUMES == CFG_TUH_DEVICE_MAX);
static void blink_led(void)
{
static absolute_time_t previous_timestamp = {0};
static bool led_state = false;
// This design has no on-board LED
if (NO_LED_GPIO == LED_GPIO)
return;
absolute_time_t now = get_absolute_time();
int64_t diff = absolute_time_diff_us(previous_timestamp, now);
if (diff > 1000000) {
#ifdef RPPICOMIDI_PICO_W
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_state);
#else
gpio_put(LED_GPIO, led_state);
#endif
led_state = !led_state;
previous_timestamp = now;
}
}
void main_loop_task()
{
#if !defined(CFG_TUH_RPI_PIO_USB) || (CFG_TUH_RPI_PIO_USB == 0)
tuh_task();
#endif
msc_demo_cli_task();
blink_led();
}
#if CFG_TUH_RPI_PIO_USB
// core1: handle host events
static volatile bool core1_booting = true;
static volatile bool core0_booting = true;
void core1_main() {
// To run USB SOF interrupt in core1, init host stack for pio_usb (roothub
// port1) on core1
tuh_init(1);
core1_booting = false;
while(core0_booting) {
}
while (true) {
tuh_task(); // tinyusb host task
}
}
#endif
int main()
{
bi_decl(bi_program_description("Provide a USB host interface for FATFS formatted USB drives."));
bi_decl(bi_1pin_with_name(LED_GPIO, "On-board LED"));
board_init();
#if !defined(CFG_TUH_RPI_PIO_USB) || (CFG_TUH_RPI_PIO_USB == 0)
tusb_init();
#else
// all USB Host task run in core1
multicore_reset_core1();
multicore_launch_core1(core1_main);
#endif
printf("Pico USB Host Mass Storage Class Demo\r\n");
#if defined(CFG_TUH_RPI_PIO_USB) && (CFG_TUH_RPI_PIO_USB == 1)
// wait for core 1 to finish claiming PIO state machines and DMA
while(core1_booting) {
}
#endif
// Map the pins to functions
#ifdef RPPICOMIDI_PICO_W
if (cyw43_arch_init()) {
printf("WiFi init failed");
return -1;
}
#else
gpio_init(LED_GPIO);
gpio_set_dir(LED_GPIO, GPIO_OUT);
#endif
msc_fat_init();
msc_demo_cli_init();
#if defined(CFG_TUH_RPI_PIO_USB) && (CFG_TUH_RPI_PIO_USB == 1)
core0_booting = false;
#endif
while (1) {
main_loop_task();
}
}
//--------------------------------------------------------------------+
// TinyUSB Callbacks
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
// MSC implementation
//--------------------------------------------------------------------+
bool inquiry_complete_cb(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data)
{
if (cb_data->csw->status != 0) {
printf("Inquiry failed\r\n");
return false;
}
// Print out Vendor ID, Product ID and Rev
printf("%.8s %.16s rev %.4s\r\n", inquiry_resp.vendor_id, inquiry_resp.product_id, inquiry_resp.product_rev);
// Get capacity of device
uint32_t const block_count = tuh_msc_get_block_count(dev_addr, cb_data->cbw->lun);
uint32_t const block_size = tuh_msc_get_block_size(dev_addr, cb_data->cbw->lun);
printf("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size));
printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size);
return true;
}
void tuh_msc_mount_cb(uint8_t dev_addr)
{
uint8_t pdrv = msc_map_next_pdrv(dev_addr);
assert(pdrv < FF_VOLUMES);
msc_fat_plug_in(pdrv);
uint8_t const lun = 0;
tuh_msc_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb, 0);
char path[3] = "0:";
path[0] += pdrv;
if ( f_mount(&fatfs[pdrv],path, 0) != FR_OK ) {
printf("mount drive %s failed\r\n", path);
return;
}
if (f_chdrive(path) != FR_OK) {
printf("f_chdrive(%s) failed\r\n", path);
return;
}
printf("\r\nMass Storage drive %u is mounted\r\n", pdrv);
printf("Run the set-date and set-time commands so file timestamps are correct\r\n\r\n");
}
void tuh_msc_umount_cb(uint8_t dev_addr)
{
uint8_t pdrv = msc_unmap_pdrv(dev_addr);
char path[3] = "0:";
path[0] += pdrv;
f_mount(NULL, path, 0); // unmount disk
msc_fat_unplug(pdrv);
printf("Mass Storage drive %u is unmounted\r\n", pdrv);
}