Read/Write SD Card Sectors directly

I want to use CPM files on an SD card. So I need to R/W SD card sectors directly, (not through the MSDOS file system etc).
There was an old module to do this back in 2011 with the Arduino IDE at that time.
(Sd card read/write sector library)

It does not seem to work with the current IDE versions.
Does anybody have function to R/W SD card sectors today.
BTW, I assume there must be one somewhere deep in the current Arduino libaries. Anybody know how to get to it.

What does that mean?

Compile errors? If so, replace #include <WProgram.h> in both library files by #include <Arduino.h>.

I have at the start of the Arduino IDE:-

#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

include <Arduino.h>
#include "WProgram.h"
#include "Wiring.h"
#include "SDCARD.h"

I get the following error:-
'fatal error: avr/io.h: No such file or directory
29 | #include <avr/io.h> '

I cannot locate the source for the correct io.h.

I actually not sure I'm on the right track anyway.

Does anybody know how to locate/use the R/W sector directly in the Arduino librarie(s) instead of a total file (R/W)?

The SdFat.h library includes an example called LowLatencyLogger which pre-defines the files at the beginning, but then to do the logging it just writes directly to the card sector. In that example the writes are to consecutive sectors which have already been erased.

Perhaps you could review the code for that example and see how he does that. There is another example called RawWrite that may be easier to deal with.

But remember that you cannot write to a sector that has not been erased. And you can't erase just one sector. So overwriting an existing sector in the middle of the file could be very complicated.

Were they written as 512 byte records? If so, then read the same way and you will read sectors.

Thanks ShermanP.
Looked at LowLatencyLogger code. Its way too complicated for me.
I would be better off using the traditional MSDOS file R/W, Then within the file R/W "CPM Sectors" etc.

I'm hoping for a single Arduino function ReadSec() and WriteSec().
It must exist but i cannot dig it out myself.

What is the sector size when the card was formatted?

Standard 512 bytes/sector.

I did see an attempt at code tags. Code tags are three back ticks (```) before and after the code block and they must be on their own line. Fixed it for you.

Which board are you compiling for?

ESP32 supports raw sectors

  bool readRAW(uint8_t *buffer, uint32_t sector);
  bool writeRAW(uint8_t *buffer, uint32_t sector);

A quick search says that CP/M's common 1.4 disk format was

  • 250 KB
  • 128-byte sectors
  • organized into 1 KB data blocks; 240 of those

So is the disk written linearly onto the front of an SD card? Lots of leftover space on a 1 GB SD. A bunch of those stacked one after another? Written as 250 KB disk-image files in the normal SD filesystem?

SD cards are not magnetic drives. They have their own controller built in that does wear leveling. What you have is a magnetic drive like interface but where sectors are written on the card is up to the card.

You can make a large file that you can treat as a drive and offset into that.

SD is different than magnetic drives. You should not overwrite what has been written to for example sort a file. You should instead write another file with sorted indexes into the original file and use those.

OTOH you could interface a floppy or hard drive to Arduino.

Thanks so much for that info kenb4. You are the first one to help here.
However I have tried to compile a small program with the following:-
'void loop() {
uint8_t buffer[1024];
uint32_t sector = 123;
bool readRAW(buffer, sector);
...
}
but I get an error in the Arduino IDE
' error: expression list treated as compound expression in initializer [-fpermissive]
223 | bool readRAW(buffer, sector);'

is it possible to use the above function in the IDE. If so what is wrong?

As to CPM sector size.
I can use two approaches:-
Use the first 128 bytes of each sector - wast the rest. CPM has only a 10MB capacity!
In CPM3 you can have sectors multiples of 128 bytes in the BIOS

John

GoForSmoke,
This issue with SD cards has come up a lot and is wrong.
Within MSDOS for example the BIOS just defines a "requested" sector for R/W.
It has no knowledge of where the SD card itself actually puts the sector. All the ware issues etc are contained within the SD card hardware. This is because MSDOS (and other software) works with old Floppy disks, hard disks, SSD drives and CF and SD cards. I actually have SD cards with CPM running with an FPGA Z80 emulator that R/W sectors without any problem.
See:- SD cards with FPGA Z80

John

I don't know if a typo slipped into the code in #3 but be careful,

include <Arduino.h>

must be

#include <Arduino.h>

emulator....

i was writing track and sector software to use floppies in the early 80's when SD was not yet thought of. When I wrote CB80 and ASM on 8085's I was running CP/M not an emulator.
For most of the 80's I ran DOS but I gave my references away before 2010. I knew track and sector where I read and wrote data, you can't do that with SD.

You have fun with SD.

Yes it was a typo here, I had #include <Arduino.h> in the code.

One last time GoForSmoke,
Yes you can read write SD sectors directly,
Did you read my above post. MSDOS does not know/care where the actual sectors actually are. The SD card reallocate each sector internally totally transparent to the outside hardware.

Kenb4 I now see that the two raw sector R/W routines are in code for the Espressif Systems (Shanghai) PTE LTD IDE. For other reasons I have to use the Arduino IDE.
Have you (or anybody else), see the equivalent functions used in that IDE.

Then you CAN'T write sectors DIRECTLY. You write to where they are mapped and like your claim you have no clue where that is.

When I wrote CPM, I knew where on the platter my data went. Mapping bad sectors was part of the job I was doing likely before you were born.

The two raw sector routines are public in the SD library for arduino-esp32 core, so they should be accessible from the Arduino IDE