Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dma_common.c « common « efm32 « lib - github.com/thirdpin/libopencm3.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e6a18cedfd455dc1c35180e4249b189f40e73847 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/** @addtogroup dma_file DMA peripheral API
 * @ingroup peripheral_apis
 */
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2015 Kuldeep Singh Dhaka <kuldeepdhaka9@gmail.com>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libopencm3/efm32/dma.h>

/**@{*/

#define CHANNEL_SUPPORT_LOOP(ch) (((ch) == DMA_CH0) || ((ch) == DMA_CH1))

/**
 * Enable DMA with privileged access
 * @see dma_enable_with_unprivileged_access()
 */
void dma_enable_with_privileged_access(void)
{
	DMA_CONFIG = DMA_CONFIG_EN | DMA_CONFIG_CHPROT;
}

/**
 * Enable DMA with un-privileged access
 * @see dma_enable_with_privileged_access()
 */
void dma_enable_with_unprivileged_access(void)
{
	DMA_CONFIG = DMA_CONFIG_EN;
}

/**
 * same as @a dma_enable_with_unprivileged_access()
 * @see dma_enable_with_unprivileged_access()
 */
void dma_enable(void)
{
	dma_enable_with_unprivileged_access();
}

/**
 * Disable DMA
 */
void dma_disable(void)
{
	DMA_CONFIG = 0;
}

/**
 * Set channel's descriptor address
 * @param[in] desc_base Address of channel's descriptor address
 * @note @a desc_base 8LSB's should be 0x00
 */
void dma_set_desc_address(uint32_t desc_base)
{
	if (desc_base & 0xFF) {
		return;
	}

	DMA_CTRLBASE = desc_base;
}

/**
 * Get channel wait on request status flag
 * @retval true if flag is set
 * @retval false if flag is not set
 */
bool dma_get_wait_on_request_flag(enum dma_ch ch)
{
	uint32_t mask = DMA_CHWAITSTATUS_CHxWAITSTATUS(ch);
	return (DMA_CHWAITSTATUS & mask) != 0;
}

/**
 * Generate a software request on channel
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_generate_software_request(enum dma_ch ch)
{
	DMA_CHSWREQ = DMA_CHSWREQ_CHxSWREQ(ch);
}

/**
 * Enable channel burst only
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_burst_only(enum dma_ch ch)
{
	DMA_CHUSEBURSTS = DMA_CHUSEBURSTS_CHxSUSEBURSTS(ch);
}

/**
 * Enable channel single and burst
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_single_and_burst(enum dma_ch ch)
{
	DMA_CHUSEBURSTC = DMA_CHUSEBURSTC_CHxSUSEBURSTC(ch);
}

/**
 * Enable channel peripherial request
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_periph_request(enum dma_ch ch)
{
	DMA_CHREQMASKC = DMA_CHREQMASKC_CHxSREQMASKC(ch);
}

/**
 * Disable channel peripherial request
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_disable_periph_request(enum dma_ch ch)
{
	DMA_CHREQMASKS = DMA_CHREQMASKS_CHxSREQMASKS(ch);
}

/**
 * Enable channel
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_channel(enum dma_ch ch)
{
	DMA_CHENS = DMA_CHENS_CHxSENS(ch);
}

/**
 * Disable channel
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_disable_channel(enum dma_ch ch)
{
	DMA_CHENC = DMA_CHENC_CHxSENC(ch);
}

/**
 * Disable channel alternate structure
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_disable_alternate_structure(enum dma_ch ch)
{
	DMA_CHALTC = DMA_CHALTC_CHxSALTC(ch);
}

/**
 * Enable channel alternate structure
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_alternate_structure(enum dma_ch ch)
{
	DMA_CHALTS = DMA_CHALTS_CHxSALTS(ch);
}

/**
 * Enable channel high priority
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_priority(enum dma_ch ch)
{
	DMA_CHPRIS = DMA_CHPRIS_CHxSPRIC(ch);
}

/**
 * Disable channel high priority
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_disable_priority(enum dma_ch ch)
{
	DMA_CHPRIC = DMA_CHPRIC_CHxSPRIC(ch);
}

/**
 * Get bus error flag
 * @retval true if flag is set
 * @retval false if flag is not set
 */
