Use SPI Flash Chip as Sd Card

hi Guys

i have seen a project based on SD Card but in my usage it is risky as just you can open and steal and go through it

so i saw this SPI Flash Chip which has smaller amount of storage like mb so i choose to go for it but i wanna use same function of an sd card on SPI Flash Chip as well but have no luck

like reading writing files erasing and everything

please help me in this by providing a library or some piece of code

SD card has a controller that makes it act as a drive.

Flash card does not, it's far more primitive. You have to code where your data is for both reads and writes.

GoForSmoke:
SD card has a controller that makes it act as a drive.

Flash card does not, it's far more primitive. You have to code where your data is for both reads and writes.

but which is the right library to use

i used serialFlash library and cant understand this part SerialFlash Library

unsigned int n;
          n = f.read(buf, 256);
          ff.write(buf, n);

does the write function erase everything and write to file or just add to the existing file

and what is this buf 256 is it the size of file or flash chip or something else

I haven't messed with flash chips but they do have to be written a sector at a time. First all the bits are set and then the sector 0's are set as the new data bytes are written.

You need to get the datasheet for the flash chip you bought to be sure.

You could also consider obfuscating or encrypting the data written to the SD card. At the most basic level, you could just XOR each byte with a constant byte. That would make the data not easily readable, but it wouldn't take much to break it. A more formal encryption method might be TEA or XTEA, for which there are probably Arduino libraries.

You can solder an sd card and hardwire to an Esp8266 or other 3.3 voltage chip.
Then glue it to the board.

Esp8266 also has more memory.

Yah, to get all those PCDOS file commands free you can pot (seal in epoxy) a wired and tested microSD with heatsink (metal washer or actual w/fins little heatsink) on one face to prevent heat buildup.

I once bought some custom shape SD's and they died of heat when copying big files and likely because of voltage-divider leveling 5V to 3V heat was insulated by the deco body with no heatsink.

But writing to serial flash might be easier and faster. You get the hard parts straight once in functions that make it easier to read/write. For example, the functions (or C++ class for better packaged code) would operate on a buffer and automatically write a 128 byte page to the flash when the sketch fills the buffer.

You can address the whole storage space of the flash using unsigned longs and it's all made of 128 byte pages.

Your serial flash has a limited number of re-writes compared to EEPROM.

ShermanP:
You could also consider obfuscating or encrypting the data written to the SD card

i already used fingerprint in my project so i cant add another level of code as lack of space

dave-in-nj:
You can solder an sd card and hardwire to an Esp8266 or other 3.3 voltage chip.
Then glue it to the board.

Esp8266 also has more memory.

i would like it to function as a keyboard also so only atmega32u4

GoForSmoke:
Yah, to get all those PCDOS file commands free you can pot (seal in epoxy) a wired and tested microSD with heatsink (metal washer or actual w/fins little heatsink) on one face to prevent heat buildup.

I once bought some custom shape SD's and they died of heat when copying big files and likely because of voltage-divider leveling 5V to 3V heat was insulated by the deco body with no heatsink.

i have same problem the chip already soldered on pcb and has pads to program it but what do i do of 3.3v devices attached to vcc

can i turn off usb communication of atmega32u4 by disconnecting the UVCC or VBUS Pins so it does not interupt communication with another usb device on same port

You have to put a circuit or chip between 5V and SD.

The 74HC4050 hex buffer will convert 5V to 3V on 6 channels, buy a few and they're < 50 cents each.

You can use a diode to block the 5V coming through, when that wire is LOW it will drain voltage from the 3.3V side of the diode. You give the 3.3V side 3.3V through a 10-20K resistor to pull the SD pin up when the 5V side is HIGH and keeping the 3.3V pullup voltage from draining away.
You would need 1 for each line the 5V transmits on (MOSI,SCLK,SEL) and none for lines the SD talks back on (MISO).

The chip is a little cleaner and can level 2 SPI devices but 3 diodes and 3 resistors are a lot cheaper and you may already have those.

GoForSmoke:
You have to put a circuit or chip between 5V and SD.

The 74HC4050 hex buffer will convert 5V to 3V on 6 channels, buy a few and they're < 50 cents each.

You can use a diode to block the 5V coming through, when that wire is LOW it will drain voltage from the 3.3V side of the diode. You give the 3.3V side 3.3V through a 10-20K resistor to pull the SD pin up when the 5V side is HIGH and keeping the 3.3V pullup voltage from draining away.
You would need 1 for each line the 5V transmits on (MOSI,SCLK,SEL) and none for lines the SD talks back on (MISO).

The chip is a little cleaner and can level 2 SPI devices but 3 diodes and 3 resistors are a lot cheaper and you may already have those.

i will try this instead of using these i will before hand program the chip on board and later solder remaining 3.3v device

can i power off usb communication from pc by having a relay on VBUS and 5V+ and have it turn on when necessary

