This should be the correct interpretation.Thought FILE_WRTIE_BEGIN was meant to write data from the beginning of the file while FILE_WRITE would append
Are there examples where this does not happen?
This should be the correct interpretation.Thought FILE_WRTIE_BEGIN was meant to write data from the beginning of the file while FILE_WRITE would append
#define FILE_READ 0
#define FILE_WRITE 1
#define FILE_WRITE_BEGIN 2
oflag_t flags = O_READ;
if (mode == FILE_WRITE) flags = O_RDWR | O_CREAT | O_AT_END;
else if (mode == FILE_WRITE_BEGIN) flags = O_RDWR | O_CREAT;
#define O_RDONLY 0X00 ///< Open for reading only.
#define O_WRONLY 0X01 ///< Open for writing only.
#define O_RDWR 0X02 ///< Open for reading and writing.
#define O_AT_END 0X04 ///< Open at EOF.
#define O_APPEND 0X08 ///< Set append mode.
#define O_CREAT 0x10 ///< Create file if it does not exist.
#define O_TRUNC 0x20 ///< Truncate file to zero length.
#define O_EXCL 0x40 ///< Fail if the file exists.
#define O_SYNC 0x80 ///< Synchronized write I/O operations.
w+
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
myFile.truncate(myFile.position());
@mjs513 - Think I figured out what the trouble was. MY FAULT!! I was compiling ReadWrite.ino In Arduino 1.8.19 but was editing ReadWrite.ino in Arduino 1.8.18. You were right. @All - Thanks for the input. I know see that if FILE_WRITE does append to the end of the current file. FILE_WRITE_BEGIN always starts at the beginning of the file. So if the same file is reused with FILE_WRITE_BEGIN the write will start at position 0. What was confusing me was say the appended file size was 144 bytes and I opened it again with FILE_WRITE_BEGIN and wrote 72 bytes then closed the file it would read 144 bytes. So in theory I would need to truncate the file at 72 bytes then close the file. Or delete the file before opening it again. Truncating the file worked but it will have to keep track of how many bytes were written to the file before truncating and closing. Possibly use myFile.position() as an argument to truncate.
Thanks everybody for the help
Wrong src edit is a pain ...
That seems a good and usable feature/process if behavior is known.
> User can read prior data and write beyond it - or seek, truncate as needed with a single open()
fcbs[file & 0xff].truncate(fcbs[file & 0xff].position());
AdestoAtmel
W25Q64
W25Q256JW*M (new chip which I ordered by mistake)
W25Q256JV*Q
W25Q01
[B]Adesto/Atmel AT25SF041 (512K): [/B]
Writing 524288 byte file... 1321 ms, bandwidth = 396887 bytes/sec
Writing 524288 byte file... 8293 ms, bandwidth = 63220 bytes/sec
Writing 524288 byte file... 8294 ms, bandwidth = 63212 bytes/sec
Writing 524288 byte file... 8311 ms, bandwidth = 63083 bytes/sec
Writing 524288 byte file... 8305 ms, bandwidth = 63129 bytes/sec
Writing 524288 byte file... 8309 ms, bandwidth = 63098 bytes/sec
Writing 524288 byte file... 8311 ms, bandwidth = 63083 bytes/sec
Writing 524288 byte file... 8298 ms, bandwidth = 63182 bytes/sec
Writing 524288 byte file... 8285 ms, bandwidth = 63281 bytes/sec
Writing 524288 byte file... 8295 ms, bandwidth = 63205 bytes/sec
Bandwidth test finished
Start Big write of 217088 Bytes
Big write /0_bigfile.txt took 3.48 Sec for 217088 Bytes : file3.size()=215040
Big write KBytes per second 62.45
Bytes Used: 225280, Bytes Total:524288
[B]Winbond W25Q64JV*Q/W25Q64FV
[/B]Writing 524288 byte file... 1272 ms, bandwidth = 412176 bytes/sec
Writing 524288 byte file... 1271 ms, bandwidth = 412500 bytes/sec
Writing 524288 byte file... 1271 ms, bandwidth = 412500 bytes/sec
Writing 524288 byte file... 1735 ms, bandwidth = 302183 bytes/sec
Writing 524288 byte file... 1273 ms, bandwidth = 411852 bytes/sec
Writing 524288 byte file... 1272 ms, bandwidth = 412176 bytes/sec
Writing 524288 byte file... 1273 ms, bandwidth = 411852 bytes/sec
Writing 524288 byte file... 1273 ms, bandwidth = 411852 bytes/sec
Writing 524288 byte file... 1273 ms, bandwidth = 411852 bytes/sec
Writing 524288 byte file... 1273 ms, bandwidth = 411852 bytes/sec
Bandwidth test finished
Start Big write of 4087808 Bytes
Big write /0_bigfile.txt took 14.10 Sec for 4087808 Bytes : file3.size()=4085760
Big write KBytes per second 289.96
Bytes Used: 4259840, Bytes Total:8388608
[B]Winbond (W25Q256JW*M)
[/B]Writing 524288 byte file... 1604 ms, bandwidth = 326862 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Writing 524288 byte file... 1603 ms, bandwidth = 327066 bytes/sec
Bandwidth test finished
Start Big write of 25124864 Bytes
Big write /0_bigfile.txt took 77.38 Sec for 25124864 Bytes : file3.size()=25122816
Big write KBytes per second 324.72
Bytes Used: 27394048, Bytes Total:33554432
[B] Winbond W25Q512JV*Q
[/B] Writing 524288 byte file... 1340 ms, bandwidth = 391259 bytes/sec
Writing 524288 byte file... 1338 ms, bandwidth = 391844 bytes/sec
Writing 524288 byte file... 1339 ms, bandwidth = 391551 bytes/sec
Writing 524288 byte file... 1338 ms, bandwidth = 391844 bytes/sec
Writing 524288 byte file... 1338 ms, bandwidth = 391844 bytes/sec
Writing 524288 byte file... 1338 ms, bandwidth = 391844 bytes/sec
Writing 524288 byte file... 1339 ms, bandwidth = 391551 bytes/sec
Writing 524288 byte file... 1338 ms, bandwidth = 391844 bytes/sec
Writing 524288 byte file... 1339 ms, bandwidth = 391551 bytes/sec
Writing 524288 byte file... 1338 ms, bandwidth = 391844 bytes/sec
Bandwidth test finished
Start Big write of 25124864 Bytes............................................................
Big write /0_bigfile.txt took 63.06 Sec for 25124864 Bytes : file3.size()=25122816
Big write KBytes per second 398.45
Bytes Used: 25296896, Bytes Total:33554432
[B] Winbond W25Q01JV*Q
[/B]Writing 524288 byte file... 1586 ms, bandwidth = 330572 bytes/sec
Writing 524288 byte file... 1589 ms, bandwidth = 329948 bytes/sec
Writing 524288 byte file... 1586 ms, bandwidth = 330572 bytes/sec
Writing 524288 byte file... 1584 ms, bandwidth = 330989 bytes/sec
Writing 524288 byte file... 1583 ms, bandwidth = 331198 bytes/sec
Writing 524288 byte file... 1581 ms, bandwidth = 331617 bytes/sec
Writing 524288 byte file... 1590 ms, bandwidth = 329740 bytes/sec
Writing 524288 byte file... 1588 ms, bandwidth = 330156 bytes/sec
Writing 524288 byte file... 1587 ms, bandwidth = 330364 bytes/sec
Writing 524288 byte file... 1582 ms, bandwidth = 331408 bytes/sec
Bandwidth test finished
Start Big write of 25124864 Bytes............................................................
Big write /0_bigfile.txt took 111.41 Sec for 25124864 Bytes : file3.size()=25122816
Big write KBytes per second 225.51
Bytes Used: 25296896, Bytes Total:134217728
Let's just use usb_mtp_status directly for now. Don't be shy to just check if it's 0x19 and write it to 0x01. We can always wrap stuff with abstractions later if there's really a need.
I'm going to look at the ZLP stuff next. Indeed BogusFS is really useful for testing. I was using it all last night for the host cancel testing.
Can you give me a quick update about the state of MTP_Teensy? I see you have 2 branches "debug" and "thowing_darts" with commits ahead of the main branch.
This is a combine of multiple commits.
Move/copy check for 0 passed in which says use root of destination storage
Uses some of the functions used of SendObject to create the new destination.
This helps when for example the destination directory already exists.
Currently this code will try to overwrite the contents of any of the files
that already existed in the destination with the contents of the source
files
It will fail if destination exists and not of same type. That is suppose the
source directory contains a file named temp and the destination already has
a directory named temp.
The changes also tries to reduce how much stack space is used to complete
the operations. The previous code for example the top level operation may
have something like 6 Record objects and if it calls of to the move/copy
directory code each recursion may add similar number of these structures
plus a source and destination pathname object.
Also there is a start for detecting some errors like we ran out of disk
space and the writes fail. Also work to propagate these errors back
up the call chain.
Fixed CopyObjects for T3.x, we were not properly returning the newly
created object handle and the host would error out.
Cleaned up debug output in storage code, put in macro DBGPrintf that you did not have to put #if around...
debug has now been merged into main...Yes, please merge to the main branch, and then let's do everything on the main branch.
I would like to start really working on the ZLP handling today, but will need to mess with both core library and MTP_Teensy.
>>w dp:2548 p:166388 len:512
push_packet: l:512 ret:0 loop:0
push_packet: l:512 ret:0 loop:1
push_packet: l:512 ret:0 loop:2
push_packet: l:512 ret:0 loop:3
push_packet: l:512 ret:0 loop:4
>>w dp:3060 p:166900 len:512
push_packet: l:512 ret:0 loop:0
push_packet: l:512 ret:0 loop:1
push_packet: l:512 ret:0 loop:2
push_packet: l:512 ret:0 loop:3
push_packet: l:512 ret:0 loop:4
>>w dp:3572 p:167412 len:512
...
I have been playing with file and directory names with SdFat. Some names containing certain characters cause the directory listing to to just keep scrolling endlessly. Working on what is causing this. Not sure yet. I ran into this several times now.
#include <SD.h>
#include <MTP_Teensy.h>
#define CS_SD BUILTIN_SDCARD // Works on T_3.6 and T_4.1
// #define CS_SD 10 // Works on SPI with this CS pin
MTPStorage storage; // TODO: storage inside MTP instance, not separate class
MTPD MTP(&storage); // TODO: MTP instance created by default
void setup()
{
Serial.begin(9600);
while (!Serial && millis() < 5000) {
// wait for serial port to connect.
}
Serial.print(CrashReport);
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
delay(1000);
// mandatory to begin the MTP session.
MTP.begin();
// Add SD Card
if (SD.begin(CS_SD)) {
storage.addFilesystem(SD, "SD Card");
Serial.println("Added SD card using built in SDIO, or given SPI CS");
} else {
Serial.println("No SD Card");
}
Serial.println("\nSetup done");
}
void loop() {
MTP.loop(); //This is mandatory to be placed in the loop code.
}