1 /*
2 * Copyright 2013 Jared Boone <jared@sharebrained.com>
3 *
4 * This file is part of HackRF.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #include <sgpio_dma.h>
23
24 #include <libopencm3/lpc43xx/creg.h>
25 #include <libopencm3/lpc43xx/gpdma.h>
26 #include <libopencm3/lpc43xx/scu.h>
27 #include <libopencm3/lpc43xx/sgpio.h>
28
29 #include <sgpio.h>
30 #include <gpdma.h>
31
sgpio_dma_configure_lli(gpdma_lli_t * const lli,const size_t lli_count,const bool direction_transmit,void * const buffer,const size_t transfer_bytes)32 void sgpio_dma_configure_lli(
33 gpdma_lli_t* const lli,
34 const size_t lli_count,
35 const bool direction_transmit,
36 void* const buffer,
37 const size_t transfer_bytes
38 ) {
39 const size_t bytes_per_word = 4;
40 const size_t transfer_words = (transfer_bytes + bytes_per_word - 1) / bytes_per_word;
41
42 gpdma_lli_create_loop(lli, lli_count);
43
44 for(size_t i=0; i<lli_count; i++) {
45 void* const peripheral_address = (void*)&SGPIO_REG_SS(0);
46 void* const memory_address = buffer + (transfer_words * bytes_per_word * i);
47
48 const uint_fast8_t source_master = direction_transmit ? 1 : 0;
49 const uint_fast8_t destination_master = direction_transmit ? 0 : 1;
50 const uint_fast8_t lli_fetch_master = direction_transmit ? 0 : 1;
51
52 lli[i].csrcaddr = direction_transmit ? memory_address : peripheral_address;
53 lli[i].cdestaddr = direction_transmit ? peripheral_address : memory_address;
54 lli[i].clli = (lli[i].clli & ~GPDMA_CLLI_LM_MASK) | GPDMA_CLLI_LM(lli_fetch_master);
55 lli[i].ccontrol =
56 GPDMA_CCONTROL_TRANSFERSIZE(transfer_words) |
57 GPDMA_CCONTROL_SBSIZE(0) |
58 GPDMA_CCONTROL_DBSIZE(0) |
59 GPDMA_CCONTROL_SWIDTH(2) |
60 GPDMA_CCONTROL_DWIDTH(2) |
61 GPDMA_CCONTROL_S(source_master) |
62 GPDMA_CCONTROL_D(destination_master) |
63 GPDMA_CCONTROL_SI(direction_transmit ? 1 : 0) |
64 GPDMA_CCONTROL_DI(direction_transmit ? 0 : 1) |
65 GPDMA_CCONTROL_PROT1(0) |
66 GPDMA_CCONTROL_PROT2(0) |
67 GPDMA_CCONTROL_PROT3(0) |
68 GPDMA_CCONTROL_I(0)
69 ;
70 }
71 }
72
sgpio_dma_enable(const uint_fast8_t channel,const gpdma_lli_t * const lli,const bool direction_transmit)73 static void sgpio_dma_enable(const uint_fast8_t channel, const gpdma_lli_t* const lli, const bool direction_transmit) {
74 gpdma_channel_disable(channel);
75 gpdma_channel_interrupt_tc_clear(channel);
76 gpdma_channel_interrupt_error_clear(channel);
77
78 GPDMA_CSRCADDR(channel) = (uint32_t)lli->csrcaddr;
79 GPDMA_CDESTADDR(channel) = (uint32_t)lli->cdestaddr;
80 GPDMA_CLLI(channel) = (uint32_t)lli->clli;
81 GPDMA_CCONTROL(channel) = lli->ccontrol;
82
83 /* 1: Memory -> Peripheral
84 * 2: Peripheral -> Memory */
85 const uint_fast8_t flowcntrl = direction_transmit ? 1 : 2;
86
87 GPDMA_CCONFIG(channel) =
88 GPDMA_CCONFIG_E(0) |
89 GPDMA_CCONFIG_SRCPERIPHERAL(0) |
90 GPDMA_CCONFIG_DESTPERIPHERAL(0) |
91 GPDMA_CCONFIG_FLOWCNTRL(flowcntrl) |
92 GPDMA_CCONFIG_IE(1) |
93 GPDMA_CCONFIG_ITC(1) |
94 GPDMA_CCONFIG_L(0) |
95 GPDMA_CCONFIG_H(0)
96 ;
97
98 gpdma_channel_enable(channel);
99 }
100
sgpio_dma_init()101 void sgpio_dma_init() {
102 /* DMA peripheral/source 0, option 2 (SGPIO14) -- BREQ */
103 CREG_DMAMUX &= ~(CREG_DMAMUX_DMAMUXPER0_MASK);
104 CREG_DMAMUX |= CREG_DMAMUX_DMAMUXPER0(0x2);
105
106 // Disable sync, maybe it is causing max speed (10MT/sec) glitches?
107 //GPDMA_DMACSYNC = (1 << 0);
108 //GPDMA_SYNC = GPDMA_SYNC_DMACSYNC(0xFFFF); // TODO: Don't do this, I'm just going nuts here.
109
110 gpdma_controller_enable();
111 }
112
113 static const uint_fast8_t dma_channel_sgpio = 0;
114
sgpio_dma_rx_start(const gpdma_lli_t * const start_lli)115 void sgpio_dma_rx_start(const gpdma_lli_t* const start_lli) {
116 sgpio_dma_enable(dma_channel_sgpio, start_lli, false);
117 }
118
sgpio_dma_tx_start(const gpdma_lli_t * const start_lli)119 void sgpio_dma_tx_start(const gpdma_lli_t* const start_lli) {
120 sgpio_dma_enable(dma_channel_sgpio, start_lli, true);
121 }
122
sgpio_dma_irq_tc_acknowledge()123 void sgpio_dma_irq_tc_acknowledge() {
124 gpdma_channel_interrupt_tc_clear(dma_channel_sgpio);
125 }
126
sgpio_dma_stop()127 void sgpio_dma_stop() {
128 gpdma_channel_disable(dma_channel_sgpio);
129 }
130
sgpio_dma_current_transfer_index(const gpdma_lli_t * const lli,const size_t lli_count)131 size_t sgpio_dma_current_transfer_index(
132 const gpdma_lli_t* const lli,
133 const size_t lli_count
134 ) {
135 const uint32_t next_lli = GPDMA_CLLI(dma_channel_sgpio);
136 for(size_t i=0; i<lli_count; i++) {
137 if( lli[i].clli == next_lli ) {
138 return i;
139 }
140 }
141 return 0;
142 }
143