aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/SAMA/SAMA5D2x/sama_classd.c
blob: 37a3ce68911b0e43c81d29f06a2616450d3e3efa (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
    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.c
 * @brief   SAMA CLASSD support code.
 *
 * @addtogroup SAMA5D2x_CLASSD
 * @{
 */

#include "hal.h"

#if SAMA_USE_CLASSD || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

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

/**
 * @brief   Enable write protection on CLASSD Mode Register
 *          and Interpolator Mode Register.
 *
 * @param[in] classdp   pointer to a CLASSD register block
 *
 * @notapi
 */
#define classdEnableWP(classdp) {                                              \
  classdp->CLASSD_WPMR = CLASSD_WPMR_WPKEY_PASSWD | CLASSD_WPMR_WPEN;          \
}

/**
 * @brief   Disable write protection on CLASSD Mode Register
 *          and Interpolator Mode Register.
 *
 * @param[in] classdp   pointer to a CLASSD register block
 *
 * @notapi
 */
#define classdDisableWP(classdp) {                                             \
  classdp->CLASSD_WPMR = CLASSD_WPMR_WPKEY_PASSWD;                             \
}

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/
/**
 * @brief   CLASSD driver identifier.
 */
CLASSDDriver CLASSDD0;

/*===========================================================================*/
/* Driver local variables.                                                   */
/*===========================================================================*/
/**
 * @brief   Type of a structure representing PMC Audio configuration.
 */
typedef struct {
  /**
   * @brief   Loop Divider Ratio.
   */
  uint32_t nd;

  /**
   * @brief   Fractional Loop Divider Setting.
   */
  uint32_t fracr;

  /**
   * @brief   Output Divider Ratio for PMC Clock.
   */
  uint32_t qdpmc;

  /**
   * @brief   Divider Value.
   */
  uint32_t div;

  /**
   * @brief   Output Divider Ratio for Pad Clock.
   */
  uint32_t qdaudio;
} pmcAudioConf;

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/**
 * @brief   Configure DSPClock.
 *
 * @param[in] dsp_clk   DSP clock type (12.288 MHz or 11.2896 MHz).
 */
static void dspclkConfigure(uint32_t dsp_clk) {
  pmcAudioConf cfg;

  /* Pad Clock: not used */
  cfg.div = 0;
  cfg.qdaudio = 0;

  /* PMC Clock: */
  /* 12Mhz * (ND + 1 + FRACR/2^22) / (QDPMC + 1) = 8 * DSPCLK */
  switch (dsp_clk) {
    case CLASSD_INTPMR_DSPCLKFREQ_12M288:
#if SAMA_MOSCXTCLK == 12000000
      /* 12Mhz * (56 + 1 + 1442841/2^22) / (6 + 1) = 8 * 12.288Mhz */
      cfg.nd = 56;
      cfg.fracr = 1442841;
      cfg.qdpmc = 6;
#elif SAMA_MOSCXTCLK == 24000000
      /* 24Mhz * (56 + 1 + 1442841/2^22) / (6 + 1) = 8 * 12.288Mhz */
      cfg.nd = 27;
      cfg.fracr = 2796203;
      cfg.qdpmc = 6;
#else
      #error "FREQUENCY NOT SUPPORTED BY CLASSD"
#endif
    break;

    case CLASSD_INTPMR_DSPCLKFREQ_11M2896:
#if SAMA_MOSCXTCLK == 12000000
      /* 12Mhz * (59 + 1 + 885837/2^22) / (7 + 1) = 8 * 11.2896Mhz */
      cfg.nd = 59;
      cfg.fracr = 885837;
      cfg.qdpmc = 7;
#elif SAMA_MOSCXTCLK == 24000000
      /* 24Mhz * (59 + 1 + 885837/2^22) / (7 + 1) = 8 * 11.2896Mhz */
      cfg.nd = 28;
      cfg.fracr = 699050;
      cfg.qdpmc = 7;
#else
      #error "FREQUENCY NOT SUPPORTED BY CLASSD"
#endif
    break;

    default:
      osalDbgAssert(NULL, "errore mask configuration");
   }

  /* Configure and enable the generic clock. */
  pmcConfigAudio(cfg.nd, cfg.qdpmc, cfg.fracr, cfg.div, cfg.qdaudio);
  pmcEnableAudio(true, false);
}

/**
 * @brief   Shared end-of-tx service routine.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver object
 * @param[in] flags     pre-shifted content of the ISR register
 */
static void classd_lld_serve_tx_interrupt(CLASSDDriver *classdp, uint32_t flags) {

  /* DMA errors handling.*/
#if defined(SAMA_CLASSD_DMA_ERROR_HOOK)
  (void)classdp;
  if ((flags & (XDMAC_CIS_WBEIS | XDMAC_CIS_ROIS)) != 0) {
    SAMA_CLASSD_DMA_ERROR_HOOK(classdp);
  }
#else
  (void)flags;
#endif

  if(classdp->config->callback != NULL) {
    classdp->config->callback(classdp);
  }
  classdMuteChannel(classdp, false, false);
  classdp->state = CLASSD_READY;
}

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Low level CLASSD driver initialization.
 *
 * @notapi
 */
