Updated testusbhostFAT example and submodules

This commit is contained in:
Kristian Lauszus 2014-05-27 09:42:13 +02:00
parent 67cb06e858
commit d9dac13225
6 changed files with 175 additions and 177 deletions

@ -1 +1 @@
Subproject commit 1cacea4e8933b37b9f98528b2a831031f69905de Subproject commit d35bb955e3818f0c14e47c8a1998003da8dc1b5a

View file

@ -9,6 +9,10 @@
BOARD = mega BOARD = mega
PROGRAMMER = arduino PROGRAMMER = arduino
#BOARD = teensypp2
#BOARD = teensy3
#BOARD = teensy31
# set your Arduino tty port here # set your Arduino tty port here
PORT = /dev/ttyUSB0 PORT = /dev/ttyUSB0

@ -1 +1 @@
Subproject commit 9108effe4d4e556198e3e7b95365d1c898680dae Subproject commit 7fd6a306ca53d08bf53b2bbfc1b80eb056f2c55b

@ -1 +1 @@
Subproject commit ab85718a917094391762b79140d8e3a03af136a4 Subproject commit 0b8e3076b5a072251e01cfc6e6333b364d4e71e7

340
examples/testusbhostFAT/testusbhostFAT.ino Normal file → Executable file
View file

@ -19,35 +19,39 @@
* *
*/ */
/////////////////////////////////////////////////////////////
// Please Note: //
// This section is for info with the Arduino IDE ONLY. //
// Unfortunately due to short sightedness of the Arduino //
// code team, that you must set the following in the //
// respective libraries. //
// Changing them here will have _NO_ effect! //
/////////////////////////////////////////////////////////////
// Uncomment to enable debugging
//#define DEBUG_USB_HOST
// This is where stderr/USB debugging goes to
//#define USB_HOST_SERIAL Serial3
// If you have external memory, setting this to 0 enables FAT table caches.
// The 0 setting is recommended only if you have external memory.
//#define _FS_TINY 1
//#define _USE_LFN 3
//#define EXT_RAM_STACK 1
//#define EXT_RAM_HEAP 1
//#define _MAX_SS 512
/////////////////////////////////////////////////////////////
// End of Arduino IDE specific information //
/////////////////////////////////////////////////////////////
// You can set this to 0 if you are not using a USB hub. // You can set this to 0 if you are not using a USB hub.
// It will save a little bit of flash and RAM. // It will save a little bit of flash and RAM.
// Set to 1 if you want to use a hub. // Set to 1 if you want to use a hub.
#define WANT_HUB_TEST 0 #define WANT_HUB_TEST 0
///////////////////////////////////////////////////////////// #if defined(__AVR__)
// Please Note: This section is for Arduino IDE ONLY. //
// Use of Make creates a flash image that is 3.3KB smaller //
/////////////////////////////////////////////////////////////
#ifndef USING_MAKEFILE
// Uncomment to enable debugging
//#define DEBUG_USB_HOST
// This is where stderr/USB debugging goes to
#define USB_HOST_SERIAL Serial3
// If you have external memory, setting this to 0 enables FAT table caches.
// The 0 setting is recommended only if you have external memory.
#define _FS_TINY 1
// These you can safely leave alone.
#define _USE_LFN 3
#define EXT_RAM_STACK 1
#define EXT_RAM_HEAP 1
#define _MAX_SS 512
#endif
/////////////////////////////////////////////////////////////
// End of Arduino IDE specific hacks //
/////////////////////////////////////////////////////////////
#if defined(AVR)
#include <xmem.h> #include <xmem.h>
#else #else
#include <spi4teensy3.h> #include <spi4teensy3.h>
@ -63,7 +67,7 @@
#include <Wire.h> #include <Wire.h>
#include <RTClib.h> #include <RTClib.h>
#include <stdio.h> #include <stdio.h>
#if defined(AVR) #if defined(__AVR__)
static FILE tty_stdio; static FILE tty_stdio;
static FILE tty_stderr; static FILE tty_stderr;
volatile uint32_t LEDnext_time; // fade timeout volatile uint32_t LEDnext_time; // fade timeout
@ -100,7 +104,7 @@ static storage_t sto[_VOLUMES];
#define mbxs 128 #define mbxs 128
static uint8_t My_Buff_x[mbxs]; /* File read buffer */ static uint8_t My_Buff_x[mbxs]; /* File read buffer */
#if defined(AVR) #if defined(__AVR__)
#define prescale1 ((1 << WGM12) | (1 << CS10)) #define prescale1 ((1 << WGM12) | (1 << CS10))
#define prescale8 ((1 << WGM12) | (1 << CS11)) #define prescale8 ((1 << WGM12) | (1 << CS11))
@ -126,7 +130,7 @@ static int tty_std_putc(char c, FILE *t) {
} }
static int tty_std_getc(FILE *t) { static int tty_std_getc(FILE *t) {
while (!Serial.available()); while(!Serial.available());
return Serial.read(); return Serial.read();
} }
@ -140,18 +144,18 @@ extern "C" {
int _write(int fd, const char *ptr, int len) { int _write(int fd, const char *ptr, int len) {
int j; int j;
for (j = 0; j < len; j++) { for(j = 0; j < len; j++) {
if (fd == 1) if(fd == 1)
Serial.write(*ptr++); Serial.write(*ptr++);
else if (fd == 2) else if(fd == 2)
USB_HOST_SERIAL.write(*ptr++); USB_HOST_SERIAL.write(*ptr++);
} }
return len; return len;
} }
int _read(int fd, char *ptr, int len) { int _read(int fd, char *ptr, int len) {
if (len > 0 && fd == 0) { if(len > 0 && fd == 0) {
while (!Serial.available()); while(!Serial.available());
*ptr = Serial.read(); *ptr = Serial.read();
return 1; return 1;
} }
@ -175,7 +179,7 @@ extern "C" {
void setup() { void setup() {
boolean serr = false; boolean serr = false;
for (int i = 0; i < _VOLUMES; i++) { for(int i = 0; i < _VOLUMES; i++) {
Fats[i] = NULL; Fats[i] = NULL;
sto[i].private_data = new pvt_t; sto[i].private_data = new pvt_t;
((pvt_t *)sto[i].private_data)->B = 255; // impossible ((pvt_t *)sto[i].private_data)->B = 255; // impossible
@ -184,7 +188,7 @@ void setup() {
// minimum 0x00, maximum 0xff // minimum 0x00, maximum 0xff
UsbDEBUGlvl = 0x51; UsbDEBUGlvl = 0x51;
#if defined(AVR) #if !defined(CORE_TEENSY) && defined(__AVR__)
// make LED pin as an output: // make LED pin as an output:
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(2, OUTPUT); pinMode(2, OUTPUT);
@ -193,11 +197,23 @@ void setup() {
// Initialize 'debug' serial port // Initialize 'debug' serial port
USB_HOST_SERIAL.begin(115200); USB_HOST_SERIAL.begin(115200);
// Do not start primary Serial port if already started. // Do not start primary Serial port if already started.
if (bit_is_clear(UCSR0B, TXEN0)) { if(bit_is_clear(UCSR0B, TXEN0)) {
Serial.begin(115200); Serial.begin(115200);
serr = true; serr = true;
} }
// Blink LED
delay(500);
analogWrite(LED_BUILTIN, 255);
delay(500);
analogWrite(LED_BUILTIN, 0);
delay(500);
#else
while(!Serial);
Serial.begin(115200); // On the Teensy 3.x we get a delay at least!
#endif
#if defined(__AVR__)
// Set up stdio/stderr // Set up stdio/stderr
tty_stdio.put = tty_std_putc; tty_stdio.put = tty_std_putc;
tty_stdio.get = tty_std_getc; tty_stdio.get = tty_std_getc;
@ -212,17 +228,7 @@ void setup() {
stdout = &tty_stdio; stdout = &tty_stdio;
stdin = &tty_stdio; stdin = &tty_stdio;
stderr = &tty_stderr; stderr = &tty_stderr;
// Blink LED
delay(500);
analogWrite(LED_BUILTIN, 255);
delay(500);
analogWrite(LED_BUILTIN, 0);
delay(500);
#else
while (!Serial);
#endif #endif
printf_P(PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n")); printf_P(PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n"));
printf_P(PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl); printf_P(PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl);
printf_P(PSTR("'+' and '-' increase/decrease by 0x01\r\n")); printf_P(PSTR("'+' and '-' increase/decrease by 0x01\r\n"));
@ -236,7 +242,7 @@ void setup() {
"Disabled" "Disabled"
#endif #endif
"\r\n")); "\r\n"));
if (serr) { if(serr) {
fprintf_P(stderr, PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n")); fprintf_P(stderr, PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n"));
fprintf_P(stderr, PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl); fprintf_P(stderr, PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl);
fprintf_P(stderr, PSTR("Long filename support: " fprintf_P(stderr, PSTR("Long filename support: "
@ -247,8 +253,8 @@ void setup() {
#endif #endif
"\r\n")); "\r\n"));
} }
#if defined(AVR)
#if !defined(CORE_TEENSY) && defined(__AVR__)
analogWrite(LED_BUILTIN, 255); analogWrite(LED_BUILTIN, 255);
delay(500); delay(500);
analogWrite(LED_BUILTIN, 0); analogWrite(LED_BUILTIN, 0);
@ -263,7 +269,7 @@ void setup() {
delay(500); delay(500);
LEDnext_time = millis() + 1; LEDnext_time = millis() + 1;
#ifdef EXT_RAM #if EXT_RAM
printf_P(PSTR("Total EXT RAM banks %i\r\n"), xmem::getTotalBanks()); printf_P(PSTR("Total EXT RAM banks %i\r\n"), xmem::getTotalBanks());
#endif #endif
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
@ -274,22 +280,22 @@ void setup() {
// I want to be able to have slightly more control. // I want to be able to have slightly more control.
// Besides, it is easier to initialize stuff... // Besides, it is easier to initialize stuff...
#if WANT_HUB_TEST #if WANT_HUB_TEST
for (int i = 0; i < MAX_HUBS; i++) { for(int i = 0; i < MAX_HUBS; i++) {
Hubs[i] = new USBHub(&Usb); Hubs[i] = new USBHub(&Usb);
#if defined(AVR) #if defined(__AVR__)
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
#endif #endif
} }
#endif #endif
// Initialize generic storage. This must be done before USB starts. // Initialize generic storage. This must be done before USB starts.
InitStorage(); Init_Generic_Storage();
while (Usb.Init(1000) == -1) { while(Usb.Init(1000) == -1) {
printf_P(PSTR("No USB HOST Shield?\r\n")); printf_P(PSTR("No USB HOST Shield?\r\n"));
Notify(PSTR("OSC did not start."), 0x40); Notify(PSTR("OSC did not start."), 0x40);
} }
#if defined(AVR) #if !defined(CORE_TEENSY) && defined(__AVR__)
cli(); cli();
TCCR3A = 0; TCCR3A = 0;
TCCR3B = 0; TCCR3B = 0;
@ -300,32 +306,10 @@ void setup() {
sei(); sei();
HEAPnext_time = millis() + 10000; HEAPnext_time = millis() + 10000;
#else
#if 0
//
// On the teensy 3 we can raise the speed of SPI here.
//
// Default seen is 0xB8011001.
//
uint32_t ctar = SPI0_CTAR0;
//printf("SPI_CTAR0 = %8.8X\r\n", ctar);
ctar &= 0x7FFCFFF0; // 1/4 fSYS, 12.5Mhz
//printf("SPI_CTAR0 = %8.8X\r\n", ctar);
ctar |= 0x80000000; // 1/2 fSYS 25Mhz
//printf("SPI_CTAR0 = %8.8X\r\n", ctar);
uint32_t mcr = SPI0_MCR;
if (mcr & SPI_MCR_MDIS) {
SPI0_CTAR0 = ctar;
} else {
SPI0_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
SPI0_CTAR0 = ctar;
SPI0_MCR = mcr;
}
#endif #endif
#if defined(__AVR__)
HEAPnext_time = millis() + 10000;
#endif #endif
} }
void serialEvent() { void serialEvent() {
@ -334,23 +318,23 @@ void serialEvent() {
// . to increase by 16, , to decrease by 16 // . to increase by 16, , to decrease by 16
// e to flick VBUS // e to flick VBUS
// * to report debug level // * to report debug level
if (Serial.available()) { if(Serial.available()) {
int inByte = Serial.read(); int inByte = Serial.read();
switch (inByte) { switch(inByte) {
case '+': case '+':
if (UsbDEBUGlvl < 0xff) UsbDEBUGlvl++; if(UsbDEBUGlvl < 0xff) UsbDEBUGlvl++;
reportlvl = true; reportlvl = true;
break; break;
case '-': case '-':
if (UsbDEBUGlvl > 0x00) UsbDEBUGlvl--; if(UsbDEBUGlvl > 0x00) UsbDEBUGlvl--;
reportlvl = true; reportlvl = true;
break; break;
case '.': case '.':
if (UsbDEBUGlvl < 0xf0) UsbDEBUGlvl += 16; if(UsbDEBUGlvl < 0xf0) UsbDEBUGlvl += 16;
reportlvl = true; reportlvl = true;
break; break;
case ',': case ',':
if (UsbDEBUGlvl > 0x0f) UsbDEBUGlvl -= 16; if(UsbDEBUGlvl > 0x0f) UsbDEBUGlvl -= 16;
reportlvl = true; reportlvl = true;
break; break;
case '*': case '*':
@ -367,10 +351,11 @@ void serialEvent() {
} }
} }
#if defined(AVR) #if !defined(CORE_TEENSY) && defined(__AVR__)
// ALL teensy versions LACK PWM ON LED
ISR(TIMER3_COMPA_vect) { ISR(TIMER3_COMPA_vect) {
if (millis() >= LEDnext_time) { if((long)(millis() - LEDnext_time) >= 0L) {
LEDnext_time = millis() + 30; LEDnext_time = millis() + 30;
// set the brightness of LED // set the brightness of LED
@ -380,11 +365,11 @@ ISR(TIMER3_COMPA_vect) {
brightness = brightness + fadeAmount; brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: // reverse the direction of the fading at the ends of the fade:
if (brightness <= 0) { if(brightness <= 0) {
brightness = 0; brightness = 0;
fadeAmount = -fadeAmount; fadeAmount = -fadeAmount;
} }
if (brightness >= 255) { if(brightness >= 255) {
brightness = 255; brightness = 255;
fadeAmount = -fadeAmount; fadeAmount = -fadeAmount;
} }
@ -404,29 +389,30 @@ void die(FRESULT rc) {
void loop() { void loop() {
FIL My_File_Object_x; /* File object */ FIL My_File_Object_x; /* File object */
#if defined(AVR) #if defined(__AVR__)
// Print a heap status report about every 10 seconds. // Print a heap status report about every 10 seconds.
if (millis() >= HEAPnext_time) { if((long)(millis() - HEAPnext_time) >= 0L) {
if (UsbDEBUGlvl > 0x50) { if(UsbDEBUGlvl > 0x50) {
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
} }
HEAPnext_time = millis() + 10000; HEAPnext_time = millis() + 10000;
} }
TCCR3B = 0; TCCR3B = 0;
#else #endif
// Arm suffers here, oh well... #if defined(CORE_TEENSY)
// Teensy suffers here, oh well...
serialEvent(); serialEvent();
#endif #endif
// Horrid! This sort of thing really belongs in an ISR, not here! // Horrid! This sort of thing really belongs in an ISR, not here!
// We also will be needing to test each hub port, we don't do this yet! // We also will be needing to test each hub port, we don't do this yet!
if (!change && !usbon && millis() >= usbon_time) { if(!change && !usbon && (long)(millis() - usbon_time) >= 0L) {
change = true; change = true;
usbon = true; usbon = true;
} }
if (change) { if(change) {
change = false; change = false;
if (usbon) { if(usbon) {
Usb.vbusPower(vbus_on); Usb.vbusPower(vbus_on);
printf_P(PSTR("VBUS on\r\n")); printf_P(PSTR("VBUS on\r\n"));
} else { } else {
@ -436,21 +422,21 @@ void loop() {
} }
Usb.Task(); Usb.Task();
current_state = Usb.getUsbTaskState(); current_state = Usb.getUsbTaskState();
if (current_state != last_state) { if(current_state != last_state) {
if (UsbDEBUGlvl > 0x50) if(UsbDEBUGlvl > 0x50)
printf_P(PSTR("USB state = %x\r\n"), current_state); printf_P(PSTR("USB state = %x\r\n"), current_state);
#if defined(AVR) #if !defined(CORE_TEENSY) && defined(__AVR__)
if (current_state == USB_STATE_RUNNING) { if(current_state == USB_STATE_RUNNING) {
fadeAmount = 30; fadeAmount = 30;
} }
#endif #endif
if (current_state == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) { if(current_state == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
#if defined(AVR) #if !defined(CORE_TEENSY) && defined(__AVR__)
fadeAmount = 80; fadeAmount = 80;
#endif #endif
partsready = false; partsready = false;
for (int i = 0; i < cpart; i++) { for(int i = 0; i < cpart; i++) {
if (Fats[i] != NULL) if(Fats[i] != NULL)
delete Fats[i]; delete Fats[i];
Fats[i] = NULL; Fats[i] = NULL;
} }
@ -462,48 +448,48 @@ void loop() {
} }
// only do any of this if usb is on // only do any of this if usb is on
if (usbon) { if(usbon) {
if (partsready && !fatready) { if(partsready && !fatready) {
if (cpart > 0) fatready = true; if(cpart > 0) fatready = true;
} }
// This is horrible, and needs to be moved elsewhere! // This is horrible, and needs to be moved elsewhere!
for (int B = 0; B < MAX_USB_MS_DRIVERS; B++) { for(int B = 0; B < MAX_USB_MS_DRIVERS; B++) {
if (!partsready && (Bulk[B]->GetAddress() != NULL)) { if(!partsready && (UHS_USB_BulkOnly[B]->GetAddress() != NULL)) {
// Build a list. // Build a list.
int ML = Bulk[B]->GetbMaxLUN(); int ML = UHS_USB_BulkOnly[B]->GetbMaxLUN();
//printf("MAXLUN = %i\r\n", ML); //printf("MAXLUN = %i\r\n", ML);
ML++; ML++;
for (int i = 0; i < ML; i++) { for(int i = 0; i < ML; i++) {
if (Bulk[B]->LUNIsGood(i)) { if(UHS_USB_BulkOnly[B]->LUNIsGood(i)) {
partsready = true; partsready = true;
((pvt_t *)(sto[i].private_data))->lun = i; ((pvt_t *)(sto[i].private_data))->lun = i;
((pvt_t *)(sto[i].private_data))->B = B; ((pvt_t *)(sto[i].private_data))->B = B;
sto[i].Read = *PRead; sto[i].Reads = *UHS_USB_BulkOnly_Read;
sto[i].Write = *PWrite; sto[i].Writes = *UHS_USB_BulkOnly_Write;
sto[i].Reads = *PReads; sto[i].Status = *UHS_USB_BulkOnly_Status;
sto[i].Writes = *PWrites; sto[i].Initialize = *UHS_USB_BulkOnly_Initialize;
sto[i].Status = *PStatus; sto[i].Commit = *UHS_USB_BulkOnly_Commit;
sto[i].TotalSectors = Bulk[B]->GetCapacity(i); sto[i].TotalSectors = UHS_USB_BulkOnly[B]->GetCapacity(i);
sto[i].SectorSize = Bulk[B]->GetSectorSize(i); sto[i].SectorSize = UHS_USB_BulkOnly[B]->GetSectorSize(i);
printf_P(PSTR("LUN:\t\t%u\r\n"), i); printf_P(PSTR("LUN:\t\t%u\r\n"), i);
printf_P(PSTR("Total Sectors:\t%08lx\t%lu\r\n"), sto[i].TotalSectors, sto[i].TotalSectors); printf_P(PSTR("Total Sectors:\t%08lx\t%lu\r\n"), sto[i].TotalSectors, sto[i].TotalSectors);
printf_P(PSTR("Sector Size:\t%04x\t\t%u\r\n"), sto[i].SectorSize, sto[i].SectorSize); printf_P(PSTR("Sector Size:\t%04x\t\t%u\r\n"), sto[i].SectorSize, sto[i].SectorSize);
// get the partition data... // get the partition data...
PT = new PCPartition; PT = new PCPartition;
if (!PT->Init(&sto[i])) { if(!PT->Init(&sto[i])) {
part_t *apart; part_t *apart;
for (int j = 0; j < 4; j++) { for(int j = 0; j < 4; j++) {
apart = PT->GetPart(j); apart = PT->GetPart(j);
if (apart != NULL && apart->type != 0x00) { if(apart != NULL && apart->type != 0x00) {
memcpy(&(parts[cpart]), apart, sizeof (part_t)); memcpy(&(parts[cpart]), apart, sizeof (part_t));
printf_P(PSTR("Partition %u type %#02x\r\n"), j, parts[cpart].type); printf_P(PSTR("Partition %u type %#02x\r\n"), j, parts[cpart].type);
// for now // for now
if (isfat(parts[cpart].type)) { if(isfat(parts[cpart].type)) {
Fats[cpart] = new PFAT(&sto[i], cpart, parts[cpart].firstSector); Fats[cpart] = new PFAT(&sto[i], cpart, parts[cpart].firstSector);
//int r = Fats[cpart]->Good(); //int r = Fats[cpart]->Good();
if (Fats[cpart]->MountStatus()) { if(Fats[cpart]->MountStatus()) {
delete Fats[cpart]; delete Fats[cpart];
Fats[cpart] = NULL; Fats[cpart] = NULL;
} else cpart++; } else cpart++;
@ -514,7 +500,7 @@ void loop() {
// try superblock // try superblock
Fats[cpart] = new PFAT(&sto[i], cpart, 0); Fats[cpart] = new PFAT(&sto[i], cpart, 0);
//int r = Fats[cpart]->Good(); //int r = Fats[cpart]->Good();
if (Fats[cpart]->MountStatus()) { if(Fats[cpart]->MountStatus()) {
//printf_P(PSTR("Superblock error %x\r\n"), r); //printf_P(PSTR("Superblock error %x\r\n"), r);
delete Fats[cpart]; delete Fats[cpart];
Fats[cpart] = NULL; Fats[cpart] = NULL;
@ -523,10 +509,9 @@ void loop() {
} }
delete PT; delete PT;
} else { } else {
sto[i].Read = NULL;
sto[i].Write = NULL;
sto[i].Writes = NULL; sto[i].Writes = NULL;
sto[i].Reads = NULL; sto[i].Reads = NULL;
sto[i].Initialize = NULL;
sto[i].TotalSectors = 0UL; sto[i].TotalSectors = 0UL;
sto[i].SectorSize = 0; sto[i].SectorSize = 0;
} }
@ -535,18 +520,18 @@ void loop() {
} }
} }
if (fatready) { if(fatready) {
if (Fats[0] != NULL) { if(Fats[0] != NULL) {
struct Pvt * p; struct Pvt * p;
p = ((struct Pvt *)(Fats[0]->storage->private_data)); p = ((struct Pvt *)(Fats[0]->storage->private_data));
if (!Bulk[p->B]->LUNIsGood(p->lun)) { if(!UHS_USB_BulkOnly[p->B]->LUNIsGood(p->lun)) {
// media change // media change
#if defined(AVR) #if !defined(CORE_TEENSY) && defined(__AVR__)
fadeAmount = 80; fadeAmount = 80;
#endif #endif
partsready = false; partsready = false;
for (int i = 0; i < cpart; i++) { for(int i = 0; i < cpart; i++) {
if (Fats[i] != NULL) if(Fats[i] != NULL)
delete Fats[i]; delete Fats[i];
Fats[cpart] = NULL; Fats[cpart] = NULL;
} }
@ -557,62 +542,64 @@ void loop() {
} }
} }
if (fatready) { if(fatready) {
FRESULT rc; /* Result code */ FRESULT rc; /* Result code */
UINT bw, br, i; UINT bw, br, i;
if(!notified) {
if (!notified) { #if !defined(CORE_TEENSY) && defined(__AVR__)
#if defined(AVR)
fadeAmount = 5; fadeAmount = 5;
#endif #endif
notified = true; notified = true;
FATFS *fs = NULL;
for(int zz = 0; zz < _VOLUMES; zz++) {
if(Fats[zz]->volmap == 0) fs = Fats[zz]->ffs;
}
printf_P(PSTR("\r\nOpen an existing file (message.txt).\r\n")); printf_P(PSTR("\r\nOpen an existing file (message.txt).\r\n"));
rc = f_open(&My_File_Object_x, "0:/MESSAGE.TXT", FA_READ); rc = f_open(&My_File_Object_x, "0:/MESSAGE.TXT", FA_READ);
if (rc) printf_P(PSTR("Error %i, message.txt not found.\r\n"), rc); if(rc) printf_P(PSTR("Error %i, message.txt not found.\r\n"), rc);
else { else {
printf_P(PSTR("\r\nType the file content.\r\n")); printf_P(PSTR("\r\nType the file content.\r\n"));
for (;;) { for(;;) {
rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &br); /* Read a chunk of file */ rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &br); /* Read a chunk of file */
if (rc || !br) break; /* Error or end of file */ if(rc || !br) break; /* Error or end of file */
for (i = 0; i < br; i++) { for(i = 0; i < br; i++) {
/* Type the data */ /* Type the data */
if (My_Buff_x[i] == '\n') if(My_Buff_x[i] == '\n')
Serial.write('\r'); Serial.write('\r');
if (My_Buff_x[i] != '\r') if(My_Buff_x[i] != '\r')
Serial.write(My_Buff_x[i]); Serial.write(My_Buff_x[i]);
Serial.flush(); Serial.flush();
} }
} }
if (rc) { if(rc) {
f_close(&My_File_Object_x); f_close(&My_File_Object_x);
goto out; goto out;
} }
printf_P(PSTR("\r\nClose the file.\r\n")); printf_P(PSTR("\r\nClose the file.\r\n"));
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) goto out; if(rc) goto out;
} }
printf_P(PSTR("\r\nCreate a new file (hello.txt).\r\n")); printf_P(PSTR("\r\nCreate a new file (hello.txt).\r\n"));
rc = f_open(&My_File_Object_x, "0:/Hello.TxT", FA_WRITE | FA_CREATE_ALWAYS); rc = f_open(&My_File_Object_x, "0:/Hello.TxT", FA_WRITE | FA_CREATE_ALWAYS);
if (rc) { if(rc) {
die(rc); die(rc);
goto outdir; goto outdir;
} }
printf_P(PSTR("\r\nWrite a text data. (Hello world!)\r\n")); printf_P(PSTR("\r\nWrite a text data. (Hello world!)\r\n"));
rc = f_write(&My_File_Object_x, "Hello world!\r\n", 14, &bw); rc = f_write(&My_File_Object_x, "Hello world!\r\n", 14, &bw);
if (rc) { if(rc) {
goto out; goto out;
} }
printf_P(PSTR("%u bytes written.\r\n"), bw); printf_P(PSTR("%u bytes written.\r\n"), bw);
printf_P(PSTR("\r\nClose the file.\r\n")); printf_P(PSTR("\r\nClose the file.\r\n"));
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) { if(rc) {
die(rc); die(rc);
goto out; goto out;
} }
outdir: outdir:{
{
#if _USE_LFN #if _USE_LFN
char lfn[_MAX_LFN + 1]; char lfn[_MAX_LFN + 1];
FILINFO My_File_Info_Object_x; /* File information object */ FILINFO My_File_Info_Object_x; /* File information object */
@ -621,55 +608,55 @@ outdir:
DIR My_Dir_Object_x; /* Directory object */ DIR My_Dir_Object_x; /* Directory object */
printf_P(PSTR("\r\nOpen root directory.\r\n")); printf_P(PSTR("\r\nOpen root directory.\r\n"));
rc = f_opendir(&My_Dir_Object_x, "0:/"); rc = f_opendir(&My_Dir_Object_x, "0:/");
if (rc) { if(rc) {
die(rc); die(rc);
goto out; goto out;
} }
printf_P(PSTR("\r\nDirectory listing...\r\n")); printf_P(PSTR("\r\nDirectory listing...\r\n"));
#if defined(AVR) #if defined(__AVR__)
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
#endif #endif
for (;;) { for(;;) {
#if _USE_LFN #if _USE_LFN
My_File_Info_Object_x.lfsize = _MAX_LFN; My_File_Info_Object_x.lfsize = _MAX_LFN;
#endif #endif
rc = f_readdir(&My_Dir_Object_x, &My_File_Info_Object_x); /* Read a directory item */ rc = f_readdir(&My_Dir_Object_x, &My_File_Info_Object_x); /* Read a directory item */
if (rc || !My_File_Info_Object_x.fname[0]) break; /* Error or end of dir */ if(rc || !My_File_Info_Object_x.fname[0]) break; /* Error or end of dir */
if (My_File_Info_Object_x.fattrib & AM_DIR) { if(My_File_Info_Object_x.fattrib & AM_DIR) {
Serial.write('d'); Serial.write('d');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
Serial.write('r'); Serial.write('r');
if (My_File_Info_Object_x.fattrib & AM_RDO) { if(My_File_Info_Object_x.fattrib & AM_RDO) {
Serial.write('-'); Serial.write('-');
} else { } else {
Serial.write('w'); Serial.write('w');
} }
if (My_File_Info_Object_x.fattrib & AM_HID) { if(My_File_Info_Object_x.fattrib & AM_HID) {
Serial.write('h'); Serial.write('h');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
if (My_File_Info_Object_x.fattrib & AM_SYS) { if(My_File_Info_Object_x.fattrib & AM_SYS) {
Serial.write('s'); Serial.write('s');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
if (My_File_Info_Object_x.fattrib & AM_ARC) { if(My_File_Info_Object_x.fattrib & AM_ARC) {
Serial.write('a'); Serial.write('a');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
#if _USE_LFN #if _USE_LFN
if (*My_File_Info_Object_x.lfname) if(*My_File_Info_Object_x.lfname)
printf_P(PSTR(" %8lu %s (%s)\r\n"), My_File_Info_Object_x.fsize, My_File_Info_Object_x.fname, My_File_Info_Object_x.lfname); printf_P(PSTR(" %8lu %s (%s)\r\n"), My_File_Info_Object_x.fsize, My_File_Info_Object_x.fname, My_File_Info_Object_x.lfname);
else else
#endif #endif
@ -677,48 +664,55 @@ outdir:
} }
} }
out: out:
if (rc) die(rc); if(rc) die(rc);
DISK_IOCTL(fs->drv, CTRL_COMMIT, 0);
printf_P(PSTR("\r\nTest completed.\r\n")); printf_P(PSTR("\r\nTest completed.\r\n"));
} }
if (runtest) { if(runtest) {
ULONG ii, wt, rt, start, end; ULONG ii, wt, rt, start, end;
FATFS *fs = NULL;
for(int zz = 0; zz < _VOLUMES; zz++) {
if(Fats[zz]->volmap == 0) fs = Fats[zz]->ffs;
}
runtest = false; runtest = false;
f_unlink("0:/10MB.bin"); f_unlink("0:/10MB.bin");
printf_P(PSTR("\r\nCreate a new 10MB test file (10MB.bin).\r\n")); printf_P(PSTR("\r\nCreate a new 10MB test file (10MB.bin).\r\n"));
rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_WRITE | FA_CREATE_ALWAYS); rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_WRITE | FA_CREATE_ALWAYS);
if (rc) goto failed; if(rc) goto failed;
for (bw = 0; bw < mbxs; bw++) My_Buff_x[bw] = bw & 0xff; for(bw = 0; bw < mbxs; bw++) My_Buff_x[bw] = bw & 0xff;
fflush(stdout); fflush(stdout);
start = millis(); start = millis();
while (start == millis()); while(start == millis());
for (ii = 10485760LU / mbxs; ii > 0LU; ii--) { for(ii = 10485760LU / mbxs; ii > 0LU; ii--) {
rc = f_write(&My_File_Object_x, My_Buff_x, mbxs, &bw); rc = f_write(&My_File_Object_x, My_Buff_x, mbxs, &bw);
if (rc || !bw) goto failed; if(rc || !bw) goto failed;
} }
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) goto failed; if(rc) goto failed;
end = millis(); end = millis();
wt = (end - start) - 1; wt = (end - start) - 1;
printf_P(PSTR("Time to write 10485760 bytes: %lu ms (%lu sec) \r\n"), wt, (500 + wt) / 1000UL); printf_P(PSTR("Time to write 10485760 bytes: %lu ms (%lu sec) \r\n"), wt, (500 + wt) / 1000UL);
rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_READ); rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_READ);
fflush(stdout); fflush(stdout);
start = millis(); start = millis();
while (start == millis()); while(start == millis());
if (rc) goto failed; if(rc) goto failed;
for (;;) { for(;;) {
rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &bw); /* Read a chunk of file */ rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &bw); /* Read a chunk of file */
if (rc || !bw) break; /* Error or end of file */ if(rc || !bw) break; /* Error or end of file */
} }
end = millis(); end = millis();
if (rc) goto failed; if(rc) goto failed;
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) goto failed; if(rc) goto failed;
rt = (end - start) - 1; rt = (end - start) - 1;
printf_P(PSTR("Time to read 10485760 bytes: %lu ms (%lu sec)\r\nDelete test file\r\n"), rt, (500 + rt) / 1000UL); printf_P(PSTR("Time to read 10485760 bytes: %lu ms (%lu sec)\r\nDelete test file\r\n"), rt, (500 + rt) / 1000UL);
failed: failed:
if (rc) die(rc); if(rc) die(rc);
DISK_IOCTL(fs->drv, CTRL_COMMIT, 0);
printf_P(PSTR("10MB timing test finished.\r\n")); printf_P(PSTR("10MB timing test finished.\r\n"));
} }
} }

@ -1 +1 @@
Subproject commit dd85091abaca7cc6055ff515a5e42f32198380d2 Subproject commit 2bf8f633e7f9bc5a7bf4c00f3f45c7b79484198e