I am using on ATMega32U4 and not any other ATMega Chips as i need to use few keyboard commands

A relay? Really? Switching that takes more current than the chip would use in a long time.

For DC, transistors are far more efficient, only electrons move. Read up on MOSFETs. MOS is the type of semiconductor, FE is for field-effect and T is for transistor.

The older BJ-Transistors (BJTs) had to have a little current on the control pin to get more current to flow through the other 2 pins, they heat up which limits their use for power switching. OTOH a BJT can be used as an electronic valve/volume control.

Arduinos have an in-system chip program header capable of loading hex files to your chip.

The Teensy 2.0 and the Arduino Micro (may be out of production) both have that USB chip.

GoForSmoke:
A relay? Really? Switching that takes more current than the chip would use in a long time.

For DC, transistors are far more efficient, only electrons move. Read up on MOSFETs. MOS is the type of semiconductor, FE is for field-effect and T is for transistor.

i am actually using a MOSFET IRF5305PBF to Control it along with 10K and 100 ohm Resistors so i am interupting the communication between pc and MCU

GoForSmoke:
Arduinos have an in-system chip program header capable of loading hex files to your chip.

The Teensy 2.0 and the Arduino Micro (may be out of production) both have that USB chip.

But i first need to burn the bootloader for which i need to use SPI Connection then program the chip and use it as a keyboard device is it ok

Arduino can run a PS2 keyboard directly.

GoForSmoke:
Arduino can run a PS2 keyboard directly.

sorry for late reply

i will be using a usb device connected on arduino board turned on and off by MCU & Mosfet and when on the MCU PC communication needs to be cut as it should not interfere in the signal of other causing it to breakdown

and i surely need the keyboard

or else can we read and write data to flash drive of random

question : can you use one Arduino, with the bootloader, to program a separate chip that does not have the bootloader ?
thereby making the space for the bootloader available ?
I know that you can get a multitude of other chips with lots more memory.
Just wondering if you can get a larger program loaded that way.

dave-in-nj:
question : can you use one Arduino, with the bootloader, to program a separate chip that does not have the bootloader ?
thereby making the space for the bootloader available ?
I know that you can get a multitude of other chips with lots more memory.
Just wondering if you can get a larger program loaded that way.

yes i have done that many times like using a arduino as isp to program the attiny85 bootloader digispark

i program my chip using avrdude or arduino

dave-in-nj:
question : can you use one Arduino, with the bootloader, to program a separate chip that does not have the bootloader ?
thereby making the space for the bootloader available ?
I know that you can get a multitude of other chips with lots more memory.
Just wondering if you can get a larger program loaded that way.

With a PC running the IDE you can, or with Crossroads & Nick Gammon's collaboration stand-alone hex file loader.

You don't have to load a bootloader, they just make it easier later, but fuses may need to be set and steps taken that are packaged up with bootloading. There are even bootloaders that let you write to flash in runtime.

The 1284P-PU was down to $5.35 for 1 at Mouser 2 days ago. It's the biggest breadboard-friendly AVR with 32 IO pins and 16K RAM, 4K EEPROM, 128K flash and 2 UARTs.

The flash is hardware addressed as 64K 16-bit words, 16-bit address gets them all and it erases to overwrite a 64 word page at a time so you change 64 bytes to change any.

This is mostly roll-your-own and does include tiny85

The Crossroads stand-alone is titled "Stand-alone programmer pre-assembled"
http://www.crossroadsfencing.com/BobuinoRev17/Programmer.html

But the OP is looking at 1+ M flash, not 128K, and I assume not looking at ARM boards like Teensy 4.0
https://www.pjrc.com/store/teensy40.html

hi, for such sd-like use of Flash chips Adafruit seams to have the most evolved library. Go through this Tutorial and skip all python-related stuff. This should give a good impression of the look and feel: Using SPI Flash | Adafruit Feather M0 Express | Adafruit Learning System

This version 3.x of the Spi flash library is very restrictive when it comes to supported SPI chips. The S25FL116K from Cypress should work (not sure). The former 2.x version was supporting more types. I successfully tested up to 64MBit(8MB) Flash chips from Winbond.
Maybe you select a late 2.x version in the library manager.
Regards Marco

Bastler:
hi, for such sd-like use of Flash chips Adafruit seams to have the most evolved library. Go through this Tutorial and skip all python-related stuff. This should give a good impression of the look and feel: Using SPI Flash | Adafruit Feather M0 Express | Adafruit Learning System

This version 3.x of the Spi flash library is very restrictive when it comes to supported SPI chips. The S25FL116K from Cypress should work (not sure). The former 2.x version was supporting more types. I successfully tested up to 64MBit(8MB) Flash chips from Winbond.
Maybe you select a late 2.x version in the library manager.
Regards Marco

i already checked that out and does not compile for atmega32u4 it is just for there board and wasted two days to realize that it does not work you simply keep getting flash transport error so i would not recommend it for this project