bool dma_get_bus_error_flag(void)
{
	return (DMA_ERRORC & DMA_ERRORC_ERRORC) != 0;
}

/**
 * Clear bus error flag
 */
void dma_clear_bus_error_flag(void)
{
	DMA_ERRORC = DMA_ERRORC_ERRORC;
}

/**
 * Get channel request flag
 * @param[in] ch Channel (use DMA_CHx)
 * @retval true if flag is set
 * @retval false if flag is not set
 */
bool dma_get_request_flag(enum dma_ch ch)
{
	uint32_t mask = DMA_CHREQSTATUS_CHxSREQSTATUS(ch);
	return (DMA_CHREQSTATUS & mask) != 0;
}

/**
 * Get bus error interrupt flag
 * @retval true if flag is set
 * @retval false if flag is not set
 */
bool dma_get_bus_error_interrupt_flag(void)
{
	return (DMA_IF & DMA_IF_ERR) != 0;
}

/**
 * Get channel done interrupt flag
 * @param[in] ch Channel (use DMA_CHx)
 * @retval true if flag is set
 * @retval false if flag is not set
 *
 */
bool dma_get_done_interrupt_flag(enum dma_ch ch)
{
	return (DMA_IF & DMA_IF_CHxDONE(ch)) != 0;
}

/**
 * Set bus error interrupt flag
 */
void dma_set_bus_error_interrupt_flag(void)
{
	DMA_IFS = DMA_IFS_ERR;
}

/**
 * Set channel done interrupt flag
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_set_done_interrupt_flag(enum dma_ch ch)
{
	DMA_IFS = DMA_IFS_CHxDONE(ch);
}

/**
 * Clear bus error interrupt flag
 */
void dma_clear_bus_error_interrupt_flag(void)
{
	DMA_IFC = DMA_IFC_ERR;
}

/**
 * Clear channel done interrupt flag
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_clear_done_interrupt_flag(enum dma_ch ch)
{
	DMA_IFC = DMA_IFC_CHxDONE(ch);
}

/**
 * Enable bus error interrupt
 */
void dma_enable_bus_error_interrupt(void)
{
	DMA_IEN |= DMA_IEN_ERR;
}

/**
 * Disable bus error interrupt
 */
void dma_disable_bus_error_interrupt(void)
{
	DMA_IEN &= ~DMA_IEN_ERR;
}

/**
 * Enable channel done interrupt
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_done_interrupt(enum dma_ch ch)
{
	DMA_IEN |= DMA_IEN_CHxDONE(ch);
}

/**
 * Disable channel done interrupt
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_disable_done_interrupt(enum dma_ch ch)
{
	DMA_IEN &= ~DMA_IEN_CHxDONE(ch);
}

/**
 * Set channel source
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] source Source (use DMA_CH_CTRL_SOURCESEL_*)
 */
void dma_set_source(enum dma_ch ch, uint32_t source)
{
	DMA_CHx_CTRL(ch) = (DMA_CHx_CTRL(ch) & ~DMA_CH_CTRL_SOURCESEL_MASK)
			    | source;
}

/**
 * Set channel source signal
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] signal Signal (use DMA_CH_CTRL_SIGSEL_*)
 */
void dma_set_signal(enum dma_ch ch, uint32_t signal)
{
	DMA_CHx_CTRL(ch) = (DMA_CHx_CTRL(ch) & ~DMA_CH_CTRL_SIGSEL_MASK)
			    | signal;
}

/**
 * Reset channel
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_channel_reset(enum dma_ch ch)
{
	/* Disable channel */
	DMA_CHENC = DMA_CHENC_CHxSENC(ch);
	/* reset channel alternate desc */
	DMA_CHALTC = DMA_CHALTC_CHxSALTC(ch);
	/* reset channel priority */
	DMA_CHPRIC = DMA_CHPRIC_CHxSPRIC(ch);
	/* clear channel interrupt */
	DMA_IFC = DMA_IFC_CHxDONE(ch);
	/* disable loop */
	if (CHANNEL_SUPPORT_LOOP(ch)) {
		DMA_LOOPx(ch) = 0;
	}
	/* reset signal {source, select} */
	DMA_CHx_CTRL(ch) = 0;
}