void classd_lld_init(void) {
#if SAMA_HAL_IS_SECURE
  mtxConfigPeriphSecurity(MATRIX1, ID_CLASSD, SECURE_PER);
#endif /* SAMA_HAL_IS_SECURE */
  /* Driver initialization. */
  classdObjectInit(&CLASSDD0);
  CLASSDD0.classd = CLASSD;
  CLASSDD0.dmatx     = NULL;
  CLASSDD0.txdmamode = XDMAC_CC_TYPE_PER_TRAN |
                       XDMAC_CC_MBSIZE_SINGLE |
                       XDMAC_CC_DSYNC_MEM2PER |
                       XDMAC_CC_PROT_SEC |
                       XDMAC_CC_CSIZE_CHK_1 |
                       XDMAC_CC_DWIDTH_WORD |
                       XDMAC_CC_SIF_AHB_IF0 |
                       XDMAC_CC_DIF_AHB_IF1 |
                       XDMAC_CC_SAM_INCREMENTED_AM |
                       XDMAC_CC_DAM_FIXED_AM |
                       XDMAC_CC_PERID(PERID_CLASSD_TX);
}

/**
 * @brief   Configures and activates the CLASSD peripheral.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver object
 *
 * @notapi
 */
void classd_lld_start(CLASSDDriver *classdp) {

  uint8_t i;
  uint32_t dsp_clk_set, frame_set;

 /* Configures the peripheral.*/
  if (classdp->state == CLASSD_STOP) {

    if (&CLASSDD0 == classdp) {
      classdp->dmatx = dmaChannelAllocate(SAMA_CLASSD_DMA_IRQ_PRIORITY,
                                       (sama_dmaisr_t)classd_lld_serve_tx_interrupt,
                                       (void *)classdp);
      osalDbgAssert(classdp->dmatx != NULL, "no channel allocated");
    }
  }

 /* Set DMA channel mode. */
  dmaChannelSetMode(classdp->dmatx, classdp->txdmamode);

  /* Set CLASSD DSP clock and Sample rate. */
  for(i = 0; i < 8; i++) {
    if ((audio_info[i].rate) == (classdp->config->frame)) {
      dsp_clk_set  = audio_info[i].dsp_clk;
      frame_set = audio_info[i].sample_rate;
      break;
    }
  }

  /* Enable the CLASSD0 clock. */
  pmcEnableCLASSD0();

  /* Configure PMC Audio structure. */
  dspclkConfigure(dsp_clk_set);

  /* Disable the CLASSD generic clock for now. */
  pmcDisableGclkCLASSD0();

  /* Configure the CLASSD generic clock */
  pmcConfigGclk(ID_CLASSD, PMC_PCR_GCKCSS_AUDIO_CLK, 1);

  /* Disable write protection. */
  classdDisableWP(classdp->classd);

  /* Perform soft reset. */
  CLASSD->CLASSD_CR  = CLASSD_CR_SWRST;
  CLASSD->CLASSD_IDR = CLASSD_IDR_DATRDY;

  /* Clean CLASSD Registers. */
  classdp->classd->CLASSD_MR = 0;
  classdp->classd->CLASSD_INTPMR = 0;

  /* CLASSD configuration. */
  classdp->classd->CLASSD_MR = classdp->config->left |
                               classdp->config->right |
                               classdp->config->left_mute |
                               classdp->config->right_mute |
                               classdp->config->pwm_mode |
                               classdp->config->non_overlap |
                               classdp->config->novrval;

  classdp->classd->CLASSD_INTPMR = classdp->config->attl |
                                   classdp->config->attr |
                                   classdp->config->deemp |
                                   classdp->config->swap |
                                   classdp->config->eqcfg |
                                   classdp->config->mono |
                                   classdp->config->mono_mode |
                                   dsp_clk_set | frame_set;

  /* Enable CLASSD generic clock. */
  pmcEnableGclkCLASSD0();

  /* Enable write protection. */
  classdEnableWP(classdp->classd);
}

/**
 * @brief   Deactivates the CLASSD peripheral.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver object
 *
 * @notapi
 */
void classd_lld_stop(CLASSDDriver *classdp) {

  /* Disable clocks. */
  pmcDisableAudio();
  pmcDisableGclkCLASSD0();
  pmcDisableCLASSD0();

  /* Disable write protection. */
  classdDisableWP(classdp->classd);

  /* Reset CLASSD. */
  classdp->classd->CLASSD_INTPMR = 0;
  classdp->classd->CLASSD_MR = 0;

  /* Enable write protection. */
  classdEnableWP(classdp->classd);

  /* Release and disable DMA channel. */
  dmaChannelRelease(classdp->dmatx);
}

/**
 *
 * @brief   Starts a CLASSD playback.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @notapi
 */
