-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
105 lines (90 loc) · 2.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2019 Michal Derkacz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Dma tests different ways of coping memory. It also shows how to use DMA for
// memory to memory transfers.
package main
import (
"embedded/rtos"
"time"
"unsafe"
"github.com/embeddedgo/stm32/hal/dma"
"github.com/embeddedgo/stm32/hal/irq"
_ "github.com/embeddedgo/stm32/devboard/nucleo-l476rg/board/system"
)
var (
src = make ([]uint32, 3000)
dst = make ([]uint32, 3000)
ch dma.Channel
tce rtos.Note
)
func copyDMA(n int) {
tce.Clear()
ch.Setup(dma.MTM | dma.IncP | dma.IncM)
ch.SetWordSize(unsafe.Sizeof(src[0]), unsafe.Sizeof(dst[0]))
ch.SetLen(n)
ch.SetAddrP(unsafe.Pointer(&src[0]))
ch.SetAddrM(unsafe.Pointer(&dst[0]))
ch.Clear(dma.EvAll, dma.ErrAll)
ch.EnableIRQ(dma.Complete, dma.ErrAll)
ch.Enable()
tce.Sleep(-1)
if _, err := ch.Status(); err != 0 {
println(err.Error())
}
}
func printSpeed(t time.Duration, n int, check bool) {
t1 := rtos.Nanotime()
t2 := rtos.Nanotime()
dt := int64(t1-t) - int64(t2-t1)
if check {
for i := 0; i < n; i++ {
if dst[i] != uint32(i) {
println(" dst != src\n")
return
}
dst[i] = 0
}
}
println("", (int64(uintptr(n)*unsafe.Sizeof(src[0]))*1e6+dt/2)/dt, "kB/s")
}
func main() {
d := dma.DMA(1)
d.EnableClock(true)
ch = d.Channel(1, 0)
irq.DMA1_CH1.Enable(rtos.IntPrioLow, 0)
for {
for n := 1000; n <= len(src); n += 1000 {
println("\nTransfer length:", n, "words\n")
print("Initialize src ")
t := rtos.Nanotime()
for i := 0; i < n; i++ {
src[i] = uint32(i)
}
printSpeed(t, n, false)
print("for i:=0; i<n; i++ { dst[i]=src[i] }")
t = rtos.Nanotime()
for i := 0; i < n; i++ {
dst[i] = src[i]
}
printSpeed(t, n, true)
print("copy(dst[:n], src[:n]) ")
t = rtos.Nanotime()
copy(dst[:n], src[:n])
printSpeed(t, n, true)
print("DMA ")
t = rtos.Nanotime()
copyDMA(n)
printSpeed(t, n, true)
time.Sleep(2 * time.Second)
}
time.Sleep(2 * time.Second)
println("\n---")
}
}
//go:interrupthandler
func DMA1_CH1_Handler() {
ch.DisableIRQ(dma.EvAll, dma.ErrAll)
ch.Disable()
tce.Wakeup()
}