Skip to content

Commit 31eed01

Browse files
committed
DMA API update
1 parent 8245dcb commit 31eed01

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

rp2-pio/piolib/dma.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type dmaArbiter struct {
2020
func (arb *dmaArbiter) ClaimChannel() (channel dmaChannel, ok bool) {
2121
for i := uint8(0); i < 12; i++ {
2222
ch := arb.Channel(i)
23-
if ch.Claim() {
23+
if ch.TryClaim() {
2424
return ch, true
2525
}
2626
}
@@ -47,8 +47,8 @@ type dmaChannel struct {
4747
idx uint8
4848
}
4949

50-
// Claim claims the DMA channel for use by a peripheral and returns if it succeeded in claiming the channel.
51-
func (ch dmaChannel) Claim() bool {
50+
// TryClaim claims the DMA channel for use by a peripheral and returns if it succeeded in claiming the channel.
51+
func (ch dmaChannel) TryClaim() bool {
5252
ch.mustValid()
5353
if ch.IsClaimed() {
5454
return false
@@ -186,6 +186,15 @@ func (ch dmaChannel) Push8(dst *byte, src []byte, dreq uint32) error {
186186

187187
// Push32 writes each element of src slice into the memory location at dst.
188188
func dmaPush[T uint8 | uint16 | uint32](ch dmaChannel, dst *T, src []T, dreq uint32) error {
189+
// If currently busy we wait until safe to edit hardware registers.
190+
deadline := ch.dl.newDeadline()
191+
for ch.busy() {
192+
if deadline.expired() {
193+
return errContentionTimeout
194+
}
195+
gosched()
196+
}
197+
189198
hw := ch.HW()
190199
hw.CTRL_TRIG.ClearBits(rp.DMA_CH0_CTRL_TRIG_EN_Msk)
191200
srcPtr := uint32(uintptr(unsafe.Pointer(&src[0])))
@@ -204,15 +213,6 @@ func dmaPush[T uint8 | uint16 | uint32](ch dmaChannel, dst *T, src []T, dreq uin
204213
cc.setWriteIncrement(false)
205214
cc.setEnable(true)
206215

207-
deadline := ch.dl.newDeadline()
208-
// If currently busy we wait.
209-
for ch.busy() {
210-
if deadline.expired() {
211-
return errContentionTimeout
212-
}
213-
gosched()
214-
}
215-
216216
// We begin our DMA transfer here!
217217
hw.CTRL_TRIG.Set(cc.CTRL)
218218

@@ -245,6 +245,15 @@ func (ch dmaChannel) Pull8(dst []byte, src *byte, dreq uint32) error {
245245

246246
// Pull32 reads the memory location at src into dst slice, incrementing dst pointer but not src.
247247
func dmaPull[T uint8 | uint16 | uint32](ch dmaChannel, dst []T, src *T, dreq uint32) error {
248+
// If currently busy we wait until safe to edit hardware registers.
249+
deadline := ch.dl.newDeadline()
250+
for ch.busy() {
251+
if deadline.expired() {
252+
return errContentionTimeout
253+
}
254+
gosched()
255+
}
256+
248257
hw := ch.HW()
249258
hw.CTRL_TRIG.ClearBits(rp.DMA_CH0_CTRL_TRIG_EN_Msk)
250259
srcPtr := uint32(uintptr(unsafe.Pointer(src)))
@@ -263,15 +272,6 @@ func dmaPull[T uint8 | uint16 | uint32](ch dmaChannel, dst []T, src *T, dreq uin
263272
cc.setWriteIncrement(true)
264273
cc.setEnable(true)
265274

266-
// If currently busy we wait.
267-
deadline := ch.dl.newDeadline()
268-
for ch.busy() {
269-
if deadline.expired() {
270-
return errContentionTimeout
271-
}
272-
gosched()
273-
}
274-
275275
// We begin our DMA transfer here!
276276
hw.CTRL_TRIG.Set(cc.CTRL)
277277

0 commit comments

Comments
 (0)