4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
32 * \brief I2S driver implementation.
35 * \author Luca Ottaviano <lottaviano@develer.com>
40 #include <drv/timer.h>
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.
50 STATIC_ASSERT(PDC_DIV % 2 == 0);
51 #define PDC_COUNT (CONFIG_PLAY_BUF_LEN / PDC_DIV)
53 static uint8_t play_buf1[CONFIG_PLAY_BUF_LEN];
54 static uint8_t play_buf2[CONFIG_PLAY_BUF_LEN];
56 // the buffer in PDC next is play_buf2
57 volatile bool is_second_buf_next;
59 uint8_t *i2s_getBuffer(unsigned buf_num)
61 LOG_INFO("getBuffer start\n");
69 if (buf_num == I2S_SECOND_BUF)
71 else if (buf_num == I2S_FIRST_BUF)
77 uint8_t *i2s_getFreeBuffer(void)
85 // wait PDC transmission end
86 if (!(SSC_SR & BV(SSC_ENDTX)))
90 // the last time we got called, the second buffer was in PDC next
91 if (is_second_buf_next)
93 is_second_buf_next = false;
96 // the last time the first buffer was in PDC next
99 is_second_buf_next = true;
105 SSC_TNPR = (reg32_t) ret_buf;
106 SSC_TNCR = PDC_COUNT;
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.
118 SSC_CR = BV(SSC_TXDIS);
121 SSC_PTCR = BV(PDC_TXTDIS);
122 SSC_TPR = (reg32_t)play_buf1;
124 SSC_TNPR = (reg32_t)play_buf2;
125 SSC_TNCR = PDC_COUNT;
126 is_second_buf_next = true;
128 SSC_PTCR = BV(PDC_TXTEN);
131 SSC_CR = BV(SSC_TXEN);
136 #define BITS_PER_CHANNEL 16
137 #define N_OF_CHANNEL 2
138 // TODO: check the computed value?
139 /* The last parameter (2) is due to the hadware on at91sam7s. */
140 #define MCK_DIV (CPU_FREQ / CONFIG_SAMPLE_FREQ / BITS_PER_CHANNEL / N_OF_CHANNEL / 2)
142 #define CONFIG_DELAY 1
143 #define CONFIG_PERIOD 15
144 #define CONFIG_DATNB 1
145 #define CONFIG_FSLEN 15
147 #define DELAY ((CONFIG_DELAY << SSC_STTDLY_SHIFT) & SSC_STTDLY_MASK)
148 #define PERIOD ((CONFIG_PERIOD << (SSC_PERIOD_SHIFT)) & SSC_PERIOD_MASK)
149 #define DATNB ((CONFIG_DATNB << SSC_DATNB_SHIFT) & SSC_DATNB_MASK)
150 #define FSLEN ((CONFIG_FSLEN << SSC_FSLEN_SHIFT) & SSC_FSLEN_MASK)
152 #define SSC_DMA_IRQ_PRIORITY 5
156 PIOA_PDR = BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD);
158 SSC_CR = BV(SSC_SWRST);
160 SSC_CMR = MCK_DIV & SSC_DIV_MASK;
161 SSC_TCMR = SSC_CKS_DIV | SSC_CKO_CONT | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_FALL_F;
162 SSC_TFMR = DATALEN | DATNB | FSLEN | BV(SSC_MSBF) | SSC_FSOS_NEGATIVE;
164 /* Disable all irqs */
165 SSC_IDR = 0xFFFFFFFF;
167 /* Enable the SSC IRQ */
168 AIC_IECR = BV(SSC_ID);
171 PMC_PCER = BV(SSC_ID);
174 SSC_CR = BV(SSC_TXEN);