ac938562033c6e3608cfd03904cb13187130a804
[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 <cpu/irq.h>
50
51 #include <io/cm3.h>
52
53 struct I2sHardware
54 {
55 };
56
57 struct I2sHardware i2s_hw;
58 static Dmac dmac;
59
60 /* We divite for 2 because the min clock for i2s i MCLK/2 */
61 #define MCK_DIV     (CPU_FREQ / (48000 * CONFIG_WORD_BIT_SIZE * CONFIG_CHANNEL_NUM * 2))
62 #define DATALEN     ((CONFIG_WORD_BIT_SIZE - 1) & SSC_DATLEN_MASK)
63 #define DELAY       ((CONFIG_DELAY << SSC_STTDLY_SHIFT) & SSC_STTDLY_MASK)
64 #define PERIOD      ((CONFIG_PERIOD << (SSC_PERIOD_SHIFT)) & SSC_PERIOD_MASK)
65 #define DATNB       ((CONFIG_WORD_PER_FRAME << SSC_DATNB_SHIFT) & SSC_DATNB_MASK)
66 #define FSLEN       ((CONFIG_FRAME_SYNC_SIZE << SSC_FSLEN_SHIFT) & SSC_FSLEN_MASK)
67 #define EXTRA_FSLEN (CONFIG_EXTRA_FRAME_SYNC_SIZE << SSC_FSLEN_EXT)
68
69
70 static void sam3_i2s_txStop(I2s *i2s)
71 {
72         (void)i2s;
73         SSC_CR = BV(SSC_TXDIS);
74 }
75
76 static void sam3_i2s_txWait(I2s *i2s)
77 {
78         (void)i2s;
79 }
80
81 static void sam3_i2s_txStart(I2s *i2s, void *buf, size_t len, size_t slice_len)
82 {
83         (void)i2s;
84         (void)slice_len;
85 }
86
87 static void sam3_i2s_rxStop(I2s *i2s)
88 {
89         (void)i2s;
90         SSC_CR = BV(SSC_TXDIS);
91 }
92
93 static void sam3_i2s_rxWait(I2s *i2s)
94 {
95         (void)i2s;
96 }
97
98 static void sam3_i2s_rxStart(I2s *i2s, void *buf, size_t len, size_t slice_len)
99 {
100         (void)i2s;
101         (void)buf;
102         (void)len;
103         (void)slice_len;
104 }
105
106
107 static bool sam3_i2s_isTxFinish(struct I2s *i2s)
108 {
109         (void)i2s;
110         return dmac_isDone(&dmac);
111 }
112
113 static bool sam3_i2s_isRxFinish(struct I2s *i2s)
114 {
115         (void)i2s;
116         return dmac_isDone(&dmac);;
117 }
118
119 static void sam3_i2s_txBuf(struct I2s *i2s, void *buf, size_t len)
120 {
121         (void)i2s;
122
123         uint32_t cfg = BV(DMAC_CFG_DST_H2SEL) |
124                                 ((3 << DMAC_CFG_DST_PER_SHIFT) & DMAC_CFG_DST_PER_MASK) | (3 & DMAC_CFG_SRC_PER_MASK);
125         uint32_t ctrla = DMAC_CTRLA_SRC_WIDTH_HALF_WORD | DMAC_CTRLA_DST_WIDTH_HALF_WORD;
126         uint32_t ctrlb = BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) |
127                                 DMAC_CTRLB_FC_MEM2PER_DMA_FC |
128                                 DMAC_CTRLB_DST_INCR_FIXED | DMAC_CTRLB_SRC_INCR_INCREMENTING;
129
130         dmac_setSources(&dmac, (uint32_t)buf, (uint32_t)&SSC_THR);
131         dmac_configureDmac(&dmac, len, cfg, ctrla, ctrlb);
132         dmac_start(&dmac);
133
134         SSC_CR = BV(SSC_TXEN);
135 }
136
137 static void sam3_i2s_rxBuf(struct I2s *i2s, void *buf, size_t len)
138 {
139         (void)i2s;
140
141         uint32_t cfg = BV(DMAC_CFG_SRC_H2SEL) |
142                                 ((3 << DMAC_CFG_DST_PER_SHIFT) & DMAC_CFG_DST_PER_MASK) | (3 & DMAC_CFG_SRC_PER_MASK);
143         uint32_t ctrla = DMAC_CTRLA_SRC_WIDTH_HALF_WORD | DMAC_CTRLA_DST_WIDTH_HALF_WORD;
144         uint32_t ctrlb = BV(DMAC_CTRLB_SRC_DSCR) | BV(DMAC_CTRLB_DST_DSCR) |
145                                                 DMAC_CTRLB_FC_PER2MEM_DMA_FC |
146                                                 DMAC_CTRLB_DST_INCR_INCREMENTING | DMAC_CTRLB_SRC_INCR_FIXED;
147
148         dmac_setSources(&dmac, (uint32_t)&SSC_RHR, (uint32_t)buf);
149         dmac_configureDmac(&dmac, len, cfg, ctrla, ctrlb);
150         dmac_start(&dmac);
151
152         SSC_CR = BV(SSC_RXEN);
153 }
154
155 static int sam3_i2s_write(struct I2s *i2s, uint32_t sample)
156 {
157         (void)i2s;
158         while(!(SSC_SR & BV(SSC_TXRDY)));
159         SSC_THR = sample;
160         return 0;
161 }
162
163 static uint32_t sam3_i2s_read(struct I2s *i2s)
164 {
165         (void)i2s;
166         while(!(SSC_SR & BV(SSC_RXRDY)));
167         return SSC_RHR;
168 }
169
170 /*
171 static DECLARE_ISR(irq_ssc)
172 {
173 }
174 */
175 void i2s_init(I2s *i2s, int channel)
176 {
177         (void)channel;
178         i2s->ctx.write = sam3_i2s_write;
179         i2s->ctx.tx_buf = sam3_i2s_txBuf;
180         i2s->ctx.tx_isFinish = sam3_i2s_isTxFinish;
181         i2s->ctx.tx_start = sam3_i2s_txStart;
182         i2s->ctx.tx_wait = sam3_i2s_txWait;
183         i2s->ctx.tx_stop = sam3_i2s_txStop;
184
185         i2s->ctx.read = sam3_i2s_read;
186         i2s->ctx.rx_buf = sam3_i2s_rxBuf;
187         i2s->ctx.rx_isFinish = sam3_i2s_isRxFinish;
188         i2s->ctx.rx_start = sam3_i2s_rxStart;
189         i2s->ctx.rx_wait = sam3_i2s_rxWait;
190         i2s->ctx.rx_stop = sam3_i2s_rxStop;
191
192         DB(i2s->ctx._type = I2S_SAM3X;)
193         i2s->hw = &i2s_hw;
194
195         PIOA_PDR = BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD);
196         PIO_PERIPH_SEL(PIOA_BASE, BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD), PIO_PERIPH_B);
197         PIOB_PDR = BV(SSC_RD) | BV(SSC_RF);
198         PIO_PERIPH_SEL(PIOB_BASE, BV(SSC_RD) | BV(SSC_RF), PIO_PERIPH_A);
199
200         /* clock the ssc */
201         pmc_periphEnable(SSC_ID);
202
203         /* reset device */
204         SSC_CR = BV(SSC_SWRST) | BV(SSC_TXDIS) | BV(SSC_RXDIS);
205
206         /* Set transmission clock */
207         SSC_CMR = MCK_DIV & SSC_DIV_MASK;
208         /* Set the transmission mode:
209          * - the clk is generate from master clock
210          * - clock only during transfer
211          * - transmit Clock Gating Selection none
212          * - DELAY cycle insert before starting transmission
213          * - generate frame sync each 2*(PERIOD + 1) tramit clock
214          * - Receive start on falling edge RF
215          */
216         SSC_TCMR = SSC_CKS_DIV | SSC_CKO_CONT | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_FALL_F;
217         /* Set the transmission frame mode:
218          * - data len DATALEN + 1
219          * - word per frame DATNB + 1
220          * - frame sync len FSLEN + (FSLEN_EXT * 16) + 1
221          * - DELAY cycle insert before starting transmission
222          * - MSB
223          * - Frame sync output selection negative
224          */
225         SSC_TFMR = DATALEN | DATNB | FSLEN | EXTRA_FSLEN | BV(SSC_MSBF) | SSC_FSOS_POSITIVE;
226
227
228         // Receiver should start on TX and take the clock from TK
229     SSC_RCMR = SSC_CKS_CLK | BV(SSC_CKI) | SSC_CKO_CONT | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_TX;
230     SSC_RFMR = DATALEN | DATNB | FSLEN  | EXTRA_FSLEN | BV(SSC_MSBF) | SSC_FSOS_POSITIVE;
231
232
233         SSC_IDR = 0xFFFFFFFF;
234         SSC_CR = BV(SSC_TXDIS) | BV(SSC_RXDIS);
235
236         dmac_init(&dmac, 1);
237 }