/**
 * Set channel loop width to ( @a count + 1)
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] count Count
 * @note @a count is expected to be equal to (n_minus_1 + 1)
 */
void dma_set_loop_count(enum dma_ch ch, uint16_t count)
{
	if (!CHANNEL_SUPPORT_LOOP(ch)) {
		return;
	}

	DMA_LOOPx(ch) = (DMA_LOOPx(ch) & ~DMA_LOOP_WIDTH_MASK)
			| DMA_LOOP_WIDTH(count - 1);
}

/**
 * Enable channel loop
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_enable_loop(enum dma_ch ch)
{
	if (!CHANNEL_SUPPORT_LOOP(ch)) {
		return;
	}

	DMA_LOOPx(ch) |= DMA_LOOP_EN;
}

/**
 * Disable channel loop
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_disable_loop(enum dma_ch ch)
{
	if (!CHANNEL_SUPPORT_LOOP(ch)) {
		return;
	}

	DMA_LOOPx(ch) &= ~DMA_LOOP_EN;
}

/**
 * Set desination size
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] size Size (use DMA_MEM_*)
 */
void dma_desc_set_dest_size(uint32_t desc_base, enum dma_ch ch,
			    enum dma_mem size)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	cfg &= ~DMA_DESC_CH_CFG_DEST_SIZE_MASK;
	cfg |= DMA_DESC_CH_CFG_DEST_SIZE(size);
	DMA_DESC_CHx_CFG(desc_base, ch) = cfg;
}

/**
 * Set destination increment
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] inc Increment (use DMA_MEM_*)
 */
void dma_desc_set_dest_inc(uint32_t desc_base, enum dma_ch ch,
			   enum dma_mem inc)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	cfg &= ~DMA_DESC_CH_CFG_DEST_INC_MASK;
	cfg |= DMA_DESC_CH_CFG_DEST_INC(inc);
	DMA_DESC_CHx_CFG(desc_base, ch) = cfg;
}

/**
 * Set source size
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] size Size (use DMA_MEM_*)
 */
void dma_desc_set_src_size(uint32_t desc_base, enum dma_ch ch,
			   enum dma_mem size)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	cfg &= ~DMA_DESC_CH_CFG_SRC_SIZE_MASK;
	cfg |= DMA_DESC_CH_CFG_SRC_SIZE(size);
	DMA_DESC_CHx_CFG(desc_base, ch) = cfg;
}

/**
 * Set source increment
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] inc Increment (use DMA_MEM_*)
 */
void dma_desc_set_src_inc(uint32_t desc_base, enum dma_ch ch, enum dma_mem inc)
{

	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	cfg &= ~DMA_DESC_CH_CFG_SRC_INC_MASK;
	cfg |= DMA_DESC_CH_CFG_SRC_INC(inc);
	DMA_DESC_CHx_CFG(desc_base, ch) = cfg;
}

/**
 * Set R Power
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] r_power R Power (Use DMA_R_POWER_*)
 */
void dma_desc_set_r_power(uint32_t desc_base, enum dma_ch ch,
			  enum dma_r_power r_power)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	cfg &= ~DMA_DESC_CH_CFG_R_POWER_MASK;
	cfg |= DMA_DESC_CH_CFG_R_POWER(r_power);
	DMA_DESC_CHx_CFG(desc_base, ch) = cfg;
}

/**
 * Enable next useburst
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_desc_enable_next_useburst(uint32_t desc_base, enum dma_ch ch)
{
	DMA_DESC_CHx_CFG(desc_base, ch) &= ~DMA_DESC_CH_CFG_NEXT_USEBURST;
}

/**
 * Disable next useburst
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 */
void dma_desc_disable_next_useburst(uint32_t desc_base, enum dma_ch ch)
{
	DMA_DESC_CHx_CFG(desc_base, ch) |= DMA_DESC_CH_CFG_NEXT_USEBURST;
}

/**
 * Set number (count) of transfer to be performed
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] count Count
 */
void dma_desc_set_count(uint32_t desc_base, enum dma_ch ch, uint16_t count)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	cfg &= ~DMA_DESC_CH_CFG_N_MINUS_1_MASK;
	cfg |= DMA_DESC_CH_CFG_N_MINUS_1(count - 1);
	DMA_DESC_CHx_CFG(desc_base, ch) = cfg;
}

