Add first implentatio of ssc module for sam3x.
[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 /*
39  * TODO: Revise the public api of this module to be more generic. Evalutate to
40  * implement the more generic layer to be common to all I2S BeRTOS drivers.
41  */
42 #include "i2s_sam3.h"
43 #include "cfg/cfg_i2s.h"
44
45 // Define log settings for cfg/log.h.
46 #define LOG_LEVEL         I2S_LOG_LEVEL
47 #define LOG_FORMAT        I2S_LOG_FORMAT
48 #include <cfg/log.h>
49
50 #include <drv/timer.h>
51 #include <drv/irq_cm3.h>
52
53 #include <cpu/irq.h>
54
55 #include <io/cm3.h>
56
57 #define DATALEN (15 & SSC_DATLEN_MASK)
58 // FIXME: this is not correct for 16 <= DATALEN < 24
59 #define PDC_DIV ((DATALEN / 8) + 1)
60 /*
61  * PDC_DIV must be 1, 2 or 4, which are the bytes that are transferred
62  * each time the PDC reads from memory.
63  */
64 STATIC_ASSERT(PDC_DIV % 2 == 0);
65 #define PDC_COUNT (CONFIG_PLAY_BUF_LEN / PDC_DIV)
66
67 static uint8_t play_buf1[CONFIG_PLAY_BUF_LEN];
68 static uint8_t play_buf2[CONFIG_PLAY_BUF_LEN];
69
70 // the buffer in PDC next is play_buf2
71 volatile bool is_second_buf_next;
72
73 uint8_t *i2s_getBuffer(unsigned buf_num)
74 {
75         LOG_INFO("getBuffer start\n");
76
77         if (i2s_isPlaying())
78         {
79                 ASSERT(0);
80                 return 0;
81         }
82
83         if (buf_num == I2S_SECOND_BUF)
84                 return play_buf2;
85         else if (buf_num == I2S_FIRST_BUF)
86                 return play_buf1;
87         else
88                 return 0;
89 }
90
91 uint8_t *i2s_getFreeBuffer(void)
92 {
93         // wait PDC transmission end
94         if (!(SSC_SR & BV(SSC_ENDTX)))
95                 return 0;
96
97         uint8_t *ret_buf = 0;
98         // the last time we got called, the second buffer was in PDC next
99         if (is_second_buf_next)
100         {
101                 is_second_buf_next = false;
102                 ret_buf = play_buf1;
103         }
104         // the last time the first buffer was in PDC next
105         else
106         {
107                 is_second_buf_next = true;
108                 ret_buf = play_buf2;
109         }
110
111         if (ret_buf)
112         {
113                 SSC_TNPR = (reg32_t) ret_buf;
114                 SSC_TNCR = PDC_COUNT;
115         }
116         return ret_buf;
117 }
118
119 void i2s_stop(void)
120 {
121         SSC_CR = BV(SSC_TXDIS);
122 }
123
124
125 bool i2s_start(void)
126 {
127         /* Some time must pass between disabling and enabling again the transmission
128          * on SSC. A good empirical value seems >15 us. We try to avoid putting an
129          * explicit delay, instead we disable the transmitter when a sound finishes
130          * and hope that the delay has passed before we enter here again.
131          */
132         SSC_CR = BV(SSC_TXDIS);
133         timer_delay(10);
134
135         SSC_PTCR = BV(PDC_PTCR_TXTDIS);
136         SSC_TPR = (reg32_t)play_buf1;
137         SSC_TCR = PDC_COUNT;
138         SSC_TNPR = (reg32_t)play_buf2;
139         SSC_TNCR = PDC_COUNT;
140         is_second_buf_next = true;
141
142         SSC_PTCR = BV(PDC_PTSR_TXTEN);
143
144         /* enable output */
145         SSC_CR = BV(SSC_TXEN);
146
147         return true;
148 }
149
150 #define BITS_PER_CHANNEL 16
151 #define N_OF_CHANNEL 2
152 // TODO: check the computed value?
153 /* The last parameter (2) is due to the hadware on at91sam7s. */
154 #define MCK_DIV (CPU_FREQ / CONFIG_SAMPLE_FREQ / BITS_PER_CHANNEL / N_OF_CHANNEL / 2)
155
156 #define CONFIG_DELAY 1
157 #define CONFIG_PERIOD 15
158 #define CONFIG_DATNB  1
159 #define CONFIG_FSLEN 15
160
161 #define DELAY ((CONFIG_DELAY << SSC_STTDLY_SHIFT) & SSC_STTDLY_MASK)
162 #define PERIOD ((CONFIG_PERIOD << (SSC_PERIOD_SHIFT)) & SSC_PERIOD_MASK)
163 #define DATNB ((CONFIG_DATNB << SSC_DATNB_SHIFT) & SSC_DATNB_MASK)
164 #define FSLEN ((CONFIG_FSLEN << SSC_FSLEN_SHIFT) & SSC_FSLEN_MASK)
165
166 #define SSC_DMA_IRQ_PRIORITY 5
167
168
169 static DECLARE_ISR(irq_ssc)
170 {
171 }
172
173 void i2s_init(void)
174 {
175         SSC_PIO_PDR = BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD);
176         PIO_PERIPH_SEL(SSC_PORT, BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD), SSC_TRAN_PERIPH);
177
178         /* reset device */
179         SSC_CR = BV(SSC_SWRST);
180
181         /* Set transmission clock */
182         SSC_CMR = MCK_DIV & SSC_DIV_MASK;
183         /* Set the transmission mode:
184          * - the clk is generate from master clock
185          * - clock only during transfer
186          * - transmit Clock Gating Selection none
187          * - DELAY cycle insert before starting transmission
188          * - generate frame sync each 2*(PERIOD + 1) tramit clock
189          * - Receive start on falling edge RF
190          */
191         SSC_TCMR = SSC_CKS_DIV | SSC_CKO_TRAN | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_FALL_F;
192         /* Set the transmission frame mode:
193          * - data len DATALEN + 1
194          * - word per frame DATNB + 1
195          * - frame sync len FSLEN + (FSLEN_EXT * 16) + 1
196          * - DELAY cycle insert before starting transmission
197          * - MSB
198          * - Frame sync output selection negative
199          */
200         SSC_TFMR = DATALEN | DATNB | FSLEN | BV(SSC_MSBF) | SSC_FSOS_NEGATIVE;
201
202         SSC_IDR = 0xFFFFFFFF;
203         sysirq_setHandler(INT_SSC, irq_ssc);
204
205         /* Clock DAC peripheral */
206         pmc_periphEnable(SSC_ID);
207
208         /* Enable SSC */
209         SSC_CR = BV(SSC_TXEN);
210 }