void classd_lld_send_audio(CLASSDDriver *classdp, const void *txbuf) {

  /* Get DMA transfert size. */
  size_t n = ((struct wav_header *)txbuf)->subchunk2_size / 4;

  osalDbgAssert(!((uint32_t) txbuf & (L1_CACHE_BYTES - 1)), "address not cache aligned");

#if 0
  osalDbgAssert(!(n & (L1_CACHE_BYTES - 1)), "size not multiple of cache line");
#endif

  /* L1 is enabled */
  cacheCleanRegion((uint8_t *) txbuf, n);

  /* Get source address. */
  uint32_t addrSource = sizeof(struct wav_header);

  /* Unmute left and right channel */
  classdMuteChannel(classdp, false, false);

  /* Writing channel */
  dmaChannelSetSource(classdp->dmatx, txbuf + addrSource);
  dmaChannelSetDestination(classdp->dmatx, &classdp->classd->CLASSD_THR);
  dmaChannelSetTransactionSize(classdp->dmatx, n);

  /* DMA start transfer. */
  dmaChannelEnable(classdp->dmatx);
}

/**
 * @brief   CLASSD mute/unmute channels.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver object
 *
 * @iclass
 */
void classd_mute_channel(CLASSDDriver *classdp, bool left, bool right) {

  /* Disable write protection. */
  classdDisableWP(classdp->classd);

  /* Mute or unmute left channel. */
  if (left)
    classdp->classd->CLASSD_MR |= CLASSD_MR_LMUTE;
  else
    classdp->classd->CLASSD_MR &= ~CLASSD_MR_LMUTE;

  /* Mute or unmute right channel. */
  if (right)
    classdp->classd->CLASSD_MR |= CLASSD_MR_RMUTE;
  else
    classdp->classd->CLASSD_MR &= ~CLASSD_MR_RMUTE;

  /* Enable write protection. */
  classdEnableWP(classdp->classd);
}

/**
 *
 * @brief   CLASSD Driver initialization.
 *
 * @init
 */
void classdInit(void) {

  classd_lld_init();
}

/**
 *
 * @brief   Initializes the standard part of a @p CLASSDDriver structure.
 *
 * @param[out] classdp  pointer to a @p CLASSDDriver object
 *
 * @init
 */
void classdObjectInit(CLASSDDriver *classdp) {
  classdp->state = CLASSD_STOP;
  classdp->config = NULL;
}

/**
 * @brief   Configures and activates the CLASSD peripheral.
 *
 * @param[in] classdp  pointer to a @p CLASSDDriver object
 * @param[in] config   pointer to a @p CLASSDConfig object
 *
 * @api
 */
void classdStart(CLASSDDriver *classdp, const CLASSDConfig *config) {

  osalDbgCheck((classdp != NULL) && (config != NULL));

  osalSysLock();
  osalDbgAssert((classdp->state == CLASSD_STOP) || (classdp->state == CLASSD_READY),
                "invalid state");
  classdp->config = config;
  classd_lld_start(classdp);
  classdp->state = CLASSD_READY;
  osalSysUnlock();
}

/**
 * @brief   Deactivates the CLASSD peripheral.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver object
 *
 * @api
 */
void classdStop(CLASSDDriver *classdp) {

  osalDbgCheck(classdp != NULL);

  osalSysLock();

  osalDbgAssert((classdp->state == CLASSD_STOP) || (classdp->state == CLASSD_READY || (classdp->state == CLASSD_ACTIVE)),
                "invalid state");

  classd_lld_stop(classdp);
  classdp->config = NULL;
  classdp->state  = CLASSD_STOP;

  osalSysUnlock();
}

/**
 *
 * @brief  Starts a CLASSD playback.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @note    This function can be used only in syslock state. todo: control comment!
 *
 * @notapi
 */
void classdSendAudioI(CLASSDDriver *classdp, const void *txbuf) {

  (classdp)->state = CLASSD_ACTIVE;
  classd_lld_send_audio(classdp, txbuf);
}

/**
 *
 * @brief  Starts a CLASSD playback.
 *
 * @param[in] classdp   pointer to the @p CLASSDDriver
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @notapi
 */
void classdSendAudio(CLASSDDriver *classdp, const void *txbuf) {

  osalSysLock();
  osalDbgAssert(classdp->state == CLASSD_READY, "not ready");
  classdSendAudioI(classdp, txbuf);
  osalSysUnlock();
}

/**
 *
 * @brief  Set the sample frame from the structure of a wav file.
 *
 * @param[in] classdconfigp  pointer to the @p CLASSDConfig
 * @param[in] music_file     pointer to the wav file
 *
 * @notapi
 */
void classdSetSampleFrame(CLASSDConfig *classdconfigp, uint8_t *music_file) {
  classdconfigp->frame = ((struct wav_header*)music_file)->sample_rate;
}

/**
 * @brief   Mute/unmute CLASSD channel.
 *
 * @param[in] classdp  pointer to the @p CLASSDDriver object
 *
 * @iclass
 */
void classdMuteChannel(CLASSDDriver *classdp, bool left, bool right) {
  classd_mute_channel(classdp, left, right);
}

#endif /* SAMA_USE_CLASSD */

/** @} */