|
| 1 | +//go:build nrf || nrf51 || nrf52 || nrf528xx || stm32f4 || stm32l4 || stm32wlx |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "unsafe" |
| 9 | +) |
| 10 | + |
| 11 | +//go:extern __flash_data_start |
| 12 | +var flashDataStart [0]byte |
| 13 | + |
| 14 | +//go:extern __flash_data_end |
| 15 | +var flashDataEnd [0]byte |
| 16 | + |
| 17 | +// Return the start of the writable flash area, aligned on a page boundary. This |
| 18 | +// is usually just after the program and static data. |
| 19 | +func FlashDataStart() uintptr { |
| 20 | + pagesize := uintptr(eraseBlockSize()) |
| 21 | + return (uintptr(unsafe.Pointer(&flashDataStart)) + pagesize - 1) &^ (pagesize - 1) |
| 22 | +} |
| 23 | + |
| 24 | +// Return the end of the writable flash area. Usually this is the address one |
| 25 | +// past the end of the on-chip flash. |
| 26 | +func FlashDataEnd() uintptr { |
| 27 | + return uintptr(unsafe.Pointer(&flashDataEnd)) |
| 28 | +} |
| 29 | + |
| 30 | +var ( |
| 31 | + errFlashCannotErasePage = errors.New("cannot erase flash page") |
| 32 | + errFlashInvalidWriteLength = errors.New("write flash data must align to correct number of bits") |
| 33 | + errFlashNotAllowedWriteData = errors.New("not allowed to write flash data") |
| 34 | + errFlashCannotWriteData = errors.New("cannot write flash data") |
| 35 | + errFlashCannotReadPastEOF = errors.New("cannot read beyond end of flash data") |
| 36 | + errFlashCannotWritePastEOF = errors.New("cannot write beyond end of flash data") |
| 37 | +) |
| 38 | + |
| 39 | +// BlockDevice is the raw device that is meant to store flash data. |
| 40 | +type BlockDevice interface { |
| 41 | + // ReadAt reads the given number of bytes from the block device. |
| 42 | + io.ReaderAt |
| 43 | + |
| 44 | + // WriteAt writes the given number of bytes to the block device. |
| 45 | + io.WriterAt |
| 46 | + |
| 47 | + // Size returns the number of bytes in this block device. |
| 48 | + Size() int64 |
| 49 | + |
| 50 | + // WriteBlockSize returns the block size in which data can be written to |
| 51 | + // memory. It can be used by a client to optimize writes, non-aligned writes |
| 52 | + // should always work correctly. |
| 53 | + WriteBlockSize() int64 |
| 54 | + |
| 55 | + // EraseBlockSize returns the smallest erasable area on this particular chip |
| 56 | + // in bytes. This is used for the block size in EraseBlocks. |
| 57 | + // It must be a power of two, and may be as small as 1. A typical size is 4096. |
| 58 | + EraseBlockSize() int64 |
| 59 | + |
| 60 | + // EraseBlocks erases the given number of blocks. An implementation may |
| 61 | + // transparently coalesce ranges of blocks into larger bundles if the chip |
| 62 | + // supports this. The start and len parameters are in block numbers, use |
| 63 | + // EraseBlockSize to map addresses to blocks. |
| 64 | + EraseBlocks(start, len int64) error |
| 65 | +} |
| 66 | + |
| 67 | +// FlashBuffer implements the ReadWriteCloser interface using the BlockDevice interface. |
| 68 | +type FlashBuffer struct { |
| 69 | + b BlockDevice |
| 70 | + start uintptr |
| 71 | + current uintptr |
| 72 | +} |
| 73 | + |
| 74 | +// OpenFlashBuffer opens a FlashBuffer. |
| 75 | +func OpenFlashBuffer(b BlockDevice, address uintptr) *FlashBuffer { |
| 76 | + return &FlashBuffer{b: b, start: address, current: address} |
| 77 | +} |
| 78 | + |
| 79 | +// Read data from a FlashBuffer. |
| 80 | +func (fl *FlashBuffer) Read(p []byte) (n int, err error) { |
| 81 | + fl.b.ReadAt(p, int64(fl.current)) |
| 82 | + |
| 83 | + fl.current += uintptr(len(p)) |
| 84 | + |
| 85 | + return len(p), nil |
| 86 | +} |
| 87 | + |
| 88 | +// Write data to a FlashBuffer. |
| 89 | +func (fl *FlashBuffer) Write(p []byte) (n int, err error) { |
| 90 | + // any new pages needed? |
| 91 | + // NOTE probably will not work as expected if you try to write over page boundary |
| 92 | + // of pages with different sizes. |
| 93 | + pagesize := uintptr(fl.b.EraseBlockSize()) |
| 94 | + currentPageCount := (fl.current - fl.start + pagesize - 1) / pagesize |
| 95 | + totalPagesNeeded := (fl.current - fl.start + uintptr(len(p)) + pagesize - 1) / pagesize |
| 96 | + if currentPageCount == totalPagesNeeded { |
| 97 | + // just write the data |
| 98 | + n, err := fl.b.WriteAt(p, int64(fl.current)) |
| 99 | + if err != nil { |
| 100 | + return 0, err |
| 101 | + } |
| 102 | + fl.current += uintptr(n) |
| 103 | + return n, nil |
| 104 | + } |
| 105 | + |
| 106 | + // erase enough blocks to hold the data |
| 107 | + page := fl.flashPageFromAddress(fl.start + (currentPageCount * pagesize)) |
| 108 | + fl.b.EraseBlocks(page, int64(totalPagesNeeded-currentPageCount)) |
| 109 | + |
| 110 | + // write the data |
| 111 | + for i := 0; i < len(p); i += int(pagesize) { |
| 112 | + var last int = i + int(pagesize) |
| 113 | + if i+int(pagesize) > len(p) { |
| 114 | + last = len(p) |
| 115 | + } |
| 116 | + |
| 117 | + _, err := fl.b.WriteAt(p[i:last], int64(fl.current)) |
| 118 | + if err != nil { |
| 119 | + return 0, err |
| 120 | + } |
| 121 | + fl.current += uintptr(last - i) |
| 122 | + } |
| 123 | + |
| 124 | + return len(p), nil |
| 125 | +} |
| 126 | + |
| 127 | +// Close the FlashBuffer. |
| 128 | +func (fl *FlashBuffer) Close() error { |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +// Seek implements io.Seeker interface, but with limitations. |
| 133 | +// You can only seek relative to the start. |
| 134 | +// Also, you cannot use seek before write operations, only read. |
| 135 | +func (fl *FlashBuffer) Seek(offset int64, whence int) (int64, error) { |
| 136 | + fl.current = fl.start + uintptr(offset) |
| 137 | + |
| 138 | + return offset, nil |
| 139 | +} |
| 140 | + |
| 141 | +// calculate page number from address |
| 142 | +func (fl *FlashBuffer) flashPageFromAddress(address uintptr) int64 { |
| 143 | + return int64(address-memoryStart) / fl.b.EraseBlockSize() |
| 144 | +} |
0 commit comments