/
main.go
140 lines (119 loc) · 2.69 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This example tests different ways of coping memory. It also shows how to use
// DMA for memory to memory transfers. In case of STM32F2xx/4xx only DMA2
// supports MTM transfer.
package main
import (
"delay"
"fmt"
"rtos"
"sync/fence"
"unsafe"
"stm32/hal/dma"
"stm32/hal/irq"
"stm32/hal/system"
"stm32/hal/system/timer/systick"
)
var (
ch *dma.Channel
dmaErr dma.Error
tce rtos.EventFlag
)
func init() {
system.Setup96(8)
systick.Setup(2e6)
d := dma.DMA2
d.EnableClock(true)
ch = d.Channel(0, 0)
ch.EnableIRQ(dma.Complete, dma.ErrAll)
rtos.IRQ(irq.DMA2_Stream0).Enable()
}
const n = 40 * 1024 / 4
var (
src = make([]uint32, n)
dst = make([]uint32, n)
)
func printSpeed(t int64, check bool) {
t1 := rtos.Nanosec()
t2 := rtos.Nanosec()
dt := (t1 - t) - (t2 - t1)
if check {
for i := range dst {
if dst[i] != uint32(i) {
fmt.Printf(" dst != src\n")
return
}
dst[i] = 0
}
}
fmt.Printf(" %6d kB/s\n", (int64(n*unsafe.Sizeof(dst[0]))*1e6+dt/2)/dt)
}
func copyDMA(mode dma.Mode) {
ch.Setup(dma.MTM | dma.IncP | dma.IncM | mode)
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]))
tce.Reset(0)
fence.W()
ch.Enable()
tce.Wait(1, 0)
if dmaErr != 0 {
fmt.Println(dmaErr)
}
}
func main() {
delay.Millisec(250) // Wait for SWO (press reset if you see nothing).
fmt.Printf("Initialize src ")
t := rtos.Nanosec()
for i := range src {
src[i] = uint32(i)
}
printSpeed(t, false)
fmt.Printf("for i := range src { dst[i] = src[i] }")
t = rtos.Nanosec()
for i := range src {
dst[i] = src[i]
}
printSpeed(t, true)
fmt.Printf("copy(dst, src) ")
t = rtos.Nanosec()
copy(dst, src)
printSpeed(t, true)
fmt.Printf("DMA ")
t = rtos.Nanosec()
copyDMA(0)
printSpeed(t, true)
fmt.Printf("DMA FT1 ")
t = rtos.Nanosec()
copyDMA(dma.FT1)
printSpeed(t, true)
fmt.Printf("DMA FT2 ")
t = rtos.Nanosec()
copyDMA(dma.FT2)
printSpeed(t, true)
fmt.Printf("DMA FT3 ")
t = rtos.Nanosec()
copyDMA(dma.FT3)
printSpeed(t, true)
fmt.Printf("DMA FT4 ")
t = rtos.Nanosec()
copyDMA(dma.FT4)
printSpeed(t, true)
fmt.Printf("DMA FT4 PB4 MB4 ")
t = rtos.Nanosec()
copyDMA(dma.FT4 | dma.PB4 | dma.MB4)
printSpeed(t, true)
}
func dmaISR() {
ev, err := ch.Status()
ch.Clear(ev, err)
if ev&dma.Complete != 0 || err != 0 {
dmaErr = err
tce.Signal(1)
}
}
//emgo:const
//c:__attribute__((section(".ISRs")))
var ISRs = [...]func(){
irq.DMA2_Stream0: dmaISR,
}