/**
 * Store user data field in channel descriptor
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] user_data User data
 */
void dma_desc_set_user_data(uint32_t desc_base, enum dma_ch ch,
			    uint32_t user_data)
{
	DMA_DESC_CHx_USER_DATA(desc_base, ch) = user_data;
}

/**
 * Extract user data field from channel descriptor
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @return user data
 */
uint32_t dma_desc_get_user_data(uint32_t desc_base, enum dma_ch ch)
{
	return DMA_DESC_CHx_USER_DATA(desc_base, ch);
}

/**
 * Calculate end from start address.
 *
 * @details
 * See "8.4.3.4 Address calculation" p68, EFM32LG-RM "d0183_Rev1.10"
 *
 * @param[in] start address to start of memory
 * @param[in] inc Increment (use DMA_MEM_*)
 * @param[in] n_minus_1 the number of transfers minus 1  (ie count - 1)
 * @return the calculate end address
 * @note can be used to calculate {source, destination} end address
 */
static inline uint32_t dma_calc_end_from_start(uint32_t start, uint8_t inc,
					uint16_t n_minus_1)
{
	switch (inc) {
	case DMA_MEM_BYTE:
		return start + n_minus_1;
	case DMA_MEM_HALF_WORD:
		return start + (n_minus_1 << 1);
	case DMA_MEM_WORD:
		return start + (n_minus_1 << 2);
	case DMA_MEM_NONE:
		return start;
	}

	return 0;
}

/**
 * Assign Source address to DMA Channel
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] src_start Source data start address
 * this function uses @ref dma_calc_end_from_start to calculate the
 * src data end address from @a src_start
 * @note dma_desc_set_count() should be called first.
 * @note dma_desc_set_src_inc() should be called first.
 */
void dma_desc_set_src_address(uint32_t desc_base, enum dma_ch ch,
			      uint32_t src_start)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	uint8_t inc = (cfg & DMA_DESC_CH_CFG_SRC_INC_MASK)
		      >> DMA_DESC_CH_CFG_SRC_INC_SHIFT;
	uint16_t n_minus_1 = (cfg & DMA_DESC_CH_CFG_N_MINUS_1_MASK)
			     >> DMA_DESC_CH_CFG_N_MINUS_1_SHIFT;
	uint32_t src_end = dma_calc_end_from_start(src_start, inc, n_minus_1);
	DMA_DESC_CHx_SRC_DATA_END_PTR(desc_base, ch) = src_end;
}

/**
 * Assign Destination address to DMA Channel
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] dest_start Destination data start address
 * this function uses @ref dma_calc_end_from_start to calculate the
 * dest data end address from @a dest_start
 * @note dma_desc_set_count() should be called first.
 * @note dma_desc_set_dest_inc() should be called first.
 */
void dma_desc_set_dest_address(uint32_t desc_base, enum dma_ch ch,
			       uint32_t dest_start)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	uint8_t inc = (cfg & DMA_DESC_CH_CFG_DEST_INC_MASK)
		      >> DMA_DESC_CH_CFG_DEST_INC_SHIFT;
	uint16_t n_minus_1 = (cfg & DMA_DESC_CH_CFG_N_MINUS_1_MASK)
			     >> DMA_DESC_CH_CFG_N_MINUS_1_SHIFT;
	uint32_t dest_end = dma_calc_end_from_start(dest_start, inc,
						    n_minus_1);
	DMA_DESC_CHx_DEST_DATA_END_PTR(desc_base, ch) = dest_end;
}

/**
 * Set the channel mode ("Cycle control")
 * @param[in] desc_base start of memory location that contain channel
 *            descriptor
 * @param[in] ch Channel (use DMA_CHx)
 * @param[in] mode Mode (use DMA_MODE_*)
 */
void dma_desc_set_mode(uint32_t desc_base, enum dma_ch ch, enum dma_mode mode)
{
	uint32_t cfg = DMA_DESC_CHx_CFG(desc_base, ch);
	cfg &= ~DMA_DESC_CH_CFG_CYCLE_CTRL_MASK;
	cfg |= DMA_DESC_CH_CFG_CYCLE_CTRL(mode);
	DMA_DESC_CHx_CFG(desc_base, ch) = cfg;
}

/**@}*/