aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/SAMA/SAMA5D2x/sama_classd.h
blob: 212e4fab62c803a5e57dff4e747fba1be629ad0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    SAMA5D2x/sama_classd.h
 * @brief   SAMA CLASSD support macros and structures.
 *
 * @addtogroup SAMA5D2x_CLASSD
 * @{
 */

#ifndef SAMA_CLASSD_LLD_H
#define SAMA_CLASSD_LLD_H

/**
 * @brief   Using the CLASSD driver.
 */
#if !defined(SAMA_USE_CLASSD) || defined(__DOXYGEN__)
#define SAMA_USE_CLASSD                          FALSE
#endif

#if SAMA_USE_CLASSD || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver constants.                                                         */
/*===========================================================================*/

/*===========================================================================*/
/* Driver pre-compile time settings.                                         */
/*===========================================================================*/

/**
 * @name    Configuration options
 * @{
 */

/**
 * @brief   CLASSD DMA interrupt priority level setting.
 */
#if !defined(SAMA_CLASSD_DMA_IRQ_PRIORITY) || defined(__DOXYGEN__)
#define SAMA_CLASSD_DMA_IRQ_PRIORITY               4
#endif

/** @} */

/*===========================================================================*/
/* Derived constants and error checks.                                       */
/*===========================================================================*/

#if !defined(SAMA_DMA_REQUIRED)
#define SAMA_DMA_REQUIRED
#endif

/*===========================================================================*/
/* Driver data structures and types.                                         */
/*===========================================================================*/

/**
 * @brief   Driver state machine possible states.
 */
typedef enum {
  CLASSD_UNINIT = 0,                /**< Not initialized.                   */
  CLASSD_STOP = 1,                  /**< Stopped.                           */
  CLASSD_READY = 2,                 /**< Ready.                             */
  CLASSD_ACTIVE = 3,                /**< Exchanging data.                   */
} classdstate_t;

/**
 * @brief   Structure representing audio info.
 */
static const struct {
  /**
   * @brief   Contains the value of the Sample Rate.
   */
  uint32_t rate;
  /**
   * @brief   Contains a mask of the Sample Rate.
   */
  uint32_t sample_rate;
  /**
   * @brief   Contains a mask of the DSP Clock.
   */
  uint32_t dsp_clk;
} audio_info[] = {
    {8000,  CLASSD_INTPMR_FRAME_FRAME_8K, CLASSD_INTPMR_DSPCLKFREQ_12M288},
    {16000, CLASSD_INTPMR_FRAME_FRAME_16K, CLASSD_INTPMR_DSPCLKFREQ_12M288},
    {32000, CLASSD_INTPMR_FRAME_FRAME_32K, CLASSD_INTPMR_DSPCLKFREQ_12M288},
    {48000, CLASSD_INTPMR_FRAME_FRAME_48K, CLASSD_INTPMR_DSPCLKFREQ_12M288},
    {96000, CLASSD_INTPMR_FRAME_FRAME_96K, CLASSD_INTPMR_DSPCLKFREQ_12M288},
    {22050, CLASSD_INTPMR_FRAME_FRAME_22K, CLASSD_INTPMR_DSPCLKFREQ_11M2896},
    {44100, CLASSD_INTPMR_FRAME_FRAME_44K, CLASSD_INTPMR_DSPCLKFREQ_11M2896},
    {88200, CLASSD_INTPMR_FRAME_FRAME_88K, CLASSD_INTPMR_DSPCLKFREQ_11M2896},
};

/**
 * @brief   Type of a structure representing Standard WAV file header information.
 */
struct wav_header {
  /**
   * @brief   Contains the letters "RIFF" in ASCII form.
   */
  uint32_t                  chunk_id;
  /**
   * @brief   Size of the rest of the chunk following this number.
   */
  uint32_t                  chunk_size;
  /**
   * @brief   Contains the letters "WAVE".
   */
  uint32_t                  format;
  /**
   * @brief   Contains the letters "fmt ".
   */
  uint32_t                  subchunk1_id;
  /**
   * @brief   16 for PCM.  This is the size of the rest of the Subchunk which follows this number.
   */
  uint32_t                  subchunk1_size;
  /**
   * @brief   PCM = 1 (i.e. Linear quantization). Values other than 1 indicate some form of compression.
   */
  uint16_t                  audio_format;
  /**
   * @brief   Mono = 1, Stereo = 2, etc.
   */
  uint16_t                  num_channels;
  /**
   * @brief   8000, 44100, etc.
   */
  uint32_t                  sample_rate;
  /**
   * @brief   SampleRate * NumChannels * BitsPerSample/8
   */
  uint32_t                  byte_rate;
  /**
   * @brief   NumChannels * BitsPerSample/8
   */
  uint16_t                  block_align;
  /**
   * @brief   8 bits = 8, 16 bits = 16, etc.
   */
  uint16_t                  bits_per_sample;
  /**
   * @brief   Contains the letters "data".
   */
  uint32_t                  subchunk2_id;
  /**
   * @brief   Number of bytes in the data.
   */
  uint32_t                  subchunk2_size;
};

/**
 * @brief   Type of a structure representing an CLASSD driver.
 */
typedef struct CLASSDDriver CLASSDDriver;

/**
  * @brief   Type of a generic CLASSD callback.
  */
typedef void (*classdcb_t)(CLASSDDriver *classdp);

/**
 * @brief   Driver configuration structure.
 * @note    It could be empty on some architectures.
 */
typedef struct {
  /**
   * @brief   Callback pointer.
   */
  classdcb_t                callback;
  /**
   * @brief   Configuration of the CLASSD left channel.
   */
  uint32_t                  left;
  /**
   * @brief   Configuration of the CLASSD right channel.
   */
  uint32_t                  right;
  /**
   * @brief   Configuration of the CLASSD left channel mute.
   */
  uint32_t                  left_mute;
  /**
   * @brief   Configuration of the CLASSD right channel mute.
   */
  uint32_t                  right_mute;
  /**
   * @brief   Configuration of the CLASSD PWM modulation type.
   */
  uint32_t                  pwm_mode;
  /**
   * @brief   Configuration of the CLASSD Non-Overlapping.
   */
  uint32_t                  non_overlap;
  /**
   * @brief   Configuration of the CLASSD Non-Overlapping value.
   */
  uint32_t                  novrval;
  /**
   * @brief   Configuration of the CLASSD left channel attenuation.
   */
  uint32_t                  attl;
  /**
   * @brief   Configuration of the CLASSD right channel attenuation.
   */
  uint32_t                  attr;
  /**
   * @brief   Configuration of the CLASSD de-emphasis filter.
   */
  uint32_t                  deemp;
  /**
   * @brief   Configuration of the CLASSD swap left right channel.
   */
  uint32_t                  swap;
  /**
   * @brief   Configuration of the CLASSD sample frequency.
   */
  uint32_t                  frame;
  /**
   * @brief   Configuration of the CLASSD EQ config.
   */
  uint32_t                  eqcfg;
  /**
   * @brief   Configuration of the CLASSD mono signal.
   */
  uint32_t                  mono;
  /**
   * @brief   Configuration of the CLASSD mono mode.
   */
  uint32_t                  mono_mode;
} CLASSDConfig;

/**
 * @brief   Structure representing an CLASSD driver.
 */
struct CLASSDDriver {
  /**
   * @brief   Driver state.
   */
  classdstate_t             state;
  /**
   * @brief   Current configuration data.
   */
  const CLASSDConfig        *config;
  /**
   * @brief   Pointer to the WDT registers block.
   */
  Classd                    *classd;
  /**
   * @brief   Transmit DMA stream.
   */
  sama_dma_channel_t        *dmatx;
  /**
   * @brief   TX DMA mode bit mask.
   */
  uint32_t                  txdmamode;
};

/*===========================================================================*/
/* Driver macros.                                                            */
/*===========================================================================*/

/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/
extern CLASSDDriver CLASSDD0;

#ifdef __cplusplus
extern "C" {
#endif
  void classdInit(void);
  void classdObjectInit(CLASSDDriver *classdp);
  void classdStart(CLASSDDriver *classdp, const CLASSDConfig *config);
  void classdStop(CLASSDDriver *classdp);
  void classdSetSampleFrame(CLASSDConfig *classdconfigp, uint8_t *music_file);
  void classdSendAudioI(CLASSDDriver *classdp, const void *txbuf);
  void classdSendAudio(CLASSDDriver *classdp, const void *txbuf);
  void classdMuteChannel(CLASSDDriver *classdp, bool left, bool right);
#ifdef __cplusplus
}
#endif

#endif /* SAMA_USE_CLASSD */

#endif /* SAMA_CLASSD_LLD_H */

/** @} */