Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / drv / i2s.h
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  *
33  * \defgroup i2s Generic I2S driver
34  * \ingroup drivers
35  * \{
36  * \brief
37  *
38  * <b>Configuration file</b>: cfg_i2s.h
39  *
40  * \author Daniele Basile <asterix@develer.com>
41  *
42  * $WIZ$ module_name = "i2s"
43  * $WIZ$ module_configuration = "bertos/cfg/cfg_i2s.h"
44  * $WIZ$ module_supports = "not all"
45  */
46
47
48 #ifndef DRV_I2S_H
49 #define DRV_I2S_H
50
51 #warning __FILTER_NEXT_WARNING__
52 #warning This API is ALPHA! we could change it..
53
54 #include <cfg/compiler.h>
55 #include <cfg/debug.h>
56 #include <cfg/macros.h>
57
58 #include <cpu/attr.h>
59
60 #include CPU_HEADER(i2s)
61
62 struct I2sContext;
63 struct I2s;
64
65 typedef int (*i2s_write_t) (struct I2s *i2s, uint32_t sample);
66 typedef uint32_t (*i2s_read_t) (struct I2s *i2s);
67 typedef void (*i2s_dma_tx_buf_t) (struct I2s *i2s, void *buf, size_t len);
68 typedef void (*i2s_dma_rx_buf_t) (struct I2s *i2s, void *buf, size_t len);
69 typedef bool (*i2s_dma_tx_is_finished_t) (struct I2s *i2s);
70 typedef bool (*i2s_dma_rx_is_finished_t) (struct I2s *i2s);
71 typedef void (*i2s_dma_callback_t) (struct I2s *i2s, void *_buf, size_t len);
72 typedef void (*i2s_dma_start_streaming_t) (struct I2s *i2s, void *buf, size_t len, size_t slice_len);
73 typedef void (*i2s_dma_wait_t) (struct I2s *i2s);
74 typedef void (*i2s_dma_stop_t) (struct I2s *i2s);
75
76 typedef struct I2sContext
77 {
78         i2s_write_t                write;
79         i2s_dma_tx_buf_t           tx_buf;
80         i2s_dma_tx_is_finished_t   tx_isFinish;
81         i2s_dma_callback_t         tx_callback;
82         i2s_dma_start_streaming_t  tx_start;
83         i2s_dma_wait_t             tx_wait;
84         i2s_dma_stop_t             tx_stop;
85         size_t tx_slice_len;
86
87         i2s_read_t                 read;
88         i2s_dma_rx_buf_t           rx_buf;
89         i2s_dma_rx_is_finished_t   rx_isFinish;
90         i2s_dma_callback_t         rx_callback;
91         i2s_dma_start_streaming_t  rx_start;
92         i2s_dma_wait_t             rx_wait;
93         i2s_dma_stop_t             rx_stop;
94         size_t rx_slice_len;
95
96         DB(id_t _type);
97
98 } I2sContext;
99
100 typedef struct I2s
101 {
102         I2sContext ctx;
103         struct I2sHardware *hw;
104 } I2s;
105
106 INLINE int i2s_write(I2s *i2s, uint32_t sample)
107 {
108         ASSERT(i2s->ctx.write);
109         return i2s->ctx.write(i2s, sample);
110 }
111
112
113 INLINE uint32_t i2s_read(I2s *i2s)
114 {
115         ASSERT(i2s->ctx.read);
116         return i2s->ctx.read(i2s);
117 }
118
119 /*
120  * Check if a dma transfer is finished.
121  *
122  * Useful for kernel-less applications.
123  */
124 INLINE bool i2s_dmaTxIsFinished(I2s *i2s)
125 {
126         ASSERT(i2s->ctx.tx_isFinish);
127         return i2s->ctx.tx_isFinish(i2s);
128 }
129
130 INLINE bool i2s_dmaRxIsFinished(I2s *i2s)
131 {
132         ASSERT(i2s->ctx.rx_isFinish);
133         return i2s->ctx.rx_isFinish(i2s);
134 }
135
136 INLINE void i2s_dmaTxBuffer(I2s *i2s, void *buf, size_t len)
137 {
138         ASSERT(i2s->ctx.tx_buf);
139         i2s->ctx.tx_buf(i2s, buf, len);
140 }
141
142 INLINE void i2s_dmaRxBuffer(I2s *i2s, void *buf, size_t len)
143 {
144         ASSERT(i2s->ctx.rx_buf);
145         i2s->ctx.rx_buf(i2s, buf, len);
146 }
147
148
149 INLINE void i2s_dmaTxWait(I2s *i2s)
150 {
151         ASSERT(i2s->ctx.tx_wait);
152         i2s->ctx.tx_wait(i2s);
153 }
154
155
156 INLINE void i2s_dmaStartTxStreaming(I2s *i2s, void *buf, size_t len, size_t slice_len, i2s_dma_callback_t callback)
157 {
158         ASSERT(i2s->ctx.tx_start);
159         ASSERT(len % slice_len == 0);
160         ASSERT(callback);
161
162         i2s->ctx.tx_callback = callback;
163         i2s->ctx.tx_slice_len = slice_len;
164         i2s->ctx.tx_start(i2s, buf, len, slice_len);
165 }
166
167 INLINE void i2s_dmaTxStop(I2s *i2s)
168 {
169         ASSERT(i2s->ctx.tx_stop);
170         i2s->ctx.tx_stop(i2s);
171 }
172
173 INLINE void i2s_dmaStartRxStreaming(I2s *i2s, void *buf, size_t len, size_t slice_len, i2s_dma_callback_t callback)
174 {
175         ASSERT(i2s->ctx.rx_start);
176         ASSERT(len % slice_len == 0);
177         ASSERT(callback);
178
179         i2s->ctx.rx_callback = callback;
180         i2s->ctx.rx_slice_len = slice_len;
181         i2s->ctx.rx_start(i2s, buf, len, slice_len);
182 }
183
184 INLINE void i2s_dmaRxStop(I2s *i2s)
185 {
186         ASSERT(i2s->ctx.rx_stop);
187         i2s->ctx.rx_stop(i2s);
188 }
189
190 void i2s_init(I2s *i2s, int channel);
191
192 /** \} */ //defgroup i2s
193 #endif /* DRV_I2S_H */