sama_classd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file SAMA5D2x/sama_classd.c
  15. * @brief SAMA CLASSD support code.
  16. *
  17. * @addtogroup SAMA5D2x_CLASSD
  18. * @{
  19. */
  20. #include "hal.h"
  21. #if SAMA_USE_CLASSD || defined(__DOXYGEN__)
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Driver local macros. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief Enable write protection on CLASSD Mode Register
  30. * and Interpolator Mode Register.
  31. *
  32. * @param[in] classdp pointer to a CLASSD register block
  33. *
  34. * @notapi
  35. */
  36. #define classdEnableWP(classdp) { \
  37. classdp->CLASSD_WPMR = CLASSD_WPMR_WPKEY_PASSWD | CLASSD_WPMR_WPEN; \
  38. }
  39. /**
  40. * @brief Disable write protection on CLASSD Mode Register
  41. * and Interpolator Mode Register.
  42. *
  43. * @param[in] classdp pointer to a CLASSD register block
  44. *
  45. * @notapi
  46. */
  47. #define classdDisableWP(classdp) { \
  48. classdp->CLASSD_WPMR = CLASSD_WPMR_WPKEY_PASSWD; \
  49. }
  50. /*===========================================================================*/
  51. /* Driver exported variables. */
  52. /*===========================================================================*/
  53. /**
  54. * @brief CLASSD driver identifier.
  55. */
  56. CLASSDDriver CLASSDD0;
  57. /*===========================================================================*/
  58. /* Driver local variables. */
  59. /*===========================================================================*/
  60. /**
  61. * @brief Type of a structure representing PMC Audio configuration.
  62. */
  63. typedef struct {
  64. /**
  65. * @brief Loop Divider Ratio.
  66. */
  67. uint32_t nd;
  68. /**
  69. * @brief Fractional Loop Divider Setting.
  70. */
  71. uint32_t fracr;
  72. /**
  73. * @brief Output Divider Ratio for PMC Clock.
  74. */
  75. uint32_t qdpmc;
  76. /**
  77. * @brief Divider Value.
  78. */
  79. uint32_t div;
  80. /**
  81. * @brief Output Divider Ratio for Pad Clock.
  82. */
  83. uint32_t qdaudio;
  84. } pmcAudioConf;
  85. /*===========================================================================*/
  86. /* Driver local functions. */
  87. /*===========================================================================*/
  88. /**
  89. * @brief Configure DSPClock.
  90. *
  91. * @param[in] dsp_clk DSP clock type (12.288 MHz or 11.2896 MHz).
  92. */
  93. static void dspclkConfigure(uint32_t dsp_clk) {
  94. pmcAudioConf cfg;
  95. /* Pad Clock: not used */
  96. cfg.div = 0;
  97. cfg.qdaudio = 0;
  98. /* PMC Clock: */
  99. /* 12Mhz * (ND + 1 + FRACR/2^22) / (QDPMC + 1) = 8 * DSPCLK */
  100. switch (dsp_clk) {
  101. case CLASSD_INTPMR_DSPCLKFREQ_12M288:
  102. #if SAMA_MOSCXTCLK == 12000000
  103. /* 12Mhz * (56 + 1 + 1442841/2^22) / (6 + 1) = 8 * 12.288Mhz */
  104. cfg.nd = 56;
  105. cfg.fracr = 1442841;
  106. cfg.qdpmc = 6;
  107. #elif SAMA_MOSCXTCLK == 24000000
  108. /* 24Mhz * (56 + 1 + 1442841/2^22) / (6 + 1) = 8 * 12.288Mhz */
  109. cfg.nd = 27;
  110. cfg.fracr = 2796203;
  111. cfg.qdpmc = 6;
  112. #else
  113. #error "FREQUENCY NOT SUPPORTED BY CLASSD"
  114. #endif
  115. break;
  116. case CLASSD_INTPMR_DSPCLKFREQ_11M2896:
  117. #if SAMA_MOSCXTCLK == 12000000
  118. /* 12Mhz * (59 + 1 + 885837/2^22) / (7 + 1) = 8 * 11.2896Mhz */
  119. cfg.nd = 59;
  120. cfg.fracr = 885837;
  121. cfg.qdpmc = 7;
  122. #elif SAMA_MOSCXTCLK == 24000000
  123. /* 24Mhz * (59 + 1 + 885837/2^22) / (7 + 1) = 8 * 11.2896Mhz */
  124. cfg.nd = 28;
  125. cfg.fracr = 699050;
  126. cfg.qdpmc = 7;
  127. #else
  128. #error "FREQUENCY NOT SUPPORTED BY CLASSD"
  129. #endif
  130. break;
  131. default:
  132. osalDbgAssert(NULL, "errore mask configuration");
  133. }
  134. /* Configure and enable the generic clock. */
  135. pmcConfigAudio(cfg.nd, cfg.qdpmc, cfg.fracr, cfg.div, cfg.qdaudio);
  136. pmcEnableAudio(true, false);
  137. }
  138. /**
  139. * @brief Shared end-of-tx service routine.
  140. *
  141. * @param[in] classdp pointer to the @p CLASSDDriver object
  142. * @param[in] flags pre-shifted content of the ISR register
  143. */
  144. static void classd_lld_serve_tx_interrupt(CLASSDDriver *classdp, uint32_t flags) {
  145. /* DMA errors handling.*/
  146. #if defined(SAMA_CLASSD_DMA_ERROR_HOOK)
  147. (void)classdp;
  148. if ((flags & (XDMAC_CIS_WBEIS | XDMAC_CIS_ROIS)) != 0) {
  149. SAMA_CLASSD_DMA_ERROR_HOOK(classdp);
  150. }
  151. #else
  152. (void)flags;
  153. #endif
  154. if(classdp->config->callback != NULL) {
  155. classdp->config->callback(classdp);
  156. }
  157. classdMuteChannel(classdp, false, false);
  158. classdp->state = CLASSD_READY;
  159. }
  160. /*===========================================================================*/
  161. /* Driver interrupt handlers. */
  162. /*===========================================================================*/
  163. /*===========================================================================*/
  164. /* Driver exported functions. */
  165. /*===========================================================================*/
  166. /**
  167. * @brief Low level CLASSD driver initialization.
  168. *
  169. * @notapi
  170. */
  171. void classd_lld_init(void) {
  172. #if SAMA_HAL_IS_SECURE
  173. mtxConfigPeriphSecurity(MATRIX1, ID_CLASSD, SECURE_PER);
  174. #endif /* SAMA_HAL_IS_SECURE */
  175. /* Driver initialization. */
  176. classdObjectInit(&CLASSDD0);
  177. CLASSDD0.classd = CLASSD;
  178. CLASSDD0.dmatx = NULL;
  179. CLASSDD0.txdmamode = XDMAC_CC_TYPE_PER_TRAN |
  180. XDMAC_CC_MBSIZE_SINGLE |
  181. XDMAC_CC_DSYNC_MEM2PER |
  182. XDMAC_CC_PROT_SEC |
  183. XDMAC_CC_CSIZE_CHK_1 |
  184. XDMAC_CC_DWIDTH_WORD |
  185. XDMAC_CC_SIF_AHB_IF0 |
  186. XDMAC_CC_DIF_AHB_IF1 |
  187. XDMAC_CC_SAM_INCREMENTED_AM |
  188. XDMAC_CC_DAM_FIXED_AM |
  189. XDMAC_CC_PERID(PERID_CLASSD_TX);
  190. }
  191. /**
  192. * @brief Configures and activates the CLASSD peripheral.
  193. *
  194. * @param[in] classdp pointer to the @p CLASSDDriver object
  195. *
  196. * @notapi
  197. */
  198. void classd_lld_start(CLASSDDriver *classdp) {
  199. uint8_t i;
  200. uint32_t dsp_clk_set, frame_set;
  201. /* Configures the peripheral.*/
  202. if (classdp->state == CLASSD_STOP) {
  203. if (&CLASSDD0 == classdp) {
  204. classdp->dmatx = dmaChannelAllocate(SAMA_CLASSD_DMA_IRQ_PRIORITY,
  205. (sama_dmaisr_t)classd_lld_serve_tx_interrupt,
  206. (void *)classdp);
  207. osalDbgAssert(classdp->dmatx != NULL, "no channel allocated");
  208. }
  209. }
  210. /* Set DMA channel mode. */
  211. dmaChannelSetMode(classdp->dmatx, classdp->txdmamode);
  212. /* Set CLASSD DSP clock and Sample rate. */
  213. for(i = 0; i < 8; i++) {
  214. if ((audio_info[i].rate) == (classdp->config->frame)) {
  215. dsp_clk_set = audio_info[i].dsp_clk;
  216. frame_set = audio_info[i].sample_rate;
  217. break;
  218. }
  219. }
  220. /* Enable the CLASSD0 clock. */
  221. pmcEnableCLASSD0();
  222. /* Configure PMC Audio structure. */
  223. dspclkConfigure(dsp_clk_set);
  224. /* Disable the CLASSD generic clock for now. */
  225. pmcDisableGclkCLASSD0();
  226. /* Configure the CLASSD generic clock */
  227. pmcConfigGclk(ID_CLASSD, PMC_PCR_GCKCSS_AUDIO_CLK, 1);
  228. /* Disable write protection. */
  229. classdDisableWP(classdp->classd);
  230. /* Perform soft reset. */
  231. CLASSD->CLASSD_CR = CLASSD_CR_SWRST;
  232. CLASSD->CLASSD_IDR = CLASSD_IDR_DATRDY;
  233. /* Clean CLASSD Registers. */
  234. classdp->classd->CLASSD_MR = 0;
  235. classdp->classd->CLASSD_INTPMR = 0;
  236. /* CLASSD configuration. */
  237. classdp->classd->CLASSD_MR = classdp->config->left |
  238. classdp->config->right |
  239. classdp->config->left_mute |
  240. classdp->config->right_mute |
  241. classdp->config->pwm_mode |
  242. classdp->config->non_overlap |
  243. classdp->config->novrval;
  244. classdp->classd->CLASSD_INTPMR = classdp->config->attl |
  245. classdp->config->attr |
  246. classdp->config->deemp |
  247. classdp->config->swap |
  248. classdp->config->eqcfg |
  249. classdp->config->mono |
  250. classdp->config->mono_mode |
  251. dsp_clk_set | frame_set;
  252. /* Enable CLASSD generic clock. */
  253. pmcEnableGclkCLASSD0();
  254. /* Enable write protection. */
  255. classdEnableWP(classdp->classd);
  256. }
  257. /**
  258. * @brief Deactivates the CLASSD peripheral.
  259. *
  260. * @param[in] classdp pointer to the @p CLASSDDriver object
  261. *
  262. * @notapi
  263. */
  264. void classd_lld_stop(CLASSDDriver *classdp) {
  265. /* Disable clocks. */
  266. pmcDisableAudio();
  267. pmcDisableGclkCLASSD0();
  268. pmcDisableCLASSD0();
  269. /* Disable write protection. */
  270. classdDisableWP(classdp->classd);
  271. /* Reset CLASSD. */
  272. classdp->classd->CLASSD_INTPMR = 0;
  273. classdp->classd->CLASSD_MR = 0;
  274. /* Enable write protection. */
  275. classdEnableWP(classdp->classd);
  276. /* Release and disable DMA channel. */
  277. dmaChannelRelease(classdp->dmatx);
  278. }
  279. /**
  280. *
  281. * @brief Starts a CLASSD playback.
  282. *
  283. * @param[in] classdp pointer to the @p CLASSDDriver
  284. * @param[in] txbuf the pointer to the transmit buffer
  285. *
  286. * @notapi
  287. */
  288. void classd_lld_send_audio(CLASSDDriver *classdp, const void *txbuf) {
  289. /* Get DMA transfert size. */
  290. size_t n = ((struct wav_header *)txbuf)->subchunk2_size / 4;
  291. osalDbgAssert(!((uint32_t) txbuf & (L1_CACHE_BYTES - 1)), "address not cache aligned");
  292. #if 0
  293. osalDbgAssert(!(n & (L1_CACHE_BYTES - 1)), "size not multiple of cache line");
  294. #endif
  295. /* L1 is enabled */
  296. cacheCleanRegion((uint8_t *) txbuf, n);
  297. /* Get source address. */
  298. uint32_t addrSource = sizeof(struct wav_header);
  299. /* Unmute left and right channel */
  300. classdMuteChannel(classdp, false, false);
  301. /* Writing channel */
  302. dmaChannelSetSource(classdp->dmatx, txbuf + addrSource);
  303. dmaChannelSetDestination(classdp->dmatx, &classdp->classd->CLASSD_THR);
  304. dmaChannelSetTransactionSize(classdp->dmatx, n);
  305. /* DMA start transfer. */
  306. dmaChannelEnable(classdp->dmatx);
  307. }
  308. /**
  309. * @brief CLASSD mute/unmute channels.
  310. *
  311. * @param[in] classdp pointer to the @p CLASSDDriver object
  312. *
  313. * @iclass
  314. */
  315. void classd_mute_channel(CLASSDDriver *classdp, bool left, bool right) {
  316. /* Disable write protection. */
  317. classdDisableWP(classdp->classd);
  318. /* Mute or unmute left channel. */
  319. if (left)
  320. classdp->classd->CLASSD_MR |= CLASSD_MR_LMUTE;
  321. else
  322. classdp->classd->CLASSD_MR &= ~CLASSD_MR_LMUTE;
  323. /* Mute or unmute right channel. */
  324. if (right)
  325. classdp->classd->CLASSD_MR |= CLASSD_MR_RMUTE;
  326. else
  327. classdp->classd->CLASSD_MR &= ~CLASSD_MR_RMUTE;
  328. /* Enable write protection. */
  329. classdEnableWP(classdp->classd);
  330. }
  331. /**
  332. *
  333. * @brief CLASSD Driver initialization.
  334. *
  335. * @init
  336. */
  337. void classdInit(void) {
  338. classd_lld_init();
  339. }
  340. /**
  341. *
  342. * @brief Initializes the standard part of a @p CLASSDDriver structure.
  343. *
  344. * @param[out] classdp pointer to a @p CLASSDDriver object
  345. *
  346. * @init
  347. */
  348. void classdObjectInit(CLASSDDriver *classdp) {
  349. classdp->state = CLASSD_STOP;
  350. classdp->config = NULL;
  351. }
  352. /**
  353. * @brief Configures and activates the CLASSD peripheral.
  354. *
  355. * @param[in] classdp pointer to a @p CLASSDDriver object
  356. * @param[in] config pointer to a @p CLASSDConfig object
  357. *
  358. * @api
  359. */
  360. void classdStart(CLASSDDriver *classdp, const CLASSDConfig *config) {
  361. osalDbgCheck((classdp != NULL) && (config != NULL));
  362. osalSysLock();
  363. osalDbgAssert((classdp->state == CLASSD_STOP) || (classdp->state == CLASSD_READY),
  364. "invalid state");
  365. classdp->config = config;
  366. classd_lld_start(classdp);
  367. classdp->state = CLASSD_READY;
  368. osalSysUnlock();
  369. }
  370. /**
  371. * @brief Deactivates the CLASSD peripheral.
  372. *
  373. * @param[in] classdp pointer to the @p CLASSDDriver object
  374. *
  375. * @api
  376. */
  377. void classdStop(CLASSDDriver *classdp) {
  378. osalDbgCheck(classdp != NULL);
  379. osalSysLock();
  380. osalDbgAssert((classdp->state == CLASSD_STOP) || (classdp->state == CLASSD_READY || (classdp->state == CLASSD_ACTIVE)),
  381. "invalid state");
  382. classd_lld_stop(classdp);
  383. classdp->config = NULL;
  384. classdp->state = CLASSD_STOP;
  385. osalSysUnlock();
  386. }
  387. /**
  388. *
  389. * @brief Starts a CLASSD playback.
  390. *
  391. * @param[in] classdp pointer to the @p CLASSDDriver
  392. * @param[in] txbuf the pointer to the transmit buffer
  393. *
  394. * @note This function can be used only in syslock state. todo: control comment!
  395. *
  396. * @notapi
  397. */
  398. void classdSendAudioI(CLASSDDriver *classdp, const void *txbuf) {
  399. (classdp)->state = CLASSD_ACTIVE;
  400. classd_lld_send_audio(classdp, txbuf);
  401. }
  402. /**
  403. *
  404. * @brief Starts a CLASSD playback.
  405. *
  406. * @param[in] classdp pointer to the @p CLASSDDriver
  407. * @param[in] txbuf the pointer to the transmit buffer
  408. *
  409. * @notapi
  410. */
  411. void classdSendAudio(CLASSDDriver *classdp, const void *txbuf) {
  412. osalSysLock();
  413. osalDbgAssert(classdp->state == CLASSD_READY, "not ready");
  414. classdSendAudioI(classdp, txbuf);
  415. osalSysUnlock();
  416. }
  417. /**
  418. *
  419. * @brief Set the sample frame from the structure of a wav file.
  420. *
  421. * @param[in] classdconfigp pointer to the @p CLASSDConfig
  422. * @param[in] music_file pointer to the wav file
  423. *
  424. * @notapi
  425. */
  426. void classdSetSampleFrame(CLASSDConfig *classdconfigp, uint8_t *music_file) {
  427. classdconfigp->frame = ((struct wav_header*)music_file)->sample_rate;
  428. }
  429. /**
  430. * @brief Mute/unmute CLASSD channel.
  431. *
  432. * @param[in] classdp pointer to the @p CLASSDDriver object
  433. *
  434. * @iclass
  435. */
  436. void classdMuteChannel(CLASSDDriver *classdp, bool left, bool right) {
  437. classd_mute_channel(classdp, left, right);
  438. }
  439. #endif /* SAMA_USE_CLASSD */
  440. /** @} */