1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
27
28
33
34
38
39
44
45
48
49
53
54
58
59
63
64
67
68
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
/* ... */
#include <stdio.h>
#include "hardware/dma.h"
#include "hardware/claim.h"
#define DMA_CHAN_STRIDE (DMA_CH1_CTRL_TRIG_OFFSET - DMA_CH0_CTRL_TRIG_OFFSET)
check_hw_size(dma_channel_hw_t, DMA_CHAN_STRIDE);
check_hw_layout(dma_hw_t, abort, DMA_CHAN_ABORT_OFFSET);
static_assert(offsetof(dma_hw_t, ch[0].ctrl_trig) == DMA_CH0_CTRL_TRIG_OFFSET, "hw mismatch");
static_assert(offsetof(dma_hw_t, ch[1].ctrl_trig) == DMA_CH1_CTRL_TRIG_OFFSET, "hw mismatch");
static_assert(NUM_DMA_CHANNELS <= 16, "");
static uint16_t _claimed;
static uint8_t _timer_claimed;
void dma_channel_claim(uint channel) {
check_dma_channel_param(channel);
hw_claim_or_assert((uint8_t *) &_claimed, channel, "DMA channel %d is already claimed");
}{ ... }
void dma_claim_mask(uint32_t mask) {
for(uint i = 0; mask; i++, mask >>= 1u) {
if (mask & 1u) dma_channel_claim(i);
}for (uint i = 0; mask; i++, mask >>= 1u) { ... }
}{ ... }
void dma_channel_unclaim(uint channel) {
check_dma_channel_param(channel);
hw_claim_clear((uint8_t *) &_claimed, channel);
}{ ... }
void dma_unclaim_mask(uint32_t mask) {
for(uint i = 0; mask; i++, mask >>= 1u) {
if (mask & 1u) dma_channel_unclaim(i);
}for (uint i = 0; mask; i++, mask >>= 1u) { ... }
}{ ... }
int dma_claim_unused_channel(bool required) {
return hw_claim_unused_from_range((uint8_t*)&_claimed, required, 0, NUM_DMA_CHANNELS-1, "No DMA channels are available");
}{ ... }
bool dma_channel_is_claimed(uint channel) {
check_dma_channel_param(channel);
return hw_is_claimed((uint8_t *) &_claimed, channel);
}{ ... }
void dma_timer_claim(uint timer) {
check_dma_timer_param(timer);
hw_claim_or_assert(&_timer_claimed, timer, "DMA timer %d is already claimed");
}{ ... }
void dma_timer_unclaim(uint timer) {
check_dma_timer_param(timer);
hw_claim_clear(&_timer_claimed, timer);
}{ ... }
int dma_claim_unused_timer(bool required) {
return hw_claim_unused_from_range(&_timer_claimed, required, 0, NUM_DMA_TIMERS-1, "No DMA timers are available");
}{ ... }
bool dma_timer_is_claimed(uint timer) {
check_dma_timer_param(timer);
return hw_is_claimed(&_timer_claimed, timer);
}{ ... }
void dma_channel_cleanup(uint channel) {
check_dma_channel_param(channel);
hw_write_masked( &dma_hw->ch[channel].al1_ctrl, (channel << DMA_CH0_CTRL_TRIG_CHAIN_TO_LSB) | (0u << DMA_CH0_CTRL_TRIG_EN_LSB), DMA_CH0_CTRL_TRIG_CHAIN_TO_BITS | DMA_CH0_CTRL_TRIG_EN_BITS );
dma_channel_set_irq0_enabled(channel, false);
dma_channel_set_irq1_enabled(channel, false);
dma_channel_abort(channel);
dma_hw->intr = 1u << channel;
}{ ... }
#ifndef NDEBUG
void print_dma_ctrl(dma_channel_hw_t *channel) {
uint32_t ctrl = channel->ctrl_trig;
int rgsz = (ctrl & DMA_CH0_CTRL_TRIG_RING_SIZE_BITS) >> DMA_CH0_CTRL_TRIG_RING_SIZE_LSB;
printf("(%08x) ber %d rer %d wer %d busy %d trq %d cto %d rgsl %d rgsz %d inw %d inr %d sz %d hip %d en %d",
(uint) ctrl,
ctrl & DMA_CH0_CTRL_TRIG_AHB_ERROR_BITS ? 1 : 0,
ctrl & DMA_CH0_CTRL_TRIG_READ_ERROR_BITS ? 1 : 0,
ctrl & DMA_CH0_CTRL_TRIG_WRITE_ERROR_BITS ? 1 : 0,
ctrl & DMA_CH0_CTRL_TRIG_BUSY_BITS ? 1 : 0,
(int) ((ctrl & DMA_CH0_CTRL_TRIG_TREQ_SEL_BITS) >> DMA_CH0_CTRL_TRIG_TREQ_SEL_LSB),
(int) ((ctrl & DMA_CH0_CTRL_TRIG_CHAIN_TO_BITS) >> DMA_CH0_CTRL_TRIG_CHAIN_TO_LSB),
ctrl & DMA_CH0_CTRL_TRIG_RING_SEL_BITS ? 1 : 0,
rgsz ? (1 << rgsz) : 0,
ctrl & DMA_CH0_CTRL_TRIG_INCR_WRITE_BITS ? 1 : 0,
ctrl & DMA_CH0_CTRL_TRIG_INCR_READ_BITS ? 1 : 0,
1 << ((ctrl & DMA_CH0_CTRL_TRIG_DATA_SIZE_BITS) >> DMA_CH0_CTRL_TRIG_DATA_SIZE_LSB),
ctrl & DMA_CH0_CTRL_TRIG_HIGH_PRIORITY_BITS ? 1 : 0,
ctrl & DMA_CH0_CTRL_TRIG_EN_BITS ? 1 : 0);
}{ ... }
#endif/* ... */
#if PARAM_ASSERTIONS_ENABLED(HARDWARE_DMA)
void check_dma_channel_param_impl(uint __unused channel) {
valid_params_if(HARDWARE_DMA, channel < NUM_DMA_CHANNELS);
}check_dma_channel_param_impl (uint __unused channel) { ... }
/* ... */#endif