bdcd98527bfb035439e871f1b62f88cd6331035e
[bertos.git] / bertos / cpu / arm / drv / i2s_at91.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 2009 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief I2S driver implementation.
33  *
34  * \version $Id$
35  * \author Luca Ottaviano <lottaviano@develer.com>
36  */
37
38 #include "i2s.h"
39
40 #include <drv/timer.h>
41 #include <cfg/log.h>
42 #include <io/arm.h>
43
44 #define DATALEN (15 & SSC_DATLEN_MASK)
45 // FIXME: this is not correct for 16 <= DATALEN < 24
46 #define PDC_DIV ((DATALEN / 8) + 1)
47 /* PDC_DIV must be 1, 2 or 4, which are the bytes that are transferred
48  * each time the PDC reads from memory.
49  */
50 STATIC_ASSERT(PDC_DIV % 2 == 0);
51 #define PDC_COUNT (CONFIG_PLAY_BUF_LEN / PDC_DIV)
52
53 static uint8_t play_buf1[CONFIG_PLAY_BUF_LEN];
54 static uint8_t play_buf2[CONFIG_PLAY_BUF_LEN];
55
56 // the buffer in PDC next is play_buf2
57 volatile bool is_second_buf_next;
58
59 uint8_t *i2s_getBuffer(unsigned buf_num)
60 {
61         LOG_INFO("getBuffer start\n");
62
63         if (i2s_isPlaying())
64         {
65                 ASSERT(0);
66                 return 0;
67         }
68
69         if (buf_num == I2S_SECOND_BUF)
70                 return play_buf2;
71         else if (buf_num == I2S_FIRST_BUF)
72                 return play_buf1;
73         else
74                 return 0;
75 }
76
77 uint8_t *i2s_getFreeBuffer(void)
78 {
79         if (!i2s_isPlaying())
80         {
81                 ASSERT(0);
82                 return 0;
83         }
84
85         // wait PDC transmission end
86         if (!(SSC_SR & BV(SSC_ENDTX)))
87                 return 0;
88
89         uint8_t *ret_buf = 0;
90         // the last time we got called, the second buffer was in PDC next
91         if (is_second_buf_next)
92         {
93                 is_second_buf_next = false;
94                 ret_buf = play_buf1;
95         }
96         // the last time the first buffer was in PDC next
97         else
98         {
99                 is_second_buf_next = true;
100                 ret_buf = play_buf2;
101         }
102
103         if (ret_buf)
104         {
105                 SSC_TNPR = (reg32_t) ret_buf;
106                 SSC_TNCR = PDC_COUNT;
107         }
108         return ret_buf;
109 }
110
111 bool i2s_start(void)
112 {
113         /* Some time must pass between disabling and enabling again the transmission
114          * on SSC. A good empirical value seems >15 us. We try to avoid putting an
115          * explicit delay, instead we disable the transmitter when a sound finishes
116          * and hope that the delay has passed before we enter here again.
117          */
118         SSC_CR = BV(SSC_TXDIS);
119         timer_delay(10);
120
121         SSC_PTCR = BV(PDC_TXTDIS);
122         SSC_TPR = (reg32_t)play_buf1;
123         SSC_TCR = PDC_COUNT;
124         SSC_TNPR = (reg32_t)play_buf2;
125         SSC_TNCR = PDC_COUNT;
126         is_second_buf_next = true;
127
128         SSC_PTCR = BV(PDC_TXTEN);
129
130         /* enable output */
131         SSC_CR = BV(SSC_TXEN);
132
133         return true;
134 }
135
136 #define CONFIG_SAMPLE_FREQ 44100
137 #define BITS_PER_CHANNEL 16
138 #define N_OF_CHANNEL 2
139 // TODO: check the computed value?
140 /* The last parameter (2) is due to the hadware on at91sam7s. */
141 #define MCK_DIV (CPU_FREQ / CONFIG_SAMPLE_FREQ / BITS_PER_CHANNEL / N_OF_CHANNEL / 2)
142
143 #define CONFIG_DELAY 1
144 #define CONFIG_PERIOD 15
145 #define CONFIG_DATNB  1
146 #define CONFIG_FSLEN 15
147
148 #define DELAY ((CONFIG_DELAY << SSC_STTDLY_SHIFT) & SSC_STTDLY_MASK)
149 #define PERIOD ((CONFIG_PERIOD << (SSC_PERIOD_SHIFT)) & SSC_PERIOD_MASK)
150 #define DATNB ((CONFIG_DATNB << SSC_DATNB_SHIFT) & SSC_DATNB_MASK)
151 #define FSLEN ((CONFIG_FSLEN << SSC_FSLEN_SHIFT) & SSC_FSLEN_MASK)
152
153 #define SSC_DMA_IRQ_PRIORITY 5
154
155 void i2s_init(void)
156 {
157         PIOA_PDR = BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD);
158         /* reset device */
159         SSC_CR = BV(SSC_SWRST);
160
161         SSC_CMR = MCK_DIV & SSC_DIV_MASK;
162         SSC_TCMR = SSC_CKS_DIV | SSC_CKO_CONT | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_FALL_F;
163         SSC_TFMR = DATALEN | DATNB | FSLEN | BV(SSC_MSBF) | SSC_FSOS_NEGATIVE;
164
165         /* Disable all irqs */
166         SSC_IDR = 0xFFFFFFFF;
167
168         /* Enable the SSC IRQ */
169         AIC_IECR = BV(SSC_ID);
170
171         /* enable i2s */
172         PMC_PCER = BV(SSC_ID);
173
174         /* Enable SSC */
175         SSC_CR = BV(SSC_TXEN);
176 }