-
-
Notifications
You must be signed in to change notification settings - Fork 1k
[feature] added flash support analogous to esp32c3 to esp32s3 #5397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wikwoj0512
wants to merge
1
commit into
tinygo-org:dev
Choose a base branch
from
Wikwoj0512:esp32s3-flash
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,390
−315
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| //go:build esp32s3 | ||
|
|
||
| package machine | ||
|
|
||
| import ( | ||
| "runtime/interrupt" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| /* | ||
| #include <stdint.h> | ||
| extern int esp_rom_spiflash_read(uint32_t src_addr, uint32_t *data, uint32_t len); | ||
| extern int esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *data, uint32_t len); | ||
| extern int esp_rom_spiflash_erase_sector(uint32_t sector_num); | ||
| extern int esp_rom_spiflash_unlock(void); | ||
| extern void Cache_Invalidate_Addr(uint32_t addr, uint32_t size); | ||
| */ | ||
| import "C" | ||
|
|
||
| // compile-time check for ensuring we fulfill BlockDevice interface | ||
| var _ BlockDevice = flashBlockDevice{} | ||
|
|
||
| var Flash flashBlockDevice | ||
|
|
||
| type flashBlockDevice struct { | ||
| } | ||
|
|
||
| // ReadAt reads the given number of bytes from the block device. | ||
| func (f flashBlockDevice) ReadAt(p []byte, off int64) (n int, err error) { | ||
| if readAddress(off)+uintptr(len(p)) > FlashDataEnd() { | ||
| return 0, errFlashCannotReadPastEOF | ||
| } | ||
|
|
||
| data := unsafe.Slice((*byte)(unsafe.Add(unsafe.Pointer(FlashDataStart()), off)), len(p)) | ||
| copy(p, data) | ||
|
|
||
| return len(p), nil | ||
| } | ||
|
|
||
| // WriteAt writes the given number of bytes to the block device. | ||
| // Only word (32 bits) length data can be programmed. | ||
| // If the length of p is not long enough it will be padded with 0xFF bytes. | ||
| // This method assumes that the destination is already erased. | ||
| func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) { | ||
| return f.writeAt(p, off) | ||
| } | ||
|
|
||
| // Size returns the number of bytes in this block device. | ||
| func (f flashBlockDevice) Size() int64 { | ||
| return int64(FlashDataEnd() - FlashDataStart()) | ||
| } | ||
|
|
||
| const writeBlockSize = 4 | ||
|
|
||
| // WriteBlockSize returns the block size in which data can be written to | ||
| // memory. It can be used by a client to optimize writes, non-aligned writes | ||
| // should always work correctly. | ||
| func (f flashBlockDevice) WriteBlockSize() int64 { | ||
| return writeBlockSize | ||
| } | ||
|
|
||
| const eraseBlockSizeValue = 1 << 12 | ||
|
|
||
| func eraseBlockSize() int64 { | ||
| return eraseBlockSizeValue | ||
| } | ||
|
|
||
| // EraseBlockSize returns the smallest erasable area on this particular chip | ||
| // in bytes. This is used for the block size in EraseBlocks. | ||
| func (f flashBlockDevice) EraseBlockSize() int64 { | ||
| return eraseBlockSize() | ||
| } | ||
|
|
||
| // EraseBlocks erases the given number of blocks. An implementation may | ||
| // transparently coalesce ranges of blocks into larger bundles if the chip | ||
| // supports this. The start and len parameters are in block numbers, use | ||
| // EraseBlockSize to map addresses to blocks. | ||
| func (f flashBlockDevice) EraseBlocks(start, length int64) error { | ||
| return f.eraseBlocks(start, length) | ||
| } | ||
|
|
||
| // return the correct address to be used for reads | ||
| func readAddress(off int64) uintptr { | ||
| return FlashDataStart() + uintptr(off) | ||
| } | ||
|
|
||
| const flashDROMStart = 0x3C000000 | ||
|
|
||
| // return the correct physical address to be used for write/erase | ||
| func writeAddress(off int64) uint32 { | ||
| // DROM maps 1:1 with flash physical offset, starting at 0x3C000000. | ||
| return uint32(readAddress(off) - flashDROMStart) | ||
| } | ||
|
|
||
| func (f flashBlockDevice) writeAt(p []byte, off int64) (n int, err error) { | ||
| if readAddress(off)+uintptr(len(p)) > FlashDataEnd() { | ||
| return 0, errFlashCannotWritePastEOF | ||
| } | ||
|
|
||
| address := writeAddress(off) | ||
| padded := flashPad(p, int(f.WriteBlockSize())) | ||
|
|
||
| state := interrupt.Disable() | ||
| defer interrupt.Restore(state) | ||
|
|
||
| C.esp_rom_spiflash_unlock() | ||
| res := C.esp_rom_spiflash_write(C.uint32_t(address), (*C.uint32_t)(unsafe.Pointer(&padded[0])), C.uint32_t(len(padded))) | ||
| C.Cache_Invalidate_Addr(C.uint32_t(readAddress(off)), C.uint32_t(len(padded))) | ||
| if res != 0 { | ||
| return 0, errFlashCannotWriteData | ||
| } | ||
|
|
||
| return len(padded), nil | ||
| } | ||
|
|
||
| func (f flashBlockDevice) eraseBlocks(start, length int64) error { | ||
| address := writeAddress(start * f.EraseBlockSize()) | ||
| if uintptr(unsafe.Add(unsafe.Pointer(uintptr(address)+flashDROMStart), length*f.EraseBlockSize())) > FlashDataEnd() { | ||
| return errFlashCannotErasePastEOF | ||
| } | ||
|
|
||
| state := interrupt.Disable() | ||
| defer interrupt.Restore(state) | ||
|
|
||
| C.esp_rom_spiflash_unlock() | ||
| sector := address / uint32(f.EraseBlockSize()) | ||
|
|
||
| for i := int64(0); i < length; i++ { | ||
| res := C.esp_rom_spiflash_erase_sector(C.uint32_t(sector + uint32(i))) | ||
| C.Cache_Invalidate_Addr(C.uint32_t(readAddress((start+i)*f.EraseBlockSize())), C.uint32_t(f.EraseBlockSize())) | ||
| if res != 0 { | ||
| return errFlashCannotErasePage | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check whether this is also true for the ESP32S3? I couldn't easily find it in the SDK: https://docs.espressif.com/projects/esp-idf/en/v6.0.1/esp32s3/api-reference/peripherals/spi_flash/index.html