Skip to content

Commit acb50e5

Browse files
committed
Adding machine.Flash example
1 parent 558c8b3 commit acb50e5

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

examples/console/example.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var (
2929
console = machine.Serial
3030
readyLED = machine.LED
3131

32-
flashdev *flash.Device
32+
// flashdev *flash.Device
33+
blockdev tinyfs.BlockDevice
3334
fs tinyfs.Filesystem
3435

3536
currdir = "/"
@@ -61,10 +62,10 @@ const (
6162
StateCSI
6263
)
6364

64-
func RunFor(dev *flash.Device, filesys tinyfs.Filesystem) {
65+
func RunFor(dev tinyfs.BlockDevice, filesys tinyfs.Filesystem) {
6566
time.Sleep(3 * time.Second)
67+
blockdev = dev
6668

67-
flashdev = dev
6869
fs = filesys
6970

7071
readyLED.Configure(machine.PinConfig{Mode: machine.PinOutput})
@@ -175,6 +176,18 @@ func dbg(argv []string) {
175176
}
176177

177178
func lsblk(argv []string) {
179+
if flashdev, ok := blockdev.(*flash.Device); ok {
180+
lsblk_flash(flashdev)
181+
return
182+
}
183+
if blockdev == machine.Flash {
184+
lsblk_machine()
185+
return
186+
}
187+
println("Unknown device")
188+
}
189+
190+
func lsblk_flash(flashdev *flash.Device) {
178191
attrs := flashdev.Attrs()
179192
status1, _ := flashdev.ReadStatus()
180193
status2, _ := flashdev.ReadStatus2()
@@ -210,6 +223,19 @@ func lsblk(argv []string) {
210223
)
211224
}
212225

226+
func lsblk_machine() {
227+
fmt.Printf(
228+
"\n-------------------------------------\r\n"+
229+
" Device Information: \r\n"+
230+
"-------------------------------------\r\n"+
231+
" flash data start: %08X\r\n"+
232+
" flash data end: %08X\r\n"+
233+
"-------------------------------------\r\n\r\n",
234+
machine.FlashDataStart(),
235+
machine.FlashDataEnd(),
236+
)
237+
}
238+
213239
func mount(argv []string) {
214240
if err := fs.Mount(); err != nil {
215241
println("Could not mount LittleFS filesystem: " + err.Error() + "\r\n")
@@ -524,7 +550,7 @@ func xxd(argv []string) {
524550
buf := make([]byte, size)
525551
//bsz := uint64(flash.SectorSize)
526552
//blockdev.ReadBlock(uint32(addr/bsz), uint32(addr%bsz), buf)
527-
flashdev.ReadAt(buf, int64(addr))
553+
blockdev.ReadAt(buf, int64(addr))
528554
xxdfprint(os.Stdout, uint32(addr), buf)
529555
}
530556

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build tinygo
2+
// +build tinygo
3+
4+
package main
5+
6+
import (
7+
"machine"
8+
9+
"tinygo.org/x/tinyfs/examples/console"
10+
"tinygo.org/x/tinyfs/littlefs"
11+
)
12+
13+
var (
14+
blockDevice = machine.Flash
15+
filesystem = littlefs.New(blockDevice)
16+
)
17+
18+
func main() {
19+
// Configure littlefs with parameters for caches and wear levelling
20+
filesystem.Configure(&littlefs.Config{
21+
CacheSize: 512,
22+
LookaheadSize: 512,
23+
BlockCycles: 100,
24+
})
25+
console.RunFor(blockDevice, filesystem)
26+
}

0 commit comments

Comments
 (0)