c900c3b690da7d62d7e9cdb61322262f72a6ef80
[bertos.git] / bertos / cpu / cortex-m3 / drv / i2s_sam3.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos 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 of the License, or
9  * (at your option) 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; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief I2S driver implementation.
33  *
34  * \author Daniele Basile <asterix@develer.com>
35  */
36
37
38 #include "cfg/cfg_i2s.h"
39
40 // Define log settings for cfg/log.h.
41 #define LOG_LEVEL         I2S_LOG_LEVEL
42 #define LOG_FORMAT        I2S_LOG_FORMAT
43 #include <cfg/log.h>
44
45 #include <drv/timer.h>
46 #include <drv/i2s.h>
47 #include <drv/dmac_sam3.h>
48
49 #include <mware/event.h>
50
51 #include <cpu/irq.h>
52
53 #include <io/cm3.h>
54
55
56 #define I2S_DMAC_CH    3
57
58 struct I2sHardware
59 {
60         bool end;
61 };
62
63 struct I2sHardware i2s_hw;
64 static Event data_ready;
65
66 DmacDesc lli0;
67 DmacDesc lli1;
68 DmacDesc *curr;
69 DmacDesc *next;
70 DmacDesc *prev;
71
72 static uint8_t *sample_buff;
73 static size_t next_idx = 0;
74 static size_t chunk_size = 0;
75 static size_t remaing_size = 0;
76 static bool single_transfer;
77
78 static uint32_t cfg;
79 static uint32_t ctrla;
80 static uint32_t ctrlb;
81
82
83 static void sam3_i2s_txStop(I2s *i2s)
84 {
85         (void)i2s;
86         SSC_CR = BV(SSC_TXDIS);
87         dmac_stop(I2S_DMAC_CH);
88
89         i2s->hw->end = true;
90         remaing_size = 0;
91
92         event_do(&data_ready);
93 }
94
95 static void sam3_i2s_txWait(I2s *i2s)
96 {
97         (void)i2s;
98 }
99
100 static void i2s_dmac_irq(void)
101 {
102         if (single_transfer)
103         {
104                 single_transfer = false;
105         }
106         else
107         {
108                 prev = curr;
109                 curr = next;
110                 next = prev;
111
112                 dmac_setSourcesLLI(I2S_DMAC_CH, curr, (uint32_t)&sample_buff[next_idx], (uint32_t)&SSC_THR, (uint32_t)next);
113                 dmac_configureDmacLLI(I2S_DMAC_CH, curr, chunk_size / 2, cfg, ctrla, ctrlb);
114
115                 event_do(&data_ready);
116         }
117 }
118
119 static void sam3_i2s_txStart(I2s *i2s, void *buf, size_t len, size_t slice_len)
120 {
121         ASSERT(buf);
122         ASSERT(len >= slice_len);
123         ASSERT(!(len % slice_len));
124
125         i2s->hw->end = false;
126         single_transfer = false;
127
128         sample_buff = (uint8_t *)buf;
129         next_idx = 0;
130         chunk_size = slice_len;
131         remaing_size = len;
132
133         //Confing DMAC
134         uint32_t cfg = BV(DMAC_CFG_DST_H2SEL) |
135                                 ((3 << DMAC_CFG_DST_PER_SHIFT) & DMAC_CFG_DST_PER_MASK) | (3 & DMAC_CFG_SRC_PER_MASK);
136         uint32_t ctrla = DMAC_CTRLA_SRC_WIDTH_HALF_WORD | DMAC_CTRLA_DST_WIDTH_HALF_WORD;
137         uint32_t ctrlb = BV(DMAC_CTRLB_SRC_DSCR) |
138                                 DMAC_CTRLB_FC_MEM2PER_DMA_FC |
139                                 DMAC_CTRLB_DST_INCR_FIXED | DMAC_CTRLB_SRC_INCR_INCREMENTING;
140
141
142         /* Program the dma with the first and second chunk of samples and update counter */
143         i2s->ctx.tx_callback(i2s, &sample_buff[0], chunk_size);
144
145         curr = &lli0;
146         prev = &lli0;
147         next = &lli1;
148
149         dmac_setSourcesLLI(I2S_DMAC_CH, curr, (uint32_t)&sample_buff[0], (uint32_t)&SSC_THR,(uint32_t)next);
150         dmac_configureDmacLLI(I2S_DMAC_CH, curr, chunk_size / 2, cfg, ctrla, ctrlb);
151
152         remaing_size -= chunk_size;
153         next_idx += chunk_size;
154
155         if (chunk_size <= remaing_size)
156         {
157                 i2s->ctx.tx_callback(i2s, &sample_buff[next_idx], chunk_size);
158
159                 prev = curr;
160                 curr = next;
161                 next = prev;
162
163                 dmac_setSourcesLLI(I2S_DMAC_CH, curr, (uint32_t)&sample_buff[next_idx], (uint32_t)&SSC_THR,(uint32_t)next);
164                 dmac_configureDmacLLI(I2S_DMAC_CH, curr, chunk_size / 2, cfg, ctrla, ctrlb);
165
166                 remaing_size -= chunk_size;
167                 next_idx += chunk_size;
168         }
169
170         if (dmac_start(I2S_DMAC_CH) < 0)
171                 kprintf("start erros[%x]\n", dmac_error(I2S_DMAC_CH));
172
173         SSC_CR = BV(SSC_TXEN);
174
175         while (1)
176         {
177                 event_wait(&data_ready);
178                 if (i2s->hw->end)
179                         break;
180
181                 remaing_size -= chunk_size;
182                 next_idx += chunk_size;
183
184                 if (remaing_size <= 0)
185                 {
186                         remaing_size = len;
187                         next_idx = 0;
188                 }
189
190                 if (dmac_start(I2S_DMAC_CH) < 0)
191                         kprintf("start erros[%x]\n", dmac_error(I2S_DMAC_CH));
192
193                 i2s->ctx.tx_callback(i2s, &sample_buff[next_idx], chunk_size);
194         }
195 }
196
197 static void sam3_i2s_rxStop(I2s *i2s)
198 {
199         (void)i2s;
200         SSC_CR = BV(SSC_TXDIS);
201 }
202
203 static void sam3_i2s_rxWait(I2s *i2s)
204 {
205         (void)i2s;
206 }
207
208 static void sam3_i2s_rxStart(I2s *i2s, void *buf, size_t len, size_t slice_len)
209 {
210         (void)i2s;
211         (void)buf;
212         (void)len;
213         (void)slice_len;
214 }
215
216
217 static bool sam3_i2s_isTxFinish(struct I2s *i2s)
218 {
219         (void)i2s;
220         return i2s->hw->end;
221 }
222
223 static bool sam3_i2s_isRxFinish(struct I2s *i2s)
224 {
225         (void)i2s;
226         return dmac_isDone(I2S_DMAC_CH);
227 }
228
229 static void sam3_i2s_txBuf(struct I2s *i2s, void *buf, size_t len)
230 {
231         (void)i2s;
232
233         single_transfer = true;
234
235         uint32_t cfg = BV(DMAC_CFG_DST_H2SEL) |
236                                 ((3 << DMAC_CFG_DST_PER_SHIFT) & DMAC_CFG_DST_PER_MASK) | (3 & DMAC_CFG_SRC_PER_MASK);
237         uint32_t ctrla = DMAC_CTRLA_SRC_WIDTH_HALF_WORD | DMAC_CTRLA_DST_WIDTH_HALF_WORD;
238         uint32_t ctrlb = BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) |
239                                 DMAC_CTRLB_FC_MEM2PER_DMA_FC |
240                                 DMAC_CTRLB_DST_INCR_FIXED | DMAC_CTRLB_SRC_INCR_INCREMENTING;
241
242         dmac_setSources(I2S_DMAC_CH, (uint32_t)buf, (uint32_t)&SSC_THR);
243         dmac_configureDmac(I2S_DMAC_CH, len, cfg, ctrla, ctrlb);
244         dmac_start(I2S_DMAC_CH);
245
246         SSC_CR = BV(SSC_TXEN);
247 }
248
249 static void sam3_i2s_rxBuf(struct I2s *i2s, void *buf, size_t len)
250 {
251         (void)i2s;
252
253         uint32_t cfg = BV(DMAC_CFG_SRC_H2SEL) |
254                                 ((4 << DMAC_CFG_DST_PER_SHIFT) & DMAC_CFG_DST_PER_MASK) | (4 & DMAC_CFG_SRC_PER_MASK);
255         uint32_t ctrla = DMAC_CTRLA_SRC_WIDTH_HALF_WORD | DMAC_CTRLA_DST_WIDTH_HALF_WORD;
256         uint32_t ctrlb = BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) |
257                                                 DMAC_CTRLB_FC_PER2MEM_DMA_FC |
258                                                 DMAC_CTRLB_DST_INCR_INCREMENTING | DMAC_CTRLB_SRC_INCR_FIXED;
259
260         dmac_setSources(I2S_DMAC_CH, (uint32_t)&SSC_RHR, (uint32_t)buf);
261         dmac_configureDmac(I2S_DMAC_CH, len / 2, cfg, ctrla, ctrlb);
262         dmac_start(I2S_DMAC_CH);
263
264         SSC_CR = BV(SSC_RXEN);
265 }
266
267 static int sam3_i2s_write(struct I2s *i2s, uint32_t sample)
268 {
269         (void)i2s;
270         SSC_CR = BV(SSC_TXEN);
271         while(!(SSC_SR & BV(SSC_TXRDY)));
272         SSC_THR = sample;
273         return 0;
274 }
275
276 static uint32_t sam3_i2s_read(struct I2s *i2s)
277 {
278         (void)i2s;
279         SSC_CR = BV(SSC_RXEN);
280         while(!(SSC_SR & BV(SSC_RXRDY)));
281         return SSC_RHR;
282 }
283
284
285 static DECLARE_ISR(irq_ssc)
286 {
287 }
288
289
290
291
292 /* We divite for 2 because the min clock for i2s i MCLK/2 */
293 #define MCK_DIV     (CPU_FREQ / (CONFIG_SAMPLE_FREQ * CONFIG_WORD_BIT_SIZE * CONFIG_CHANNEL_NUM * 2))
294 #define DATALEN     ((CONFIG_WORD_BIT_SIZE - 1) & SSC_DATLEN_MASK)
295 #define DELAY       ((CONFIG_DELAY << SSC_STTDLY_SHIFT) & SSC_STTDLY_MASK)
296 #define PERIOD      ((CONFIG_PERIOD << (SSC_PERIOD_SHIFT)) & SSC_PERIOD_MASK)
297 #define DATNB       ((CONFIG_WORD_PER_FRAME << SSC_DATNB_SHIFT) & SSC_DATNB_MASK)
298 #define FSLEN       ((CONFIG_FRAME_SYNC_SIZE << SSC_FSLEN_SHIFT) & SSC_FSLEN_MASK)
299 #define EXTRA_FSLEN (CONFIG_EXTRA_FRAME_SYNC_SIZE << SSC_FSLEN_EXT)
300
301 void i2s_init(I2s *i2s, int channel)
302 {
303         (void)channel;
304         i2s->ctx.write = sam3_i2s_write;
305         i2s->ctx.tx_buf = sam3_i2s_txBuf;
306         i2s->ctx.tx_isFinish = sam3_i2s_isTxFinish;
307         i2s->ctx.tx_start = sam3_i2s_txStart;
308         i2s->ctx.tx_wait = sam3_i2s_txWait;
309         i2s->ctx.tx_stop = sam3_i2s_txStop;
310
311         i2s->ctx.read = sam3_i2s_read;
312         i2s->ctx.rx_buf = sam3_i2s_rxBuf;
313         i2s->ctx.rx_isFinish = sam3_i2s_isRxFinish;
314         i2s->ctx.rx_start = sam3_i2s_rxStart;
315         i2s->ctx.rx_wait = sam3_i2s_rxWait;
316         i2s->ctx.rx_stop = sam3_i2s_rxStop;
317
318         DB(i2s->ctx._type = I2S_SAM3X;)
319         i2s->hw = &i2s_hw;
320
321         PIOA_PDR = BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD);
322         PIO_PERIPH_SEL(PIOA_BASE, BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD), PIO_PERIPH_B);
323         PIOB_PDR = BV(SSC_RD) | BV(SSC_RF);
324         PIO_PERIPH_SEL(PIOB_BASE, BV(SSC_RD) | BV(SSC_RF), PIO_PERIPH_A);
325
326         /* clock the ssc */
327         pmc_periphEnable(SSC_ID);
328
329         /* reset device */
330         SSC_CR = BV(SSC_SWRST) | BV(SSC_TXDIS) | BV(SSC_RXDIS);
331
332         /* Set transmission clock */
333         SSC_CMR = MCK_DIV & SSC_DIV_MASK;
334         /* Set the transmission mode:
335          * - the clk is generate from master clock
336          * - clock only during transfer
337          * - transmit Clock Gating Selection none
338          * - DELAY cycle insert before starting transmission
339          * - generate frame sync each 2*(PERIOD + 1) tramit clock
340          * - Receive start on falling edge RF
341          */
342         SSC_TCMR = SSC_CKS_DIV | SSC_CKO_CONT | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_FALL_F;
343         /* Set the transmission frame mode:
344          * - data len DATALEN + 1
345          * - word per frame DATNB + 1
346          * - frame sync len FSLEN + (FSLEN_EXT * 16) + 1
347          * - DELAY cycle insert before starting transmission
348          * - MSB
349          * - Frame sync output selection negative
350          */
351         SSC_TFMR = DATALEN | DATNB | FSLEN | EXTRA_FSLEN | BV(SSC_MSBF) | SSC_FSOS_NEGATIVE;
352
353
354         // Receiver should start on TX and take the clock from TK
355     SSC_RCMR = SSC_CKS_CLK | BV(SSC_CKI) | SSC_CKO_CONT | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_TX;
356     SSC_RFMR = DATALEN | DATNB | FSLEN  | EXTRA_FSLEN | BV(SSC_MSBF) | SSC_FSOS_NEGATIVE;
357
358
359         SSC_IDR = 0xFFFFFFFF;
360
361         dmac_enableCh(I2S_DMAC_CH, i2s_dmac_irq);
362         event_initGeneric(&data_ready);
363 }