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