129c94941216b8cc0d8e05c6bc77bd70a11e2c5e
[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
58
59 #define DATALEN (15 & SSC_DATLEN_MASK)
60 #define BITS_PER_CHANNEL 16
61 #define N_OF_CHANNEL 2
62 // TODO: check the computed value?
63 /* The last parameter (2) is due to the hadware on at91sam7s. */
64 #define MCK_DIV (CPU_FREQ / (44100 * BITS_PER_CHANNEL * N_OF_CHANNEL* 2))
65
66 #define CONFIG_DELAY 0
67 #define CONFIG_PERIOD 15
68 #define CONFIG_DATNB  1
69 #define CONFIG_FSLEN 15
70
71
72 #define DELAY ((CONFIG_DELAY << SSC_STTDLY_SHIFT) & SSC_STTDLY_MASK)
73 #define PERIOD ((CONFIG_PERIOD << (SSC_PERIOD_SHIFT)) & SSC_PERIOD_MASK)
74 #define DATNB ((CONFIG_DATNB << SSC_DATNB_SHIFT) & SSC_DATNB_MASK)
75 #define FSLEN ((CONFIG_FSLEN << SSC_FSLEN_SHIFT) & SSC_FSLEN_MASK)
76
77
78
79 void i2s_stop(void)
80 {
81         SSC_CR = BV(SSC_TXDIS);
82 }
83
84
85 bool i2s_start(void)
86 {
87
88         /* enable output */
89         SSC_CR = BV(SSC_TXEN);
90
91         return true;
92 }
93
94
95 static DECLARE_ISR(irq_ssc)
96 {
97 }
98
99 void i2s_init(void)
100 {
101         PIOA_PDR = BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD);
102         PIO_PERIPH_SEL(SSC_PORT, BV(SSC_TK) | BV(SSC_TF) | BV(SSC_TD), PIO_PERIPH_B);
103         pmc_periphEnable(SSC_ID);
104
105         /* reset device */
106         SSC_CR = BV(SSC_SWRST) | BV(SSC_TXDIS) | BV(SSC_RXDIS);
107
108         /* Set transmission clock */
109         SSC_CMR = MCK_DIV & SSC_DIV_MASK;
110         /* Set the transmission mode:
111          * - the clk is generate from master clock
112          * - clock only during transfer
113          * - transmit Clock Gating Selection none
114          * - DELAY cycle insert before starting transmission
115          * - generate frame sync each 2*(PERIOD + 1) tramit clock
116          * - Receive start on falling edge RF
117          */
118         SSC_TCMR = SSC_CKS_DIV | SSC_CKO_CONT | SSC_CKG_NONE | DELAY | PERIOD | SSC_START_FALL_F;
119         /* Set the transmission frame mode:
120          * - data len DATALEN + 1
121          * - word per frame DATNB + 1
122          * - frame sync len FSLEN + (FSLEN_EXT * 16) + 1
123          * - DELAY cycle insert before starting transmission
124          * - MSB
125          * - Frame sync output selection negative
126          */
127         SSC_TFMR = DATALEN | DATNB | FSLEN | BV(SSC_MSBF) | SSC_FSOS_POSITIVE;
128 }