Skip to content

Commit 618a57e

Browse files
authored
Merge pull request #313 from arduino/karlsoderby/usb-review
Giga USB changes
2 parents ef0b624 + a117538 commit 618a57e

File tree

3 files changed

+126
-102
lines changed

3 files changed

+126
-102
lines changed

content/hardware/08.mega/boards/giga-r1/tutorials/giga-getting-started/giga-getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To install it, you will need a version of the Arduino IDE, which you can downloa
1111

1212
## Software & Hardware Needed
1313

14-
- [Arduino GIGA R1](/hardware/giga-r1) or [Arduino GIGA R1 WiFi](/hardware/giga-r1-wifi)
14+
- [Arduino GIGA R1](/-r1) or [Arduino GIGA R1 WiFi](/hardware/giga-r1-wifi)
1515
- [Arduino IDE](https://docs.arduino.cc/software/ide-v2)
1616

1717
***You can also use the [Web Editor](https://create.arduino.cc/editor) which comes with all Arduino boards pre-installed.***
Loading

content/hardware/08.mega/boards/giga-r1/tutorials/giga-usb/giga-usb.md

Lines changed: 125 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ It can easily be configured to act as a mouse or keyboard (HID device) or as a U
1111

1212
In this guide, we will take a look at the available features, how to enable them in a sketch and what circuit (if any) is required.
1313

14-
***To get inspired on what you can do with the USB features, please see [use cases](#use-cases) at the end of this article.***
14+
## Hardware & Software Needed
1515

16-
## Hardware Overview
16+
- [GIGA R1](/hardware/giga-r1-wifi) or [GIGA R1 WiFi](/hardware/giga-r1).
17+
- USB Mass Storage Device (USB Stick).
18+
- Keyboard.
19+
20+
## USB Overview
1721

1822
The GIGA R1 have two USB connectors:
1923
- **USB-C** - for powering, programming & HID communication.
@@ -57,38 +61,30 @@ USB mass storage devices connected needs to be formatted with the **FAT32** as a
5761

5862
To access the correct USB mass storage device, we need to specify the **designation** in the code.
5963

60-
```
64+
```arduino
6165
mbed::FATFileSystem usb("USB_DRIVE_DESIGNATION")
6266
```
6367

6468
This is so that our GIGA R1 can target the right USB device.
6569

66-
### List File Directory
67-
68-
Below is an example sketch that can be used to **list** files in a USB mass storage device.
70+
Please note that when writing/reading to files, you will need to specify the correct path, for example:
6971

7072
```arduino
71-
/*
72-
Portenta - DirList
7373
74-
The sketch shows how to mount an usb storage device and how to
75-
get a list of the existing folders and files.
74+
```
7675

77-
The circuit:
78-
- Portenta H7
76+
### List File Directory
7977

80-
This example code is in the public domain.
81-
*/
78+
Below is an example sketch that can be used to **list** files in a USB mass storage device.
8279

80+
```arduino
8381
#include <DigitalOut.h>
8482
#include <FATFileSystem.h>
8583
#include <USBHostMbed5.h>
8684
8785
USBHostMSD msd;
8886
mbed::FATFileSystem usb("usb");
8987
90-
// If you are using a Portenta Machine Control uncomment the following line
91-
// mbed::DigitalOut otg(PB_14, 0);
9288
9389
void setup()
9490
{
@@ -167,20 +163,6 @@ void loop()
167163
Below is an example sketch that can be used to **read** files from a USB mass storage device.
168164

169165
```arduino
170-
/*
171-
Portenta - FileRead
172-
173-
The sketch shows how to mount an usb storage device and how to
174-
read from an existing file.
175-
to use this sketch create a .txt file named Arduino.txt,
176-
in your storage device and write some content inside.
177-
178-
The circuit:
179-
- Portenta H7
180-
181-
This example code is in the public domain.
182-
*/
183-
184166
#include <USBHostMbed5.h>
185167
#include <DigitalOut.h>
186168
#include <FATFileSystem.h>
@@ -254,18 +236,6 @@ void loop() {
254236
Below is an example sketch that can be used to **write** files from a USB mass storage device.
255237

256238
```arduino
257-
/*
258-
Portenta - FileWrite
259-
260-
The sketch shows how to mount an usb storage device and how to
261-
write a file, eventually overwriting the original content.
262-
263-
The circuit:
264-
- Portenta H7
265-
266-
This example code is in the public domain.
267-
*/
268-
269239
#include <USBHostMbed5.h>
270240
#include <DigitalOut.h>
271241
#include <FATFileSystem.h>
@@ -336,6 +306,113 @@ void loop() {
336306
}
337307
```
338308

309+
### Datalogger Example
310+
311+
In the example below, we are reading logging the `A0` pin, where we are defining two parameters:
312+
- `interval` - how long between each reading.
313+
- `number_of_readings` - how many readings we should take.
314+
315+
This is useful if you e.g. want to log a specific amount of samples for a specific amount of time.
316+
317+
```arduino
318+
#include <USBHostMbed5.h>
319+
#include <DigitalOut.h>
320+
#include <FATFileSystem.h>
321+
322+
USBHostMSD msd;
323+
mbed::FATFileSystem usb("usb");
324+
325+
int err;
326+
int count;
327+
int number_of_readings = 100; //how many readings you want to take
328+
int interval = 10; //how long between readings (milliseconds)
329+
330+
void setup() {
331+
Serial.begin(115200);
332+
333+
pinMode(PA_15, OUTPUT); //enable the USB-A port
334+
digitalWrite(PA_15, HIGH);
335+
336+
while (!Serial); //stop program from executing until serial port opens
337+
338+
msd.connect();
339+
340+
while (!msd.connected()) {
341+
Serial.print("MSD not found.");
342+
delay(1000);
343+
}
344+
345+
Serial.println("Mounting USB device...");
346+
347+
err = usb.mount(&msd);
348+
if (err) {
349+
Serial.print("Error mounting USB device ");
350+
Serial.println(err);
351+
while (1)
352+
;
353+
}
354+
Serial.print("read done ");
355+
356+
//function to write to file
357+
WriteToFile();
358+
}
359+
360+
void loop() {
361+
}
362+
363+
void WriteToFile() {
364+
mbed::fs_file_t file;
365+
struct dirent *ent;
366+
int dirIndex = 0;
367+
int res = 0;
368+
369+
Serial.println("Opening file..");
370+
FILE *f = fopen("/usb/log.txt", "w+");
371+
372+
for (int i = 0; i < number_of_readings; i++) {
373+
count += 1;
374+
375+
Serial.print("Reading Nr: ");
376+
Serial.print(count);
377+
Serial.print(", Value: ");
378+
Serial.println(analogRead(A0));
379+
380+
fflush(stdout);
381+
382+
int reading = analogRead(A0);
383+
384+
err = fprintf(f, "%s", "Reading Nr: ");
385+
err = fprintf(f, "%d", count);
386+
err = fprintf(f, "%s", ", Value: ");
387+
err = fprintf(f, "%d\n", reading);
388+
389+
if (err < 0) {
390+
Serial.println("Fail :(");
391+
error("error: %s (%d)\n", strerror(errno), -errno);
392+
}
393+
delay(interval);
394+
}
395+
396+
Serial.println("File closing");
397+
fflush(stdout);
398+
err = fclose(f);
399+
400+
if (err < 0) {
401+
Serial.print("fclose error:");
402+
Serial.print(strerror(errno));
403+
Serial.print(" (");
404+
Serial.print(-errno);
405+
Serial.print(")");
406+
} else {
407+
Serial.println("File closed");
408+
}
409+
}
410+
```
411+
412+
After logging data, remove the USB stick from your board, and insert it in your computer to see the data logged:
413+
414+
![Data logged in .txt file.](assets/giga-file-write.png)
415+
339416
## USB Host Keyboard
340417

341418
It is possible to connect generic USB keyboards to the GIGA R1's USB-A connector without any additional circuitry. This library is included in the core, so it does not require any additional installation.
@@ -456,63 +533,10 @@ delay(1000);
456533
Mouse.release();
457534
```
458535

459-
## GIGA R1 as a USB Stick
460-
461-
It is possible to expose the external flash (16MB) on the GIGA R1 as a USB device. This makes it possible to store smaller files directly on the GIGA R1, which can be accessed through the sketch.
462-
463-
```arduino
464-
#include "PluggableUSBMSD.h"
465-
#include "QSPIFBlockDevice.h"
466-
#include "MBRBlockDevice.h"
467-
#include "FATFileSystem.h"
468-
469-
static QSPIFBlockDevice root;
470-
mbed::MBRBlockDevice wifi_data(&root, 1);
471-
mbed::MBRBlockDevice ota_data(&root, 2);
472-
static mbed::FATFileSystem wifi("wifi");
473-
static mbed::FATFileSystem ota("ota");
474-
475-
void USBMSD::begin()
476-
{
477-
int err = wifi.mount(&wifi_data);
478-
if (err) {
479-
while (!Serial);
480-
Serial.println("Please run WiFiFirmwareUpdater before");
481-
return;
482-
}
483-
ota.mount(&ota_data);
484-
}
485-
536+
## Summary
486537

487-
USBMSD MassStorage(&root);
488-
489-
void setup() {
490-
Serial.begin(115200);
491-
MassStorage.begin();
492-
}
493-
494-
void printDirectory(char* name) {
495-
DIR *d;
496-
struct dirent *p;
497-
498-
d = opendir(name);
499-
if (d != NULL) {
500-
while ((p = readdir(d)) != NULL) {
501-
Serial.println(p->d_name);
502-
}
503-
}
504-
closedir(d);
505-
}
506-
507-
void loop() {
508-
if (MassStorage.media_removed()) {
509-
// list the content of the partitions
510-
// you may need to restart the board for the list to update if you copied new files
511-
Serial.println("Content of WiFi partition:");
512-
printDirectory("/wifi");
513-
Serial.println("Content of OTA partition:");
514-
printDirectory("/ota");
515-
}
516-
delay(1000);
517-
}
518-
```
538+
The goal with this guide was to provide a summary of all the GIGA R1's features, including:
539+
- Enabling and disabling the USB-A port.
540+
- Read & Write to a USB mass storage device (MSD).
541+
- Connecting keyboards and reading key presses.
542+
- Emulate a mouse/keyboard through the HID interface.

0 commit comments

Comments
 (0)